MQTT – why it is worth to look at?

Hi Readers,

mqttorg-glow

Today I would like to introduce you MQTT (MQ Telemetry Transport). I would also like to describe it’s benefits and why it is better then HTTP protocol in Internet of Things (IoT) world.

HTTP is mature and really established protocol. Is easy to use and know by almost everybody. Is perfect for request and response. But in our IoT world is not ideal. It is text based protocol which requires more bandwidth. Also requires working web server which requires small or bigger server but will not work on really small IoT devices. This could be a bit a limitation for users as web server and long text response and request will require more power which unfortunately for IoT uptime (battery )is crucial.

However MQTT is publish and subscribe (pattern) based light weight messaging protocol with quality of service. I think this already give us flexibility. In large scale systems which based on network with small end-devices being able to subscribe to one or another sensor without writing a parser sound really useful. It requires minimal bandwidth as it is binary format. It header is build from just two bytes. Which also shows that devices uses MQTT requires less battery.

In MQTT our central point is a broker which sends received messages publishes to subscribers. As simply like that:

push-mqtt-for-the-internet-of-things-7-638

 

There are number of brokers but the one most is Mosquitto (mosquitto.org). It supports most of the platform and can be installed on almost any computer.

 

2013-11-02-comms_overview

If you would like to read more about mqtt protocol in more technical view you can read it here:

http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html

 

Okay done with theory. I have just installed Mosquitto on my Raspberry PI so in next post I would like to show you a bit more DIY base on ESP8266 sensors which we used previously. But if you haven’t do it then I am inviting you to read previous posts and get more familiar with ESP8266.

-> https://gettoknowthebob.wordpress.com/

Have a nice day!

 

12295299_1691497957737189_7709343665767663600_n

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

Be positive and stay calm!

Cheers,

Radek

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