Begin typing your search above and press return to search. Press Esc to cancel.

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. 







19 comments:

  1. Very nice and helpful information has been given in this article. I like the way you explain the things. Keep posting. Thanks..

    http://jakinid.com/iot-solutions/

    ReplyDelete
  2. hello.

    PLEASE HELP ME SORTING THIS ERROR

    ""exit status 1
    Error compiling for board Arduino/Genuino Uno.""

    THIS ERROR OCCUR IN EACH AND EVERY BOARD I HAVE SELECTED.

    ReplyDelete
    Replies
    1. MY ALSO SHOWING SAME PROBLEM.. have you got any solution

      Delete
  3. Hello!
    Why I have this erorr ?? : #error "Please define I/O register types here"
    Thanks !

    ReplyDelete
    Replies
    1. I am having the same issue, can you help?

      Delete
  4. https://github.com/adafruit/ESP8266-Arduino/tree/esp8266/libraries/OneWire

    ReplyDelete
  5. in arduino software i am unable to see port:COM14 it is showing COM3 for me .so how to change the port to COM14 you mentioned in the video .if any change i have to do please send it to mothe.manideep502@gmail.com

    ReplyDelete
  6. This is gigantic thus exact here. Much obliged to you for posting them. Extraordinary bit of writing given that much drawing closer into the part. These sorts of articles are truly prepared to assist for the humanity and one who are secretive with these. Custom essay Writing Service is an accomplished, multitasking and solid online custom writing affiliation, went for giving competent writing help to understudies wherever all through the world. this service center their endeavors on the understudies' most astonishing results, smart illuminating improvement and information change.

    ReplyDelete
  7. Hi, I have problem: stray '\302' in program. Any ideas?

    ReplyDelete
  8. I think that all it's correct but always temperature is -127. I tried with differents DS18b.... Some ideas?

    ReplyDelete
  9. Hi,

    I've got the same probems like nelun and Abhinav. (#error "Please define I/O register types here")
    I saw that dubnikov replied with a solution but I still don't understand.
    Can I have more information about that. Thanks

    ReplyDelete
  10. Hi, Problem solved for the I/O register.
    Now got problem that connect wifi was not declared. Anybody some info?
    Thanks

    ReplyDelete
  11. Thanks for this. I've been trying to do this by various means but this work straight off. Just needed to change the pin number. I would say that a delay in the first code is needed because it reads too quickly. Thank you.

    ReplyDelete
  12. I have enjoyed reading through the article and the comments. I have learned a lot and understood the topic under discussion from a different point of view. I take this opportunity to thank the author for the continual commitment to keep us updated with quality articles that are not only entertaining but also educative. Feel free to hire our professional writers by clicking on PhD Dissertation Data Analysis Using Stata Software.

    ReplyDelete
  13. The internet has changes the way the people apply their thinking into the development of their own ideas. I am very sure that the scholars are one of the categories of people who are applying the internet of thinking into their Research Paper Sentences so as to produce high-quality research content. this article has clearly unveiled the importance of the internet of thinking.

    ReplyDelete
  14. Nice work with good ideas and concepts, lots of great information which we all need, helpful information. I would like to thank you for the efforts you shown remarkable writing skill in this article Custom Essay Writing Service

    ReplyDelete