Problem with machine.reset

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
DJShadow1966
Posts: 60
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Problem with machine.reset

Post by DJShadow1966 » Thu Mar 12, 2020 2:22 pm

Hello

I have a problem with machine.reset when using an interupt to trigger the command machine.reset, the macine resets then I get the error

Code: Select all

File "main.py", line 4, in <module>
OSError: TCP/IP IF Not Ready
MicroPython v1.12-256-geae495a71 on 2020-03-12; ESP32 module with ESP32
Type "help()" for more information.
Currently using the downloaded version built with ESP-IDF v4.x, I ram a test with the downloaded version built with ESP-IDF v3.x and do not experience the issue, the error also happens on v4.x if I press the reset button, or even GND off the ENable pin !

Main.py

Code: Select all

import network, ntptime, utime, machine
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.config(dhcp_hostname="ComputerPIR")
sta_if.connect('*******', '********')
print("Connecting ")
while not sta_if.isconnected():
    print(".", end="")
    utime.sleep(1)

try:
    ntptime.settime()
except:
    pass

import mqtt

Main Code

Code: Select all

# Routine for the Interupt handler
def callback(pin):
  global interruptCounter
  interruptCounter = interruptCounter+1
  
# Setup the Interupt Counter
interruptCounter = 0

# Pin 25 for the push to make switch to allow reset
p25 = machine.Pin(25, machine.Pin.IN, machine.Pin.PULL_UP) 
p25.irq(trigger=machine.Pin.IRQ_FALLING, handler=callback)

while True:

    # CHeck the Interrupt and reset machine if button on Pin 25 is pressed
    if interruptCounter>0:
        machine.reset()

Can you please advise of what is going on ?

Regards Mike
Last edited by DJShadow1966 on Thu Mar 12, 2020 9:16 pm, edited 1 time in total.

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Problem with machine.reset

Post by tve » Thu Mar 12, 2020 5:47 pm

I recommend you edit your message to remove your wifi creds... ;)

User avatar
tve
Posts: 216
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Problem with machine.reset

Post by tve » Thu Mar 12, 2020 5:53 pm

As you noticed how you trigger the reset is not relevant. It looks to me like active(True) isn't as instantaneous as it leads to believe. Try adding a sleep_ms(100) after it.

DJShadow1966
Posts: 60
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Re: Problem with machine.reset

Post by DJShadow1966 » Thu Mar 12, 2020 9:19 pm

Hello

Yeah that was my thought of adding a sleep_ms of various sizes before hand and still get the same error, and have edited my credentials as forgot them as was so caught up in troubleshooting the issue.

It seems strange that the same code works fine on the same ESP32 but using IDF 3 instead of IDF 4.

Regards Mike

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: Problem with machine.reset

Post by palivoda » Thu Mar 12, 2020 10:42 pm

This works for me:

Code: Select all

def wifi(reconnect=False, scan=False):
    from network import WLAN, STA_IF
    from time import sleep_ms, time
    wlan = WLAN(STA_IF)

    if reconnect:
        print('[wifi] Deactivating...')
        wlan.active(False)
        sleep_ms(100)

    if not wlan.isconnected():
        print('[wifi] Connecting...')
        wlan.active(True)
        sleep_ms(100)
        wlan.connect('ssid', 'pass')
        t = time()
        while not wlan.isconnected():
            if time() - t > 10:
                wlan.disconnect()
                wlan.active(False)
                break
            else: sleep_ms(1000)
    else:
        if scan:
            print('[wifi] Scanning...')
            for item in wlan.scan():
                print(item)

    print('[wifi] Config:', wlan.ifconfig())

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

Re: Problem with machine.reset

Post by kevinkk525 » Fri Mar 13, 2020 6:47 am

The problem might be in calling:

sta_if.config(dhcp_hostname="ComputerPIR")

What happens if you comment it out or put it after a successful connection has been established?
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

DJShadow1966
Posts: 60
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Re: Problem with machine.reset

Post by DJShadow1966 » Fri Mar 13, 2020 6:51 am

Hello

Tried commenting the line out then the error of OSError: TCP/IP IF Not Ready drops down too sta_if.connect('*******', '********')

Regards Mike

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

Re: Problem with machine.reset

Post by kevinkk525 » Fri Mar 13, 2020 7:11 am

If neither that nor adding a sleep after sta.active(True) is helping, then I'm out of ideas sadly..
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

DJShadow1966
Posts: 60
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Re: Problem with machine.reset

Post by DJShadow1966 » Fri Mar 13, 2020 1:21 pm

Hello

I have done a bit more testing and from what it looks like is that IDF 4.x builds of Micropython do not like the command "sta_if.config(dhcp_hostname="TestEnviron")" when the device is reset by whatever means you do i.e. Grounding the ENable pin, pressing the reset button, or even with the command machine.reset, will only work from power cycling by pull USB cable out, the code in my first post works fine with a Micropython build with IDF 3.x

Regards Mike

Post Reply