⬆️ ⬇️

People monitoring ESP8266 MQTT Micropython

How quickly, without much investment, to start uploading weather data to popular monitoring?

I will describe one of the solutions based on ESP8266.



The operation algorithm is simple: the controller connects to the wi-fi once every five minutes, connects to the broker, measures the temperature and sends it to the broker. The rest of the time is in sleep mode.

This article does not address the installation of micropython on esp8266 and the physical connection of the sensor. This is all easily googled.



Let's start the implementation with debugging the sleep mode and the activation period.



import machine rtc = machine.RTC() rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP) rtc.alarm(rtc.ALARM0, 5*60*1000) #   deepsleep    5    ,   reset #   wake  reset,   LoLin: D0  RST machine.deepsleep() 


Next, set up a connection to the wi-fi network:

')

 import network sta_if = network.WLAN(network.STA_IF) sta_if.active(True) sta_if.connect('SSID', 'password') while not sta_if.isconnected(): pass 


We check everything through the REPL that sta_if.isconnected () returns True.



Now I propose to learn the mac address of esp8266, it will come in handy further:



 import network import ubinascii ubinascii.hexlify(network.WLAN().config('mac'),':').decode() 


Next you need to register on the site of national monitoring.

Next sensors -> add my device -> enter our MAC.



Now the most interesting, sending temperature on MQTT:



 #   from umqtt.simple import MQTTClient client=MQTTClient(client_id='01:02:03:04:05:06', server='narodmon.ru', port=1883, user='login', password='12345') client.connect() #  import onewire ow=onewire.OneWire(machine.Pin(0)) import ds18x20 ds=ds18x20.DS18X20(ow) roms=ds.scan() ds.convert_temp() time.sleep_ms(750) temperature=ds.read_temp(roms[0]) # client.publish('dinartal/esp8266/temperature', str(temperature)) #  time.sleep(3) 


We look into the log through the repl, if all the rules, then you can go to admire the testimony of the national monitoring.



image



PS: If you assemble a normal circuit, without a usb-uart bridge, and a linear stub with poor efficiency, then from a battery such a device will be able to show good autonomy.

Source: https://habr.com/ru/post/341716/



All Articles