# Complete project details at https://RandomNerdTutorials.com/micropython-mqtt-publish-ds18b10-esp32-esp8266/
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 = 'Wokwi-GUEST'
password = ''
mqtt_server = 'ip-server'
USER = b""
PASSWORD = b""
#EXAMPLE IP ADDRESS
#mqtt_server = '192.168.1.106'
#from machine import WDT
# enable the WDT with a timeout of 5s (1s is the minimum)
#wdt = WDT(timeout=5000)
#wdt.feed()
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(32)
ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server,user=USER, password=PASSWORD)
#client = MQTTClient(client_id, mqtt_server, user=your_username, password=your_password)
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.')
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()