Pages

Tuesday, 8 May 2018

Connecting ESP8266 to MQTT broker



I have been looking to improve my knowledge of IOT, understand the range of devices and sensors available, data protocols, tools, and software used for development. 

I put together a simple setup with the recently purchased Wemos D1 mini board based on the ESP8266 mircocontroller, a DHT12 temperature/humidity shield, and a relay shield. The temperature and humidity is read from the DHT12 sensor at regular intervals and published to a MQTT broker. The mircocontroller also subscribes to a MQTT topic to control the relay state. 

As there are a lot of excellent references online, rather than giving a step by step ''how to guide" I will link to the references I found most useful and highlight some of the issues encountered and how to debug and address these:
  • I am using the Arduino IDE as it is easy to get up and running and has a very active community.
  • Wemos have a good guide on adding ESP8266 support to Arduino here. Once installed you will have the option in the Tools > Board:xxxx menu to select the board type. I am using 'Wemos D1 R2 & mini". I have also set Tools & Flash Size to "4M (1M SPIFFS)".
  • Install the Wemos DHT12 library from Github. Communication with the DHT sensor is over the I2C bus with default address 0x5c. 
  • The relay module is simply controlled by writing a high/low value to the appropriate digital IO pin. The ESP8266, DHT and relay shields are mounted on the Wemos tripler base. This is where I hit a problem but it is easily resolved. The relay default control is via Wemos pin D1 which conflicts with the default pins for the SPI bus used by the DHT sensor, D1 (SCL)  and D2 (SDA). The two options to solve the issue are to remap the SPI bus pins in code or reconfigure the default pin used by the relay. I decided to leave the SPI bus using the default pin assignments as the bus is widely used on projects and I want to keep it consistent across projects as it will help with debugging knowing that the SPI devices are always connects to the same pins on the controller. That left me with changing the relay control pin which was a simple task of cutting a track on the underside of the board and shorting the control pin to use D0 (GPIO16). 
  • Andreas Spiess' excellent Youtube channel gives a nice use-case and overview of MQTT with references to popular tools and setup. I installed Mosquitto MQTT broker on an old Raspberry Pi. I also found it useful to set a static IP address for the Pi as it had a habit of changing IP when not in regular use.
  • I use the PubSubClient Ardruino library for publishing the subscribing to MQTT topics on the ESP8066 client and the mqtt_esp8266.ino example provided with the library provides a simple framework to set up a connection to your MQTT server, register a callback function to handle messages received, and subscribing to and publishing topics. I set up the following three topics in the ESP8266 code:
    • #define humidity_topic "sensor/humidity"  // Topic for ESP to publish humidity data. 
    • #define temperature_topic "sensor/temperature"  // Topic for ESP to publish temperature data .
    • #define relay_topic "sensor/relay"  // ESP subscribes to this topic and parses messages to control the relay state .
  • The mosquitto_pub and mosquitto_sub client utilities installed with Mosquitto provide simple command line tools to debug your setup. Steve's Internet Guide provides extensive information of MQTT and related topics. I installed these tools on my PC and used them to listen for all published sensor topics using mosquitto_sub.exe -h MQTT_BROKER_IP -t sensor/# -v . To turn on the relay and verify the ESP callback and subscription was working I use mosquitto_pub.exe -h MQTT_BROKER_IP -t sensor/relay -m "1" -d . 

Hopefully some of these references and debug tips will help with your MQTT and ESP8266 project.



No comments:

Post a Comment