Any code snippets on using ESP NOW in a loop where Wi-Fi is enabled and disabled recursively using a timer loop and implementing lowest power in between transmissions.
Typical sleep time would be for 15 minutes at a time.
Thanks Steve
Any code snippets on using ESP NOW in a loop where Wi-Fi is enabled and disabled recursively using a timer loop and implementing lowest power in between transmissions.
Typical sleep time would be for 15 minutes at a time.
Thanks Steve
Try Google or use “Search Forum” in this window, up to the right.
try something like this for the sender (you will need to figure out the receiver's code yourself)
#include <esp_now.h>
#include <WiFi.h>
#define DEEP_SLEEP_WAKEUP_SEC 900 // 15 min in seconds
#define uS_TO_S_FACTOR 1000000UL // Conversion factor for micro seconds to seconds
// put here the MAC Address of the receiving device
uint8_t peerMAC[] = {0xAA, 0xBB, 0xBB, 0xDD, 0xEE, 0xFF};
// some kind of data structure for sending
typedef struct message {
int integerData;
float floatData;
} message;
message msgToSend;
esp_now_peer_info_t peerInfo;
// callback function
void dataSentCallback(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA); // you need this
delay(1000);
if (esp_now_init() != ESP_OK) {
Serial.println(F("oops, could not initizlize esp_now"));
return;
}
esp_now_register_send_cb(esp_now_send_cb_t(dataSentCallback));
memcpy(peerInfo.peer_addr, peerMAC, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
esp_now_add_peer(&peerInfo);
// the MCU will wake up from deep sleep every 15 minutes
// then execute the setup()
// and then the loop() which ends in deep sleep again
// clear all wakeup sources
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
// enable the timer wakeup source for 15min
esp_sleep_enable_timer_wakeup(DEEP_SLEEP_WAKEUP_SEC * uS_TO_S_FACTOR);
}
void loop() {
msgToSend.integerData = 42;
msgToSend.floatData = 4.2;
esp_now_send(peerMAC, (uint8_t *) &msgToSend, sizeof(msgToSend));
esp_now_deinit(); // you need this too
WiFi.mode(WIFI_OFF); // for the deepest sleep
Serial.println(F("going to sleep"));
Serial.flush();
esp_deep_sleep_start();
}
edit: I added the esp_now_deinit() call
note that the C3 has issues with having Serial activity when there's no USB connected and deep sleep is involved. As in it doesn't always wake up.
So for production code, make sure you only call Serial methods when the USB-serial is connected.
You can check with this function call:
usb_serial_jtag_is_connected();