Created
January 4, 2025 10:06
-
-
Save barii/21f25ec89f04b9f1a581628ef1688a89 to your computer and use it in GitHub Desktop.
sender
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Adafruit_MAX1704X.h" | |
#include <esp_now.h> | |
#include <WiFi.h> | |
#include <esp_wifi.h> | |
//#define DEBUG | |
#define CHANNEL 1 | |
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */ | |
#define TIME_TO_SLEEP 600 /* Time ESP32 will go to sleep (in seconds) */ | |
const int MOTION_PIN = D0; | |
Adafruit_MAX17048 maxlipo; | |
uint8_t broadcastAddress[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
typedef struct { | |
char location[32]; | |
char mac[18]; | |
float cellPercent; | |
float cellVoltage; | |
} esp_now_message; | |
bool send = false; | |
esp_now_message status_message; | |
esp_now_peer_info_t peerInfo; | |
volatile bool motionDetected = false; | |
const int LED_BUILTIN = D10; | |
// the setup function runs once when you press reset or power the board | |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
#ifdef DEBUG | |
Serial.print("\r\nLast Packet Send Status:\t"); | |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); | |
// Go back to sleep for 10 minutes | |
Serial.println("Sleeping for 10 minutes..."); | |
#endif | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | |
esp_deep_sleep_start(); | |
} | |
void setup() { | |
#ifdef DEBUG | |
Serial.begin(115200); | |
while (!Serial) delay(10); // wait until serial monitor opens | |
Serial.println("Start"); | |
#endif | |
delay(10); //why is this needed? | |
if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_GPIO) { | |
// initialize digital pin LED_BUILTIN as an output. | |
pinMode(LED_BUILTIN, OUTPUT); | |
while (!maxlipo.begin()) { | |
#ifdef DEBUG | |
Serial.println(F("Couldnt find Adafruit MAX17048?\nMake sure a battery is plugged in!")); | |
#endif | |
delay(2000); | |
} | |
WiFi.mode(WIFI_STA); | |
while (esp_now_init() != ESP_OK) { | |
#ifdef DEBUG | |
Serial.println("Error initializing ESP-NOW"); | |
Serial.flush(); | |
#endif | |
delay(20); | |
} | |
esp_now_register_send_cb(OnDataSent); | |
// Register peer | |
memcpy(peerInfo.peer_addr, broadcastAddress, 6); | |
peerInfo.channel = CHANNEL; | |
peerInfo.encrypt = false; | |
esp_wifi_set_channel(CHANNEL, WIFI_SECOND_CHAN_NONE); | |
// Add peer | |
while (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
#ifdef DEBUG | |
Serial.println("Failed to add peer"); | |
Serial.flush(); | |
#endif | |
delay(20); | |
} | |
strcpy(status_message.location, "house1"); // Customize with your location | |
strcpy(status_message.mac, "device1"); // Get MAC address of this ESP32 | |
status_message.cellVoltage = maxlipo.cellVoltage(); | |
status_message.cellPercent = maxlipo.cellPercent(); | |
while (status_message.cellVoltage < 0.1 || status_message.cellPercent < 0.1) { | |
status_message.cellVoltage = maxlipo.cellVoltage(); | |
status_message.cellPercent = maxlipo.cellPercent(); | |
#ifdef DEBUG | |
Serial.println(F("waiting")); | |
#endif | |
delay(200); | |
} | |
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t*)&status_message, sizeof(status_message)); | |
} else if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_TIMER) { | |
attachInterrupt(digitalPinToInterrupt(MOTION_PIN), NULL, RISING); | |
esp_deep_sleep_enable_gpio_wakeup(1 << 2, ESP_GPIO_WAKEUP_GPIO_HIGH); // Wake on LOW signal | |
esp_deep_sleep_start(); | |
} else { | |
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); | |
esp_deep_sleep_start(); | |
} | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment