# 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 = 'REPLACE_WITH_YOUR_SSID' password = 'REPLACE_WITH_YOUR_PASSWORD' mqtt_server = '192.168.1.XXX' #EXAMPLE IP ADDRESS #mqtt_server = '192.168.1.106' 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) #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()