16  Intro to Sensor Modules and Thingspeak

16.1 DS18B20 Temperature Sensor

Pretty accurate temperature sensor. Each sensor has its own unique address, enabling the connection of numerous sensors to only on pin on the Arduino/ESP32.

In this tutorial you can find a lot more information.

Code for finding the sensor’s address:

Download code
/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

#include <OneWire.h>

// Based on the OneWire library example

OneWire ds(4);  //data wire connected to GPIO 4

void setup(void) {
  Serial.begin(115200);
}

void loop(void) {
  byte i;
  byte addr[8];
  
  if (!ds.search(addr)) {
    Serial.println(" No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }
  Serial.print(" ROM =");
  for (i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }
}


16.2 SHT31 Temperature & Humidity Sensor Using I2C communication

In this tutorial you can find a lot more information.

Code for reading the sensor:

Download code
/*
 * This program is a modification of
 * http://www.esp32learning.com/code/esp32-and-sht31-sensor-example.php
 */
#include <Arduino.h>   // Include the Arduino library
#include <Wire.h>      // Include the Wire library for I2C communication
#include "Adafruit_SHT31.h" // Include the SHT31 library from Adafruit

Adafruit_SHT31 sht31 = Adafruit_SHT31(); // Create an instance of the SHT31 object

void setup() {
  Serial.begin(9600);        // Initialize the serial communication with a baud rate of 112500
  if (! sht31.begin(0x44)) {   // Check if SHT31 is connected and start it with address 0x44
    // Print an error message if SHT31 is not found
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);  // Wait indefinitely
  }
}

void loop() {
  // Read temperature from SHT31 and assign it to "temperature"
  float temperature = sht31.readTemperature(); 
  // Read relative humidity from SHT31 and assign it to "humidity"
  float humidity = sht31.readHumidity();

  if (! isnan(temperature)) {     // Check if t is not NaN (not a number)
    Serial.print("temp_deg_C:");  // Print temperature label
    Serial.print(temperature);    // Print temperature value
    Serial.print("\t");           // Write a tab space
  } 
  else {
    // Print an error message if temperature reading fails
    Serial.println("Failed to read temperature");
  }

  if (! isnan(humidity)) {        // Check if h is not NaN (not a number)
    Serial.print("rel_hum_%:");   // Print humidity label
    Serial.print(humidity);       // Print humidity value
  } 
  else {
    // Print an error message if humidity reading fails
    Serial.println("Failed to read humidity");
  }

  Serial.println();    // Break line
  delay(1000);         // Wait 1 second
}


16.3 VL53L0X Time-of-Flight Distance Sensor Using I2C communication

In this tutorial you can find a lot more information.

Code for reading the sensor:

Download code
#include "Adafruit_VL53L0X.h"

// also on http://www.esp32learning.com/code/vl53l0x-time-of-flight-sensor-and-esp32.php
Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
  Serial.begin(115200);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}


void loop() {
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
    
  delay(100);
}

16.4 Thingspeak

Thingspeak webpage.

16.5 Basic Code for Uploading Data to Thingspeak

Download code
// #include <ESP8266WiFi.h>
#include <WiFi.h>

#include "ThingSpeak.h"
unsigned long myChannelNumber = 1358139;
const char * myWriteAPIKey = "4HZXSWG1JXQCVHM2";

const char* ssid = "HUJI-guest"; // your wifi SSID name
const char* password = "" ;// wifi pasword
 
const char* server = "api.thingspeak.com";

WiFiClient client;


 
void setup() {
  Serial.begin(9600);
  WiFi.disconnect();
  delay(10);
  WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

    ThingSpeak.begin(client);
 
  WiFi.begin(ssid, password);
  
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("NodeMcu connected to wifi...");
  Serial.println(ssid);
  Serial.println();
}
 
void loop() {

 
  ThingSpeak.setField(1,11);
  ThingSpeak.setField(2,22);
  ThingSpeak.setField(3,33);
  ThingSpeak.setField(4,44);
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

  Serial.println("uploaded to Thingspeak server....");

  client.stop();
 
  Serial.println("Waiting to upload next reading...");
  Serial.println();
  // thingspeak needs minimum 15 sec delay between updates

 
  
  delay(20000);
}

16.6 Example: Sending Data from Sensors Above to Thingspeak

Download code
//------------------------------------------------------------------
//Thingspeak:
// #include <ESP8266WiFi.h>
#include <WiFi.h>

#include "ThingSpeak.h"
unsigned long myChannelNumber = 1689148;
const char * myWriteAPIKey = "A58V29JE8UIGWI16";

const char* ssid = "HUJI-guest"; // your wifi SSID name
const char* password = "" ;// wifi pasword
 
const char* server = "api.thingspeak.com";

WiFiClient client;

//------------------------------------------------------------------
//DS18B20:
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is connected to GPIO4
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);

//Insert the right address for your sensors
DeviceAddress sensor1 = { 0x28, 0xFF, 0x64, 0x02, 0xEF, 0x01, 0xA7, 0x31 };
DeviceAddress sensor2 = { 0x28, 0x35, 0xD3, 0x11, 0x00, 0x00, 0x00, 0xBF };

//------------------------------------------------------------------
//SHT31:
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

Adafruit_SHT31 sht31 = Adafruit_SHT31();
//------------------------------------------------------------------
//VL53LOX:

#include "Adafruit_VL53L0X.h"
Adafruit_VL53L0X lox = Adafruit_VL53L0X();


void setup() {
  Serial.begin(9600);
  //------------------------------------------------------------------
  //Thingspeak and wifi:
  WiFi.disconnect();
  delay(10);
  WiFi.begin(ssid, password);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

    ThingSpeak.begin(client);
 
  WiFi.begin(ssid, password);
  
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("NodeMcu connected to wifi...");
  Serial.println(ssid);
  Serial.println();
  //------------------------------------------------------------------
  //DS18B20:
  sensors.begin();

  //------------------------------------------------------------------
  //SHT31:
    if (! sht31.begin(0x44)) {
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }

  //------------------------------------------------------------------
  //VL53LOX:
  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
  
}
 
void loop() {
  //------------------------------------------------------------------
  //DS18B20:
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  
  Serial.print("Sensor 1(*C): ");
  Serial.print(sensors.getTempC(sensor1)); 
  Serial.print(" Sensor 1(*F): ");
  Serial.println(sensors.getTempF(sensor1)); 
 
  Serial.print("Sensor 2(*C): ");
  Serial.print(sensors.getTempC(sensor2)); 
  Serial.print(" Sensor 2(*F): ");
  Serial.println(sensors.getTempF(sensor2)); 

  //------------------------------------------------------------------
  //SHT31:
  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t))  {
    Serial.print("Temp *C = "); Serial.println(t);
  }
  else  {
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h))  {
  Serial.print("Hum. % = "); Serial.println(h);
  }
  else  {
  Serial.println("Failed to read humidity");
  }
  
  Serial.println();
  delay(1000);

  //------------------------------------------------------------------
  //VL53LOX:
   VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
    
  delay(100);

  //------------------------------------------------------------------
  //Thingspeak:
  ThingSpeak.setField(1,t); // SHT temp
  ThingSpeak.setField(2,h); // SHT humidity
  ThingSpeak.setField(3,sensors.getTempC(sensor1)); // DS18B20 in
  ThingSpeak.setField(4,sensors.getTempC(sensor2)); // DS18B20 out
  ThingSpeak.setField(5,measure.RangeMilliMeter); // distance
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);

  Serial.println("uploaded to Thingspeak server....");

  client.stop();
 
  Serial.println("Waiting to upload next reading...");
  Serial.println();
  
  // thingspeak free needs minimum 15 sec delay between updates 
  // payed can do 1sec

 
  
  delay(1000);
}