Reader small image

You're reading from  Full Stack Web Development with Raspberry Pi 3

Product typeBook
Published inAug 2017
Reading LevelExpert
PublisherPackt
ISBN-139781788295895
Edition1st Edition
Languages
Right arrow
Author (1)
Soham Kamani
Soham Kamani
author image
Soham Kamani

Soham Kamani is a full-stack developer who has extensive experience in the JavaScript ecosystem. He works as a consultant, developing end-to-end web-based solutions for clients around the world. He is an open source enthusiast and an avid blogger. He has worked on many frameworks and technologies such as React, Angular, Node, Express, Sails, SQLite, Postgres, and MySQL, and he has worked on many projects in the IoT space using Arduino and Raspberry Pi systems.
Read more about Soham Kamani

Right arrow

Retrieving Sensor Readings from the Server

In the previous chapter, we managed to get hands-on with interfacing our sensors with the Raspberry Pi's GPIO pins and were able to get the temperature and humidity readings of the surroundings. However, we still require a way to get this information to our users. After all, information is useless if nobody can see it. Another interesting concept that we saw was that our sensor was digital and not analog, which meant that it sent us information in the form of multiple bits at a regular interval, as opposed to analog sensors whose current we can read in real time. While the interval in which our sensor takes reading is more or less real time, it poses unique problems when we couple it with on-demand readings.

In this chapter, you will learn how to integrate our sensor readings, which we obtained in Chapter 4, Extracting Information...

Understanding how our node process takes readings

In order to integrate our sensor reading code into our server, first. let's understand what is happening under the hood whenever we execute the node script that takes our sensor readings. Let's review the snippet of code that actually called for the sensor readings:

    /*
Section A
*/
sensor.read(11, 4, function(err, temperature, humidity) {
/*
Section B
*/
//After reading the sensor, we get the temperature
and humidity readings
if (!err) {
//If there is no error, log the readings to the
console
console.log('temp: ' + temperature.toFixed(1) +
'°C, ' +
'humidity: ' + humidity.toFixed(1) + '%'

)
}
});
/*
Section C
*/

Immediately, we can see that the...

Modifying our server code to show sensor readings

In Chapter 3, Running a Node Server on the Pi, we developed a node server that had APIs to obtain the temperature and humidity. We did not return real data but used a mock hardcoded response to give the impression of real data. In this section, we will be integrating the code that we discussed in Chapter 4, Extracting Information from the GPIO Pins, and including it in our server code so that every time you make a request to the temperature and humidity APIs, you get real, live data.

Let's take the example of the temperature API:

    app.get('/temperature', function(req, res) {
res.send('24 °C');
});

Now, we add the code for reading from the sensor inside this API:

    app.get('/temperature', function (req, res) {
sensor.read(11, 4, function (err, temperature,
humidity)...

Optimizing our server

So it looks like we got our server working with the sensor readings and everything... great! However, there are a few issues with our implementation that we need to handle. Looking at the server code from the previous example, we can note the following points:

  • We are repeating a lot of code; not only does this make our code look bad, but it also makes it less malleable. What if we want to change our sensor pin number from 4 to 6? We need to change the code in two places. Even worse, what if we decide to change the sensor or even the way in which we get sensor readings in the future?
  • We are making calls to the native library every time someone hits the API. This is fine when we are testing it on our local machine, but what if we had multiple users hitting our API at the same time? It is highly inefficient to make so many calls to the native library, especially...

Summary

This chapter was the glue between the last two chapters. We began the chapter by taking a deep dive into how our node server communicates with native libraries. You learned about the asynchronous model that node uses to make I/O operations more efficient. This model is important to understand because it will also carry forward to the coming chapters.

We proceeded to integrate the sensor API from the previous chapter with our node server, and then we went on to see some of its limitations due to the hardware and sampling rate. Caching turned out to be a convenient and effective way to get around these limitations. The kind of local memory caching used in this chapter, however, would not be effective in a system that has multiple Raspberry Pi systems and sensors working together. In the upcoming chapters, you will learn about a more production-grade model to store and read...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Full Stack Web Development with Raspberry Pi 3
Published in: Aug 2017Publisher: PacktISBN-13: 9781788295895
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Soham Kamani

Soham Kamani is a full-stack developer who has extensive experience in the JavaScript ecosystem. He works as a consultant, developing end-to-end web-based solutions for clients around the world. He is an open source enthusiast and an avid blogger. He has worked on many frameworks and technologies such as React, Angular, Node, Express, Sails, SQLite, Postgres, and MySQL, and he has worked on many projects in the IoT space using Arduino and Raspberry Pi systems.
Read more about Soham Kamani