ESP8266 with webserver-MQTT-Emoncms-Thingspeak-OTA

The graph shows that  when in automode, the humidifier is switched on when the  humidity  goes  2 degrees (the hysteresis) under the Setpoint
The graph shows that when in automode, the humidifier is switched on when the humidity goes 2 degrees (the hysteresis) under the Setpoint

On Github, Krzysztof has published a truly excellent series of articles regarding the use of an ESP8266 that reads a humidity sensor and publishes the data on a personal webserver, as well as on emoncms (an energymonitoring cloud) and allows to  take action depending on the reading of the  sensor, either manually or automatically and to do that via a webserver, or via MQTT or OpenHAB.
Anyone who is interested in this subject should surely read his  set of articles that starts basically from scratch and keeps adding functionality.

As I am not really partial to emoncms, but more of a Thingspeak user, I decided to add a Thingspeak module to his program.
As a base I used this program.
To add Thingspeak functionality do the following:
under the section: Emoncms configuration add:

// Thingspeak configuration
const char* thingspeakServer = "";
const char* writeAPIKey = "yourThingspeakWriteApi";
// function prototypes required by Arduino IDE 1.6.7
void  sendDataToThingspeak(void);

in the main loop (thus in void loop(void) )
above (or below) sendDataToEmoncms();
add: sendDataToThingspeak();

Add a tab called “Thingspeak” and add there the following:

void  sendDataToThingspeak(void)
// make TCP connections
WiFiClient client;//this one is probably not necessary
const int httpPort = 80;
if (!client.connect(thingspeakServer, httpPort)) {

String url = "/update?key=";
url += writeAPIKey;
url += "&field1=";
url += String(humidity);
url += "&field2=";
url += String(humiditySetPoint );
url += "&field3=";
url += String(humidifier);
url += "&field4=";
url += String(autoMode);

url += "\r\n";

// Send request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + thingspeakServer + "\r\n" +
"Connection: close\r\n\r\n");

Ofcourse one still needs to setup a Thingspeak account, but that is well known how I presume. One also needs to setup an Emoncms account, buthat is well described in the series of articles, or one can decide to delete that functionality.
For MQTT I have used a public broker, but one can decide to set it up on say a Raspberry Pi


8 thoughts on “ESP8266 with webserver-MQTT-Emoncms-Thingspeak-OTA

  1. Great tip, thank you. If your want to use emoncms, be aware that the JSON string krzychb assembles is incorrect; it does not adhere to json standards. Formal JSON requires the keyword to also be enclosed in quotes. Some parsers are easy on this and it seems emoncms uses such parser.

    1. Thanks Jeroen. I am not really partial to Emoncms, it is mainly focussed on Energy monitoring, hence my addition of Thingspeak, but useful to know your correction.
      Just for completeness sake I will see if I can add a Twitter module as well, not because I need it, but because I can 🙂
      All in all though Krzysztof wrote a great educational piece.

  2. I also tried to connect a DHT 11 to a NodeMCU but that didn’t work with the standard DHT library from ADA fruit.
    Had to adjust the library file and copy some other library files to compile without any errors..

    1. Indeed. Although the error is described somewhere on the Adafruit forum, as far as I know it still is in the library

    2. Tom I reasise my reply must have been cryptic. I presumed you were talking about a problem with the adafruit library and the dht sensor that Idescribed elsewhere. I wasnt aware of a compilation error, but i am happy you had it solved

Leave a Reply

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

You are commenting using your 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