Browse Source

try static ip assignment

lightsleep
Hendrik Langer 7 years ago
parent
commit
ef587c5d23
  1. 57
      src/main.cpp

57
src/main.cpp

@ -28,6 +28,7 @@ const char* server = "ingress.opensensemap.org";
constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden constexpr unsigned int postingInterval = 60000; //Uploadintervall in Millisekunden
constexpr unsigned int powerbankInterval = 10000; constexpr unsigned int powerbankInterval = 10000;
constexpr unsigned int dhcp_interval = 60*60*1000;
//senseBox ID //senseBox ID
#define SENSEBOX_ID "5a9e9e38f55bff001a494877" #define SENSEBOX_ID "5a9e9e38f55bff001a494877"
@ -75,6 +76,10 @@ 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; 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 { struct __attribute__((packed)) SENSOR_DATA {
float temperature; float temperature;
@ -91,7 +96,7 @@ struct __attribute__((packed)) SENSOR_DATA {
void sendESPNOW() { void sendESPNOW() {
unsigned long espnowmillis = millis(); // unsigned long espnowmillis = millis();
uint8_t remoteMac[] = {0x5C, 0xCF, 0x7F, 0x5, 0xFD, 0xF0}; uint8_t remoteMac[] = {0x5C, 0xCF, 0x7F, 0x5, 0xFD, 0xF0};
#define WIFI_CHANNEL 1 #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("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("Temperature : %6.2f°C (BMP180)\n", sd.temp2);
Serial.printf("Pressure : %6.2fhPa (BMP180)\n", sd.pressure); Serial.printf("Pressure : %6.2fhPa (BMP180)\n", sd.pressure);
if (!sds_error) Serial.printf("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); if (!sds_error) Serial.printf("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10);
@ -214,6 +219,36 @@ void ICACHE_FLASH_ATTR sendValues() {
wifiMulti.addAP("nether.net", "password1"); wifiMulti.addAP("nether.net", "password1");
wifiMulti.addAP("LNet", "password2"); wifiMulti.addAP("LNet", "password2");
wifiMulti.addAP("Freifunk", ""); wifiMulti.addAP("Freifunk", "");
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);
}
Serial.println(" [CONNECTED, static]");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("dhcp");
int tries = 0; int tries = 0;
constexpr unsigned int retry_delay = 500; constexpr unsigned int retry_delay = 500;
while (wifiMulti.run() != WL_CONNECTED) { while (wifiMulti.run() != WL_CONNECTED) {
@ -228,10 +263,20 @@ void ICACHE_FLASH_ATTR sendValues() {
} }
delay(retry_delay); delay(retry_delay);
} }
Serial.println(" [CONNECTED]"); Serial.println(" [CONNECTED, dhcp]");
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); 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();
}
int httpCode = 0; int httpCode = 0;
for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) { for (int tries=0; tries<3 && httpCode != HTTP_CODE_CREATED; tries++) {
@ -294,12 +339,14 @@ void setup() {
} }
void loop() { void loop() {
//Serial.println(millis() - last_wifi_activity);
sendValues(); sendValues();
/* keep powerbank happy */ /* keep powerbank happy */
for(int i=0; i<(postingInterval/powerbankInterval)-1; i++) { for(int i=0; i<(postingInterval/powerbankInterval); i++) {
delay(powerbankInterval); delay(powerbankInterval);
//Serial.println(millis() - last_wifi_activity);
sendESPNOW(); sendESPNOW();
} }
delay(powerbankInterval); //delay(powerbankInterval);
} }

Loading…
Cancel
Save