Introduction: IoT Made Simple: Monitoring Multiple Sensors

About: Engineer, writer and forever student. Passionate to share knowledge of electronics with focus on IoT and robotics.

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

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

  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

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

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

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