Using the base code from OpenWeatherMap API in the Book MicroPython Programming with ESP32.
My boot.py imports the same libraries, except I use ‘from utime import sleep‘ instead of ‘import time‘.
From ‘print(station.ifconfig())‘ I moved the API connection code to ‘main.py.
In main.py, essentially I have:
Setup variables for 4 towns and the API key, and the time to sleep (called ‘snooze’).
def process_data(url):
this gets the data from the API and processes it.
Returns a string I can send to my website with the readings as PHP variables.
while True:
//for each of 4 preset towns create a URL to get weather data.
//Call the process_data(url) function to get the data.
//Print return string to serial window.
//request a webpage from my server ‘www.mysite.com/insert.php + msg’
//This page adds the data to a MySQL database.
//Print the response from server
//Then sleep for 30 minutes, using
Sleep(snooze)
My problem is the code works perfectly as expected while connected to Thonny. Connects every 30 minutes, downloads data and updates my website.
If I stop the code, connect the ESP32 to a battery, and reboot it, the code runs continuously, in that it does not sleep but updates the website every few seconds (the time it takes to process all 4 requests).
I notice the example in the book has all the code in the one file ‘boot.py’. Is there a specific reason for this?
Hi David.
That’s weird. Are you sure you have the right code uploaded to your ESP32? Notice that when you “run” the code on Thonny IDE, you’re not uploading the code. To make sure you have the right code uploaded you can check the folders on the left tab and see that you actually have the right code uploaded.
In our example, we have everything on the boot.py just to exemplify how to make a request to get data. The idea is then to use that code to integrate in other projects. So, there isn’t a specific reason for that, it’s just to explain the concept.
Regards,
Sara
Hi Sara
Yes, the code is definately downloaded to the ESP32. I get the code from the ESP32 each time I try it on battery and then plug it back into Thonny. Do you want a full copy of the code?
Hi Sara
Here is the code. I’ve restricted it to myself and Admin, but if you want to make it available for others I have no objection. I have removed passwords and API key.
I’ve just noticed the code has lost all of it’s indentation, so I have deleted it.
I will make another post.
Hi.
Here you have the instructions: https://rntlab.com/question/need-help-using-pastebin/
Regards,
Sara
Hi David.
I’ve taken a look at your code and I don’t know why it doesn’t work.
However, I’m not sure if you know that the sleep() function is like the Arduino delay() function. The code waits there before proceeding.
If you are using batteries, it may be better to use deep sleep instead. Have you considered using deep sleep? It may help you save battery.
Regards,
Sara
Hi Sara
Its on the agenda, but currently I use a usb power bank and it lasts about 3 days so not a problem at the moment.
I transferred the code [altered to suit] to a raspberry Pi and that works as expected. I also picked up on having to use close() after each call to requests.
Just to be clear on how this all works, does the code for the sleep function get transferred directly to the ESP32 when you copy the boot.py file there, or are the libraries resident there from flashing it with python?
I did wonder if while connected to Thonny, Thonny was providing the function each time it is invoked, and when on battery the function is not available.
Hi again.
The utime library is part of the MicroPython firmware. It’s a built-in library. So, after uploading the code, it has everything to run properly.
So, you can say that “the code for the sleep function get transferred directly to the ESP32 when you copy the boot.py file”.
Here you can see all the built-in libraries: https://docs.micropython.org/en/latest/library/index.html
I don’t know what might be causing the issue.
Regards,
Sara
Thank you Sarah, that is a very useful document. If [when] I find out the problem I will let you know.
Hi Sara
Just to keep you posted, still not resolved this but do have more info. I re-wrote some code to do very little. It creates an RTC class, gets the time from a network time server, then starts the loop as soon as seconds become zero.
The loop reads a sensor, makes one GET requst, turs some LEDs on and off, then calculates from the RTC the number of seconds to sleep before doing it again.
As you can see from the printout in Thonny, this works perfectly, updating the DB every full minute.
Connection successful
(‘xxx.xxx.xxx.xxx’, ‘255.255.255.0’, ‘xxx.xxx.xxx.xxx’, ‘xxx.xxx.xxx.xxx’)
ESP32 woken up at 2020-6-18 16:42:6
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.2&hum=57.3
16:42:12 Going to sleep
48 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:43:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.5&hum=59.9
16:43:7 Going to sleep
53 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:44:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.2&hum=56.5
16:44:7 Going to sleep
53 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:45:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.1&hum=56.9
16:45:6 Going to sleep
54 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:46:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.0&hum=56.8
16:46:7 Going to sleep
53 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:47:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.1&hum=56.9
16:47:7 Going to sleep
53 #calculation from RTC of seconds to next minute
ESP32 woken up at 2020-6-18 16:48:0
http://xxx.xxx.xxx.xxx/insertdata.php?temp=24.1&hum=56.9
16:48:7 Going to sleep
53 #calculation from RTC of seconds to next minute
When I run it from a battery, then check the DB entries, they are updated irregularly, but more or less every 30 seconds. All very strange.
Hi.
That’s weird.
Do you know if your board is still relatively close to your router? I don’t know if it has something to do with the wi-fi connection :/
Yes, It is about 2 metres away. The battery I am using is a 5v 7500mhA USB power bank.
Some “smart” power banks go into deep sleep when they are not charging anything. Or go into “power saving mode” when you’re not draining enough current from them. I don’t know if your power bank has these features, but it may cause issues if that’s the case.
Regards,
Sara
If the is a print statement in the code each loop, it acts as a ‘keep alive’ to the power bank.
I think I have the final answer here. Running directly on a USB power supply it ran 24 hours without problem. I think in the very first instance, rather than running twice as fast it was just looping once and then rebooting. Other tests have shown weird behaviour, so the bottom line is do not run small boards from power banks.