Upload DS18b20 Temperature Sensor Data to Thingspeak from Esp8266 (nodemcu)

Hello all in this tutorial you will know how to use Ds18b20 Temperature data to thingspeak.com, you can follow above fritzing circuit diagram to control the Ds18b20 temperature sensor, This sensor follows one wire protocol which means you can connect many sensors as you want to the single pin and access temperature data calling the sensor by address.

check the video below on how to work with this sensor and also proof of code working .



There is no much work to get work with this sensor, as there are plenty of example library that already available in the internet which anyone can make use and get started to work with this sensor. 

Test the below code to know check whether you can get reading from the temperature sensor with ESP8266 or Nodemcu

if you get any error , make sure you have downloaded library for Ds18b20 Temperature sensor. 
----------------------------------------------------------------------------------------------------------------------
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
 // start serial port
 Serial.begin(9600);
 Serial.println("Dallas Temperature IC Control Library Demo");

 // Start up the library
 sensors.begin();
}


void loop(void)
{
 // call sensors.requestTemperatures() to issue a global temperature
 // request to all devices on the bus
 Serial.print(" Requesting temperatures...");
 sensors.requestTemperatures(); // Send the command to get temperatures
 Serial.println("DONE");

 Serial.print("Temperature for Device 1 is: ");
 Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
   // You can have more than one IC on the same bus.
   // 0 refers to the first IC on the wire
}


-----------------------------------------------------------------------------------------------------------------------

copy paste above code to your Arduino IDE and upload to your ESP8266 or Nodemcu if you are not sure about how to do this please check the video about to know how things work, if you don't know yet to search search on this blog for getting started with Esp8266 in Arduino IDE.

below you can see the code for sending temperature data to thingspeak.com from Esp8266 or Nodemcu to do this you need have thingspeak api key which can get easily by registering to the website.

change SSID and password to your router password and also update the Nodemcu api key.

--------------------------------------------------------------------------------------------------------------------------
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

#define myPeriodic 15 //in sec | Thingspeak pub is 15sec
#define ONE_WIRE_BUS 2  // DS18B20 on arduino pin2 corresponds to D4 on physical board

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float prevTemp = 0;
const char* server = "api.thingspeak.com";
String apiKey ="Things speak api key";
const char* MY_SSID = "your router name ";
const char* MY_PWD = "your wifi password";
int sent = 0;
void setup() {
 Serial.begin(115200);
 connectWifi();
}

void loop() {
 float temp;
 //char buffer[10];
 DS18B20.requestTemperatures();
 temp = DS18B20.getTempCByIndex(0);
 //String tempC = dtostrf(temp, 4, 1, buffer);//handled in sendTemp()
 Serial.print(String(sent)+" Temperature: ");
 Serial.println(temp);
 
 //if (temp != prevTemp)
 //{
 //sendTeperatureTS(temp);
 //prevTemp = temp;
 //}
 
 sendTeperatureTS(temp);
 int count = myPeriodic;
 while(count--)
 delay(1000);
}

void connectWifi()
{
 Serial.print("Connecting to "+*MY_SSID);
 WiFi.begin(MY_SSID, MY_PWD);
 while (WiFi.status() != WL_CONNECTED) {
 delay(1000);
 Serial.print(".");
 }
 
 Serial.println("");
 Serial.println("Connected");
 Serial.println("");  
}//end connect

void sendTeperatureTS(float temp)
{  
  WiFiClient client;
 
  if (client.connect(server, 80)) { // use ip 184.106.153.149 or api.thingspeak.com
  Serial.println("WiFi Client connected ");
  
  String postStr = apiKey;
  postStr += "&field1=";
  postStr += String(temp);
  postStr += "\r\n\r\n";
  
  client.print("POST /update HTTP/1.1\n");
  client.print("Host: api.thingspeak.com\n");
  client.print("Connection: close\n");
  client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  client.print("Content-Type: application/x-www-form-urlencoded\n");
  client.print("Content-Length: ");
  client.print(postStr.length());
  client.print("\n\n");
  client.print(postStr);
  delay(1000);
  
  }//end if
  sent++;
client.stop();
}//end send






--------------------------------------------------------------------------------------------------------------------------


if you like the above tutorial and if you want try out with cool projects you can also check this link here , that's the amazon book link where you can use that book to make IoT with Esp8266 or Nodemcu, that books gives you basic coverage on how to do simple things and get yourself started with arduino and goes on developing projects like sending data to webserver and creating a webserver, uploading and controlling data from a webpage, how to interface TFT LCD and I2C devices and many more things can find on the link.

Thanks for stopping by and read this blog. 

Comments