Page 1 of 1

Long running scripts not working

Posted: Sun Mar 11, 2018 8:16 pm
by peterkz
I have an ESP8266 that reads CO2 sensor data over the UART0 and then posts the data to a local InfluxDB server (using urequests library). This works fine for a couple of hours but then stops. Since my sensor is connected to UART0 (in 9600) it is hard to debug while the script is running. Has anyone tried logging errors etc to a file?

I try to do garbage collection on each loop but maybe I should do a machine.restart() instead?

Re: Long running scripts not working

Posted: Sun Mar 11, 2018 9:10 pm
by ad525
Post the code to see what may go wrong.

Re: Long running scripts not working

Posted: Sat Mar 17, 2018 10:02 pm
by peterkz
Here's some sample code that will stop after a couple of hours.

Code: Select all

import machine, time
from machine import Pin
from machine import UART
import urequests as requests
import gc

def debug_log(message):
    print(message) #for now

def error_log(message):
    debug_log(message)

def push_sensor_value(server, dbname, username, password, measurement, co2val):
    try:
        led(0)
        if not sta_if.isconnected():
            do_connect()

        url = "{server}/write?db={dbname}".format(server=server, dbname=dbname)
        if username and password:
            url += "&u={username}&p={password}".format(username=username, password=password)

        debug_log("Posting to %s" % url)

        linedata = "{measurement},station=co2 CO2={value}".format(measurement=measurement, value=co2val)

        debug_log("Data: %s" % linedata)

        r = requests.post(url, data=linedata)
        debug_log("Response: %s" % r)
        r.close()
        led(1)
    except Exception as err:
        error_log(err)


def get_sensor_value():
    """Get value from MHZ19B CO2 sensor.
    :returns: CO2 integer value
    """
    debug_log("Getting sensor val")
    try:
        led(0)
        result = u.write(b'\xff\x01\x86\x00\x00\x00\x00\x00\x79')
        time.sleep(0.2)
        s = u.read(9)
        if s:
            if s[0] == 255 and s[1] == 134:
                co2value = int(s[2]*256) + int(s[3])
                debug_log("CO2: %s" % co2value)
                led(1)
                return co2value
            else:
                debug_log("No sensor value %s" % s)
        return -1
    except Exception as err:
        error_log(err)
        return -1


if __name__=="__main__":
    # setup sensor comms
    u = UART(1, baudrate=9600)
    u.init(9600, bits=8, parity=None, stop=1, tx=17, rx=16)
    gc.collect()

    while True:
        val = get_sensor_value()
        if val > 0:
            push_sensor_value("http://0.0.0.0:8086", "luftdata", "username", "*****", "home", val)

        # Wait until next measurement
        for i in range(30):
            led(1)
            debug_log("Sleeping %s" % i)
            time.sleep(1)
            led(0)
        led(1)
        gc.collect()


Re: Long running scripts not working

Posted: Wed Mar 21, 2018 11:35 am
by ad525
I've got the same problem with a long running script. To check if the script is running I added a led which was blinking. After a while I was receiving 'dev1143' error and the board was stuck in an infinite loop. This exception only seems to happen in the rare event a client tries to connect right as an I/O interrupt happens. That will immediately trigger the "dev 1143" line, and a processor lock. Try to desable the LED and let the board running to see if that was the cause.

Also see : https://bbs.espressif.com/viewtopic.php?t=2615

Re: Long running scripts not working

Posted: Sat Mar 24, 2018 9:51 am
by peterkz
Thank you! That was it! I dropped all led blinking stuff and now it has been running without a hitch for 10 hrs.

Re: Long running scripts not working

Posted: Mon Mar 26, 2018 11:19 am
by ad525
Great ! :D