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
[env:nodemcuv2]
platform = espressif8266
platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
;platform = espressif8266
board = nodemcu
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 =
ArduinoJson
Adafruit BMP085 Library
DHT sensor library
; SDS011 sensor Library
https://github.com/Zanop/SDS011.git#more-sensor-modes
EspSoftwareSerial
RunningAverage
MQTT

39
src/XD0OTA.cpp

@ -1,4 +1,5 @@
#include "XD0OTA.h"
#include "main.h"
XD0OTA::XD0OTA() {
@ -19,12 +20,12 @@ void XD0OTA::update(void) {
int newVersion = checkForUpdates();
if (newVersion < 1) {
Serial.println("[update] connection error");
DEBUG_MSG("[update] connection error\n");
return;
}
if( newVersion > FW_VERSION ) {
Serial.println( "Preparing to update." );
DEBUG_MSG( "Preparing to update.\n" );
String mac = getMAC();
String fwURL = String( fwUrlBase );
@ -32,24 +33,24 @@ void XD0OTA::update(void) {
String fwImageURL = fwURL;
fwImageURL.concat( ".bin" );
Serial.print( "Firmware image URL: " );
Serial.println( fwImageURL );
DEBUG_MSG( "Firmware image URL: " );
DEBUG_MSG( "%s\n", fwImageURL.c_str() );
t_httpUpdate_return ret = ESPhttpUpdate.update( fwImageURL, String(FW_VERSION), httpsFingerprint );
switch(ret) {
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;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
DEBUG_MSG("HTTP_UPDATE_NO_UPDATES\n");
break;
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;
}
} 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;
fwVersionURL.concat( ".version" );
Serial.println( "Checking for firmware updates." );
Serial.print( "MAC address: " );
Serial.println( mac );
Serial.print( "Firmware version URL: " );
Serial.println( fwVersionURL );
DEBUG_MSG( "Checking for firmware updates.\n" );
DEBUG_MSG( "MAC address: " );
DEBUG_MSG( "%s\n", mac.c_str() );
DEBUG_MSG( "Firmware version URL: " );
DEBUG_MSG( "%s\n", fwVersionURL.c_str() );
HTTPClient httpClient;
httpClient.setTimeout(5000);
@ -75,15 +76,15 @@ int XD0OTA::checkForUpdates() {
if( httpCode == 200 ) {
String newFWVersion = httpClient.getString();
Serial.print( "Current firmware version: " );
Serial.println( FW_VERSION );
Serial.print( "Available firmware version: " );
Serial.println( newFWVersion );
DEBUG_MSG( "Current firmware version: " );
DEBUG_MSG( "%s\n", String(FW_VERSION).c_str() );
DEBUG_MSG( "Available firmware version: " );
DEBUG_MSG( "%s\n", newFWVersion.c_str() );
newVersion = newFWVersion.toInt();
} else {
Serial.print( "Firmware version check failed, got HTTP response code " );
Serial.println( httpCode );
DEBUG_MSG( "Firmware version check failed, got HTTP response code " );
DEBUG_MSG( "%d\n", httpCode );
newVersion = -1;
}
httpClient.end();

2
src/XD0OTA.h

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

79
src/main.cpp

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