Skip to content

Instantly share code, notes, and snippets.

@sarasantos
Created Aug 21, 2020
Embed
What would you like to do?
Publish to Node-RED on Digital Ocean (MicroPython)
import time
from umqttsimple import MQTTClient
import ubinascii
import machine
import micropython
import network
import esp
from machine import Pin
#import onewire
#import ds18x20
esp.osdebug(None)
import gc
gc.collect()
ssid = 'REPLACE_WITH_YOUR_SSID'
password = 'REPLACE_WITH_YOUR_PASSWORD'
#EXAMPLE IP ADDRESS
#add your mqtt server below
mqtt_server = '178.62.83.XXX'
client_id = ubinascii.hexlify(machine.unique_id())
topic_pub_temp = b'esp/ds18b20/temperature'
last_message = 0
message_interval = 5
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid, password)
while station.isconnected() == False:
pass
print('Connection successful')
#ds_pin = machine.Pin(4)
#ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server, user='your_user', password='your_pass')
client.connect()
print('Connected to %s MQTT broker' % (mqtt_server))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(10)
machine.reset()
def read_sensor():
#try:
# roms = ds_sensor.scan()
# ds_sensor.convert_temp()
# time.sleep_ms(750)
# for rom in roms:
# temp = ds_sensor.read_temp(rom)
# # uncomment for Fahrenheit
# temp = temp * (9/5) + 32.0
# if (isinstance(temp, float) or (isinstance(temp, int))):
# temp = (b'{0:3.1f},'.format(temp))
# return temp
# else:
# return('Invalid sensor readings.')
#except OSError as e:
# return('Failed to read sensor.')
return b'5'
try:
client = connect_mqtt()
except OSError as e:
restart_and_reconnect()
while True:
try:
if (time.time() - last_message) > message_interval:
temp = read_sensor()
print(temp)
client.publish(topic_pub_temp, temp)
last_message = time.time()
except OSError as e:
restart_and_reconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment