MicroPython MQTT on ESP8266

I’ve got an ESP8266 running MicroPython collecting temperature and RH using a AHT10 sensor. Now I’d like to send the data to my emonSD server using MQTT. But MicroPython and MQTT are both new to me. I’ve seen some MQTT examples for the ESP8266 but they don’t use authentication to connect to the broker. Can anyone provide (or point to) working examples of MQTT running with MicroPython that successfully sends data to emonSD?

Unless you are trying to learn MicroPython, why not simply use ESPHome - AHT10 Temperature+Humidity Sensor — ESPHome

Sorry can’t help with MicroPython.

Thanks - I am trying to learn MicroPython! But I will also put learning ESPHome and Home Assistant on my to-do list!

I did get MicroPython code running on a ESP8266 board that successfully reads 2 separate AHT10 boards (on separate I2C instances) attached to the ESP8266 and posts readings to my local emonSD server using MQTT. I’m happy to share the code if anyone is interested.

1 Like

Please do :slight_smile:

Here’s how I setup an ESP8266 board with MicroPython to read AHT10 temperature boards and publish readings via MQTT. I am just learning about these boards and MicroPython, so I welcome any suggestions for improvement. I pretty much copied examples I found online and changed things here and there until it worked. My ESP8266 is a NodeMCU CP2102 ESP-12E.

When using MicroPython with the ESP8266, there are always boot.py and main.py files at the root. The boot.py code runs first then main.py. Additional drivers, libraries, and packages can also be installed. I installed drivers for MQTT and the AHT10 temperature/RH board by saving the files listed below to root using the uPyCraft IDE.

The MQTT library I got from https://raw.githubusercontent.com/RuiSantosdotme/ESP-MicroPython/master/code/MQTT/umqttsimple.py. I changed the def __init__ line by deleting the two instances of =None that were after user and password so I could pass the authentication that emon uses. Here’s the modified line:
def __init__(self, client_id, server, user, password, port=0, keepalive=0, ssl=False, ssl_params={}):
Nothing else needed changing in this file.

The AHT10 driver file I used is from micropython_ahtx0/ahtx0.py at master · targetblank/micropython_ahtx0 · GitHub and it did not require any modifications.

For boot.py, I use an example I found online that simply provides the code to connect the ESP8266 with my wifi:

#boot.py - boot.py just contains boilerplate to connect to wifi

def connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        sta_if.active(True)
        sta_if.connect('mySSID', 'mySSIDpassphrase')
        while not sta_if.isconnected():
            pass # wait till connection
    print('network config:', sta_if.ifconfig())
    
connect()

Finally, the main.py file:

#main.py - Reads a AHT10 sensor and reports temperature and RH to emon via MQTT

from umqttsimple import MQTTClient
import ahtx0
import utime
from machine import Pin, I2C

#I2C setup
i2c = I2C(scl=Pin(5), sda=Pin(4))
sensor = ahtx0.AHT10(i2c)

#MQTT setup
CLIENT_NAME = 'Board-002'
BROKER_ADDR = '192.168.1.82'
USER = USER = 'emonpi' 
PASSWORD = 'emonpimqtt2016' 
TOPIC_1 = 'emon/Basement/Basement_T1'
TOPIC_2 = 'emon/Basement/Basement_RH1'
mqttc = MQTTClient(CLIENT_NAME, BROKER_ADDR, USER, PASSWORD)
mqttc.connect()

while True:
    #Read and print temperature and humidity
    print('\nTemperature: %0.2f C' % sensor.temperature)
    print('Humidity: %0.2f %%' % sensor.relative_humidity)
    
    #Briefly flash on-board LED for each measurement 
    Pin(2, Pin.OUT).value(0) 
    utime.sleep(0.1)
    Pin(2, Pin.OUT).value(1)
    
    #Publish 
    mqttc.publish(TOPIC_1, str(sensor.temperature))
    mqttc.publish(TOPIC_2, str(sensor.relative_humidity)) 

    #Pause for desired interval
    utime.sleep(60)

utime, Pin, and I2C are standard libraries in MicroPython.
The ESP8266 board uses software I2C, so any two GPIO pins can be designated for SCL and SDA (in fact two or more I2C instances can be created, so two or more AHT10 boards can be used on one ESP8266). I put 4.7k Ohm pull-up resistors connecting pins 5 (labeled “D1” on my board) and 4 (“D2”) to 3v3.

Hopefully the rest of the main.py code is clear, but ask me if it isn’t.

1 Like