Introduction: IoT Made Simple: Monitoring Multiple Sensors

Picture of IoT Made Simple: Monitoring Multiple Sensors

A few weeks ago, I published here a tutorial about monitoring temperature using a DS18B20, a digital sensor that communicates over a 1-Wire bus, sending data over the internet with NodeMCU and Blynk:

IoT Made Simple: Monitoring Temperature Anywhere

But what we missed in exploration, was one of the great advantages of this kind of sensor that is the possibility of collecting multiple data, from multiple sensors connected to the same 1-wire bus. And, now it is time to also explore it.

We will expand what was developed on the last tutorial, monitoring now two DS18B20 sensors, configured one in Celcius and the another in Fahrenheit. The data will be sent to a Blynk App, as shown in the above block diagram.

Step 1: Bill of Material

(*) Any type of ESP device can be used here. The most common are the NodeMCU V2 or V3. Both will always work fine.

Step 2: DS18B20 Temperature Sensor

Picture of DS18B20 Temperature Sensor

We will use in this tutorial a waterproofed version of the DS18B20 sensor. It is very useful for remote temperature in wet conditions, for example on a humid soil. The sensor is isolated and can take measurements until 125oC (Adafrut does not recommend to use it over 100oC due to its cable PVC jacket).

The DS18B20 is a digital sensor what makes it good to use even over long distances! These 1-wire digital temperature sensors are fairly precise (±0.5°C over much of the range) and can give up to 12 bits of precision from the onboard digital-to-analog converter. They work great with the NodeMCU using a single digital pin, and you can even connect multiple ones to the same pin, each one has a unique 64-bit ID burned in at the factory to differentiate them.

The sensor works from 3.0 to 5.0V, what means that it can be powered directly from one of the 3.3V NodeMCU pins.

The sensor has 3 wires:

  • Black: GND
  • Red: VCC
  • Yellow: 1-Wire Data

Here, you can find the full data: DS18B20 Datasheet

Step 3: Connecting the Sensors to NodeMCU

Picture of Connecting the Sensors to NodeMCU
  1. Connect the 3 wires from each sensor at the mini Breadboard as shown at the above photo. I used special connectors to better fix the sensor's cable on it.
  2. Note that both sensors are in parallel. If you have more than 2 sensors, you should do the same.
    • Red ==> 3.3V
    • Black ==> GND
    • Yellow ==> D4
  3. Use a 4.7K ohms resistor between VCC (3.3V) and Data (D4)

Step 4: Installing the Appropriated Libraries

In order to use the DS18B20 properly, two libraries will be necessary:

  1. OneWire
  2. DallasTemperature

Install both libraries in your Arduino IDE Library depository.

Note that the OneWire library MUST be the special one, modified to be used with ESP8266, otherwise you will get an error during compilation. You will find the last version at above link.

Step 5: Testing the Sensors

Picture of Testing the Sensors

For testing the sensors, download the below file from my GitHub:

NodeMCU_DS18B20_Dual_Se nsor_test.ino

/**************************************************************
 * Multiple Temperature Sendor Test
 * 
 *  2 x OneWire Sensor: DS18B20
 *  Connected to NodeMCU D4 (or Arduino Pin 2)
 * 
 * Developed by Marcelo Rovai - 25 August 2017
 **************************************************************/

#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 // DS18B20 on NodeMCU pin D4 
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

void setup() 
{
  Serial.begin(115200);
  DS18B20.begin();
  Serial.println("Testing Dual Sensor data");
}

void loop() {
  float temp_0;
  float temp_1;
  DS18B20.requestTemperatures(); 
  temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
  temp_1 = DS18B20.getTempFByIndex(1); // Sensor 0 will capture Temp in Fahrenheit

  Serial.print("Temp_0: ");
  Serial.print(temp_0);
  Serial.print(" oC . Temp_1: ");
  Serial.print(temp_1);
  Serial.println(" oF");
  delay(1000);
}

Looking at the above code, we should notice that the most important lines are:

  temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
  temp_1 = DS18B20.getTempFByIndex(1); // Sensor 0 will capture Temp in Fahrenheit

The first one will return a value from Sensor [0] (look the "index(0)") in Celcius ( look the portion of the code: "getTempC". The second line is related with Sensor[1] and will return data in Fahrenheit. You could have here "n" sensors since you have a different "index" for each one of them.

Upload now the code in your NodeMCU and monitor the temperature using the Serial Monitor.

The above photo shows the expected result. Hold each one of the sensors in your hand, you should see the temperature going up.

Step 6: Using Blynk

Picture of Using Blynk

Once you start capturing temperature data, it's time to see it from anywhere. We will do this using Blynk. So, all captured data will be displayed in real time on your mobile device and also we will build a historical depository for that.

Follow the below steps:

  1. Create a New Project.
  2. Give it a name (in my case "Dual Temperature Monitor")
  3. Select New Device - ESP8266(WiFi) as "My Devices"
  4. Copy the AUTH TOKEN to be used in the code (you can send it to your email).
  5. Includes two "Gauge" Widgets, defining:
    • Virtual pin to be used with each sensor: V10 (Sensor[0]) and V11 (Sensor[1])
    • The temperature range: -5 to 100 oC for Sensor [0]
    • The temperature range: 25 to 212 oC for Sensor [1]
    • The frequency to read data: 1 second
  6. Includes a "History Graph" Widget, defining V10 and V11 as virtual pins
  7. Press "Play" (The triangle at right up corner)

Of course, the Blynk App will tel you that the NodeMCU is off line. It's time to upload the full code at your Arduino IDE. You can get it here:

NodeMCU_Dual_Sensor_Blynk_Ext.ino

Change the "dummy data" with your own credentials.

/* Blynk credentials */
char auth[] = "YOUR BLYNK AUTH CODE HERE";

/* WiFi credentials */
char ssid[] = "YOUR SSID";
char pass[] = "YOUR PASSWORD";

And that's it!

Bellow the full code. It is basically the previous code, where we entered with Blynk parameters and specific functions. Note the 2 last lines of the code. Those are the most important here. If you have more sensors collecting data, you should also have equivalent new lines as those ones (with pertinent new virtual pins defined).

/**************************************************************
 * IoT Multiple Temperature Monitor with Blynk
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 * 
 * Multiple OneWire Sensor: DS18B20
 * Developed by Marcelo Rovai - 25 August 2017
 **************************************************************/

/* ESP & Blynk */
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space

/* Blynk credentials */
char auth[] = "YOUR BLYNK AUTH CODE HERE";

/* WiFi credentials */
char ssid[] = "YOUR SSID";
char pass[] = "YOUR PASSWORD";

/* TIMER */
#include <SimpleTimer.h>
SimpleTimer timer;

/* DS18B20 Temperature Sensor */
#include <OneWire.h>
#include<DallasTemperature.h> 
#define ONE_WIRE_BUS 2 // DS18B20 on arduino pin2 corresponds to D4 on physical board
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
int temp_0;
int temp_1;

void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  DS18B20.begin();
  timer.setInterval(1000L, getSendData);
  Serial.println(" ");
  Serial.println("Testing Dual Sensor data");
}

void loop() 
{
  timer.run(); // Initiates SimpleTimer
  Blynk.run();
}

/***************************************************
 * Send Sensor data to Blynk
 **************************************************/
void getSendData()
{
  DS18B20.requestTemperatures(); 
  temp_0 = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
  temp_1 = DS18B20.getTempFByIndex(1); // Sensor 0 will capture Temp in Fahrenheit

  Serial.print("Temp_0: ");
  Serial.print(temp_0);
  Serial.print(" oC . Temp_1: ");
  Serial.print(temp_1);
  Serial.println(" oF");
   
  Blynk.virtualWrite(10, temp_0); //virtual pin V10
  Blynk.virtualWrite(11, temp_1); //virtual pin V11
}

Once the code is uploaded and running, check the Blynk app. It should be now also running as shown at above print screen from my iPhone.

Step 7: Conclusion

Picture of Conclusion

As always, I hope this project can help others find their way in the exciting world of electronics, robotics, and IoT!

Please visit my GitHub for updated files: NodeMCU Dual Temp Monitor

For more projects, please visit my blog: MJRoBot.org

Saludos from the south of the world!

See you at my next instructable!

Thank you,

Marcelo

Comments

TruthS3 (author)2017-08-26

Hello Marcelo,

I opened a new file in IDE, cleared out all the writing on the field followed your link to github and pasted the code there in the field and ran a check compile. Here is the error:

Arduino: 1.8.3 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

C:\Users\longb\Documents\Arduino\sketch_aug26b\sketch_aug26b.ino:21:25: fatal error: SimpleTimer.h: No such file or directory

#include <SimpleTimer.h>

^

compilation terminated.

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Did I do something wrong? I tried it twice, it looks like some issue about the timer?

mjrovai (author)TruthS32017-08-29

As DavidW mentioned, install SimpleTimer library on your IDE it is missing. To facilitate your Job, I included the link on my answer below.

dBuggz (author)TruthS32017-08-29

Dave,

Another Dave here. I'll call myself DaveW..

I've read a few of your posts and I have to say, Slow down and take your time to read and understand what's going on.

e.g. when you put your "YOUR SSID" in look at the code

char ssid[] = "YOUR SSID".....

this says:

char.... which means it is the declaration of a 'character' array

ssid[].... is the name of the 'thing' and it may be easier to think of it as a 'String'

YOUR SSID .... is the 'value' being assigned to the String, and is put in quote marks to tell the computer is is text.

In the above errors,

SimpleTimer.h: No such file or directory....

the computer is telling your that it cannot find SimpleTimer.h.

You have to find that file (which is actually a library) and make sure it has been added to the Arduino IDE. You will have to look back through Marcelo's previous Instructable (he doesn't mention it here) to see where it comes from and check that you have added it.

'Regards,

DaveW

mjrovai (author)dBuggz2017-08-29

Hi DaveW,

Thanks for the help. Using the opportunity, the SimpleTimer Library can be downloaded from its GitHub here:

https://github.com/marcelloromani/Arduino-SimpleTimer

I really love to work with this library. It was developed to work primaryly with Arduino but works very well with NodeMCU. It is very simple and organize the job to be done in more complex projects. I used this library on my ArduFarmBot 2.0 project:

https://www.instructables.com/id/Automatic-Gardening-System-With-NodeMCU-and-Blynk-/

There, the timing to organ all different tasks was key.

Thanks

dBuggz (author)2017-08-29

Marcelo,

How do you work out which sensor is which?

DaveW

mjrovai (author)dBuggz2017-08-29

Hi DaveW,

I realized that the closest sensor to Pin D4, was the one that got the [0] index. Of course, when I put my finger over it, I confirmed that was the "Celsius"one and I labeled them.

TruthS3 (author)2017-08-26

Starting over.

Open new sketch, clear out all writing from field, follow link to github, copy code and paste it to the new sketch, then include libraries onewire and dallas. I did go back to part 2 and got the libraries again and tried to add them again but IDE told me they were already there. Check compile same error.

Paste:

Arduino: 1.8.3 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, 115200, 4M (3M SPIFFS)"

C:\Users\longb\Documents\Arduino\sketch_aug26b\sketch_aug26b.ino:25:25: fatal error: SimpleTimer.h: No such file or directory

#include <SimpleTimer.h>

^

compilation terminated.

exit status 1
Error compiling for board NodeMCU 1.0 (ESP-12E Module).

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I'm stupid I guess, I can't seem to get a handle on what is wrong.

This other link you gave in your last response is it different? Was there a link to its zip file that I missed in part 3? I tried following that link in the reply but it didn't have a zip to download and I hesitated about it because I was unsure what I was supposed to do there.

As for just adding the lines to my previous part 2 code, I am unsure about what to do there, that is why I have been doing simple copy/pastes.

Thanks,

Dave

About This Instructable

867views

21favorites

License:

Bio: Engineer, writer and forever student. Passionate to share knowledge of electronics with focus on IoT and robotics.
More by mjrovai:ESP32 and OLED Display: Internet Clock - DHT22 IOT Made Simple: Playing With the ESP32 on Arduino IDEIoT Made Simple: Monitoring Multiple Sensors
Add instructable to: