Temperature logger on ESP8266 + DS18B20 and ThingsPeak

Hello everyone. Here came the decisive day when I tried what it is Arduino . Today we build temperature logger ESP8266 + DS18B20 and ThingsPeak.
temp logger


Tried ESP8266 in conjunction with the external controller I decided to write my own code directly for this module.

There are three options for the development environment ESP8266: Direct writing code in the Eclipse environment with adobe source SDK ( it seemed to me so far complicated ), Lua script using firmware NodeMCU ( a bit strange and uncomfortable sintaksis of code for me ), Arduino IDE to program directly ESP8266 – that we will try use today.

[adsense type=”banner”]

Setting IDE:

 

 

Here is the link how to do that

 http://iot-playground.com/2-uncategorised/38-esp8266-and-arduino-ide-blink-example

And another variant in russian:

http://esp8266.ru/arduino-ide-esp8266/
Register on ThingSpeak.com – It’s a server on which we will send data.

At this stage it is important to copy somewhere “Write API Key” in video it is on 1:41 minute.

Writing sketch basis I found here.
http://iot-playground.com/2-uncategorised/41-esp8266-ds18b20-temperature-sensor-arduino-ide

/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

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


#define ONE_WIRE_BUS 2  // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);

const char* ssid     = "Konst";
const char* password = "";

const char* host = "api.thingspeak.com";
const char* APIkey   = "FQSPTF3RHFF9D3CE";


float oldTemp;



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

  // We start by connecting to a WiFi network

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

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



void loop() {
  delay(30000);

  float temp;


  DS18B20.requestTemperatures();
  temp = DS18B20.getTempCByIndex(0);
  Serial.print("Temperature: ");
  Serial.println(temp);

  char charVal[12];

  dtostrf(temp, 8, 2, charVal);

  Serial.print("connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }




  // We now create a URI for the request
  String url = "/update?key=";
  url += APIkey;
  url += "&field1=";
  url += charVal;//String(temp);



  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  delay(10);

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
}

Where

const char* ssid = "Konst";
const char* password = "";

Are fields in wich you need to enter name and password to your WIFI network .

const char* APIkey = "FQSPTF3RHFF9D3CE";

That’s Write API Key

Compiling the code CTRL+R.

Assembling schematics for ESP8266 programing.

 

 

Uploading firmware by button “Upload” at this moment GIPIO 0 must be connected to the ground.

 

 

DS18B20must be connected like so.

 

 

Let’s run our code:

If you press”serial monitor” button

 

You will see status of the program .

Temperature: 27.50
connecting to api.thingspeak.com
Requesting URL: /update?key=FQSPTF3RHFF9D3CE&field1=27.50
closing connection
Temperature: 27.56
connecting to api.thingspeak.com
Requesting URL: /update?key=FQSPTF3RHFF9D3CE&field1=27.56
closing connection

Data has sent, let’s look at result:

 

 

The data can be both readen and writen transfer rate is limited to 15 seconds per packet. That mean you can make an excellent system of “things” controll if they do not need an instant reaction. That’s all thank you for your attention.

P.S. You also should change part of dtostrf() function in file core_esp8266_noniso.c
To avoid bug that I found. For example it was 24.06 temperature but dtostrf() make 24.6 in string.
So you should replace

while(prec-- > 0) {
remainder *= 10.0;
}

to

while(prec-- > 0) {
        remainder *= 10.0;
        if((int)remainder == 0){
                *out = '0';
                 ++out;
        }
    }

4 Comments to Temperature logger on ESP8266 + DS18B20 and ThingsPeak

  1. daniel.minsonp says:

    It is showing always 85ºF. What I need to do and how can I make it show in Celsius? Thank you for this awesome tutorial!

    • dexter says:

      Looks like you have wrong ds18b20 wire connection, check it first. And by default it must write temperature in celsius. So after you checked wires and connection, watch your serial log to the value after “Temperature: ” it must be correct and in celsius compare it with etalon thermometer.

  2. Esplod says:

    Thanks for a great article!
    Connection to thingspeak worked on first try. I haven’t found many good sources of how to do this with the ESP8266 and the arduino environment. Almost all articles use Arduino and AT-commands. But what’s the point of that? 🙂
    Now I need to get it to work witn my air quality sensor.

    • dexter says:

      Yes AT commands firmware is too unstable today. Now I am using Eclipse+SDK environment and it is great, you can run web server, local socket server, mqtt client, send and receive data on thingspeak. And it works great all in same time and all you need ESP8266 module and linear regulator.

Leave a Reply