Thingspeak – with Nodemcu (ESP8266) and DS18B20 thermometer sensor (part2)

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.

Account

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).
Chan1

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() {
Serial.begin(115200);
delay(10);

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print(“Connecting with “);
Serial.println(ssid);

WiFi.begin(ssid, password);

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

void loop() {
float temp;
DS18B20.requestTemperatures();
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: “);
client.print(postStr.length());
client.print(“\n\n”);
client.print(postStr);

Serial.print(“Temperature: “);
Serial.println(temp);
}
client.stop();

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

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

IMG_20160424_191928

 

Thingspeak & ESP8266Network channel

So this is how it looks after couple hours:

Chan2

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:
readAPIKey = ‘BR6TVGG4CVSK9EID’;

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

And results:

Chan3

 

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

Have a nice day!

 

1919035_1699744840245834_8385952294039797213_n

 

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

Be positive and stay calm!

Cheers,

Radek

 

Thingspeak – easy to use IoT platform (part1)

Hi Readers,

Today I would like to introduce Thingspeak – open data platform for the IoT.

This platform allows you to simply collect data from your sensor then analyze it and trigger selected action:

CollectCollect – Send sensor data to the cloud.

AnalyzeAnalyze – Analyze and visualize your data.

ActAct – Trigger a reaction.

ThingSpeak Features

  • Real-time data collection and storage
  • MATLAB® analytics and visualizations
  • Alerts
  • Scheduling
  • Device communication
  • Open API
  • Geolocation data
  • Available on GitHub®

 

Personally I especially like ThingSpeak from its easy to use web panel and API. Last couple months I had many problems with ESP8266, nodemcu or some with domoticz configuration, uploading firmwares or testing tools and IDEs. I have spend many times hours on small problems which had resulting during tasks. And here I found ThingSpeak which I wanted to test. And you know what? In just 10minutes (really) I have created new account I have added new channel (device) and I have learned basic API example. Easy peazy, first time.

In my next blog post I will show you example source code I have created to upload data to thingspeak base on their API. Which again, wasn’t so hard.

Next thing I did base on temperature data I have tried to create basic Matlab project which was calculating average temperature in my home. Pretty easy. I will paste here Matlan source as and example.

% Channel ID to read data from
readChannelID = YourChannelId;
% Temperature Field ID
TemperatureFieldID = YourFieldId;

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

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

This is very useful feature. Last year we have rent new apartment. It is a lovely place but walls are really thin. When winter came it was really cold. I was heating apartment to 22-23 degrees in evening to have in the morning something close to 6-8 degrees which is really cold when you are getting up from under your linen. And today I see how useful it would be to store this data and next day see how fast temperature is decreasing. After that use this matlab code to get average and then easily program your central heating to be effective.

Of course we can buy thermometers which already do it. There is a lot of devices on the market but here you can do it in DIY manner based on 2$ nodemcu and spending time on something useful. Only benefits and all of it for almost free.

This is why I liked ThingSpeak so much. It is really cool platform for everybody.

 

12938301_1730343557185962_4719402062987769740_n

 

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

Be positive and stay calm!

Cheers,

Radek