Page 1 of 1

reconnecting to boards running endless loops

Posted: Sat Mar 17, 2018 7:00 am
by philwilkinson40
A basic question that may have more to do with programing on microcontollers than Micropython specifically.

I am using a ESP8266 WEMOS D1 mini using vanilla micropython.

I have written a program that uses an endless loop of
connecting to wifi -> reading a sensor -> publishing the sensor data by mqtt-> then entering a deep sleep using

Code: Select all

rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
rtc.alarm(rtc.ALARM0, 300000)
machine.deepsleep() 
this works fine and will happily cycle endlessly.

However, if it want to stop the program running, and adjust some parameters, I hit a problem.
I cannot connect to the dev board via USB serial port (using rshell). Presumably because the code is running or the esp is in deep sleep.

I have no idea how to deal with the obvious issue. Should I include something in the script to allow me to stop the code running?

How to detect USB connected status on ESPx?

Posted: Sun Mar 18, 2018 1:35 pm
by pythoncoder
As you've discovered in deep sleep USB is turned off to save power.

The way I deal with this on the Pyboard is, when my application starts, to detect whether USB is connected. If it is, I don't go into deep sleep. This facilitates debugging while enabling it to run normally when you start it without a USB connection.

Unfortunately I don't know how to detect the presence of a USB connection on ESPx boards.

Re: reconnecting to boards running endless loops

Posted: Sun Mar 18, 2018 1:59 pm
by Roberthh
Obviously you can flash the device and wipe out any code running. Otherwise, what happens if the device does not get a WiFi connection upon boot?
As an fix for the future, you could add a time window at boot where the device waits for a few seconds to get interrupted on the USB line yb Ctrl-C. You can also attach the serial input as UART and check with uart.any() if characters are received through USB.