Browse Source

prevent delays > powerbankInterval

lightsleep
Hendrik Langer 7 years ago
parent
commit
fce306f7fe
  1. 77
      src/main.cpp

77
src/main.cpp

@ -74,6 +74,7 @@ DHT dht(DHT22_PIN, DHTTYPE);
SDS011 sds; SDS011 sds;
volatile unsigned long geiger_counts = 0; volatile unsigned long geiger_counts = 0;
unsigned long geiger_previousMillis; unsigned long geiger_previousMillis;
unsigned long last_wifi_activity = 0;
struct __attribute__((packed)) SENSOR_DATA { struct __attribute__((packed)) SENSOR_DATA {
float temperature; float temperature;
@ -88,6 +89,42 @@ struct __attribute__((packed)) SENSOR_DATA {
float rssi; float rssi;
} sd; } sd;
void sendESPNOW() {
unsigned long espnowmillis = millis();
uint8_t remoteMac[] = {0x5C, 0xCF, 0x7F, 0x5, 0xFD, 0xF0};
#define WIFI_CHANNEL 1
WiFi.forceSleepWake();
delay(1); // yield();
digitalWrite(LED_BUILTIN, LOW);
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));
for (int i=0; i<20; i++)
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");
}
last_wifi_activity = millis();
esp_now_deinit();
WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin();
delay(1); // yield();
digitalWrite(LED_BUILTIN, HIGH);
// Serial.printf("sendESPNOW() took %d ms\n", millis()-espnowmillis);
}
void ICACHE_FLASH_ATTR sendValues() { void ICACHE_FLASH_ATTR sendValues() {
sd.temperature = dht.readTemperature(); sd.temperature = dht.readTemperature();
sd.humidity = dht.readHumidity(); sd.humidity = dht.readHumidity();
@ -178,16 +215,18 @@ void ICACHE_FLASH_ATTR sendValues() {
wifiMulti.addAP("LNet", "password2"); wifiMulti.addAP("LNet", "password2");
wifiMulti.addAP("Freifunk", ""); wifiMulti.addAP("Freifunk", "");
int tries = 0; int tries = 0;
constexpr unsigned int retry_delay = 500;
while (wifiMulti.run() != WL_CONNECTED) { while (wifiMulti.run() != WL_CONNECTED) {
tries++; tries++;
Serial.print("."); Serial.print(".");
if (tries > 20) { if (tries*retry_delay >= powerbankInterval) {
Serial.println(" [ERROR]"); Serial.println(" [ERROR]");
Serial.println("Retrying in 10 seconds.."); Serial.println("Retrying in 10 seconds..");
sendESPNOW();
delay(powerbankInterval); delay(powerbankInterval);
ESP.restart(); ESP.restart();
} }
delay(500); delay(retry_delay);
} }
Serial.println(" [CONNECTED]"); Serial.println(" [CONNECTED]");
Serial.print("IP address: "); Serial.print("IP address: ");
@ -218,6 +257,7 @@ void ICACHE_FLASH_ATTR sendValues() {
} }
last_wifi_activity = millis();
WiFi.disconnect(); WiFi.disconnect();
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
WiFi.forceSleepBegin(); WiFi.forceSleepBegin();
@ -225,38 +265,6 @@ 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();
digitalWrite(LED_BUILTIN, LOW);
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));
for (int i=0; i<20; i++)
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();
digitalWrite(LED_BUILTIN, HIGH);
// 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++;
@ -287,6 +295,7 @@ void setup() {
void loop() { void loop() {
sendValues(); sendValues();
/* keep powerbank happy */ /* keep powerbank happy */
for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) { for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) {
delay(powerbankInterval); delay(powerbankInterval);

Loading…
Cancel
Save