Raspberry Pi, Arduino, Sensors and the data they produce

Programming the 8266 as MQTT Publisher (with DHT-11)

You may recall my earlier post about the weather monitor from the Squix blog that I purchased. I built this unit, and then built the NodeMCU based local weather sensor, but wanted to get away from the cloud based service introduced by that author.  There are many good articles on how to use MQTT and the publish / subscription capabilities that I had to stop looking and just work with one.  I did end up with a blend of code, but a good start was found here: ESP8266 Send DHT  .

We will start with setting up the Arduino IDE, which is crucial to getting the ability to program the 8266.  As you recall there are two ways, one with Arduino IDE and the native one with NodeMCU firmware and Lua programming.  I chose Arduino. I will repeat the disclaimer: I do not know how to get back to the original code if you follow this path!


Our first step is to get the board manager installed:

Start the Adrduino IDE, then open the preferences window, and find the” Additional Boards Manager URLs” location, and enter the following:


Now we will go to add  the actual files:  Tools -> Board -> Boards Manager -> search the ESP8266 and then click Install. (choose the newest version).

Now you wait a bit.


You will need to carefully select your board, exactly as you did when choosing the Arduino Uno versus an Arduino Mini, in this case pick the proper 8266 chipset.  In my case I am using the NodeMCU 1.0 (ESP-12E). You will also find the correct port, mine is “com3:”.

Now we need to ensure the libraries are available. The way you accomplish this is: Open Arduino IDE,  Sketch->Include Library -> manage libraries, search for and find this group of libraries:



The next step is the coding. Since I started with the  Thingspeak cloud based device, I started with this code, which I then modified to update for my wfi ssid and password, and updated to segregate the temperature from the humidity, and then updated to have my  MQTT server IP address.

You can play with the code from OSOYOO above and modify, or you can clone my code here: Github Link.

So to recap, the key changes to this code are as follows.

Put your Wifi information  (SSID and Password) in here:

const char* ssid = “your_SSID”;

const char* password = “Your_PASSWORD”;

Define the Pin you have attached your DHT-11 to here:

// Define NodeMCU D6 pin to as temperature data pin of  DHT11

#define DHT11_PIN D6

And finally, put in the IP of you MQTT Broker/Server:

const char* mqtt_server = “your MQTT server IP”;

You should be ready to send your program to the 8266 with the Arduino IDE at this point.

In the next post, we’ll talk about MQTT clients, testing your environment, and then introduce Node-RED environment.










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

%d bloggers like this: