Can Deep Sleep be used with AsyncWebServer that uses NTP time server?
This is the project I am wanting to use deep sleep; hoping to drop from a peak of 150 mA.
Planning to power by Solar; using your tutorial “Power ESP32/ESP8266 with Solar Panels (includes battery level monitoring),”
William
Tried the “Arduino IDE” example for “Node32s” board ->ESP32->Deep Sleep->Timer example; before WiFi connects, project goes to sleep. There are “allot of moving parts” to project; to name a few Async Web Server, FTP, NTP , OTA, as well as two interrupts. I am new to the use of deep sleep for the ESP32. I do not know if I can use “deep sleep” due to “all the moving parts.”
I do understan WiFi is disabled in “Deep Sleep;” so this does not look to promising.
William
Hi.
During deep sleep, Wi-Fi is disabled. So, you can’t access your web server if the ESP32 is sleeping.
What some people do is to have two ESP32 boards. One is in deep sleep, and when it wakes up, gets readings and sends to another ESP32 board that is always awake. This ESP32 runs a web server to display the readings from the other board.
Regards,
Sara
Thank you Sara. I like your suggestion. One running Wi-Fi could be on mains power and the other could be solar powered. Sounds good…
Regards,
William
Great!
Good luck with your project.
If you need any help, just ask.
Regards,
Sara
I have been able to get deep sleep for 57 minutes out 60 minutes with project. I am using my five minute routine to put project in deep sleep for 4.75 minutes. Have not metered anything yet; looks promising.
I am using this BME280 Library; I have not been able to use your ESP32 Client-Server Wi-Fi Communication Between Two Boards guide.
Specifically:
serverAsync.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){ request->send_P(200, "text/plain", readTemp().c_str()); });
BME280I2C.h use float data type; readTemp().c_str() from guide has to be replaced, temp = bme.temp() would be normally used to find temperature. I would like to keep this BME280 library. Any suggestion/s?
Regards,
William
To convert a float
to a C string, you must use a buffer large enough to hold all the characters that make up the value you want to send. For example, assuming that the temperatures you want to send are positive, that the integer part does not exceed 99, and that you only want to display one decimal place, you need a total of 5 characters:
- 2 for the integer part,
- 1 for the decimal point,
- 1 for the decimal part,
- 1 for the ending character of the string (
\0
orchr(0)
).
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request) { float t = bme.temp(); char buffer[5]; snprintf(buffer, 5, "%.1f", t); request->send_P(200, "text/plain", buffer); });
If you want to be able to handle sub-zero temperatures, you will have to allow 1 extra character for the sign. Similarly, if you want to be able to handle temperatures between -999.9 °C and 999.9 °C you will need to allow 1 extra character for the hundreds.
snprintf
is more secure than sprintf
because it prevents buffer overflow.
Hopefully that’s clear enough.
Thank you Steph; clear, complete and consive, appreciate the help!
Regards,
William
Iinfinite/BME280 library with Two, ESP32’s in Client and Server operation; with Steph’s help, we were able to resolve issues associated with using this library. Code for using Infinite/BME280 Library
Source of Infinite/BME280 Library
Regards,
William
Thank you Sara. I am trying to run acess point mode and station mode on the client. All I get on Http client is “Error code -1:.” Httpclient works with “200” responses; if I only use access point mode. I am able to ping both the server and the client.
Network configuration:
WiFi.mode(WIFI_AP);
WiFi.softAP(Apssid, Appassword);
Serial.println(“Wait 100 ms for AP_START…”);
delay(100);
Serial.println(“Set softAPConfig”);
IPAddress Ip(192, 168, 4, 2);
IPAddress NMask(255, 255, 255, 0);
WiFi.softAPConfig(Ip, Ip, NMask);
IPAddress myIP = WiFi.softAPIP();
Serial.print(“AP IP address: “);
Serial.println(myIP);
//Comment below code if you are using Access point only
//ESP32 connects to your wifi ———————————–
WiFi.mode(WIFI_STA); //Connectto your wifi
IPAddress local_IP2(10,0,0,200);
IPAddress gateway2(10,0,0,1);
IPAddress subnet2(255,255,255,0);
WiFi.config(local_IP2, gateway2, subnet2);
WiFi.begin(ssid, password);
Password have been configured. I suspect a network configuration for access point.
Result only AP:
Version 1.0 ‘Client_side.ino’ 06/23/2020 @ 13:42 EDT
Please wait; for network connection…
Setting AP (Access Point)…Connecting
……………………………………………..
Connected to WiFi network with IP Address: 192.168.4.2
SPIFFS opened!
No GPS data received: check wiring
GPS Ready
HTTP Response code: 200
HTTP Response code: 200
HTTP Response code: 200
Temperature: 80.24 *F – Humidity: 42.4 % – Pressure: 29.94 inHg
HTTP Response code: 200
HTTP Response code: 200
HTTP Response code: 200
Temperature: 80.06 *F – Humidity: 42.4 % – Pressure: 29.95 inHg
HTTP Response code: 200
HTTP Response code: 200
HTTP Response code: 200
Temperature: 80.42 *F – Humidity: 42.4 % – Pressure: 29.95 inHg
HTTP Response code: 200
HTTP Response code: 200
HTTP Response code: 200
Temperature: 80.42 *F – Humidity: 42.3 % – Pressure: 29.95 inHg
Result AP and STA:
Version 1.0 ‘Client_side.ino’ 06/23/2020 @ 13:42 EDT
Please wait; for network connection…
WiFi connected
IP address:
10.0.0.200
Setting AP (Access Point)…Connecting
.
Connected to WiFi network with IP Address: 10.0.0.200
SPIFFS opened!
No GPS data received: check wiring
GPS Ready
Error code: -1
Error code: -1
Error code: -1
Temperature: 32.00 *F – Humidity: — % – Pressure: 0.90 inHg
Error code: -1
Error code: -1
Error code: -1
Temperature: 32.00 *F – Humidity: — % – Pressure: 0.90 inHg
Error code: -1
Error code: -1
Error code: -1
Temperature: 32.00 *F – Humidity: — % – Pressure: 0.90 inHg
William
Hi.
Are you using this line of code:
WiFi.mode(WIFI_AP);
and this one:
WiFi.mode(WIFI_STA); //Connectto your wifi
On the same code?
To set up the ESP32 as an access point and station simultaneously, use the following line instead:
WiFi.mode(WIFI_AP_STA);
Regards,
Sara
Thank you Sara.
Having trouble pinging the client AP; server is functional, I know this since I have reached URL requests using cell phone. I think, I follow the use of WiFi.mode(WIFI_AP_STA). Just to clarify; do you need to use it for both server and client? How many instances do you need? Does placement matter?
Question on assigning Static ip address; are curley braces required?
Last couple of days I have been frustrated with this pinging issue. If I restart Windows 10,2004 I can briefly ping server and client. Next, I am going to try laptop (hope it still has older Win 10 build,) just tp rule out the OS build.
Regards,
William
Hi.
You just need to use AP_STA in both, if both will act as access point and station.
For static IP address, follow this tutorial: https://randomnerdtutorials.com/esp32-static-fixed-ip-address-arduino-ide/
Ate the moment, I’m with very little access to the internet. I’m sorry for the short response.
Regards, Sara
Followed the static/fixed ip tutorial; still can not get client to ping with good responses, If Client, Station configuration is completely commeted out; HTTP responses are all 200’s and pings are good. I am using “Wifi.mode(WIFI_AP_STA);” still no joy. Appreciate the help.
What is the meaning of “Error: -1”? I assume, no connection.
Regards,
William
Can this be done? Need to have Solar powered sensor, outside running AP WiFi server, sensor updates pushed ever 15 nimutes. Inside on mains power, need to have updates received on AP WiFi, and after sensor update has been pushed, return to Asyncwebserver on STA WiFi.
Function of Asyncwebserver is to “drive” two HTML web sites. One, ESP32 web based HTML web site and the other a Domain hosted webpage. Due to free domain hosting restrictions there is no file downloading; ESP32 based web site has access to data, log files.
ESP32 based web site is only down for project developement.
Regards,
William
Why does the outside sensor need AP WiFi server? That will kill any solar powered supply.
The way I would do this is to use an always on USB power bank (Like Voltaic Systems V25) and a solar charger (Like Voltaic Systems 10W Solar Charger). Depending on average power consumption and location these may need to be larger. You may want to check whether 15 minutes is needed and slow to 20 or 30 minutes.
The outside sensor would take a reading, connect to WiFi and push reading to server – whether another ESP32 or a cloud server (dweet.io comes to mind) then goes into deep sleep mode for 15 minutes. Rinse and repeat.
Thank you Steve; looking into “always on” power bank. Read that some power banks you can power on, then hold down the power button 3 seconds, then LEDs should flash. Will check my power bank for this “always on” mode. Never heard of “Voltaic Systems” power banks, thank you.
Good suggestions.
Along the line of Client-Server and what I am hoping to accomplish with my existing project code.
Check out G6EJD – David’s “YouTube” video and “Github” code:
Tech Note 083 – ESP Sensor Server and Clients (for DHT, SHT, BMP085, BMP180, DS18B20, etc)
William
Whilst searching for an always on power bank the only one I ever found that specifically mentions it for IOT use was Voltaic Systems. Whilst there must be others out there they seemed reasonably priced. What I did find was that realistically you will need a fairly large solar panel to provide enough power to keep the power bank charged especially for those overcast days.
My particular project is designed to wake up once per day, take a picture and upload it to a server. The server then texts and/or emails the picture. The relatively small power bank and charger I mentioned can keep up with that. My next endeavor is to use a SIM7000A and Hologram SIM to send via LTE Cat-M rather than WiFi which, theoretically, should use less power than WiFi. I also want to use IOTAppStory to be able to download OTA updates.
From the video you provided it looks like you want multiple sensors sending data to a centralized server. I don’t really think a ESP32 is the right choice for a server. I would use something a little more powerful that can run a standard LAMP stack (Linux, Apache, MySQL, PHP). You could use a RPi4, Odroid-C2, Odroid-N2 (https://ameridroid.com). I have tried all of them and can recommend the Odroid-N2 running either Ubuntu or DietPi (https://dietpi.com). That is quite the powerful little server. With some PHP programming you could save all the readings from all the sensors in a database and then you could graph over time what those readings were.
On a different note, are you doing this as a learning exercise? You may want to look at other projects out there using Arduino based sensors and RPi servers like the Moteino
Steve, how many Watts is your solar panel you use with your “Voltaic Systems” power bank?
Here is G6EJD – David’s (Excel Spreadsheet): Processor-Solar-Power-Sleep-Calc I will be using, once I get project coded. Perhaps others will find it useful for their calculations.
Someday, multiple sensors; including BME280, Rain Gauge, Wind Speed, Lightening, maybe additiona sensors. I am only hobbyist, self-taught, C++ programmer; was given an “Arduino Uno,” Fall of 2012 with no clue what to do with the device! Reason for the 15 minute interval; is to limit the number of writes to flash memory, hopefully extending flash memory life. ESP32’s for now will be the server in my project. I have some basic graphing on “Thingspeak.com.”
Thank you for the detailed feedback, it is appreciated.
William
I use the Voltaic Systems ARC 10W USB Solar Charger.
Thanks for the link to the spreadsheet. I normally just do the calculations myself but this should make that a bit easier.
Please…
Stripped down to basic ESP32 Server and Client. When ever adding a Station mode WiFi Network to Client and using WiFi.mode(WIFI_AP_STA); all AP, URL request produce are “Error code: -1.” Same sketch with commented out Station mode, WiFi Network in Client; produces all, HTTPResponse codes of 200’s with valid readings.
Basic ESP32 Server and Client sketchs discussed.
I have exhasted many “Google” searchs trying to find a resolution, with no success.
Regards,
William
Hi.
I’m sorry for taking so long to get back to you.
I’ve searched for a while, and I don’t know how to set up that approach.
I was thinking that you could use a different approach for your project.
Instead of making a client-server interaction between your boards, you could use ESP-NOW to send the readings from the sensor node (outside) to the server node (inside). I’m not sure, but I think this approach doesn’t consume so much power on the sender side.
Additionally, I’ve tested this approach and it worked well. I haven’t tested your approach, though.
In this scenario, the receiver is set as an access point (to receive readings) and station simultaneously (to serve pages to visualize the readings).
This project shows how to do that: https://randomnerdtutorials.com/esp32-esp-now-wi-fi-web-server/ It has two senders, but you can adapt the code to use only one.
Let me know if you need help with this.
I don’t know how to solve your other issue (I’m sorry about that and for taking so long to responde 🙁 )
Regards,
Sara
Excellant suggestion Sara; thank you! 72 year old man happy to be able to proceed with project of moving on to solar power; with receiver on mains power. Thanks!
I have almost everything working. Exception is I need to assign sensor variables to ESP AsyncWebserver.
float incomingReadings.temp = temperature;
float incomingReadings.hum = hum;
float incomingReadings.press = currentPressure;
This is not working; suggestions?
Regards,
William
Just looking at it doesn’t look right. You need to create a struct or object and define each parameter. I don’t believe you can declare each parameter that way. So something like:
Struct incomingReadings
{
float temp;
float hum
;
float press
;
};
then to declare each one you would
struct incomingReadings sensor1;
sensor1.temp = temperature;
sensor1.hum = hum;
sensor1.press = currentPressure;
Thank you Steve!
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int id;
float temp;
float hum;
float press;
unsigned int readingId;
} struct_message;
struct_message incomingReadings;
Structure included in the raw receiver code; I am relative new to structures, do I need to create:
Struct incomingReadings
{
float temp;
float hum
;
float press
;
};
then declare:
struct incomingReadings sensor1; sensor1.temp = temperature; sensor1.hum = hum; sensor1.press = currentPressure;
Have not yet been able to get this to work.
Did find:
Serial.println(incomingReadings.temp); Serial.println(incomingReadings.hum); Serial.println(incomingReadings.press);
Are valid statements and print correct values.
Regards,
William
Hi.
If your structure only includes temperature, humidity and pressure, create a structure for that.
For example:
typedef struct struct_message {
float temp; float hum; float pres; } struct_message;
Then, create an instance of that structure called incomingReadings:
struct_message incomingReadings;
The onDataRecv() will put the received readings on the structure.
If you want to access the readings inside that structure and save them on variables to be used in your web server, declare the variables first, before the setup():
foat incomingTemp;
float incomingHum;
float incomingPres;
Then, when you receive a message, you can assign those variables to the received readings:
incomingTem=incomingReadings.temp;
incomingHum=incomingReadings.hum;
incomingPres=incomingReadings.pres;
Then, you can use the incomingTemp, incomingHum, and incomingPres variables in your web server code.
These are just examples of names for your variables.
I hope this helps.
Regards,
Sara
Thank you Sara, appreciate the detailed explanation!
“Receiver sketch I have ported to my “Rain_Gauge” code. Code includes remote BME280 sensor, GPS, local ESP32 web site, Domain hosted web site, FTP, OTA updates, log files for: access, server restarts, WiFi restarts. “Lots of moving parts”; in development since getting “Arduino Uno” given to me Fall 2013.
Thanks to all the community forums that have help me along the way of my coding experience.
Best Regards,
William