esp8266-sensor/esp8266-sensor.ino

136 lines
4.0 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
const bool metric = true;
/************************* generic libs *********************************/
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#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 = "http://10.0.4.2/fota/";
/************************* MQTT Broker Setup *********************************/
#define name "test"
//see secrets.h
char MQTT_CLIENTID[10];
const char MQTT_WILL_TOPIC[] = "test/sys/will";
const int MQTT_WILL_QOS = 0;
const int MQTT_WILL_RETAIN = 0;
const char MQTT_WILL_MESSAGE[] = "Finally died...";
/****************************** Feeds ***************************************/
//ToDo: generate feed from CLIENTID and determine the ClientID from the ChipID
#if defined(BME2_USE) || defined(BME6_USE)
const char TEMPERATURE_FEED[] = "test/sensors/temperature";
const char PRESSURE_FEED[] = "test/sensors/pressure";
const char HUMIDITY_FEED[] = "test/sensors/humidity";
const char ABSHUMIDITY_FEED[] = "test/sensors/abshumidity";
#endif
#if defined(SHT_USE)
const char TEMPERATURE_FEED[] = "test/sensors/temperature";
const char HUMIDITY_FEED[] = "test/sensors/humidity";
const char ABSHUMIDITY_FEED[] = "test/sensors/abshumidity";
#endif
#ifdef BME6_USE
const char IAQ_FEED[] = "test/sensors/iaq";
const char IAQPREC_FEED[] = "test/sensors/iaqprec";
#endif
#ifdef VEML_USE
const char UVA_FEED[] = "test/sensors/uva";
const char UVB_FEED[] = "test/sensors/uvb";
const char UVINDEX_FEED[] = "test/sensors/uvindex";
#endif
#ifdef BH_USE
const char LIGHT_FEED[] = "test/sensors/light";
#endif
#if defined (CCS_USE) || defined(SGP30_USE)
const char ECO2_FEED[] = "test/sensors/eco2";
const char TVOC_FEED[] = "test/sensors/tvoc";
#endif
#ifdef BATTERY_USE
const char BATTERY_FEED[] = "test/sys/battery";
#endif