Hi Readers,
Last post I wrote about Thingspeak – easy to use IoT platform (part1) where I hope I showed you why I really liked this platform. I invite you to read it before reading this post.
Today I would like to show you how to use your device (in my case it was Nodemcu and ESP8266) with thingspeak account. How to read temperature from DS18B20 sensor and upload it to right thingspeak channel field.


First thing you need to do you need to create account on thingspeak.com.

Channels / device

Next thing you need to add new channel which could be your device or group of devices. Each channel have 8 fields which then you can use to store data from your device or devices. I have used one channel with one field where I am writing temperature values every 17 seconds (thingspeak is able to store data after 16s).

Source code

I think there is not ESP6288 on supported list of devices but as they API is really easy to use we can use it to send data to ThingSpeak from any device which has internet connection.
Below I have added source code I have used on my Nodemcu (ESP8266-12E).
It read data from DS18B20 sensor then send it to thingspeak server. This way updates temperature value.

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

#define myPeriodic 15 // Seconds
#define ONE_WIRE_BUS 2 // DS18B20 on arduino pin2 corresponds to D4 on physical board

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
WiFiClient client;

// replace with your channel’s thingspeak API key,
String apiKey = “B9HSYHJWRKYUILAA”;
const char* ssid = “ssid”;
const char* password = “password”;
const char* server = “api.thingspeak.com”;

void setup() {

WiFi.begin(ssid, password);

Serial.print(“Connecting with “);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
Serial.println(“WiFi Connected”);

void loop() {
float temp;
temp = DS18B20.getTempCByIndex(0);

if (client.connect(server,80)) {
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: “);

Serial.print(“Temperature: “);

delay(17000); // 17sec delay between updates

Source code file you can download from my gettoknowthebob github repository.



Thingspeak & ESP8266Network channel

So this is how it looks after couple hours:


Thingspeak also offers matlab code run tool which you can use to process all gained data in the way you need.

For example I have used example matlab code to calculate average temperature I have at home.

Matlab source code:

% Channel ID to read data from
readChannelID = 118421;
% Temperature Field ID
TemperatureFieldID = 1;

% Channel Read API Key
% If your channel is private, then enter the read API
% Key between the ” below:

temperature = thingSpeakRead(readChannelID, ‘Fields’, TemperatureFieldID, ‘NumMinutes’, 60, ‘ReadKey’, readAPIKey);
% Calculate the average temperature
avgTemperature = mean(temperature);
display(avgTemperature, ‘Average Temperature’)

And results:



I hope it helped you. Give me a should in comments when you will have some questions.

Have a nice day!




Github repository: https://goo.gl/ii9xsz

Be positive and stay calm!





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s