reconnecting to boards running endless loops

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Posts: 63
Joined: Tue Nov 14, 2017 3:11 am
Location: Perth, Australia

reconnecting to boards running endless loops

Post by philwilkinson40 » Sat Mar 17, 2018 7:00 am

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)
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?

User avatar
Posts: 5819
Joined: Fri Jul 18, 2014 8:01 am
Location: UK

How to detect USB connected status on ESPx?

Post by pythoncoder » Sun Mar 18, 2018 1:35 pm

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.
Peter Hinch
Index to my micropython libraries.

User avatar
Posts: 3359
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: reconnecting to boards running endless loops

Post by Roberthh » Sun Mar 18, 2018 1:59 pm

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.

Post Reply