Browse Source

make serial output toggleable

lightsleep
Hendrik Langer 7 years ago
parent
commit
20e0fbbc08
  1. 13
      platformio.ini
  2. 39
      src/XD0OTA.cpp
  3. 2
      src/XD0OTA.h
  4. 79
      src/main.cpp
  5. 12
      src/main.h

13
platformio.ini

@ -9,15 +9,26 @@
; http://docs.platformio.org/page/projectconf.html ; http://docs.platformio.org/page/projectconf.html
[env:nodemcuv2] [env:nodemcuv2]
platform = espressif8266 platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
;platform = espressif8266
board = nodemcu board = nodemcu
framework = arduino framework = arduino
build_flags =
-DUSERDEBUG
; -DDEBUG_ESP_PORT=Serial
; -DDEBUG
; -DDEBUG_ESP_SSL
; -DDEBUG_ESP_TLS_MEM
; -DDEBUG_ESP_HTTP_CLIENT
; -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH
lib_deps = lib_deps =
ArduinoJson ArduinoJson
Adafruit BMP085 Library Adafruit BMP085 Library
DHT sensor library DHT sensor library
; SDS011 sensor Library ; SDS011 sensor Library
https://github.com/Zanop/SDS011.git#more-sensor-modes https://github.com/Zanop/SDS011.git#more-sensor-modes
EspSoftwareSerial
RunningAverage RunningAverage
MQTT MQTT

39
src/XD0OTA.cpp

@ -1,4 +1,5 @@
#include "XD0OTA.h" #include "XD0OTA.h"
#include "main.h"
XD0OTA::XD0OTA() { XD0OTA::XD0OTA() {
@ -19,12 +20,12 @@ void XD0OTA::update(void) {
int newVersion = checkForUpdates(); int newVersion = checkForUpdates();
if (newVersion < 1) { if (newVersion < 1) {
Serial.println("[update] connection error"); DEBUG_MSG("[update] connection error\n");
return; return;
} }
if( newVersion > FW_VERSION ) { if( newVersion > FW_VERSION ) {
Serial.println( "Preparing to update." ); DEBUG_MSG( "Preparing to update.\n" );
String mac = getMAC(); String mac = getMAC();
String fwURL = String( fwUrlBase ); String fwURL = String( fwUrlBase );
@ -32,24 +33,24 @@ void XD0OTA::update(void) {
String fwImageURL = fwURL; String fwImageURL = fwURL;
fwImageURL.concat( ".bin" ); fwImageURL.concat( ".bin" );
Serial.print( "Firmware image URL: " ); DEBUG_MSG( "Firmware image URL: " );
Serial.println( fwImageURL ); DEBUG_MSG( "%s\n", fwImageURL.c_str() );
t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL, String(FW_VERSION), httpsFingerprint ); t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL, String(FW_VERSION), httpsFingerprint );
switch(ret) { switch(ret) {
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); DEBUG_MSG("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
break; break;
case HTTP_UPDATE_NO_UPDATES: case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES"); DEBUG_MSG("HTTP_UPDATE_NO_UPDATES\n");
break; break;
case HTTP_UPDATE_OK: case HTTP_UPDATE_OK:
Serial.println("[update] Update ok."); // may not called we reboot the ESP DEBUG_MSG("[update] Update ok.\n"); // may not called we reboot the ESP
break; break;
} }
} else { } else {
Serial.println( "Already on latest version" ); DEBUG_MSG( "[update] Already on latest version\n" );
} }
} }
@ -62,11 +63,11 @@ int XD0OTA::checkForUpdates() {
String fwVersionURL = fwURL; String fwVersionURL = fwURL;
fwVersionURL.concat( ".version" ); fwVersionURL.concat( ".version" );
Serial.println( "Checking for firmware updates." ); DEBUG_MSG( "Checking for firmware updates.\n" );
Serial.print( "MAC address: " ); DEBUG_MSG( "MAC address: " );
Serial.println( mac ); DEBUG_MSG( "%s\n", mac.c_str() );
Serial.print( "Firmware version URL: " ); DEBUG_MSG( "Firmware version URL: " );
Serial.println( fwVersionURL ); DEBUG_MSG( "%s\n", fwVersionURL.c_str() );
HTTPClient httpClient; HTTPClient httpClient;
httpClient.setTimeout(5000); httpClient.setTimeout(5000);
@ -75,15 +76,15 @@ int XD0OTA::checkForUpdates() {
if( httpCode == 200 ) { if( httpCode == 200 ) {
String newFWVersion = httpClient.getString(); String newFWVersion = httpClient.getString();
Serial.print( "Current firmware version: " ); DEBUG_MSG( "Current firmware version: " );
Serial.println( FW_VERSION ); DEBUG_MSG( "%s\n", String(FW_VERSION).c_str() );
Serial.print( "Available firmware version: " ); DEBUG_MSG( "Available firmware version: " );
Serial.println( newFWVersion ); DEBUG_MSG( "%s\n", newFWVersion.c_str() );
newVersion = newFWVersion.toInt(); newVersion = newFWVersion.toInt();
} else { } else {
Serial.print( "Firmware version check failed, got HTTP response code " ); DEBUG_MSG( "Firmware version check failed, got HTTP response code " );
Serial.println( httpCode ); DEBUG_MSG( "%d\n", httpCode );
newVersion = -1; newVersion = -1;
} }
httpClient.end(); httpClient.end();

2
src/XD0OTA.h

@ -6,7 +6,7 @@
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h> #include <ESP8266httpUpdate.h>
const int FW_VERSION = 10; //const int FW_VERSION = 13; // see main.h
class XD0OTA { class XD0OTA {
public: public:

79
src/main.cpp

@ -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);
} }

12
src/main.h

@ -0,0 +1,12 @@
#ifndef _MAIN_H
#define _MAIN_H
#ifdef USERDEBUG
#define DEBUG_MSG(...) Serial.printf( __VA_ARGS__ )
#else
#define DEBUG_MSG(...)
#endif
#define FW_VERSION 13
#endif /* _MAIN_H */
Loading…
Cancel
Save