From ef587c5d23c4c8cbed27e76b41218ee88374d840 Mon Sep 17 00:00:00 2001 From: Hendrik Langer Date: Tue, 27 Mar 2018 14:37:58 +0200 Subject: [PATCH] try static ip assignment --- src/main.cpp | 85 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f942395..b8800d0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,6 +28,7 @@ const char* server = "ingress.opensensemap.org"; constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden constexpr unsigned int powerbankInterval = 10000; +constexpr unsigned int dhcp_interval = 60*60*1000; //senseBox ID #define SENSEBOX_ID "5a9e9e38f55bff001a494877" @@ -75,6 +76,10 @@ SDS011 sds; volatile unsigned long geiger_counts = 0; unsigned long geiger_previousMillis; unsigned long last_wifi_activity = 0; +unsigned long last_dhcp = 0; +IPAddress ip, dns, gateway, subnet; +char ssid[64]; +char password[64]; struct __attribute__((packed)) SENSOR_DATA { float temperature; @@ -91,7 +96,7 @@ struct __attribute__((packed)) SENSOR_DATA { void sendESPNOW() { - unsigned long espnowmillis = millis(); +// unsigned long espnowmillis = millis(); uint8_t remoteMac[] = {0x5C, 0xCF, 0x7F, 0x5, 0xFD, 0xF0}; #define WIFI_CHANNEL 1 @@ -156,7 +161,7 @@ void ICACHE_FLASH_ATTR sendValues() { } Serial.printf("Temperature : %6.2f°C (DHT22)\n", sd.temperature); - Serial.printf("Humidity : %6.2f% (DHT22)\n", sd.humidity); + Serial.printf("Humidity : %6.2f%% (DHT22)\n", sd.humidity); Serial.printf("Temperature : %6.2f°C (BMP180)\n", sd.temp2); Serial.printf("Pressure : %6.2fhPa (BMP180)\n", sd.pressure); if (!sds_error) Serial.printf("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); @@ -214,23 +219,63 @@ void ICACHE_FLASH_ATTR sendValues() { wifiMulti.addAP("nether.net", "password1"); wifiMulti.addAP("LNet", "password2"); wifiMulti.addAP("Freifunk", ""); - int tries = 0; - constexpr unsigned int retry_delay = 500; - while (wifiMulti.run() != WL_CONNECTED) { - tries++; - Serial.print("."); - if (tries*retry_delay >= powerbankInterval) { - Serial.println(" [ERROR]"); - Serial.println("Retrying in 10 seconds.."); - sendESPNOW(); - delay(powerbankInterval); - ESP.restart(); + + if ( ip != INADDR_NONE && dns != INADDR_NONE && gateway != INADDR_NONE && subnet != INADDR_NONE + && ((ip[0] == 192 && ip[1] == 168) || (ip[0] == 172 && ip[1] == 16)) + && strlen(ssid) > 0 && strlen(password) > 0 + && (millis() - last_dhcp < dhcp_interval) + ) { + + Serial.println("static ip"); + WiFi.config(ip, dns, gateway, subnet); + WiFi.begin(ssid, password); + int tries = 0; + constexpr unsigned int retry_delay = 500; + while (WiFi.status() != WL_CONNECTED) { + tries++; + Serial.print("."); + if (tries*retry_delay >= powerbankInterval) { + Serial.println(" [ERROR]"); + Serial.println("Retrying in 10 seconds.."); + sendESPNOW(); + delay(powerbankInterval); + ESP.restart(); + } + delay(retry_delay); } - delay(retry_delay); + Serial.println(" [CONNECTED, static]"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + } else { + Serial.println("dhcp"); + int tries = 0; + constexpr unsigned int retry_delay = 500; + while (wifiMulti.run() != WL_CONNECTED) { + tries++; + Serial.print("."); + if (tries*retry_delay >= powerbankInterval) { + Serial.println(" [ERROR]"); + Serial.println("Retrying in 10 seconds.."); + sendESPNOW(); + delay(powerbankInterval); + ESP.restart(); + } + delay(retry_delay); + } + Serial.println(" [CONNECTED, dhcp]"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + ip = WiFi.localIP(); + dns = WiFi.dnsIP(); + gateway = WiFi.gatewayIP(); + subnet = WiFi.subnetMask(); + strncpy(ssid, WiFi.SSID().c_str(), 64); + strncpy(password, WiFi.psk().c_str(), 64); + last_dhcp = millis(); } - Serial.println(" [CONNECTED]"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); + int httpCode = 0; for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) { @@ -294,12 +339,14 @@ void setup() { } void loop() { + //Serial.println(millis() - last_wifi_activity); sendValues(); /* keep powerbank happy */ - for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) { + for(int i=0; i<(postingInterval/powerbankInterval); i++) { delay(powerbankInterval); + //Serial.println(millis() - last_wifi_activity); sendESPNOW(); } - delay(powerbankInterval); + //delay(powerbankInterval); }