ESP8266 goes down after sometime [SOLVED]

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
pythoncoder
Posts: 3506
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP8266 goes down after sometime

Post by pythoncoder » Tue May 07, 2019 10:05 am

Given that your main loop runs every 180s I think you need to change the WDT time from 120s to (say) 300s. You should then be able to place the feed() statement in your main loop. I'd suggest after the while(True) statement.

The way a WDT works is that it resets the board if it is not fed during its timeout period. So, with the above change, the main loop should always feed it within five minutes. If the code stops running this won't occur and the board will be reset.
Peter Hinch

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Thu May 16, 2019 6:19 pm

pythoncoder wrote:
Tue May 07, 2019 10:05 am
Given that your main loop runs every 180s I think you need to change the WDT time from 120s to (say) 300s. You should then be able to place the feed() statement in your main loop. I'd suggest after the while(True) statement.

The way a WDT works is that it resets the board if it is not fed during its timeout period. So, with the above change, the main loop should always feed it within five minutes. If the code stops running this won't occur and the board will be reset.
Now i get it, i need to feed in the loop, ok.

btw, esp8266 still getting down after i've changed a power source. So it's a software problem or mcu hardware problem.
Meanwhile, i am rewriting new python code on my esp32 and after i finished it, i will upload new code with your correction on esp32 and esp8266 both. Thank you, pythoncoder!

P.S. I've recheck, that esp8266 get into work after i pushed reset button. So, i believe mcu.reset should work.

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Sat May 18, 2019 9:15 am

damn. its still go to sleep. Even with your WDT.
Uptime was 1 day. Any ideas what it may be?
I will put it on notebook and will see whats happening on terminal, maybe i will see some debug.

Code: Select all

import machine
import time
from machine import Pin
import bme280_float
import network
import micropython
from umqtt.simple import MQTTClient

class WDT:
    def __init__(self, id=0, timeout=300):
        self._timeout = timeout / 10
        self._counter = 0
        self._timer = machine.Timer(id)
        self.init()

    def _wdt(self, t):
        self._counter += self._timeout
        if self._counter >= self._timeout * 10:
            print("WDT timeout")
            machine.reset()

    def feed(self):
        self._counter = 0

    def init(self, timeout=None):
        timeout = timeout or self._timeout
        self._timeout = timeout
        self._timer.init(period=int(self._timeout * 1000), mode=machine.Timer.PERIODIC, callback=self._wdt)

    def deinit(self):  # will not stop coroutine
        self._timer.deinit()
wdt=WDT()

WiFi_SSID = "ssid"
WiFi_PASS = "pass"


SERVER = "mqtt.thingspeak.com"
CHANNEL_ID = "77"
WRITE_API_KEY = "xxx"
led = Pin(2, Pin.OUT)

# Exception in an ISR should be handled, reserve memory for that
micropython.alloc_emergency_exception_buf(100)


def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network... ')
        wlan.connect(WiFi_SSID, WiFi_PASS)
        print
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    print('SSID: ', WiFi_SSID)

def collectData():
    temp1, pas1, hum1 = bme.values
    #temp1 = result[0]
    #hum1 = result[2]
    hum1 = hum1.replace("%", "")
    temp1 = temp1.replace("C", "")
    led.on()
    return temp1, hum1

try:

    # esp8266 scl pin 5, sda pin 4
    # esp32 scl pin 22 (gpio 22), sda pin 21 (gpio 21)
    i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
    #bme= BME280(i2c=i2c, mode=BME280_OSAMPLE_8, address=BME280_I2CADDR)
    bme = bme280_float.BME280(i2c=i2c)
    client = MQTTClient("umqtt_client", SERVER)
    topic = "channels/" + CHANNEL_ID + "/publish/" + WRITE_API_KEY

    wlan = network.WLAN(network.STA_IF)
    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(False)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network... ')
        wlan.connect(WiFi_SSID, WiFi_PASS)
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    print('SSID: ', WiFi_SSID)


    while True:
        wdt.feed() # feeding a reset timer
        temp1, hum1 = collectData()
        #esp8266 bme code: payload = "field1="+str(hum1)+"&field2="+str(temp1)
        payload = "field1="+str(hum1)+"&field2="+str(temp1)
        print(payload)
        try:
            client.connect()
            client.publish(topic, payload)
            client.disconnect()
        except:
            continue
        led.off()
        time.sleep(180)
except:
    print('Catched Exception in main. Reset.')
    machine.reset()

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Sat May 18, 2019 10:01 am

Image

I think i found a bug. I have a poor connection on pins between esp and bme280, so when it import a library it cant found a bme, gives me error and crashes.
Should WDT works meanwhile or it crashes too?

can i use except on import?

Code: Select all

try:
    import bme280_float
except:
    print('Catched Exception in lib. Reset.')
    machine.reset()

User avatar
pythoncoder
Posts: 3506
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: ESP8266 goes down after sometime

Post by pythoncoder » Sat May 18, 2019 10:58 am

I'd fix the electrical connections first.
Peter Hinch

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Sat May 18, 2019 4:39 pm

finally i catch an error:

Code: Select all

field1=58.55&field2=19.06
field1=60.30&field2=19.41
field1=59.20&field2=19.12
field1=58.71&field2=18.65
field1=57.84&field2=18.50
field1=59.69&field2=18.98
field1=60.58&field2=18.99
field1=58.97&field2=18.52
field1=58.40&field2=18.38
field1=60.12&field2=18.97
field1=60.62&field2=18.90
Traceback (most recent call last):
  File "main.py", line 55, in <module>
  File "umqtt/simple.py", line 97, in connect
IndexError: bytes index out of range

MicroPython v1.10-8-g8b7039d7d on 2019-01-26; ESP module with ESP8266
Type "help()" for more information.
>>>
so after this crash ESP dont work any more and turned off a led.

I wondering why WDT dont work and why my try/except dont catch that?

Code: Select all

import machine
import time
from machine import Pin
import bme280_float
import network
import micropython
from umqtt.simple import MQTTClient

class WDT:
    def __init__(self, id=0, timeout=300):
        self._timeout = timeout / 10
        self._counter = 0
        self._timer = machine.Timer(id)
        self.init()

    def _wdt(self, t):
        self._counter += self._timeout
        if self._counter >= self._timeout * 10:
            print("WDT timeout")
            machine.reset()

    def feed(self):
        self._counter = 0

    def init(self, timeout=None):
        timeout = timeout or self._timeout
        self._timeout = timeout
        self._timer.init(period=int(self._timeout * 1000), mode=machine.Timer.PERIODIC, callback=self._wdt)

    def deinit(self):  # will not stop coroutine
        self._timer.deinit()
wdt=WDT()

WiFi_SSID = "ssid"
WiFi_PASS = "pass"


SERVER = "mqtt.thingspeak.com"
CHANNEL_ID = "77"
WRITE_API_KEY = "xxx"
led = Pin(2, Pin.OUT)

# Exception in an ISR should be handled, reserve memory for that
micropython.alloc_emergency_exception_buf(100)


def do_connect():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network... ')
        wlan.connect(WiFi_SSID, WiFi_PASS)
        print
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    print('SSID: ', WiFi_SSID)

def collectData():
    temp1, pas1, hum1 = bme.values
    #temp1 = result[0]
    #hum1 = result[2]
    hum1 = hum1.replace("%", "")
    temp1 = temp1.replace("C", "")
    led.on()
    return temp1, hum1

try:

    # esp8266 scl pin 5, sda pin 4
    # esp32 scl pin 22 (gpio 22), sda pin 21 (gpio 21)
    i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
    #bme= BME280(i2c=i2c, mode=BME280_OSAMPLE_8, address=BME280_I2CADDR)
    bme = bme280_float.BME280(i2c=i2c)
    client = MQTTClient("umqtt_client", SERVER)
    topic = "channels/" + CHANNEL_ID + "/publish/" + WRITE_API_KEY

    wlan = network.WLAN(network.STA_IF)
    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(False)
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network... ')
        wlan.connect(WiFi_SSID, WiFi_PASS)
        while not wlan.isconnected():
            pass
    print('network config:', wlan.ifconfig())
    print('SSID: ', WiFi_SSID)


    while True:
        wdt.feed() # feeding a reset timer
        temp1, hum1 = collectData()
        #esp8266 bme code: payload = "field1="+str(hum1)+"&field2="+str(temp1)
        payload = "field1="+str(hum1)+"&field2="+str(temp1)
        print(payload)
        try:
            client.connect()
            client.publish(topic, payload)
            client.disconnect()
        except:
            continue
        led.off()
        time.sleep(180)
except:
    print('Catched Exception in main. Reset.')
    machine.reset()

kevinkk525
Posts: 417
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP8266 goes down after sometime

Post by kevinkk525 » Sat May 18, 2019 8:22 pm

The wdt should work. Are you waiting long enough? You have a timeout of 300s (5 minutes).

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Sun May 19, 2019 7:15 am

yes. i've check that receiving of data stops (widget on my android), come to ESP and see there is no led on. After that i go to sleep and come back at the morning, it was still off.
I just reset it and setted up an usb cable to see what happening.

kevinkk525
Posts: 417
Joined: Sat Feb 03, 2018 7:02 pm

Re: ESP8266 goes down after sometime

Post by kevinkk525 » Sun May 19, 2019 7:58 am

That's strange. Can you check the wdt only to make sure it works? I'm using the same wdt code and it works all the time.

navy
Posts: 15
Joined: Sat May 04, 2019 6:37 am

Re: ESP8266 goes down after sometime

Post by navy » Sun May 19, 2019 10:45 am

i've refactored code a little bit, and have started it to testing. It still working from yesterday.
Put connection to wifi and WDT to separate module. Maybe it make sense.
All exception works when i try to CTRL+C in REPL.

Code: Select all

import machine
import time
from machine import Pin
import bme280_float
import network
import micropython
from umqtt.simple import MQTTClient
from wdt import WDT
import ConnectWiFi

ConnectWiFi.connect()
wdt = WDT(timeout=300)
SERVER = "mqtt.thingspeak.com"
CHANNEL_ID = "777"
WRITE_API_KEY = "XXX"


blue_led = Pin(2, Pin.OUT)

# Exception in an ISR should be handled, reserve memory for that
micropython.alloc_emergency_exception_buf(100)

def collectData():
    temp1, pas1, hum1 = bme.values
    #temp1 = result[0]
    #hum1 = result[2]
    hum1 = hum1.replace("%", "")
    temp1 = temp1.replace("C", "")
    return temp1, hum1

try:

    # esp8266 scl pin 5, sda pin 4
    # esp32 scl pin 22 (gpio 22), sda pin 21 (gpio 21)
    i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
    #bme= BME280(i2c=i2c, mode=BME280_OSAMPLE_8, address=BME280_I2CADDR)
    bme = bme280_float.BME280(i2c=i2c)
    client = MQTTClient("umqtt_client", SERVER)
    topic = "channels/" + CHANNEL_ID + "/publish/" + WRITE_API_KEY

    while True:
        wdt.feed() # feeding a reset timer
        temp1, hum1 = collectData()
        #esp8266 bme code: payload = "field1="+str(hum1)+"&field2="+str(temp1)
        payload = "field1="+str(hum1)+"&field2="+str(temp1)
        print(payload)
        try:
            client.connect()
            client.publish(topic, payload)
            client.disconnect()
        except:
            print("MQTT connect exception")
            continue
        for i in range(5):
            time.sleep_ms(50)          
            blue_led.on()
            time.sleep_ms(50) 
            blue_led.off()
        wdt.feed()  # feeding a reset timer
        time.sleep(180)
except:
    print('Catched Exception in main. Reset.')
    machine.reset()
    

Post Reply