esp8266-sensor/esp8266-sensor.ino
2019-12-29 20:06:22 +01:00

158 lines
4.5 KiB
C++

/************************* settings *********************************/
#define SERIAL_DEBUG
//#define OLED_OUTPUT
//#define BATTERY_USE
//#define BME2_USE
//#define BME6_USE
//#define BH_USE
//#define SHT_USE
//#define VEML_USE
//#define CCS_USE
//#define SGP30_USE
#define SDS011_USE
#define SI7021_USE
const bool metric = true;
/************************* generic libs *********************************/
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#endif
#if defined(ESP32)
#include <WiFi.h>
#endif
#include <Wire.h>
#include <MQTTClient.h>
#include <IPAddress.h>
#include "EnvironmentCalculations.h"
#include "secrets.h"
/*
reset causes:
0:
1: normal boot
2: reset pin
3: software reset
4: watchdog reset
boot device:
0:
1: ram
3: flash
Label to GPIO NodeMCU
D0 = 16;
D1 = 5; SCL
D2 = 4; SDA
D3 = 0;
D4 = 2;
D5 = 14;
D6 = 12;
D7 = 13;
D8 = 15;
D9 = 3;
D10 = 1; builtinLED
Wemos D1 mini
builtinLED = 2
D0 = 16;
D1 = 5; SCL
D2 = 4; SDA
D3 = 0;
D4 = 2; builtinLED
D5 = 14;
D6 = 12;
D7 = 13; CCS INTERRUPT
D8 = 15;
RX = 3;
TX = 1;
*/
/************************* state machine *********************************/
//the charger board cuts at 2.5V
//state definitions
#define STATE_COLDSTART 0
#define STATE_SLEEP_WAKE 1
#define STATE_FIRMWARE 2
#define STATE_MEASUREMENT 4
#define SLEEP_TIME 60*1000000UL // sleep intervalls in us (1min)
#define SLEEP_TIME_MEASUREMENT 1*SLEEP_TIME // sleep 60s(1min) after measurement
#define SLEEP_TIME_WIFI_TIMEOUT 1*SLEEP_TIME // sleep a minute
#ifdef BATTERY_USE
#define SLEEP_TIME_LOW_BAT 60*SLEEP_TIME // sleep 3600s(60min) after measurement
#define BATT_WARNING_VOLTAGE 270 // Voltage for Low-Bat warning in mV --rember the drop-out of the voltage regulator -- Be aware not to measure the VCC!
#define BATT_MEASUREMENT_OFFSET 30 //Meh, somewhere and somehow is something not that well...
#endif
/************************* WiFi Client *********************************/
//see secrets.h
const int WIFI_CONNECT_TIMEOUT = 10; // seconds - max time for wifi connect to router, if exceeded go to sleep
//ToDo Legacy IPv4
/*
IPAddress ip(10, 0, 4, 14);
IPAddress gateway(10, 0, 4, 1);
IPAddress subnet(255, 255, 255, 240);
IPAddress dns(10, 0, 4, 1);
*/
IPAddress mqttBroker(10, 0, 4, 1); //failsafe
/********************* Firmware update **************************************/
const int FW_VERSION = 10;
const char* fwUrlBase = "";
/************************* MQTT Broker Setup *********************************/
#define name "bund"
//see secrets.h
char MQTT_CLIENTID[10];
const int MQTT_PORT = 1883;
const char MQTT_SERVER[] = "mqtt.koelner.dynvpn.de";
const char MQTT_USERNAME[] = mqttUser;
const char MQTT_PASSWORD[] = mqttPass;
const char MQTT_WILL_TOPIC[] = "unsafe/sys/will";
const int MQTT_WILL_QOS = 0;
const int MQTT_WILL_RETAIN = 0;
const char MQTT_WILL_MESSAGE[] = "Look out of the window!";
/****************************** Feeds ***************************************/
//ToDo: generate feed from CLIENTID and determine the ClientID from the ChipID
#if defined(BME2_USE) || defined(BME6_USE)
const char TEMPERATURE_FEED[] = "unsafe/sensors/temperature";
const char PRESSURE_FEED[] = "unsafe/sensors/pressure";
const char HUMIDITY_FEED[] = "unsafe/sensors/humidity";
const char ABSHUMIDITY_FEED[] = "unsafe/sensors/abshumidity";
#endif
#if defined(SHT_USE) || defined(SI7021_USE)
const char TEMPERATURE_FEED[] = "unsafe/sensors/temperature";
const char HUMIDITY_FEED[] = "unsafe/sensors/humidity";
const char ABSHUMIDITY_FEED[] = "unsafe/sensors/abshumidity";
#endif
#ifdef BME6_USE
const char IAQ_FEED[] = "unsafe/sensors/iaq";
const char IAQPREC_FEED[] = "unsafe/sensors/iaqprec";
#endif
#ifdef VEML_USE
const char UVA_FEED[] = "unsafe/sensors/uva";
const char UVB_FEED[] = "unsafe/sensors/uvb";
const char UVINDEX_FEED[] = "unsafe/sensors/uvindex";
#endif
#ifdef BH_USE
const char LIGHT_FEED[] = "unsafe/sensors/light";
#endif
#if defined (CCS_USE) || defined(SGP30_USE)
const char ECO2_FEED[] = "unsafe/sensors/eco2";
const char TVOC_FEED[] = "unsafe/sensors/tvoc";
#endif
#if defined (SDS011_USE) || defined(SGP30_USE)
const char PM10_FEED[] = "unsafe/sensors/pm10";
const char PM10CORR_FEED[] = "unsafe/sensors/pm10corr";
const char PM25_FEED[] = "unsafe/sensors/pm25";
const char PM25CORR_FEED[] = "unsafe/sensors/pm25corr";
#endif
#ifdef BATTERY_USE
const char BATTERY_FEED[] = "unsafe/sys/battery";
#endif