|
@ -27,6 +27,7 @@ extern "C" { |
|
|
#include <SDS011.h> |
|
|
#include <SDS011.h> |
|
|
|
|
|
|
|
|
#include <XD0OTA.h> |
|
|
#include <XD0OTA.h> |
|
|
|
|
|
#include "main.h" |
|
|
|
|
|
|
|
|
const char* server = "ingress.opensensemap.org"; |
|
|
const char* server = "ingress.opensensemap.org"; |
|
|
|
|
|
|
|
@ -130,10 +131,10 @@ void sendESPNOW() { |
|
|
esp_now_send(NULL, bs, sizeof(sd)); // max ESP_NOW_MAX_DATA_LEN
|
|
|
esp_now_send(NULL, bs, sizeof(sd)); // max ESP_NOW_MAX_DATA_LEN
|
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
Serial.println("error configuring ESP NOW"); |
|
|
DEBUG_MSG("error configuring ESP NOW\n"); |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
Serial.println("error initializing ESP NOW"); |
|
|
DEBUG_MSG("error initializing ESP NOW\n"); |
|
|
} |
|
|
} |
|
|
last_wifi_activity = millis(); |
|
|
last_wifi_activity = millis(); |
|
|
esp_now_deinit(); |
|
|
esp_now_deinit(); |
|
@ -141,7 +142,7 @@ void sendESPNOW() { |
|
|
WiFi.forceSleepBegin(); |
|
|
WiFi.forceSleepBegin(); |
|
|
delay(1); // yield();
|
|
|
delay(1); // yield();
|
|
|
digitalWrite(LED_BUILTIN, HIGH); |
|
|
digitalWrite(LED_BUILTIN, HIGH); |
|
|
// Serial.printf("sendESPNOW() took %d ms\n", millis()-espnowmillis);
|
|
|
// DEBUG_MSG("sendESPNOW() took %d ms\n", millis()-espnowmillis);
|
|
|
} |
|
|
} |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
@ -161,9 +162,9 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
|
|
|
|
|
|
int sds_error = sds.read(&(sd.p25), &(sd.p10)); |
|
|
int sds_error = sds.read(&(sd.p25), &(sd.p10)); |
|
|
if(!sds_error) { |
|
|
if(!sds_error) { |
|
|
// Serial.println("SDS011 updated.");
|
|
|
// DEBUG_MSG("SDS011 updated.\n");
|
|
|
} else { |
|
|
} else { |
|
|
// Serial.println("SDS011 no new values.");
|
|
|
// DEBUG_MSG("SDS011 no new values.\n");
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// sds.sleep();
|
|
|
// sds.sleep();
|
|
@ -173,14 +174,14 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
float constexpr own_cpm = OWN_BACKGROUND_CPS * 60; |
|
|
float constexpr own_cpm = OWN_BACKGROUND_CPS * 60; |
|
|
sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR; |
|
|
sd.radioactivity = (sd.cpm - own_cpm) * CONV_FACTOR; |
|
|
|
|
|
|
|
|
Serial.printf("Temperature : %6.2f°C (DHT22)\n", sd.temperature); |
|
|
DEBUG_MSG("Temperature : %6.2f°C (DHT22)\n", sd.temperature); |
|
|
Serial.printf("Humidity : %6.2f%% (DHT22)\n", sd.humidity); |
|
|
DEBUG_MSG("Humidity : %6.2f%% (DHT22)\n", sd.humidity); |
|
|
Serial.printf("Temperature : %6.2f°C (BMP180)\n", sd.temp2); |
|
|
DEBUG_MSG("Temperature : %6.2f°C (BMP180)\n", sd.temp2); |
|
|
Serial.printf("Pressure : %6.2fhPa (BMP180)\n", sd.pressure); |
|
|
DEBUG_MSG("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) DEBUG_MSG("Particles 10 : %6.2fµg/m³ (SDS011)\n", sd.p10); |
|
|
if (!sds_error) Serial.printf("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25); |
|
|
if (!sds_error) DEBUG_MSG("Particles 2.5: %6.2fµg/m³ (SDS011)\n", sd.p25); |
|
|
if (sd.cpm > 0) Serial.printf("Radiation : %6.2fµSv/h (J305)\n", sd.radioactivity); |
|
|
if (sd.cpm > 0) DEBUG_MSG("Radiation : %6.2fµSv/h (J305)\n", sd.radioactivity); |
|
|
Serial.printf("Voltage : %6.2fV (ESP8266)\n", sd.voltage); |
|
|
DEBUG_MSG("Voltage : %6.2fV (ESP8266)\n", sd.voltage); |
|
|
|
|
|
|
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
DynamicJsonBuffer jsonBuffer; |
|
|
JsonArray& array = jsonBuffer.createArray(); |
|
|
JsonArray& array = jsonBuffer.createArray(); |
|
@ -251,7 +252,7 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
&& (millis() - last_dhcp < dhcp_interval) |
|
|
&& (millis() - last_dhcp < dhcp_interval) |
|
|
) { |
|
|
) { |
|
|
|
|
|
|
|
|
Serial.println("static ip"); |
|
|
DEBUG_MSG("static ip\n"); |
|
|
WiFi.config(ip, dns, gateway, subnet); |
|
|
WiFi.config(ip, dns, gateway, subnet); |
|
|
WiFi.begin(ssid, password); |
|
|
WiFi.begin(ssid, password); |
|
|
int tries = 0; |
|
|
int tries = 0; |
|
@ -259,36 +260,36 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
constexpr unsigned int max_retry_delay = 10000; |
|
|
constexpr unsigned int max_retry_delay = 10000; |
|
|
while (WiFi.status() != WL_CONNECTED) { |
|
|
while (WiFi.status() != WL_CONNECTED) { |
|
|
tries++; |
|
|
tries++; |
|
|
Serial.print("."); |
|
|
DEBUG_MSG("."); |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
Serial.println(" [ERROR]"); |
|
|
DEBUG_MSG(" [ERROR]\n"); |
|
|
Serial.println("Rebooting.."); |
|
|
DEBUG_MSG("Rebooting..\n"); |
|
|
ESP.restart(); |
|
|
ESP.restart(); |
|
|
} |
|
|
} |
|
|
delay(retry_delay); |
|
|
delay(retry_delay); |
|
|
} |
|
|
} |
|
|
Serial.println(" [CONNECTED, static]"); |
|
|
DEBUG_MSG(" [CONNECTED, static]\n"); |
|
|
Serial.print("IP address: "); |
|
|
DEBUG_MSG("IP address: "); |
|
|
Serial.println(WiFi.localIP()); |
|
|
DEBUG_MSG("%s\n", String(WiFi.localIP()).c_str()); |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
Serial.println("dhcp"); |
|
|
DEBUG_MSG("dhcp\n"); |
|
|
int tries = 0; |
|
|
int tries = 0; |
|
|
constexpr unsigned int retry_delay = 500; |
|
|
constexpr unsigned int retry_delay = 500; |
|
|
constexpr unsigned int max_retry_delay = 12000; |
|
|
constexpr unsigned int max_retry_delay = 12000; |
|
|
while (wifiMulti.run() != WL_CONNECTED) { |
|
|
while (wifiMulti.run() != WL_CONNECTED) { |
|
|
tries++; |
|
|
tries++; |
|
|
Serial.print("."); |
|
|
DEBUG_MSG("."); |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
Serial.println(" [ERROR]"); |
|
|
DEBUG_MSG(" [ERROR]\n"); |
|
|
Serial.println("Rebooting.."); |
|
|
DEBUG_MSG("Rebooting..\n"); |
|
|
ESP.restart(); |
|
|
ESP.restart(); |
|
|
} |
|
|
} |
|
|
delay(retry_delay); |
|
|
delay(retry_delay); |
|
|
} |
|
|
} |
|
|
Serial.println(" [CONNECTED, dhcp]"); |
|
|
DEBUG_MSG(" [CONNECTED, dhcp]\n"); |
|
|
Serial.print("IP address: "); |
|
|
DEBUG_MSG("IP address: "); |
|
|
Serial.println(WiFi.localIP()); |
|
|
DEBUG_MSG("%s\n", String(WiFi.localIP()).c_str()); |
|
|
|
|
|
|
|
|
ip = WiFi.localIP(); |
|
|
ip = WiFi.localIP(); |
|
|
dns = WiFi.dnsIP(); |
|
|
dns = WiFi.dnsIP(); |
|
@ -316,9 +317,9 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
if (httpCode > 0) { |
|
|
if (httpCode > 0) { |
|
|
if (httpCode == HTTP_CODE_CREATED) { |
|
|
if (httpCode == HTTP_CODE_CREATED) { |
|
|
httpclient.writeToStream(&Serial); |
|
|
httpclient.writeToStream(&Serial); |
|
|
Serial.println(); |
|
|
DEBUG_MSG("\n"); |
|
|
} else { |
|
|
} else { |
|
|
Serial.printf("[HTTP] POST... failed, error: %s\n", httpclient.errorToString(httpCode).c_str()); |
|
|
DEBUG_MSG("[HTTP] POST... failed, error: %s\n", httpclient.errorToString(httpCode).c_str()); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -335,10 +336,10 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
constexpr unsigned int max_retry_delay = 5000; |
|
|
constexpr unsigned int max_retry_delay = 5000; |
|
|
while (!mqttclient.connect(mqttusername, mqttusername, mqttpassword)) { |
|
|
while (!mqttclient.connect(mqttusername, mqttusername, mqttpassword)) { |
|
|
tries++; |
|
|
tries++; |
|
|
Serial.print("."); |
|
|
DEBUG_MSG("."); |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
if (tries*retry_delay >= max_retry_delay) { |
|
|
Serial.println(" [ERROR]"); |
|
|
DEBUG_MSG(" [ERROR]\n"); |
|
|
Serial.println("Rebooting.."); |
|
|
DEBUG_MSG("Rebooting..\n"); |
|
|
ESP.restart(); |
|
|
ESP.restart(); |
|
|
} |
|
|
} |
|
|
delay(retry_delay); |
|
|
delay(retry_delay); |
|
@ -347,9 +348,9 @@ void ICACHE_FLASH_ATTR sendValues() { |
|
|
mqttclient.loop(); |
|
|
mqttclient.loop(); |
|
|
root.printTo(buffer, sizeof(buffer)); |
|
|
root.printTo(buffer, sizeof(buffer)); |
|
|
if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { |
|
|
if (mqttclient.publish("sensor/esp-weatherstation/01/json", buffer, strlen(buffer))) { |
|
|
Serial.println("mqtt done"); |
|
|
DEBUG_MSG("mqtt done\n"); |
|
|
} else { |
|
|
} else { |
|
|
Serial.println("mqtt failed"); |
|
|
DEBUG_MSG("mqtt failed\n"); |
|
|
} |
|
|
} |
|
|
mqttclient.loop(); |
|
|
mqttclient.loop(); |
|
|
mqttclient.disconnect(); |
|
|
mqttclient.disconnect(); |
|
@ -375,14 +376,16 @@ void ICACHE_RAM_ATTR ISR_geiger_impulse() { |
|
|
|
|
|
|
|
|
// will be called every 6 seconds
|
|
|
// will be called every 6 seconds
|
|
|
void timerCallback(void *pArg) { |
|
|
void timerCallback(void *pArg) { |
|
|
//Serial.printf("running average counts: %d average: %6.2f\n", geiger_counts, geigeraverage.getAverage());
|
|
|
//DEBUG_MSG("running average counts: %d average: %6.2f\n", geiger_counts, geigeraverage.getAverage());
|
|
|
geigeraverage.addValue(geiger_counts); |
|
|
geigeraverage.addValue(geiger_counts); |
|
|
geiger_counts = 0; |
|
|
geiger_counts = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void setup() { |
|
|
void setup() { |
|
|
|
|
|
#ifdef USERDEBUG |
|
|
Serial.begin(115200); |
|
|
Serial.begin(115200); |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT); |
|
|
pinMode(LED_BUILTIN, OUTPUT); |
|
|
digitalWrite(LED_BUILTIN, HIGH); // turn OFF board led
|
|
|
digitalWrite(LED_BUILTIN, HIGH); // turn OFF board led
|
|
@ -392,7 +395,7 @@ void setup() { |
|
|
|
|
|
|
|
|
Wire.begin(BMP_SDA, BMP_SCL); |
|
|
Wire.begin(BMP_SDA, BMP_SCL); |
|
|
if (!bmp.begin(BMP085_MODE_STANDARD)) { |
|
|
if (!bmp.begin(BMP085_MODE_STANDARD)) { |
|
|
Serial.println("No valid BMP085 sensor!"); |
|
|
DEBUG_MSG("No valid BMP085 sensor!\n"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
dht.begin(); |
|
|
dht.begin(); |
|
@ -405,11 +408,11 @@ void setup() { |
|
|
os_timer_setfn(&Timer1, timerCallback, NULL); |
|
|
os_timer_setfn(&Timer1, timerCallback, NULL); |
|
|
os_timer_arm(&Timer1, 6000, true); |
|
|
os_timer_arm(&Timer1, 6000, true); |
|
|
|
|
|
|
|
|
Serial.println("ready."); Serial.flush(); |
|
|
DEBUG_MSG("ready.\n"); Serial.flush(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
//Serial.println(millis() - last_wifi_activity);
|
|
|
//DEBUG_MSG("%d\n", millis() - last_wifi_activity);
|
|
|
sendValues(); |
|
|
sendValues(); |
|
|
delay(postingInterval); |
|
|
delay(postingInterval); |
|
|
} |
|
|
} |
|
|