I'm running micropython v1.17 on a Wemos D1 mini 4MiB flash.
I have created a little script originaly in C with Arduino and have ported this to micropython, as I wanted to try something new. Now the script is running fine for a couple of hours or sometimes even days, befor the sensor is not found anymore. The only thing which works is, to reset the board. The same code in C is running for years without any issues. This brings me a bit to a software related issue, as it's the same esp, the same sensor and wiring.
I have captured the the result form the ds.scan() which looks like this when it works and when it's not working anymore:
Code: Select all
Found DS devices: [bytearray(b'(\x86\xady\x97\x06\x03\xb8')]
Found DS devices: []
Code: Select all
import time
from umqtt.simple import MQTTClient
import urequests
import network
import onewire
import ds18x20
import machine
ssid = '****'
password = '*****'
mqtt_server = '*****'
client_id = 'esp8266'
dat = machine.Pin(2)
ds = ds18x20.DS18X20(onewire.OneWire(dat))
relay = machine.Pin(4, machine.Pin.OUT)
tempsoll = 26
url = "http://*****:8086/write?db=aquarium"
# Relay Test
relay.on()
time.sleep(2)
relay.off()
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.connect(ssid, password)
while not sta_if.isconnected():
pass
print('Connection successful:', sta_if.ifconfig())
def connect_mqtt():
global client_id, mqtt_server
client = MQTTClient(client_id, mqtt_server)
client.connect()
print('Connected to MQTT broker %s ' % (mqtt_server))
print('Connected as %s' % (client_id))
return client
def restart_and_reconnect():
print('Failed to connect to MQTT broker. Reconnecting...')
time.sleep(5)
machine.reset()
def read_ds():
roms = ds.scan()
print('Found DS devices: ', roms)
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
temp = ds.read_temp(rom)
if isinstance(temp, float):
temp = round(temp, 2)
# print(temp, end=' ')
# print()
return temp
return '0'
# machine.reset() # little workaround
try:
client = connect_mqtt()
except OSError as e:
restart_and_reconnect()
while True:
messung = read_ds()
messung_json = '{"temperatur":%s}' % messung
print(messung_json)
# client.publish(b'fisch', b'%s' % messung_json)
# daten = "data temperatur=%s" % messung
# urequests.post(url, data = daten)
if (messung > tempsoll):
# print("bad")
relay.on()
elif (messung <= tempsoll):
# print("good")
relay.off()
time.sleep(5)