Close

Improving uptime on the ESP8266 side of things

A project log for Remote environmental logging

simple way to log temperature, humidity, etc. with the help of one or more ESP8266 boards

mcunerdmcu_nerd 03/27/2021 at 19:060 Comments

The script for taking readings from the DS18B20 temperature sensor can run fine for some months but once in a blue moon a read fails for some reason (not sure if it's an issue with MicroPython, the ESP8266 itself, or something else) an exception is thrown and everything comes to a halt.  To deal with such annoyances, I put just about everything else in a try statement to allow the script to continue running when one of those read failures occurs.  I simulated a read failure by disconnecting the DS18B20, then plugged it back in and it resumed taking readings.  I didn't put the roms = ds.scan() line in the loop, so if that fails an exception will be thrown that's not handled when the script is initially started but that's almost certain to be noticed when first starting the script up.  It could be an issue if it fails after starting up again after a reboot, so I might decide later on to put in in the loop.

def go():
	import time
	import machine
	import urequests
	import network
	import onewire, ds18x20
	sta_if = network.WLAN(network.STA_IF)

	
	dat = machine.Pin(12)
	ds = ds18x20.DS18X20(onewire.OneWire(dat))
	roms = ds.scan()

	time.sleep(2)

	

	def send_data():
		url = 'http://[server ip address here]/sensorlogging/php/rsensor_ds18b20.php'
		headerd = {'Content-Type': 'application/x-www-form-urlencoded'}
		jsons = "data=" + str(ds18b20temp_f)
		if sta_if.isconnected() == True:	
			try:
				r = urequests.post(url, data=jsons, headers= headerd)
				r.close()
			except OSError:
				pass
	while True:
		try:
			ds.convert_temp()
			time.sleep(15)
			ds18b20temp_c=ds.read_temp(roms[0])
			ds18b20temp_f=ds18b20temp_c * 9.0/5.0 +32
			send_data()
		except:
			pass
	

Discussions