2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- CONFIG ---------------------------------- */
|
|
|
|
/* Adapt this sketch to your needs by modifying the config_device.h*/
|
|
|
|
/* --------------------- GPIO ------------------------------------ */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* Generic GPIO Control by HTTP REST interface */
|
|
|
|
/* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* -------------------- SETUP ------------------------------------ */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* At first boot it creates a WiFi access point */
|
|
|
|
/* and provide a web server on it, so you can configure Wifi ssid */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* Wifi passwd, and a mDNS HOSTNAME and a mqtt server */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* In this mode, device will be available at 192.168.4.1 */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- OTA ------------------------------------- */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* Device can also be put in OTA Mode: In this case, if you have */
|
|
|
|
/* a little flash (512K), it better to disable SPIFFS in */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* "Flash Size" Menu to save some place for the sketch to upload */
|
|
|
|
/* Use espota.py to upload OTA */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* After passing in OTA mode, next boot will be in setup mode */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- BMP180 -----------------------------------*/
|
|
|
|
/* if BMP180 is available temperature will be published by mqtt */
|
|
|
|
/* and on web server interface */
|
|
|
|
/* --------------------- MQTT ------------------------------------ */
|
|
|
|
/* Send information to mqtt server configured in the setup mode */
|
|
|
|
/* GPIO value configured in config_device.h can be get by */
|
2016-06-01 01:22:47 +02:00
|
|
|
/* subscribing to /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO] and */
|
|
|
|
/* modified by publishin to */
|
|
|
|
/* /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO]/set */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* BMP180 will be published to /feeds/[HOSTNAME]/temperature and */
|
|
|
|
/* /feeds/[HOSTNAME]/pressure */
|
|
|
|
|
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2019-05-14 23:44:46 +02:00
|
|
|
#include <vector>
|
2016-03-11 00:43:24 +01:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WiFiClient.h>
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
#include <ESP8266mDNS.h>
|
|
|
|
#include <EEPROM.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <errno.h>
|
2016-11-16 00:00:47 +01:00
|
|
|
#include <HIB.h>
|
2016-04-01 01:06:13 +02:00
|
|
|
|
2016-03-30 00:44:37 +02:00
|
|
|
#include "config.h"
|
2016-09-16 11:42:33 +02:00
|
|
|
#include "utils.h"
|
2016-03-28 23:28:33 +02:00
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
#include "debug_sketch.h"
|
2016-03-26 15:27:23 +01:00
|
|
|
#include "BMP180.h"
|
2020-02-28 17:16:39 +01:00
|
|
|
#include "BME680.h"
|
2020-07-28 19:37:19 +02:00
|
|
|
#include "BME680_BSEC.h"
|
2016-04-07 15:26:01 +02:00
|
|
|
#include "sensor_DHT.h"
|
2016-06-02 01:31:57 +02:00
|
|
|
#include "dry_sensor.h"
|
2016-04-14 01:15:42 +02:00
|
|
|
#include "MQTT.h"
|
2016-03-11 01:31:03 +01:00
|
|
|
#include "Adafruit_MQTT.h"
|
|
|
|
#include "Adafruit_MQTT_Client.h"
|
2016-06-01 15:21:32 +02:00
|
|
|
#include "EEPROM.h"
|
2021-02-18 00:25:45 +01:00
|
|
|
#include "Teleinfo.h"
|
2016-03-11 01:31:03 +01:00
|
|
|
|
2016-04-01 01:06:13 +02:00
|
|
|
extern "C" {
|
|
|
|
#include <user_interface.h>
|
|
|
|
}
|
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-12-14 22:57:01 +01:00
|
|
|
#define BOOTMODE_SETUP 1
|
|
|
|
#define BOOTMODE_NORMAL 2
|
|
|
|
#define BOOTMODE_OTA 3
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-16 00:54:13 +01:00
|
|
|
double temp, pressure;
|
2016-04-07 15:26:01 +02:00
|
|
|
float dhtTemp, dhtHumidity;
|
2020-02-27 00:07:30 +01:00
|
|
|
float bme680T, bme680P, bme680H, bme680G, bme680A;
|
2020-07-28 19:37:19 +02:00
|
|
|
float bme680BSECT, bme680BSECP, bme680BSECH, bme680BSECIaq, bme680BSECIaqAcc;
|
2016-06-02 01:31:57 +02:00
|
|
|
int dryness;
|
2016-03-14 17:18:36 +01:00
|
|
|
uint8_t mode;
|
2017-01-28 14:29:28 +01:00
|
|
|
int reconfig = 0;
|
2021-02-18 00:25:45 +01:00
|
|
|
productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL};
|
2016-12-13 22:31:14 +01:00
|
|
|
// Should have less that MAXSUBSCRIPTIONS elements
|
|
|
|
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
|
|
|
|
const int gpioControlled[] = CONFIG_CONTROLLED_GPIO;
|
|
|
|
const int gpioObserved[] = CONFIG_OBSERVED_GPIO;
|
|
|
|
const int pwmControlled[] = CONFIG_CONTROLLED_PWM;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
/* Set these to your desired credentials. */
|
2016-03-30 00:44:37 +02:00
|
|
|
const char *ssid = CONFIG_SSID_NAME;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
|
|
|
|
/* WebServer decl*/
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleRoot();
|
|
|
|
void WebHandleSetup();
|
|
|
|
void WebHandleGpio();
|
|
|
|
void WebHandleSave();
|
|
|
|
void WebHandleOTA();
|
|
|
|
void WebHandleNotFound();
|
|
|
|
void WebSetupServer(int bootmode);
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
|
2016-11-16 00:00:47 +01:00
|
|
|
#ifdef CONFIG_SETUP_BUTTON
|
2019-05-14 23:44:46 +02:00
|
|
|
void onLongButtonPressed(uint8_t pin) {
|
|
|
|
if (pin == CONFIG_SETUP_BUTTON) {
|
2017-01-28 14:29:28 +01:00
|
|
|
reconfig = 1;
|
2016-11-16 00:00:47 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Putting device in setup mode");
|
|
|
|
mode = BOOTMODE_SETUP;
|
|
|
|
WiFi.disconnect();
|
|
|
|
WiFi.softAP(ssid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-11-20 00:06:31 +01:00
|
|
|
void WifiSetup(productConfig conf) {
|
2016-03-11 00:43:24 +01:00
|
|
|
IPAddress myIP;
|
2018-12-19 22:36:19 +01:00
|
|
|
int connectionTry = 0;
|
2017-01-28 14:29:28 +01:00
|
|
|
if (mode == BOOTMODE_SETUP) {
|
2017-01-01 23:35:19 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Configuring access point: " CONFIG_SSID_NAME);
|
2016-03-11 00:43:24 +01:00
|
|
|
/* You can set a password to the AP here */
|
2016-12-14 22:57:01 +01:00
|
|
|
WiFi.softAP(CONFIG_SSID_NAME);
|
2016-03-11 00:43:24 +01:00
|
|
|
myIP = WiFi.softAPIP();
|
|
|
|
} else {
|
2016-11-20 00:23:16 +01:00
|
|
|
//Disable previous AP mode
|
2016-04-10 02:01:25 +02:00
|
|
|
WiFi.softAPdisconnect(true);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
|
2019-05-14 23:44:46 +02:00
|
|
|
if (conf.ip_mode == 1) {
|
2016-04-07 12:05:37 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("Use static ip configuration");
|
2016-11-20 00:06:31 +01:00
|
|
|
WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2));
|
2016-04-07 02:02:51 +02:00
|
|
|
}
|
2018-12-19 22:36:19 +01:00
|
|
|
uint8_t *bssidConf = NULL;
|
|
|
|
uint8_t bssid[6];
|
|
|
|
if (conf.bssid[0] != '\0') {
|
|
|
|
String bssidStr = conf.bssid;
|
2019-05-14 23:44:46 +02:00
|
|
|
bssid[0] = strtoul(bssidStr.substring(0, 2).c_str(), NULL, 16);
|
|
|
|
bssid[1] = strtoul(bssidStr.substring(3, 5).c_str(), NULL, 16);
|
|
|
|
bssid[2] = strtoul(bssidStr.substring(6, 8).c_str(), NULL, 16);
|
|
|
|
bssid[3] = strtoul(bssidStr.substring(9, 11).c_str(), NULL, 16);
|
|
|
|
bssid[4] = strtoul(bssidStr.substring(12, 14).c_str(), NULL, 16);
|
|
|
|
bssid[5] = strtoul(bssidStr.substring(15, 17).c_str(), NULL, 16);
|
2019-06-04 23:52:25 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Using BSSID: %02x:%02x:%02x:%02x:%02x:%02x\n", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
|
2018-12-19 22:36:19 +01:00
|
|
|
bssidConf = bssid;
|
|
|
|
}
|
2019-06-04 23:52:25 +02:00
|
|
|
|
|
|
|
// WA https://github.com/esp8266/Arduino/issues/2186
|
|
|
|
WiFi.persistent(false);
|
|
|
|
WiFi.mode(WIFI_OFF); // this is a temporary line, to be removed after SDK update to 1.5.4
|
|
|
|
WiFi.mode(WIFI_STA);
|
2018-12-19 22:36:19 +01:00
|
|
|
WiFi.begin(conf.ssid, conf.password, conf.channel, bssidConf);
|
2016-03-11 00:43:24 +01:00
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
2019-06-04 23:52:25 +02:00
|
|
|
delay(50);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT(".");
|
2019-05-14 23:44:46 +02:00
|
|
|
if (reconfig == 1) {
|
2017-01-28 14:29:28 +01:00
|
|
|
reconfig = 0;
|
|
|
|
return;
|
|
|
|
}
|
2020-07-28 19:37:19 +02:00
|
|
|
if (connectionTry == 60) {
|
2019-06-04 23:52:25 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("Cannot connect to wifi. Try without BSSID and channel");
|
2018-12-19 22:36:19 +01:00
|
|
|
WiFi.begin(conf.ssid, conf.password);
|
|
|
|
}
|
|
|
|
connectionTry++;
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-11-20 00:23:16 +01:00
|
|
|
SKETCH_DEBUG_PRINTF("\nWiFi connected\n");
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-06-01 01:22:47 +02:00
|
|
|
#ifdef CONFIG_ENABLE_MDNS
|
2016-11-20 00:06:31 +01:00
|
|
|
if (!MDNS.begin(conf.host)) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!");
|
2016-11-20 00:23:16 +01:00
|
|
|
} else {
|
|
|
|
SKETCH_DEBUG_PRINTLN("mDNS responder started");
|
2017-03-29 21:55:14 +02:00
|
|
|
#ifndef CONFIG_DISABLE_WEB
|
|
|
|
//Needed for OTA by HTTP
|
|
|
|
MDNS.addService("http", "tcp", 80);
|
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-04-06 16:03:14 +02:00
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
myIP = WiFi.localIP();
|
|
|
|
}
|
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT("My IP address: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(myIP);
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-11-20 00:23:16 +01:00
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void OTASetup() {
|
2016-10-28 18:03:36 +02:00
|
|
|
#ifndef CONFIF_DISABLE_OTA
|
2016-03-11 00:43:24 +01:00
|
|
|
// Port defaults to 8266
|
|
|
|
// ArduinoOTA.setPort(8266);
|
|
|
|
|
|
|
|
// Hostname defaults to esp8266-[ChipID]
|
|
|
|
// ArduinoOTA.setHostname("myesp8266");
|
|
|
|
|
|
|
|
// No authentication by default
|
|
|
|
// ArduinoOTA.setPassword((const char *)"123");
|
|
|
|
|
|
|
|
ArduinoOTA.onStart([]() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Start");
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onEnd([]() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("\nEnd");
|
2016-12-14 22:57:01 +01:00
|
|
|
//Force BOOTMODE_SETUP in case eeprom layout have changed
|
|
|
|
EepromSaveBootMode(BOOTMODE_SETUP);
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
2016-06-04 18:45:11 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Progress: %u%%\n", (progress / (total / 100)));
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTF("Error[%u]: ", error);
|
2016-04-01 01:05:29 +02:00
|
|
|
if (error == OTA_AUTH_ERROR) {
|
|
|
|
SKETCH_DEBUG_PRINTLN("Auth Failed");
|
|
|
|
}
|
|
|
|
else if (error == OTA_BEGIN_ERROR) {
|
|
|
|
SKETCH_DEBUG_PRINTLN("Begin Failed");
|
|
|
|
}
|
|
|
|
else if (error == OTA_CONNECT_ERROR) {
|
|
|
|
SKETCH_DEBUG_PRINTLN("Connect Failed");
|
|
|
|
}
|
|
|
|
else if (error == OTA_RECEIVE_ERROR) {
|
|
|
|
SKETCH_DEBUG_PRINTLN("Receive Failed");
|
|
|
|
}
|
|
|
|
else if (error == OTA_END_ERROR) {
|
|
|
|
SKETCH_DEBUG_PRINTLN("End Failed");
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Ready");
|
2016-11-20 00:23:16 +01:00
|
|
|
SKETCH_DEBUG_PRINTF("IP address: ");
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN(WiFi.localIP());
|
2016-09-28 00:29:54 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Free Space: %d\n", ESP.getFreeSketchSpace());
|
2016-10-28 18:03:36 +02:00
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
2017-01-28 14:29:28 +01:00
|
|
|
#ifdef CONFIG_SETUP_BUTTON
|
|
|
|
new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, NULL, onLongButtonPressed);
|
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
pinMode(3, OUTPUT);
|
|
|
|
|
|
|
|
delay(1000);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_INIT(115200);
|
|
|
|
SKETCH_DEBUG_PRINTLN();
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
// Get GPIO 3 Status
|
2021-02-18 00:25:45 +01:00
|
|
|
#if CONFIG_SERIAL_SHOULD_SWAP
|
|
|
|
SKETCH_DEBUG_PRINTLN("SWAP UART");
|
2016-03-11 00:43:24 +01:00
|
|
|
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
2021-02-18 00:25:45 +01:00
|
|
|
#endif
|
2016-06-18 01:49:29 +02:00
|
|
|
pinMode(CONFIG_SETUP_GPIO, INPUT_PULLUP);
|
|
|
|
int txStatus = digitalRead(CONFIG_SETUP_GPIO);
|
2021-02-18 00:25:45 +01:00
|
|
|
//#if !defined(CONFIG_ENABLE_EXTRA_GPIO) && CONFIG_SERIAL_SHOULD_SWAP
|
|
|
|
// Serial.swap(); // Switch back on GPIO 1 & 3
|
|
|
|
// SKETCH_DEBUG_PRINTLN("SWAP UART");
|
|
|
|
//#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-03-30 00:49:57 +02:00
|
|
|
EEPROM.begin(CONFIG_EEPROM_SIZE);
|
2016-11-20 00:06:31 +01:00
|
|
|
EepromReadConfig(conf);
|
2016-03-25 00:03:25 +01:00
|
|
|
|
2016-11-20 00:06:31 +01:00
|
|
|
mode = conf.bootMode;
|
2016-03-11 00:43:24 +01:00
|
|
|
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Configuration Found !:");
|
2016-11-20 00:06:31 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN(conf.bootMode);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.ssid);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.host);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.mqttServer);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.mqttUser);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.mqttPasswd);
|
|
|
|
SKETCH_DEBUG_PRINTLN(conf.mqttPort);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN();
|
2016-03-11 00:43:24 +01:00
|
|
|
} else {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("No configuration saved");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2019-05-14 23:44:46 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Force Setup Mode ? : %s\n", txStatus ? "No" : "Yes");
|
|
|
|
if (txStatus == 0) {
|
2016-04-10 02:01:25 +02:00
|
|
|
mode = BOOTMODE_SETUP;
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-11-20 00:06:31 +01:00
|
|
|
WifiSetup(conf);
|
2016-04-07 12:24:30 +02:00
|
|
|
if (mode == BOOTMODE_NORMAL) {
|
2016-11-20 00:06:31 +01:00
|
|
|
MqttSetup(conf.mqttServer, conf.mqttUser, conf.mqttPasswd, conf.mqttPort, conf.host);
|
2016-11-16 16:59:17 +01:00
|
|
|
MqttPublishIP(WiFi.localIP().toString());
|
2016-04-07 12:24:30 +02:00
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
if (mode == BOOTMODE_OTA) {
|
2016-03-24 14:54:55 +01:00
|
|
|
OTASetup();
|
2016-03-11 00:43:24 +01:00
|
|
|
} else {
|
2019-05-14 23:44:46 +02:00
|
|
|
if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("BMP180 init success");
|
2016-04-07 15:26:01 +02:00
|
|
|
}
|
2019-05-14 23:44:46 +02:00
|
|
|
if (!DHTSetup(CONFIG_DHT_PIN)) {
|
2016-04-07 15:26:01 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("DHT init success");
|
|
|
|
}
|
2019-05-14 23:44:46 +02:00
|
|
|
if (!DrySetup(CONFIG_DRY_POWER_PIN)) {
|
2016-06-02 01:31:57 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("DRY init success");
|
|
|
|
}
|
2020-02-27 00:07:30 +01:00
|
|
|
if(!BME680Setup()){
|
|
|
|
SKETCH_DEBUG_PRINTLN("BME680 init success");
|
|
|
|
}
|
2020-07-28 19:37:19 +02:00
|
|
|
if(!BME680BSECSetup()){
|
|
|
|
SKETCH_DEBUG_PRINTLN("BME680 with BSEC init success");
|
|
|
|
}
|
2021-02-18 00:25:45 +01:00
|
|
|
if(!TeleinfoSetup()){
|
|
|
|
SKETCH_DEBUG_PRINTLN("Teleinfo init success");
|
|
|
|
}
|
2016-03-24 14:54:55 +01:00
|
|
|
WebSetupServer(mode);
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-04-06 16:03:14 +02:00
|
|
|
#ifdef CONFIG_ENABLE_POWER_SAVE
|
2016-04-01 01:06:13 +02:00
|
|
|
wifi_set_sleep_type(LIGHT_SLEEP_T);
|
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2016-06-01 01:23:23 +02:00
|
|
|
uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS;
|
2016-03-11 00:43:24 +01:00
|
|
|
void loop() {
|
|
|
|
if (mode == BOOTMODE_OTA) {
|
|
|
|
ArduinoOTA.handle();
|
|
|
|
} else {
|
|
|
|
server.handleClient();
|
2019-05-14 23:44:46 +02:00
|
|
|
if (mode == BOOTMODE_NORMAL) {
|
2016-11-15 22:49:04 +01:00
|
|
|
MqttCheckSubscription();
|
2016-12-14 00:08:47 +01:00
|
|
|
MqttCheckIRQ();
|
|
|
|
}
|
2016-03-30 00:44:37 +02:00
|
|
|
delay(CONFIG_WEB_DELAY_MS);
|
2016-03-21 00:56:27 +01:00
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
nbCycle++;
|
2016-03-30 00:44:37 +02:00
|
|
|
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
|
2019-05-14 23:44:46 +02:00
|
|
|
std::vector<struct mqttInfo> batchInfo;
|
2016-06-02 01:28:16 +02:00
|
|
|
if (!BMP180GetTempAndPressure(temp, pressure)) {
|
2019-05-14 23:44:46 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure);
|
|
|
|
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0});
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-06-02 01:28:16 +02:00
|
|
|
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
|
2019-05-14 23:44:46 +02:00
|
|
|
SKETCH_DEBUG_PRINTF("Current %f°C %f%% Humidity\n", dhtTemp, dhtHumidity);
|
|
|
|
batchInfo.push_back({dhtTemp, TEMPERATURE_DHT_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({dhtHumidity, HUMIDITY_DHT_FEED_FORMAT, 0, 0});
|
2016-04-07 15:26:01 +02:00
|
|
|
}
|
2019-05-14 23:44:46 +02:00
|
|
|
if (!DryGetMeasure(dryness)) {
|
|
|
|
SKETCH_DEBUG_PRINTF("Current dryness %d %%\n", (dryness * 100) / 1024);
|
2020-07-28 19:37:19 +02:00
|
|
|
batchInfo.push_back({(float)dryness, DRY_FEED_FORMAT, 0, 0});
|
2016-06-02 01:31:57 +02:00
|
|
|
}
|
2020-02-27 00:07:30 +01:00
|
|
|
if (!BME680GetMeasure(bme680T, bme680P, bme680H, bme680G, bme680A)) {
|
|
|
|
SKETCH_DEBUG_PRINT("Current T°C: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680T);
|
|
|
|
SKETCH_DEBUG_PRINT(" Pressure hPa: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680P);
|
|
|
|
SKETCH_DEBUG_PRINT(" Humidity %: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(bme680H);
|
|
|
|
SKETCH_DEBUG_PRINT(" Gaz kOhms: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(bme680G);
|
|
|
|
SKETCH_DEBUG_PRINT(" Altitude M: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(bme680A);
|
|
|
|
batchInfo.push_back({bme680T, BME680_TEMPERATURE_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680P, BME680_PRESSURE_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680H, BME680_HUMIDITY_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680G, BME680_GAZ_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680A, BME680_ALT_FEED_FORMAT, 0, 0});
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:37:19 +02:00
|
|
|
if (!BME680BSECGetMeasure(bme680BSECT, bme680BSECP, bme680BSECH, bme680BSECIaq,
|
|
|
|
bme680BSECIaqAcc)) {
|
|
|
|
SKETCH_DEBUG_PRINT("Current T°C: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680BSECT);
|
|
|
|
SKETCH_DEBUG_PRINT(" Pressure hPa: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680BSECP);
|
|
|
|
SKETCH_DEBUG_PRINT(" Humidity %: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680BSECH);
|
|
|
|
SKETCH_DEBUG_PRINT(" Indoor Air Quality: ");
|
|
|
|
SKETCH_DEBUG_PRINT(bme680BSECIaq);
|
|
|
|
SKETCH_DEBUG_PRINT(" IAQ Accuracy: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(bme680BSECIaqAcc);
|
|
|
|
batchInfo.push_back({bme680BSECT, BME680_TEMPERATURE_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680BSECP, BME680_PRESSURE_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680BSECH, BME680_HUMIDITY_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680BSECIaq, BME680_IAQ_FEED_FORMAT, 0, 0});
|
|
|
|
batchInfo.push_back({bme680BSECIaqAcc, BME680_IAQ_ACC_FEED_FORMAT, 0, 0});
|
|
|
|
}
|
|
|
|
|
2019-05-14 23:44:46 +02:00
|
|
|
if (mode == BOOTMODE_NORMAL)
|
|
|
|
MqttBatchPublish(batchInfo, conf.mqttUser, conf.host);
|
2016-03-11 00:43:24 +01:00
|
|
|
nbCycle = 0;
|
|
|
|
}
|
2021-02-18 00:25:45 +01:00
|
|
|
TeleinfoProcess();
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
}
|