members
Basics: Project 070m ESP8266 ESP-12E module MQTT
of Acoptex.com in ESP8266 ESP-12
Basics: Project 070m
Project name: ESP8266 ESP-12E module MQTT
Tags: ESP, ESP8266, WI FI module, ESP-12E, Ai Thinker, AI-Cloud, SOC, GPIO, General Purpose Input Output, System On a Chip, IOT, internet of things, FTDI232, FTDI 232, how to upload sketch, LoLin NODEMCU V3, NODEMCU, V3, Development Board with USB-to-Serial Onboard, NodeMCU DevKit, MQTT, DHT 11 21 22, DHT11, DHT21, DHT22 module, temperature, humidity, Raspberry, Raspberry PI 3 model B board
Attachments: library1, library2, library4, sketch1
In this project, you need these parts :
1.Raspberry PI 3 model B 1 pc
2. Micro SD card and SD card adapter 1 pc
3. Micro USB power supply (2.1 A, max 2.5 A) 1 pc
4. USB keyboard 1 pc
5. USB mouse 1 pc
6. TV or PC monitor 1 pc
7. ESP8266 ESP-12E module with micro USB cable 1pc
8.Arduino IDE ( you can download it from here )
9.Jumper cables F-M, M-M
10. Resistor 2 pcs (1pc - 10 KOhm, 1pc - 220 Ohm)
11. DHT11 21 22 Humidity and temperature module 1pc
12. Breadboard 1 pc
13. LED 1 pc (any color)

General
We will learn how to control ESP8266 ESP-12E module (LoLin NODEMCU V3) outputs and display sensor data from the ESP8266 ESP-12E module on Node-RED. The Node-RED software is running on a Raspberry PI 3 model B, and the communication between the ESP8266 ESP-12E module and the Node-RED software is achieved with the MQTT communication protocol.
MQTT Protocol
In this project we are going to establish a communication between a Raspberry PI 3 model B running the Node-RED software and an ESP8266 ESP-12E module using MQTT.
MQTT stands for MQ Telemetry Transport and it is a nice lightweight publish and subscribe system where you can publish and receive messages as a client. It is a simple messaging protocol, designed for constrained devices and with low-bandwidth.
Mosquitto Broker
The broker is primarily responsible for receiving all messages, filtering the messages, decide who is interested in it and then publishing the message to all subscribed clients in MQTT.
There are several brokers you can use. In this project we’re going to use the Mosquitto Broker which needs to be installed on Raspberry PI 3 model B.
Understanding the Raspberry PI 3 model B
The Raspberry Pi 3 is the third-generation Raspberry Pi. It replaced the Raspberry Pi 2 Model B in February 2016.
Specification:
- Quad Core 1.2GHz Broadcom BCM2837 64bit CPU
- 1GB RAM
- BCM43438 wireless LAN and Bluetooth Low Energy (BLE) on board
- 40-pin extended GPIO
- 4 USB 2 ports
- 4 Pole stereo output and composite video port
- Full size HDMI
- CSI camera port for connecting a Raspberry Pi camera
- DSI display port for connecting a Raspberry Pi touchscreen display
- Micro SD port for loading your operating system and storing data
- Upgraded switched Micro USB power source up to 2.5A
Understanding the ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3)
You can read more about it here.
Understanding the DHT 11 21 22 module
You can read more about it here.
Signals and connections of the DHT 11 21 22 module
There are two different versions of the DHT11 you might come across. One type has four pins, and the other type has three pins and is mounted to a small PCB. The PCB mounted version is nice because it includes a surface mounted 10K Ohm pull up resistor for the signal line. Here are the pin outs for both versions:
Signals and connections of the Raspberry PI 3 model B
TX - transmit pin. GPIO pin
RX - receive pin. GPIO pin
3V3 (or 3V or 3.3V) - power supply pin (3-3.6V).
GND ( or G) - ground pin.
RST - reset pin. Keep it on high (3.3V) for normal operation. Put it on 0V to reset the chip.
EN - Chip enable. Keep it on high (3.3V) for normal operation.
Vin - External power supply 5VDC.
D0-D8 - GPIO (General Purpose Input Output) pins
D5-D8 - SPI interface
D1-D2– I²C/TWI Interface
SC (or CMD) - (Chip Select) - the pin that the master can use to enable and disable specific devices. GPIO pin
SO (or SDO) - Master In Slave Out (MISO) - SPI communication. The Slave line for sending data to the master. GPIO pin
SK (or CLK) - SCK (Serial Clock) - SPI communication.The clock pulses which synchronize data transmission generated by the master. GPIO pin
S1 (or SD1) - Master Out/Slave In (MOSI). SPI communication. The Master line for sending data to the peripherals. GPIO pin
S2 (or SD2) - GPIO pin
S3 (or SD3) - GPIO pin
VU (or VUSB) - external power 5VDC.
A0 - ADC output.
RSV - reserved
Wiring
Step by Step instruction
1. Preparations
- Insert your micro SD card with Raspbian OS to Raspberry Pi 3 board.
- Connect the TV or PC monitor Display port (HDMI Port) to your Raspberry PI 3 board HDMI Port (HDMI cable required).
- Make sure that your monitor or TV is turned on, and that you have selected the right input (e.g. HDMI 1, etc).
- Plug in your USB mouse and USB keyboard to Raspberry PI 3 board USB ports.
- Get connected. Connect an Ethernet cable to 10/100 LAN port of Raspberry Pi 3 or plug in WiFi adapter (see the list of supported here) to USB port of Raspberry Pi 3.
- Connect Micro USB power supply to Raspberry PI 3 board micro USB input.
- The Raspberry PI desktop will start up.
- Install Node-RED on you Raspberry PI.
- Install Node-RED Dashboard on you Raspberry PI.
- Install Mosquitto Broker on your Raspberry PI.
2. Establishing a MQTT communication with Node-RED
- We will create the dashboard layout. In this project, we’ll have a button to control an ESP8266 ESP-12E module output; a chart and a gauge to display temperature and humidity readings from the DHT11 sensor.
- Go to Terminal.
- You need to know your Raspberry Pi 3 IP address in order to access your video streaming web server. Use this command: hostname -I
- You will see Raspberry Pi 3 IP address. In our case it is 192.168.0.121.
- Run the commands: sudo apt-get update and sudo apt-get upgrade to make sure that you have all packages up to date.
- Go to Raspberry icon-> Programming -> Node-RED
- You should see a window displaying information about Node-RED starting up.
- On your PC go to web browser (Google chrome or Microsoft Edge). We are using PC with Windows 10.
- Locate the address bar at the top and type: http://your_Raspberry_Pi_IP_address:1880 (in our case that is http://192.168.0.121:1880) then press Enter button. You will see the Node-RED.
- On right top corner of the Node-RED window you need to go to menu to select the dashboard.
- To add a tab to the user interface click on the +tab button.
- Once created, you can edit the tab by clicking on the edit button.
- You can edit the tab’s name and change its icon. Name: you can use any (we used Room); Icon: you should use a name accordingly to the icon’s names here. Press Update button.
- To add a group to the tab, you need to click on the +group button. You can edit the created group by clicking on the edit button. You can edit its name, select its corresponding tab and change its width. Press Update button. Create two groups: Lamp and Sensor inside the Room tab.
- Drag the following nodes to the flow, from Input - MQTT two times, from Output - MQTT one time, from Dashboard - Switch, Chart, Gauge:
switch to control the ESP8266 ESP-12E module output; mqtt output node – to publish a message to the ESP8266 ESP-12E module accordingly to the switch state;2x mqtt input nodes – these nodes will be subscribed to the temperature and humidity topics to receive sensor data from the ESP8266 ESP-12E module; chart – to display the temperature sensor readings; gauge – to display the humidity sensor readings.
- Node-RED and the MQTT broker should be connected. To connect the MQTT broker to Node-REd, double-click the MQTT output node. A new window pops up.
- Click the Add new mqtt-broker option (pencil sign). Type localhost in the server field. All the other settings are configured properly by default.
- Press Add button.
- Edit all the node's properties as shown in the following figure and press Done button - the MQTT output node automatically connects to your broker.
- switch – the switch sends an on string message when it’s ON; and sends an off string message when it’s OFF. This node will publish on the room/lamp topic. Your ESP8266 ESP-12E module will then be subscribed to this topic, to receive its messages. Double-click the switch output node.
- Edit all the node's properties as shown in the following figure and press Done button.
- Double-click on mqtt input node. This node is subscribed to the room/temperature topic to receive temperature sensor data from the ESP8266 ESP-12E module. The ESP8266 ESP-12E module will be pusblishing the temperature readings on this topic. Edit all the node's properties as shown in the following figure and press Done button.
- Double-click on chart node. The chart will display the readings received on the room/temperature topic. Edit all the node's properties as shown in the following figure and press Done button.
- Double-click on mqtt input node. This node is subscribed to the room/humidity topic to receive humidity sensor data from the ESP8266 ESP-12E module. The ESP8266 ESP-12E module will be pusblishing the humidity readings on this same topic. Edit all the node's properties as shown in the following figure and press Done button.
- Double-click on gauge node. The gauge will display the readings received on the room/humidity topic. Edit all the node's properties as shown in the following figure and press Done button.
- Wire your nodes:
- Your Node-RED application is ready. Click Deploy button on the top right corner.
- Go to Web browser.
- Locate the address bar at the top and type: http://your_Raspberry_Pi_IP_address:1880/ui (in our case that is http://192.168.0.121:1880/ui) then press Enter button. This will display the Node-RED dashboard.
3. Adding ESP8266 platform to Arduino IDE
The Arduino environment has to be set up to make it compatible with the ESP8266 ESP-12E module.
We are using PC with Windows 10 OS.
1. Installation of ESP8266 add-on in Arduino IDE on Windows OS.
- Download and install the Arduino IDE version 1.6.13 Windows Installer. You can download it here.
- Open Arduino IDE.
- Open the Preferences window from the Arduino IDE. Go to File -> Preferences.
- Enter http://arduino.esp8266.com/stable/package_esp8266com_index.json into Additional Board Manager URLs field and click the “OK” button. If you already have a URL in there, and want to keep it, you can separate multiple URLs by placing a comma between them. (Arduino 1.6.5 added an expanded text box, separate links in here by line.)
- Open Boards manager. Go to Tools -> Board -> Boards Manager…
- There should be a couple new entries in addition to the standard Arduino boards. Look for esp8266. or scroll down to the ESP8266 entry (usually at the bottom). Select the ESP8266 entry. When you click it an install option will appear. Select the latest version and click install.
- The board definitions and tools for the ESP8266 include a whole new set of gcc, g++, and other reasonably large, compiled binaries, so it may take a few minutes to download and install (the archived file is ~110MB). Once the installation has completed, an Arduino-blue “INSTALLED” will appear next to the entry.
2. Uploading sketch to ESP8266 ESP-12E development board
If you’re using an ESP-12E NodeMCU Kit, uploading the sketch is very simple, since it has built-in programmer.
Before use ESP8266 ESP-12E WI FI module (LoLin NODEMCU V3), you need to download the manufacture's driver (CH340) for this chip and install it in your PC. Here is the link. See the description of driver installation package below: CH340 / CH341 USB to serial WINDOWS driver installation package that supports 32/64 bit Windows 10 / 8.1 / 8/7 / VISTA / XP, SERVER 2016/2012/2008/2003, 2000 / ME / 98, through Microsoft digital signature authentication, support USB to 3-wire and 9-wire serial port, with the product release To the end user. Applicable scope: CH340G, CH340C, CH340B, CH340E, CH340T, CH340R, CH341A, CH341T, CH341H chips.
If you have CP2102 chip then you need to download the manufacture's driver for this chip and install it in your PC. Driver for Mac, Windows, Linux or more..
- Do wiring.
- Open Arduino IDE.
- Plug your ESP8266 ESP-12E module into your PC USB port.
- Choose your NodeMCU board. Go to Tools -> Board -> NodeMCU 1.0 (ESP-12E Module
)
- Select the correct com port.
- Open and edit the sketch1 with your own SSID, password and Raspberry IP address.
- Compile and upload it to your ESP8266 ESP-12E module. If everything went as expected, you should see a “Done uploading” message.
- Press RST button on your ESP8266 ESP-12E module to reset it.
- This code publishes messages of the temperature and humidity from the DHT11 sensor on the room/temperature and room/humidity topics trough MQTT protocol. The ESP8266 ESP-12E module is subscribed to the room/lamp topic to receive the messages published on that topic by the Node-RED application, to turn the lamp ON/OFF. This code is also compatible with other DHT sensors – you just need to uncomment and comment the right lines of code to chose your sensor.
- Open the Serial Monitor at a baud rate of 115200. When your Raspberry Pi is running your Node-RED application and the Mosquitto broker, you can see what’s happening in real time. This is helpful to check if the ESP8266 ESP-12E module has established a successful connection to your router and to the Mosquitto broker. You can also see the messages the ESP8266 ESP-12E module is receiving and publishing.
4. Running your application
- Go to Web browser.
- Locate the address bar at the top and type: http://your_Raspberry_Pi_IP_address:1880/ui (in our case that is http://192.168.0.121:1880/ui) then press Enter button. This will display the Node-RED dashboard.
- Congrats!!! You project is now completed.
Summary
We have learnt how to control ESP8266 ESP-12E module (LoLin NODEMCU V3) outputs and display sensor data from the ESP8266 ESP-12E module on Node-RED.
Libraries
- All libraries attached on the begining of this project description.
- Wire library included in your Arduino IDE.
- PubSubClient library included. Download, unzip and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here. Provides a client for doing simple publish/subscribe messaging with a server that supports MQTT (basically allows your ESP8266 ESP-12E module to talk with Node-RED).
- Adafruit_Sensor library included. Download, unzip and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. You can read more about it here.
- DHT-sensor library included. Download, unzip and add to libraries in our PC, for example C:\Users\toshiba\Documents\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC. OR You can install it in Arduino IDE too: select Sketch-> Include library->Manage your libraries->type DHT in Filter your search line and you will see DHT sensor library by Adafruit->More info->select version->install. If it will be problem with compilation - remove 2 files from the library - DHT_U.cpp and DHT_U.h.
Sketch
- See attachments on the begining of this project
Other projects of Acoptex.com










Viewed: 3245 times