Browse Source

add powerbank stay awake code

lightsleep
Hendrik Langer 7 years ago
parent
commit
30b2902775
  1. 53
      src/main.cpp

53
src/main.cpp

@ -7,6 +7,9 @@
#include <Arduino.h> #include <Arduino.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
extern "C" {
#include <espnow.h>
}
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -24,6 +27,7 @@ const char* server = "ingress.opensensemap.org";
//const char* password = ""; //const char* password = "";
constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden
constexpr unsigned int powerbankInterval = 10000;
//senseBox ID //senseBox ID
#define SENSEBOX_ID "5a9e9e38f55bff001a494877" #define SENSEBOX_ID "5a9e9e38f55bff001a494877"
@ -71,6 +75,18 @@ SDS011 sds;
volatile unsigned long geiger_counts = 0; volatile unsigned long geiger_counts = 0;
unsigned long geiger_previousMillis; 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() { void ICACHE_FLASH_ATTR sendValues() {
float temperature = dht.readTemperature(); float temperature = dht.readTemperature();
float humidity = dht.readHumidity(); 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() { void ICACHE_RAM_ATTR ISR_geiger_impulse() {
geiger_counts++; geiger_counts++;
} }
@ -234,5 +280,10 @@ void setup() {
void loop() { void loop() {
sendValues(); sendValues();
delay(postingInterval); /* keep powerbank happy */
for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) {
delay(powerbankInterval);
sendESPNOW();
}
delay(powerbankInterval);
} }

Loading…
Cancel
Save