ESP8266 - rst cause:2

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Tsubasa
Posts: 2
Joined: Sat Jul 08, 2017 5:53 pm

ESP8266 - rst cause:2

Post by Tsubasa » Sat Jul 08, 2017 6:14 pm

Hi,
I'm trying to run simple code measuring signal frequency(around 1kHZ to 2kHz) by counting rising edges and returning their divided by time period number. After some time(from 1s to up to 20s - seems kinda random) ESP8266 board is being reset with rst cause: 2 and boot mode:(3,7).

Here is program I'm trying to run:

import machine

PERIOD = 1000
number = 0

def increase(p):
global number
number+=1

def count(p):
global number, PERIOD
print(number/(PERIOD/1000))
number = 0

btn = machine.Pin(2, machine.Pin.IN, machine.Pin.PULL_UP)
tim = machine.Timer(-1)

tim.init(period=PERIOD, mode=machine.Timer.PERIODIC, callback=count)
btn.irq(trigger=machine.Pin.IRQ_RISING, handler=increase)


My best guess would be that it's caused by colliding timer and interrupt events. Is something wrong with my program? Can anything be done to stop board from reseting?

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

Re: ESP8266 - rst cause:2

Post by pythoncoder » Sun Jul 09, 2017 6:23 am

Your problem may be down to the lousy interrupt latency of the ESP8266. See https://github.com/micropython/micropython/issues/2972 and viewtopic.php?f=18&t=2904&p=19060&hilit ... ncy#p19060. So 2KHz may be pushing it. I'd try testing at a lower frequency (500Hz perhaps). Interrupt collisions are also a possibility depending on the relative priorities of Pin and Timer interrupts. The crash may result from reentrancy where a Pin interrupt occurs while a prior Pin interrupt is still executing because the first was delayed by a Timer interrupt and/or latency.

An option might be to dispense with the timer interrupt. Have a main loop which runs forever, and use the time module for accurate timing.

Something along the lines of

Code: Select all

while True:
	start = time.ticks_us()
	number = 0
	time.sleep_ms(PERIOD)
	n = number
	end = time.ticks_us()
	print(n/(time.ticks_diff(end, start)/1000000))
Peter Hinch
Index to my micropython libraries.

Tsubasa
Posts: 2
Joined: Sat Jul 08, 2017 5:53 pm

Re: ESP8266 - rst cause:2

Post by Tsubasa » Sun Jul 09, 2017 8:40 pm

Thanks a lot for your help.
Seems like using loop ESP8266 really doesn't reset itself, and manages to properly measure frequency up to 10kHz, which is more than I need.

kpg
Posts: 6
Joined: Tue Jun 27, 2017 3:31 pm

Re: ESP8266 - rst cause:2

Post by kpg » Mon Aug 07, 2017 3:29 pm

Hello guys i am working with esp8266 and vl53l0x sensor, but after "x" time of cicles the script crash and show me this error:

ets Jan 8 2013,rst cause:2, boot mode:(3,6)

load 0x40100000, len 32000, room 16
tail 0
chksum 0xee
load 0x3ffe8000, len 1104, room 8
tail 8
chksum 0xaa
load 0x3ffe8450, len 3000, room 0
tail 8
chksum 0xc0
csum 0xc0


this my code:

import network
from network import WLAN
import machine
from machine import I2C, Pin
import vl53l0x
import time
import gc
import socket
import ubinascii

#realizar conexion wifi
def do_connect():
sta_if = network.WLAN(network.STA_IF)
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
wlan = WLAN() # get current object, without changing the mode
nets = wlan.scan()
if not sta_if.isconnected():
print('\nconnecting to network...')
sta_if.active(True)
for net in nets:
net=str(net).split('\'')[1]
print (net)
if (net == 'XXXX' or net =='XXXX' or net =='XXXX'):
if net == 'XXXX':
pwd='xxxxx'
break
elif net == 'XXXX':
pwd='xxxx'
break
elif net == 'XXXX':
pwd='xxx'
break
sta_if.connect(net, pwd)
while not sta_if.isconnected():
machine.idle() # save power while waiting
#pass
print('network config:', sta_if.ifconfig())
return sta_if.ifconfig()

#revisar conexion a internet
def internet_ok():
#import socket
host="216.58.192.68" #ip de google (cambiar por ip de mi servidor)
port=80
s = socket.socket()
s.settimeout(2) # Configure the timeout of the socket
try:
s.connect((host, port))
s.close()
return True
except Exception as err:
print("Exception:", err)
return False

#obtener mac
def mac_get():
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
return mac

#http request a server
def http_get(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(100)
if data:
print(str(data, 'utf8'), end='')
print('')
else:
break
s.close()

#extraccion de ip local
def iplocal(string):
string=str(string).split('\'')[1]
return string

#lectura de distancia
def read_dist():
i2c = I2C(scl=Pin(4), sda=Pin(5))
sensor = vl53l0x.VL53L0X(i2c)
distance_mm=sensor.read()
print(distance_mm)
return distance_mm
#--------------------------------------------------------------------------------------------------
ip=iplocal(do_connect())
cnt = 0

while True:
if internet_ok():
gc.mem_free()
print("free in: %d" % gc.mem_free())
print ("contador:"+str(cnt))

#-para ver datos http://dweet.io/follow/distance o cambiar por la variable que quieres ver
url="https://xxx.xxx.xxx.xxx/wlan%20IP?wlan_IP="+str(ip) # url con datos a enviar
http_get(url)

url_dist="https://xxx.xxx.xxx.xxx/distance_mm?distance_mm="+str(read_dist())
#url_dist="https://52.204.113.94/dweet/for/distance?distance="+str(read_dist())
http_get(url_dist)

url_ciclo="https://xxx.xxx.xxx.xxx/ciclo?ciclo="+str(cnt) # url con datos a enviar
http_get(url_ciclo)

cnt += 1
time.sleep(1)#delay en segundos
print("free out: %d" % gc.mem_free())
else:
print ('internet [NOK]....waiting 10 sec to try again')
time.sleep(10)#delay en segundos

i am using nodemcu board.
in other topic people sugest that could be the power supply, i tested and this continue appear.
any sugestions?

Thanks

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: ESP8266 - rst cause:2

Post by deshipu » Mon Aug 07, 2017 4:04 pm

I guess the best way to figure out what is happening is to start removing things from you program until it stops crashing, then that last thing you removed is probably the reason. Then you can write a separate small program with only that thing and work with that to see why it crashes.

kpg
Posts: 6
Joined: Tue Jun 27, 2017 3:31 pm

Re: ESP8266 - rst cause:2

Post by kpg » Wed Aug 09, 2017 9:00 pm

the solution in my case was put the code of function involve in http request inside of "try":

def http_get(url):
import usocket
_, _, host, path = url.split('/', 3)
s = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
try:
address = socket.getaddrinfo(host, 80)[0][-1]
s.connect(address)
s.settimeout(5)
print (s)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(1024)
if data:
print(str(data, 'utf8'), end='')
print('')
else:
break
s.close()
except OSError as error:
print("OSError:\r\n", error)
s.close()
print (s)
except:
print("An error occurred.")


i not am expert but i think that the problem is because the function did a http request but when the server does not reply the code crash.
including a timeout to the socket connection and catch the error, now the code is runnig 4550 cicles and counting...

Post Reply