From 30b2902775ca0d360b4f3f86685f7a861e7df8fd Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Thu, 8 Mar 2018 15:55:44 +0100 Subject: [PATCH] add powerbank stay awake code --- src/main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 3377f59..3f03ef6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,9 @@ #include #include #include +extern "C" { +#include +} #include #include @@ -24,6 +27,7 @@ const char* server = "ingress.opensensemap.org"; //const char* password = ""; constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden +constexpr unsigned int powerbankInterval = 10000; //senseBox ID #define SENSEBOX_ID "5a9e9e38f55bff001a494877" @@ -71,6 +75,18 @@ SDS011 sds; volatile unsigned long geiger_counts = 0; unsigned long geiger_previousMillis; +struct __attribute__((packed)) SENSOR_DATA { + float temperature; + float humidity; + float pressure; + float temp2; + float p10; + float p25; + float radioactivity; + float voltage; + float rssi; +} sd; + void ICACHE_FLASH_ATTR sendValues() { float temperature = dht.readTemperature(); float humidity = dht.readHumidity(); @@ -210,6 +226,36 @@ void ICACHE_FLASH_ATTR sendValues() { } +void sendESPNOW() { + unsigned long espnowmillis = millis(); + uint8_t remoteMac[] = {0x5C, 0xCF, 0x7F, 0x5, 0xFD, 0xF0}; + #define WIFI_CHANNEL 1 + + WiFi.forceSleepWake(); + delay(1); // yield(); + WiFi.persistent(false); // don't load and save credentials to flash + WiFi.mode(WIFI_STA); + if (esp_now_init()==0) { + if(esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER)==0){ + + esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0); + u8 bs[sizeof(sd)]; + memcpy(bs, &sd, sizeof(sd)); + esp_now_send(NULL, bs, sizeof(sd)); // max ESP_NOW_MAX_DATA_LEN + + } else { + Serial.println("error configuring ESP NOW"); + } + } else { + Serial.println("error initializing ESP NOW"); + } + esp_now_deinit(); + WiFi.mode(WIFI_OFF); + WiFi.forceSleepBegin(); + delay(1); // yield(); + Serial.printf("sendESPNOW() took %d ms\n", millis()-espnowmillis); +} + void ICACHE_RAM_ATTR ISR_geiger_impulse() { geiger_counts++; } @@ -234,5 +280,10 @@ void setup() { void loop() { sendValues(); - delay(postingInterval); + /* keep powerbank happy */ + for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) { + delay(powerbankInterval); + sendESPNOW(); + } + delay(powerbankInterval); }