Compare commits

..

No commits in common. "master" and "bssid_channel" have entirely different histories.

22 changed files with 208 additions and 909 deletions

View File

@ -4,10 +4,7 @@ before_script:
- arduino --install-boards esp8266:esp8266 || true - arduino --install-boards esp8266:esp8266 || true
#replaced by a fork https://github.com/shmuelzon/Adafruit_MQTT_Library.git that have mqtt retains #replaced by a fork https://github.com/shmuelzon/Adafruit_MQTT_Library.git that have mqtt retains
# - arduino --install-library "Adafruit MQTT Library" || true # - arduino --install-library "Adafruit MQTT Library" || true
- arduino --install-library "Adafruit BME680 Library" || true
- arduino --install-library "Adafruit Unified Sensor" || true
- arduino --install-library "DHT sensor library" || true - arduino --install-library "DHT sensor library" || true
- arduino --install-library "BSEC Software Library" || true
- arduino --board esp8266:esp8266:generic --save-prefs - arduino --board esp8266:esp8266:generic --save-prefs
- arduino --pref "compiler.warning_level=all" --save-prefs - arduino --pref "compiler.warning_level=all" --save-prefs

View File

@ -31,17 +31,5 @@ Those measure can be shared by MQTT(s). MQTT details (server, username, passwd,
To interface with BMP180, the following library should be installed into Arduino environment: https://github.com/mmaret/BMP180_Breakout_Arduino_Library/archive/master.zip To interface with BMP180, the following library should be installed into Arduino environment: https://github.com/mmaret/BMP180_Breakout_Arduino_Library/archive/master.zip
To use mqtt, a fork of the Adafruit Mqtt library should be installed (gitlab@gitlab.mathux.org:Mathieu/adaAdafruit_MQTT_Libraryfruit_mqtt_library.git To use mqtt, a fork of the Adafruit Mqtt library should be installed (https://github.com/shmuelzon/Adafruit_MQTT_Library/tree/support_retain).
-b retain_support).
This have been tested against a mosquitto server This have been tested against a mosquitto server
Quick cmdline to install deps:
cd ~/Arduino/libraries/
git clone gitlab@gitlab.mathux.org:Mathieu/ESP8266_HIB.git
git clone git@github.com:mmaret/BMP180_Breakout_Arduino_Library.git
git clone gitlab@gitlab.mathux.org:Mathieu/adaAdafruit_MQTT_Libraryfruit_mqtt_library.git -b support_retain
arduino --install-library "Adafruit BME680 Library"
arduino --install-library "Adafruit Unified Sensor"
arduino --install-library "DHT sensor library"
arduino --install-library "BSEC Software Library" # Then you have to follow the instruction in the README.md in the BSEC install folder

View File

@ -1,26 +0,0 @@
#pragma once
#ifdef CONFIG_ENABLE_BME680
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"
#include "debug_sketch.h"
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
//Use Default i2c pin GPIO4(D2): SDA, GPIO5(D1):SCL
#define SEALEVELPRESSURE_HPA (1013.25)
int BME680GetMeasure(float &t, float &p, float &h, float &g, float &a);
int BME680Setup();
bool BME680IsConnected();
#else //CONFIG_ENABLE_BME680
int BME680GetMeasure(float &, float &, float&, float &, float &){return -1;};
int BME680Setup(){SKETCH_DEBUG_PRINTLN("BME680 is disabled at build time"); return -1;};
bool BME680IsConnected(){return false;};
#endif

View File

@ -1,40 +0,0 @@
#ifdef CONFIG_ENABLE_BME680
#include "BME680.h"
Adafruit_BME680 bme; // I2C
//Adafruit_BME680 bme(BME_CS); // hardware SPI
//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);
int bme680Connected = 0;
int BME680Setup() {
bme680Connected = bme.begin();
if (!bme680Connected){
SKETCH_DEBUG_PRINTLN("Cannot connect to BME680");
return -1;
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
return 0;
}
bool BME680IsConnected() {
return bme680Connected != 0;
}
int BME680GetMeasure(float &t, float &p, float &h, float &g, float &a){
if(!bme.performReading()){
SKETCH_DEBUG_PRINTLN("Cannot read BME680 measure");
return -1;
}
t = bme.temperature;
p = bme.pressure / 100.0;
h = bme.humidity;
g = bme.gas_resistance / 1000.0;
a = bme.readAltitude(SEALEVELPRESSURE_HPA);
return 0;
}
#endif

View File

@ -1,16 +0,0 @@
#pragma once
#ifdef CONFIG_BME680_BSEC_ENABLE
int BME680BSECGetMeasure(float &t, float &p, float &h, float &iaq, float &iaqAcc);
int BME680BSECSetup();
bool BME680BSECIsConnected();
#else // CONFIG_ENABLE_BME680_BSEC
#define BSEC_MAX_STATE_BLOB_SIZE 0
int BME680BSECGetMeasure(float &, float &, float &, float &, float &) {
return -1;
};
int BME680BSECSetup() {
SKETCH_DEBUG_PRINTLN("BME680 BSEC is disabled at build time");
return -1;
};
bool BME680BSECIsConnected() { return false; };
#endif

View File

@ -1,153 +0,0 @@
#ifdef CONFIG_BME680_BSEC_ENABLE
#include "BME680_BSEC.h"
#include "bsec.h"
/* Configure the BSEC library with information about the sensor
18v/33v = Voltage at Vdd. 1.8V or 3.3V
3s/300s = BSEC operating mode, BSEC_SAMPLE_RATE_LP or BSEC_SAMPLE_RATE_ULP
4d/28d = Operating age of the sensor in days
generic_18v_3s_4d
generic_18v_3s_28d
generic_18v_300s_4d
generic_18v_300s_28d
generic_33v_3s_4d
generic_33v_3s_28d
generic_33v_300s_4d
generic_33v_300s_28d
*/
const uint8_t bsec_config_iaq[] = {
#if CONFIG_SAMPLING_PERIOD_MS == 3000
#include "config/generic_33v_3s_4d/bsec_iaq.txt"
#elif CONFIG_SAMPLING_PERIOD_MS == 300000
#include "config/generic_33v_300s_4d/bsec_iaq.txt"
#else
#error "Unsupport CONFIG_SAMPLING_PERIOD_MS (3000 and 300000 are supported)"
#endif
};
#define STATE_SAVE_PERIOD \
UINT32_C(360 * 60 * 1000) // 360 minutes - 4 times a day
uint16_t stateUpdateCounter = 0;
Bsec iaqSensor;
uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE] = {0};
// Helper function definitions
int checkIaqSensorStatus(void) {
if (iaqSensor.status != BSEC_OK) {
if (iaqSensor.status < BSEC_OK) {
SKETCH_DEBUG_PRINTLN("BSEC error code : " + String(iaqSensor.status));
return -2;
} else {
SKETCH_DEBUG_PRINTLN("BSEC warning code : " + String(iaqSensor.status));
return -1;
}
}
if (iaqSensor.bme680Status != BME680_OK) {
if (iaqSensor.bme680Status < BME680_OK) {
SKETCH_DEBUG_PRINTLN("BME680 error code : " + String(iaqSensor.bme680Status));
return -2;
} else {
SKETCH_DEBUG_PRINTLN("BME680 warning code : " + String(iaqSensor.bme680Status));
return -1;
}
}
iaqSensor.status = BSEC_OK;
return 0;
}
void updateState(void)
{
bool update = false;
/* Set a trigger to save the state. Here, the state is saved every STATE_SAVE_PERIOD with the
* first state being saved once the algorithm achieves full calibration, i.e. iaqAccuracy = 3
*/
if (stateUpdateCounter == 0) {
if (iaqSensor.iaqAccuracy >= 3) {
update = true;
stateUpdateCounter++;
}
} else {
/* Update every STATE_SAVE_PERIOD milliseconds */
if ((stateUpdateCounter * STATE_SAVE_PERIOD) < millis()) {
update = true;
stateUpdateCounter++;
}
}
if (update) {
iaqSensor.getState(bsecState);
checkIaqSensorStatus();
SKETCH_DEBUG_PRINTLN("Writing state to EEPROM");
EepromSaveBME680State(bsecState);
}
}
int BME680BSECGetMeasure(float &t, float &p, float &h, float &iaq, float &iaqAcc)
{
if (iaqSensor.run()) { // If new data is available
t = iaqSensor.temperature;
p = iaqSensor.pressure/100;
h = iaqSensor.humidity;
iaq = iaqSensor.iaq;
iaqAcc = iaqSensor.iaqAccuracy;
updateState();
} else {
return -1;
}
return 0;
}
int BME680BSECSetup()
{
Wire.begin();
iaqSensor.begin(CONFIG_BME680_BSEC_I2C_ADDR, Wire);
SKETCH_DEBUG_PRINTLN("\nBSEC library version " + String(iaqSensor.version.major) + "." +
String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) +
"." + String(iaqSensor.version.minor_bugfix));
if (checkIaqSensorStatus())
return -1;
iaqSensor.setConfig(bsec_config_iaq);
if (checkIaqSensorStatus())
return -1;
if (!EepromLoadBME680State(bsecState))
iaqSensor.setState(bsecState);
if (checkIaqSensorStatus())
return -1;
bsec_virtual_sensor_t sensorList[7] = {
BSEC_OUTPUT_RAW_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_RAW_HUMIDITY,
BSEC_OUTPUT_RAW_GAS,
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
};
iaqSensor.updateSubscription(sensorList, 7, BSEC_SAMPLE_RATE_LP);
if (checkIaqSensorStatus())
return -1;
return 0;
}
bool BME680BSECIsConnected()
{
if (checkIaqSensorStatus())
return false;
return true;
}
#endif

View File

@ -8,9 +8,11 @@ int BMP180Setup(int sda, int scl) {
bmp180Connected = bmp180.begin(sda, scl); bmp180Connected = bmp180.begin(sda, scl);
if (!bmp180Connected){ if (!bmp180Connected){
SKETCH_DEBUG_PRINTLN("Cannot connect to BMP180"); SKETCH_DEBUG_PRINTLN("Cannot connect to BMP180");
return -1; goto err;
} }
return 0; return 0;
err:
return -1;
} }
bool BMP180IsConnected() { bool BMP180IsConnected() {

View File

@ -1,15 +0,0 @@
#pragma once
#ifdef CONFIG_ENABLE_BMP280
#include "debug_sketch.h"
int BMP280GetTemperature(double &t);
int BMP280GetTempAndPressure(double &t, double &p);
int BMP280Setup();
bool BMP280IsConnected();
#else //CONFIG_ENABLE_BMP80
int BMP280GetTemperature(double &){return -1;};
int BMP280GetTempAndPressure(double &, double &){return -1;};
int BMP280Setup(){SKETCH_DEBUG_PRINTLN("BMP280 is disabled at build time"); return -1;};
bool BMP280IsConnected(){return false;};
#endif

View File

@ -1,44 +0,0 @@
#ifdef CONFIG_ENABLE_BMP280
#include "BMP280.h"
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
int bmpConnected = 0;
int BMP280Setup()
{
bmpConnected = bmp.begin(BMP280_ADDRESS_ALT);
if (!bmpConnected) {
SKETCH_DEBUG_PRINTLN("Cannot connect to BMP280");
return -1;
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
return 0;
}
bool BMP280IsConnected()
{
return bmpConnected != 0;
}
int BMP280GetTemperature(double &t)
{
if (!BMP280IsConnected())
return -1;
t = bmp.readTemperature();
return 0;
}
int BMP280GetTempAndPressure(double &t, double &p)
{
if (!BMP280IsConnected())
return -1;
t = bmp.readTemperature();
p = bmp.readPressure()/100;
return 0;
}
#endif

View File

@ -15,14 +15,12 @@ typedef struct productConfig_t {
uint32_t mask; uint32_t mask;
uint32_t dns; uint32_t dns;
uint32_t dns2; uint32_t dns2;
uint8_t channel; int32_t channel;
char *bssid; char *bssid;
uint32_t samplingPeriod;
} productConfig; } productConfig;
int EepromSaveConfig(productConfig &config); int EepromSaveConfig(productConfig &config);
int EepromSaveBootMode(uint8_t bootMode); int EepromSaveBootMode(uint8_t bootMode);
void EepromReadConfig(productConfig &config); void EepromReadConfig(productConfig &config);
int EepromSaveBME680State(uint8_t *bsecState);
int EepromLoadBME680State(uint8_t *bsecState);

View File

@ -6,12 +6,6 @@
*/ */
#include "EEPROM.h" #include "EEPROM.h"
#define BME680_BSEC_EEPROM_ORIG (CONFIG_EEPROM_SIZE)
#if (CONFIG_EEPROM_SIZE + BSEC_MAX_STATE_BLOB_SIZE) >= SPI_FLASH_SEC_SIZE
#error "CONFIG_EEPROM_SIZE too big"
#endif
char eeprom[CONFIG_EEPROM_SIZE]; char eeprom[CONFIG_EEPROM_SIZE];
int EepromSaveConfig(productConfig &config) { int EepromSaveConfig(productConfig &config) {
@ -24,7 +18,7 @@ int EepromSaveConfig(productConfig &config) {
+ String(config.ip_mode) + ";" + String(config.ip_mode) + ";"
+ config.ip + ";" + config.gw + ";" + config.mask + ";" + config.ip + ";" + config.gw + ";" + config.mask + ";"
+ config.dns + ";" + config.dns2 + ";" + config.channel + ";" + config.dns + ";" + config.dns2 + ";" + config.channel + ";"
+ config.bssid + ";" + config.samplingPeriod + ";"; + config.bssid + ";";
if (eeprom.length() > CONFIG_EEPROM_SIZE ) if (eeprom.length() > CONFIG_EEPROM_SIZE )
return -EMSGSIZE; return -EMSGSIZE;
@ -60,6 +54,7 @@ void readConfElement(char** element, int &i) {
SKETCH_DEBUG_PRINTLN("Looks like there is a configuration issue (too long)"); SKETCH_DEBUG_PRINTLN("Looks like there is a configuration issue (too long)");
**element = '\0'; **element = '\0';
} }
} }
void EepromReadConfig(productConfig &config) { void EepromReadConfig(productConfig &config) {
@ -92,58 +87,17 @@ void EepromReadConfig(productConfig &config) {
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.ip_mode = atoi(tmpString); config.ip_mode = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.ip = atoll(tmpString); config.ip = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.gw = atoll(tmpString); config.gw = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.mask = atoll(tmpString); config.mask = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.dns = atoll(tmpString); config.dns = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.dns2 = atoll(tmpString); config.dns2 = atoi(tmpString);
readConfElement(&tmpString, i); readConfElement(&tmpString, i);
config.channel = atoi(tmpString); config.channel = atoi(tmpString);
readConfElement(&config.bssid, i); readConfElement(&config.bssid, i);
readConfElement(&tmpString, i);
config.samplingPeriod = atoi(tmpString);
} }
int EepromSaveBME680State(uint8_t *bsecState)
{
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE; i++) {
EEPROM.write(i + 1, bsecState[i]);
}
EEPROM.write(BME680_BSEC_EEPROM_ORIG, BSEC_MAX_STATE_BLOB_SIZE);
EEPROM.commit();
return 0;
}
int EepromLoadBME680State(uint8_t *bsecState)
{
if (EEPROM.read(BME680_BSEC_EEPROM_ORIG) == BSEC_MAX_STATE_BLOB_SIZE) {
// Existing state in EEPROM
SKETCH_DEBUG_PRINTLN("Reading BME680 state from EEPROM");
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE; i++) {
bsecState[i] = EEPROM.read(i + 1);
}
return 0;
} else {
// Erase the EEPROM with zeroes
SKETCH_DEBUG_PRINTLN("Erasing EEPROM for BME680 state");
for (uint16_t i = BME680_BSEC_EEPROM_ORIG;
i < BME680_BSEC_EEPROM_ORIG + BSEC_MAX_STATE_BLOB_SIZE + 1; i++)
EEPROM.write(i, 0);
EEPROM.commit();
return -1;
}
}

View File

@ -1,46 +1,15 @@
#pragma once #pragma once
//FEED have the following formats /feeds/USER/DEVICE_NAME/....
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/temperature"
#define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity"
#define DRY_FEED_FORMAT "/feeds/%s/%s/dry"
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define PWM_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
#define BME680_TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/bme680/temperature"
#define BME680_PRESSURE_FEED_FORMAT "/feeds/%s/%s/bme680/pressure"
#define BME680_HUMIDITY_FEED_FORMAT "/feeds/%s/%s/bme680/humidity"
#define BME680_GAZ_FEED_FORMAT "/feeds/%s/%s/bme680/gaz"
#define BME680_ALT_FEED_FORMAT "/feeds/%s/%s/bme680/alt"
#define BME680_IAQ_FEED_FORMAT "/feeds/%s/%s/bme680/iaq"
#define BME680_IAQ_ACC_FEED_FORMAT "/feeds/%s/%s/bme680/iaqAcc"
#define TELEINFO_IINST_FEED_FORMAT "/feeds/%s/%s/teleinfo/iinst"
#define TELEINFO_PAPP_FEED_FORMAT "/feeds/%s/%s/teleinfo/papp"
#define TELEINFO_BASE_FEED_FORMAT "/feeds/%s/%s/teleinfo/base"
#define SCD4X_TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/scd4x/temperature"
#define SCD4X_HUMIDITY_FEED_FORMAT "/feeds/%s/%s/scd4x/humidity"
#define SCD4X_CO2_FEED_FORMAT "/feeds/%s/%s/scd4x/co2"
#ifndef CONFIG_DISABLE_MQTT #ifndef CONFIG_DISABLE_MQTT
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
typedef struct {uint8_t updated; int value;} gpioInfo; typedef struct {uint8_t updated; int value;} gpioInfo;
struct mqttInfo {
float value;
const char *topic;
uint8_t retain;
uint8_t qos;
};
int MqttBatchPublish(std::vector<struct mqttInfo> tab, ...);
Adafruit_MQTT_Publish *MqttCreatePublisher(uint8_t qos, uint8_t retain, const char *fmt, ...); Adafruit_MQTT_Publish *MqttCreatePublisher(uint8_t qos, uint8_t retain, const char *fmt, ...);
int MqttConnect(); int MqttConnect();
int MqttIsConnected(); int MqttIsConnected();
int MqttSetup(const char *server, const char *user, const char *passwd, int port, const char * hostname); int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value); template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value);
int MqttPublishBMP180(double temp, double pressure);
int MqttPublishDHT(float temp, float humidity);
int MqttPublishDry(int dry);
int MqttPublishIp(const String &ip); int MqttPublishIp(const String &ip);
void MqttCheckSubscription(); void MqttCheckSubscription();
void MqttCheckIRQ(); void MqttCheckIRQ();
@ -49,11 +18,13 @@ void MqttChangePWMValue(int gpio, int value);
void MqttNofityIRQ(uint8_t gpio, int value); void MqttNofityIRQ(uint8_t gpio, int value);
void MqttNofity(int gpio, int value); void MqttNofity(int gpio, int value);
#else #else
int MqttBatchPublish(std::vector<struct mqttInfo> tab, ...){return 0;}
int MqttConnect(){return 0;} int MqttConnect(){return 0;}
int MqttIsConnected(){return 0;} int MqttIsConnected(){return 0;}
int MqttSetup(const char *server, const char *user, const char *passwd, int port, const char * hostname){return 0;} int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname){return 0;}
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){return 0;} template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){return 0;}
int MqttPublishBMP180(double temp, double pressure){return 0;}
int MqttPublishDHT(float temp, float humidity){return 0;}
int MqttPublishDry(int dry){return 0;}
int MqttPublishIP(const String &ip){return 0;} int MqttPublishIP(const String &ip){return 0;}
void MqttCheckSubscription(){} void MqttCheckSubscription(){}
void MqttCheckIRQ(){} void MqttCheckIRQ(){}

View File

@ -6,6 +6,11 @@
#define MAX_PIN 15 #define MAX_PIN 15
#define MAX_GPIO_OBSERVED (MAXSUBSCRIPTIONS*2) #define MAX_GPIO_OBSERVED (MAXSUBSCRIPTIONS*2)
Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Client *mqtt;
Adafruit_MQTT_Publish *mqtt_temp;
Adafruit_MQTT_Publish *mqtt_pressure;
Adafruit_MQTT_Publish *mqtt_dht_temp;
Adafruit_MQTT_Publish *mqtt_dht_humidity;
Adafruit_MQTT_Publish *mqtt_dry;
Adafruit_MQTT_Publish *mqtt_ip; Adafruit_MQTT_Publish *mqtt_ip;
Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttGpio[MAXSUBSCRIPTIONS] = {};
Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {}; Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {};
@ -14,56 +19,76 @@ gpioInfo mqttIRQ[MAX_PIN + 1] = {};
#define FEED_MAX_SIZE 96 #define FEED_MAX_SIZE 96
//FEED have the following formats /feeds/USER/DEVICE_NAME/....
#define TEMPERATURE_FEED_FORMAT "/feeds/%s/%s/temperature"
#define PRESSURE_FEED_FORMAT "/feeds/%s/%s/pressure"
#define TEMPERATURE_DHT_FEED_FORMAT "/feeds/%s/%s/dht/temperature"
#define HUMIDITY_DHT_FEED_FORMAT "/feeds/%s/%s/dht/humidity"
#define DRY_FEED_FORMAT "/feeds/%s/%s/dry"
#define GPIO_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define GPIO_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define PWM_FEED_FORMAT "/feeds/%s/%s/gpio/%d"
#define PWM_SET_FEED_FORMAT "/feeds/%s/%s/gpio/%d/set"
#define IP_FEED_FORMAT "/feeds/%s/%s/configuration/ip/addr"
char *mqttId;
bool isMqttConfigured = false; bool isMqttConfigured = false;
bool useMqtts = false; bool useMqtts = false;
int MqttSetup(const char *server, const char *user, const char *passwd, int port, const char *hostname) { int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname) {
mqttId = hostname;
useMqtts = (port == 8883); useMqtts = (port == 8883);
isMqttConfigured = server[0] != '\0'; isMqttConfigured = server[0] != '\0';
if (!isMqttConfigured) if(!isMqttConfigured)
return 0; return 0;
#ifndef CONFIG_DISABLE_SSL #ifndef CONFIG_DISABLE_SSL
if (useMqtts) if(useMqtts)
mqtt = new Adafruit_MQTT_Client(new WiFiClientSecure(), server, port, user, passwd); mqtt = new Adafruit_MQTT_Client(new WiFiClientSecure(), server, port, user, passwd);
else else
#endif #endif
mqtt = new Adafruit_MQTT_Client(new WiFiClient(), server, port, user, passwd); mqtt = new Adafruit_MQTT_Client(new WiFiClient(), server, port, user, passwd);
mqtt_ip = MqttCreatePublisher(0, 1, IP_FEED_FORMAT, user, hostname); mqtt_dht_temp = MqttCreatePublisher(0, 0, TEMPERATURE_DHT_FEED_FORMAT, user, mqttId);
mqtt_dht_humidity = MqttCreatePublisher(0, 0, HUMIDITY_DHT_FEED_FORMAT, user, mqttId);
mqtt_temp = MqttCreatePublisher(0, 0, TEMPERATURE_FEED_FORMAT, user, mqttId);
mqtt_pressure = MqttCreatePublisher(0, 0, PRESSURE_FEED_FORMAT, user, mqttId);
mqtt_dry = MqttCreatePublisher(0, 0, DRY_FEED_FORMAT, user, mqttId);
mqtt_ip = MqttCreatePublisher(0, 1, IP_FEED_FORMAT, user, mqttId);
if (NB_ELEMENTS(gpioControlled) + NB_ELEMENTS(pwmControlled) > MAXSUBSCRIPTIONS) { if (NB_ELEMENTS(gpioControlled) + NB_ELEMENTS(pwmControlled) > MAXSUBSCRIPTIONS){
SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d", SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d",
NB_ELEMENTS(gpioControlled), NB_ELEMENTS(pwmControlled), MAXSUBSCRIPTIONS); NB_ELEMENTS(gpioControlled), NB_ELEMENTS(pwmControlled), MAXSUBSCRIPTIONS);
return -1; return -1;
} }
if (NB_ELEMENTS(gpioObserved) > MAX_GPIO_OBSERVED) { if (NB_ELEMENTS(gpioObserved) > MAX_GPIO_OBSERVED){
SKETCH_DEBUG_PRINTF("Too much gpio observed\n Nb gpio %d Nb is %d", SKETCH_DEBUG_PRINTF("Too much gpio observed\n Nb gpio %d Nb is %d",
NB_ELEMENTS(gpioObserved), MAX_GPIO_OBSERVED); NB_ELEMENTS(gpioObserved), MAX_GPIO_OBSERVED);
return -1; return -1;
} }
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled); i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled); i++) {
mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, hostname, gpioControlled[i])); mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioControlled[i]));
mqttGpio[i] = MqttCreatePublisher(0, 0, GPIO_FEED_FORMAT, user, hostname, gpioControlled[i]); mqttGpio[i] = MqttCreatePublisher(0, 0, GPIO_FEED_FORMAT, user, mqttId, gpioControlled[i]);
} }
for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) && i < MAX_GPIO_OBSERVED ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) && i < MAX_GPIO_OBSERVED ; i++) {
mqttGpioObserved[i] = MqttCreatePublisher(0, 0, GPIO_FEED_FORMAT, user, hostname, gpioObserved[i]); mqttGpioObserved[i] = MqttCreatePublisher(0, 0, GPIO_FEED_FORMAT, user, mqttId, gpioObserved[i]);
new HIB(gpioObserved[i], HIGH, MqttNofityIRQ , MqttNofityIRQ, NULL ); new HIB(gpioObserved[i], HIGH, MqttNofityIRQ , MqttNofityIRQ, NULL );
} }
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled); i++) { for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled); i++) {
mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, hostname, pwmControlled[i])); mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmControlled[i]));
mqttPwm[i] = MqttCreatePublisher(0, 0, PWM_FEED_FORMAT, user, hostname, pwmControlled[i]); mqttPwm[i] = MqttCreatePublisher(0, 0, PWM_FEED_FORMAT, user, mqttId, pwmControlled[i]);
} }
return 0; return 0;
} }
Adafruit_MQTT_Publish *MqttCreatePublisher( uint8_t qos, uint8_t retain, const char *fmt, ...) { Adafruit_MQTT_Publish *MqttCreatePublisher( uint8_t qos, uint8_t retain, const char *fmt, ...){
char buf[FEED_MAX_SIZE]; char buf[FEED_MAX_SIZE];
va_list args; va_list args;
va_start (args, fmt); va_start (args, fmt);
@ -72,27 +97,7 @@ Adafruit_MQTT_Publish *MqttCreatePublisher( uint8_t qos, uint8_t retain, const c
return new Adafruit_MQTT_Publish(mqtt, strdup(buf), qos, retain); return new Adafruit_MQTT_Publish(mqtt, strdup(buf), qos, retain);
} }
Adafruit_MQTT_Subscribe *MqttCreateSubscribe(const char *fmt, ...){
int MqttBatchPublish(std::vector<struct mqttInfo> tab, ...) {
if (MqttConnect()) {
SKETCH_DEBUG_PRINTLN("cannot connect to mqtt");
return -1;
}
for (auto info : tab) {
char buf[FEED_MAX_SIZE];
va_list args;
va_start (args, tab);
vsnprintf(buf, sizeof(buf), (const char *)info.topic, args);
va_end(args);
// SKETCH_DEBUG_PRINTF("publishing %f for %s\n", info.value, buf);
Adafruit_MQTT_Publish client(mqtt, buf, info.qos, info.retain);
if (!client.publish(info.value))
SKETCH_DEBUG_PRINTLN("Fail :(");
}
return 0;
}
Adafruit_MQTT_Subscribe *MqttCreateSubscribe(const char *fmt, ...) {
char buf[FEED_MAX_SIZE]; char buf[FEED_MAX_SIZE];
va_list args; va_list args;
va_start (args, fmt); va_start (args, fmt);
@ -132,7 +137,7 @@ int MqttConnect() {
return 0; return 0;
} }
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value) { template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){
if (MqttConnect() == 0) { if (MqttConnect() == 0) {
publisher->publish(value); publisher->publish(value);
return 0; return 0;
@ -140,10 +145,22 @@ template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value)
return -1; return -1;
} }
int MqttPublishBMP180(double temp, double pressure) {
return MqttPublish(mqtt_temp, temp) + MqttPublish(mqtt_pressure, pressure);
}
int MqttPublishDry(int dry) {
return MqttPublish(mqtt_dry, (dry*100)/1024);
}
int MqttPublishIP(const String &ip) { int MqttPublishIP(const String &ip) {
return MqttPublish(mqtt_ip, ip.c_str()); return MqttPublish(mqtt_ip, ip.c_str());
} }
int MqttPublishDHT(float temp, float humidity) {
return MqttPublish(mqtt_dht_temp, temp) + MqttPublish(mqtt_dht_humidity, humidity);
}
int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) { int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *pattern) {
char *temp = strstr(subscription->topic, pattern); char *temp = strstr(subscription->topic, pattern);
if (!temp) if (!temp)
@ -158,12 +175,12 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *p
return -1; return -1;
} }
void MqttNofityIRQ(uint8_t gpio, int value) { void MqttNofityIRQ(uint8_t gpio, int value){
mqttIRQ[gpio].updated = 1; mqttIRQ[gpio].updated = 1;
mqttIRQ[gpio].value = value; mqttIRQ[gpio].value = value;
} }
void MqttNofity(int gpio, int value) { void MqttNofity(int gpio, int value){
if (MqttIsConnected()) { if (MqttIsConnected()) {
int watchIdx = findIndex(gpio, gpioControlled); int watchIdx = findIndex(gpio, gpioControlled);
if (watchIdx >= 0 ) { if (watchIdx >= 0 ) {
@ -189,7 +206,7 @@ void MqttChangePWMValue(int gpio, int value) {
void MqttCheckIRQ() { void MqttCheckIRQ() {
for (uint i = 0 ; i < NB_ELEMENTS(mqttIRQ); i++) { for (uint i = 0 ; i < NB_ELEMENTS(mqttIRQ); i++) {
if (mqttIRQ[i].updated == 1) { if(mqttIRQ[i].updated == 1){
mqttIRQ[i].updated = 0; mqttIRQ[i].updated = 0;
MqttNofity(i, mqttIRQ[i].value); MqttNofity(i, mqttIRQ[i].value);
} }
@ -197,17 +214,14 @@ void MqttCheckIRQ() {
} }
void MqttCheckSubscription() void MqttCheckSubscription() {
{
if (mqtt->getSubscriptionCount() == 0)
return;
if (MqttConnect() == 0) { if (MqttConnect() == 0) {
Adafruit_MQTT_Subscribe *subscription; Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt->readSubscription(0))) { while ((subscription = mqtt->readSubscription(0))) {
int gpio = getGpioFromSubscription(subscription, "/gpio/"); int gpio = getGpioFromSubscription(subscription, "/gpio/");
if (gpio > 0 && findIndex(gpio, gpioControlled) >= 0) { if (gpio > 0 && findIndex(gpio, gpioControlled) >= 0) {
SKETCH_DEBUG_PRINTF("Got Subscription for GPIO %d\n", gpio); SKETCH_DEBUG_PRINTF("Got Subscription for GPIO %d\n", gpio);
char *value = (char *)subscription->lastread; char *value = (char *) subscription->lastread;
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value); SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
MqttChangeGpioValue(gpio, atoi(value)); MqttChangeGpioValue(gpio, atoi(value));
} }
@ -215,7 +229,7 @@ void MqttCheckSubscription()
gpio = getGpioFromSubscription(subscription, "/pwm/"); gpio = getGpioFromSubscription(subscription, "/pwm/");
if (gpio > 0 && findIndex(gpio, pwmControlled) >= 0) { if (gpio > 0 && findIndex(gpio, pwmControlled) >= 0) {
SKETCH_DEBUG_PRINTF("Got Subscription for PWM %d\n", gpio); SKETCH_DEBUG_PRINTF("Got Subscription for PWM %d\n", gpio);
char *value = (char *)subscription->lastread; char *value = (char *) subscription->lastread;
SKETCH_DEBUG_PRINTF("Receive data: %s\n", value); SKETCH_DEBUG_PRINTF("Receive data: %s\n", value);
MqttChangePWMValue(gpio, atoi(value)); MqttChangePWMValue(gpio, atoi(value));
} }

View File

@ -1,23 +0,0 @@
#pragma once
#ifdef CONFIG_ENABLE_SCD4X
#include "debug_sketch.h"
int SCD4XGetMeasure(float &t, float &h, uint16_t &co2);
int SCD4XSetup();
bool SCD4XIsConnected();
#else
int SCD4XGetMeasure(float &, float &, uint16_t &)
{
return -1;
};
int SCD4XSetup()
{
SKETCH_DEBUG_PRINTLN("SCD4X is disabled at build time");
return -1;
};
bool SCD4XIsConnected()
{
return false;
};
#endif

View File

@ -1,61 +0,0 @@
#ifdef CONFIG_ENABLE_SCD4X
#include "SCD4X.h"
#include <Arduino.h>
#include <SensirionI2CScd4x.h>
#include <Wire.h>
SensirionI2CScd4x scd4x;
int SCD4XConnected = 0;
int SCD4XSetup()
{
Wire.begin();
scd4x.begin(Wire);
// Stop previous measurement
uint16_t error = scd4x.stopPeriodicMeasurement();
if (error) {
SKETCH_DEBUG_PRINTLN("Cannot connect to SCD4X");
return -1;
}
// Start new measurement
error = scd4x.startPeriodicMeasurement();
if (error) {
SKETCH_DEBUG_PRINTLN("Cannot start measurement for SCD4X");
return -1;
}
SCD4XConnected = 1;
return 0;
}
int SCD4XGetMeasure(float &temperature, float &humidity, uint16_t &co2)
{
// Read Measurement
bool isDataReady = false;
uint16_t error = scd4x.getDataReadyFlag(isDataReady);
if (error) {
SKETCH_DEBUG_PRINTLN("Error trying to execute getDataReadyFlag() for SCD4X ");
return -1;
}
if (!isDataReady) {
return -1;
}
error = scd4x.readMeasurement(co2, temperature, humidity);
if (error || co2 == 0) {
char errorMsg[256];
SKETCH_DEBUG_PRINT("Error with reading measurement. Error : ");
errorToString(error, errorMsg, sizeof(errorMsg));
SKETCH_DEBUG_PRINTF(" Co2: %d\n", co2);
return -1;
}
return 0;
}
bool SCD4XIsConnected()
{
return SCD4XConnected != 0;
}
#endif

View File

@ -1,23 +0,0 @@
#pragma once
#include "debug_sketch.h"
#ifdef CONFIG_ENABLE_TELEINFO
#include <LibTeleinfo.h>
int TeleinfoSetup();
int TeleinfoRetrieve(float &iinst, float &papp, float &base);
int TeleinfoRetrieve(float &iinst, float &papp, float &base,
std::vector<struct mqttInfo> &batchInfo);
#else
int TeleinfoSetup() {
SKETCH_DEBUG_PRINTLN("Teleinfo is disabled at build time");
return -1;
};
int TeleinfoRetrieve(float &, float &, float &){
return 0;
}
int TeleinfoRetrieve(float &, float &, float &, std::vector<struct mqttInfo> &)
{
return 0;
}
#endif

View File

@ -1,77 +0,0 @@
#ifdef CONFIG_ENABLE_TELEINFO
#include "Teleinfo.h"
#include <LibTeleinfo.h>
#define TELESerial Serial
TInfo tinfo;
int TeleinfoSetup()
{
TELESerial.begin(1200, SERIAL_7E1);
tinfo.init();
return 0;
}
int TeleinfoRetrieve(float &iinst, float &papp, float &base){
int c;
while ((c = TELESerial.read()) >= 0) {
tinfo.process(c);
}
ValueList *me = tinfo.getList();
if (me)
me = me->next;
while (me) {
if (strcmp(me->name, "IINST") == 0) {
iinst = atof(me->value);
}
if (strcmp(me->name, "PAPP") == 0) {
papp = atof(me->value);
}
if (strcmp(me->name, "BASE") == 0) {
float tmp = atof(me->value);
if(tmp != 0){
base = tmp;
}
}
me = me->next;
}
return 0;
}
int TeleinfoRetrieve(float &iinst, float &papp, float &base,
std::vector<struct mqttInfo> &batchInfo)
{
int c;
while ((c = TELESerial.read()) >= 0) {
tinfo.process(c);
}
ValueList *me = tinfo.getList();
if (me)
me = me->next;
while (me) {
if (strcmp(me->name, "IINST") == 0) {
iinst = atof(me->value);
batchInfo.push_back({iinst, TELEINFO_IINST_FEED_FORMAT, 0, 0});
}
if (strcmp(me->name, "PAPP") == 0) {
papp = atof(me->value);
batchInfo.push_back({papp, TELEINFO_PAPP_FEED_FORMAT, 0, 0});
}
if (strcmp(me->name, "BASE") == 0) {
float tmp = atof(me->value);
if(tmp != 0){
base = tmp;
batchInfo.push_back({base, TELEINFO_BASE_FEED_FORMAT, 0, 0});
}
}
me = me->next;
}
return 0;
}
#endif

View File

@ -6,8 +6,8 @@ String gpioControlHTML = "";
String pwmControlHTML = ""; String pwmControlHTML = "";
void WebBuildGpioObserved(String &html) { void WebBuildGpioObserved(String &html){
if (NB_ELEMENTS(gpioObserved) > 0) { if (NB_ELEMENTS(gpioObserved) > 0){
html += "<fieldset>" html += "<fieldset>"
"<legend>Detector</legend>"; "<legend>Detector</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioObserved) ; i++) {
@ -22,41 +22,34 @@ void WebHandleRoot() {
String optimiseConfig = ""; String optimiseConfig = "";
WebBuildGpioObserved(gpioObserved); WebBuildGpioObserved(gpioObserved);
if (WiFi.status() == WL_CONNECTED) { if(WiFi.status() == WL_CONNECTED){
optimiseConfig = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>"; optimiseConfig = "<a href=\"/setupPreConfig\">Optimize Config</a><br/>";
} }
server.send(200, "text/html", server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIOD_MS / 1000) + "\" ></head>" "<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
"<h1>You are connected to " + String(conf.host) + "</h1>" "<h1>You are connected to " + String(conf.host) + "</h1>"
"<fieldset>" "<fieldset>"
"<legend>Sensors</legend>" "<legend>Sensors</legend>"
#if defined CONFIG_ENABLE_BMP180 || defined CONFIG_ENABLE_BMP280 #ifdef CONFIG_ENABLE_BMP180
"" + ((BMP180IsConnected() || BMP280IsConnected()) ? "<h6>BMP180/280</h6>Temperature " + String(temp, 2) + "C<br/> Pressure " + String(pressure, 2) + "hPa<br/>" : "BMP180/280 Disconnected" ) + "" "Temperature " + String(temp, 2) + "C<br/>"
"Pressure " + String(pressure, 2) + "mB<br/>"
#endif #endif
#ifdef CONFIG_ENABLE_DHT #ifdef CONFIG_ENABLE_DHT
"" + (DHTIsConnected() ? "<h6>DHT</h6>Temperature " + String(dhtTemp, 0) + "C<br/> Humidity " + String(dhtHumidity, 0) + "%<br/>" : "DHT Disconnected" ) + "" "Temperature DHT " + String(dhtTemp, 0) + "C<br/>"
#endif "Humidity DHT " + String(dhtHumidity, 0) + "%<br/>"
#ifdef CONFIG_ENABLE_SCD4X
"" + (SCD4XIsConnected() ? "<h6>SCD4X</h6>Temperature " + String(SCD4xT, 0) + "C<br/> Humidity " + String(SCD4xH, 0) + "%<br/> CO2 " + String(SCD4xCo2) + "ppm<br/>" : "SCD4X Disconnected" ) + ""
#endif
#ifdef CONFIG_ENABLE_BME680
"" + (BME680IsConnected() ? "<h6>BME680</h6>Temperature " + String(bme680T, 2) + "C<br/> Pressure " + String(bme680P, 2) + "hPa<br/> Humidity " + String(bme680H, 2) + "%<br/> Gaz " + String(bme680G, 2) + "kOhm<br/>" : "BME680 Disconnected" ) + ""
#endif
#ifdef CONFIG_BME680_BSEC_ENABLE
"" + (BME680BSECIsConnected() ? "<h6>BME680 BSEC</h6>Temperature " + String(bme680BSECT, 2) + "C<br/> Pressure " + String(bme680BSECP, 2) + "hPa<br/> Humidity " + String(bme680BSECH, 2) + "%<br/> Indoor Air Quality " + String(bme680BSECIaq, 2) + "/500<br/> IAQ Accuraccy" + String(bme680BSECIaqAcc) + "/3<br/>": "BME680 Disconnected" ) + ""
#endif #endif
#ifdef CONFIG_ENABLE_DRY_SENSOR #ifdef CONFIG_ENABLE_DRY_SENSOR
"Dryness " + String((dryness * 100) / 1024) + "%<br/>" "Dryness " + String((dryness*100)/1024) + "%<br/>"
#endif
#ifdef CONFIG_ENABLE_TELEINFO
"<h6>Power Consumption</h6> Base " + String(teleBase, 2) +"Kwh<br/> AC Power " + telePapp +"VA<br/> Instant Current" + teleIinst +"A<br/>"
#endif #endif
"</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>" "</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>" "<legend>Settings</legend>"
"<a href=\"/setup\">Enter Setup</a><br/>" "<a href=\"/setup\">Enter Setup</a><br/>"
"<a href=\"/upload\">Update firmware</a><br/>" + optimiseConfig + "<a href=\"/upload\">Update firmware</a><br/>" + optimiseConfig +
"MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>" "MQTT Status: " + (MqttIsConnected() ? "Connected" : "Disconnected") + "<br/>"
#ifdef CONFIG_ENABLE_BMP180
"BMP 180 (Temp+Pression) Status: " + (BMP180IsConnected() ? "Connected" : "Disconnected") + "<br/>"
#endif
"Wifi Strength: " + WiFi.RSSI() + "dBm<br/>" "Wifi Strength: " + WiFi.RSSI() + "dBm<br/>"
"Free space: " + ESP.getFreeSketchSpace() + "<br/>" "Free space: " + ESP.getFreeSketchSpace() + "<br/>"
"Free heap: " + ESP.getFreeHeap() + "<br/>" "Free heap: " + ESP.getFreeHeap() + "<br/>"
@ -69,7 +62,7 @@ void WebSendError(const char *error) {
server.send(500, "text/plain", error); server.send(500, "text/plain", error);
} }
void WebBuildSSIDList(String &datalist) { void WebBuildSSIDList(String &datalist){
int n = WiFi.scanNetworks(); int n = WiFi.scanNetworks();
datalist = "<datalist id=\"scan_ssid\">"; datalist = "<datalist id=\"scan_ssid\">";
// sort by RSSI // sort by RSSI
@ -84,14 +77,14 @@ void WebBuildSSIDList(String &datalist) {
} }
} }
} }
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i){
datalist += "<option value=\"" + WiFi.SSID(indices[i]) + "\">"; datalist += "<option value=\""+WiFi.SSID(indices[i])+"\">";
} }
datalist += "</datalist>"; datalist += "</datalist>";
} }
void WebHandleSetupPreConfig() { void WebHandleSetupPreConfig() {
conf.bssid = strdup( WiFi.BSSIDstr().c_str()); conf.bssid =strdup( WiFi.BSSIDstr().c_str());
conf.channel = WiFi.channel(); conf.channel = WiFi.channel();
conf.ip_mode = 1; conf.ip_mode = 1;
conf.ip = WiFi.localIP(); conf.ip = WiFi.localIP();
@ -105,8 +98,6 @@ void WebHandleSetupPreConfig() {
void WebHandleSetup() { void WebHandleSetup() {
String ssidlist; String ssidlist;
WebBuildSSIDList(ssidlist); WebBuildSSIDList(ssidlist);
String dhcpChecked = conf.ip_mode == 0 ? "checked" : "";
String staticChecked = conf.ip_mode == 1 ? "checked" : "";
server.send(200, "text/html", "<form action=\"/save\" method=\"get\">" server.send(200, "text/html", "<form action=\"/save\" method=\"get\">"
@ -114,31 +105,27 @@ void WebHandleSetup() {
"<legend>Wifi configuration:</legend>" "<legend>Wifi configuration:</legend>"
"<div><label for=\"ssid\">Wifi SSID: </label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>" "<div><label for=\"ssid\">Wifi SSID: </label> <br/><input list=\"scan_ssid\" type=\"text\" name=\"ssid\" value=\"" + String(conf.ssid) + "\" /></div>"
"" + ssidlist + "" "" + ssidlist + ""
"<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" value=\"" + String(conf.password) + "\"/> </div>" "<div><label for=\"password\">Wifi Password: </label><br/><input type=\"password\" name=\"password\" style=\"border-color:red\" /> </div>"
"<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>" "<div><label for=\"host\">Hostname: </label><br/><input type=\"text\" name=\"host\" value=\"" + String(conf.host) + "\" /> </div>"
"<div><label for=\"channel\">Channel (0 for auto): </label><br/><input type=\"text\" name=\"channel\" value=\"" + String(conf.channel) + "\" /> </div>" "<div><label for=\"channel\">Channel (0 for auto): </label><br/><input type=\"text\" name=\"channel\" value=\"" + String(conf.channel) + "\" /> </div>"
"<div><label for=\"bssid\">BSSID (Empty for auto): </label><br/><input type=\"text\" name=\"bssid\" value=\"" + String(conf.bssid) + "\" /> </div>" "<div><label for=\"bssid\">BSSID (Empty for auto): </label><br/><input type=\"text\" name=\"bssid\" value=\"" + String(conf.bssid) + "\" /> </div>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>IP Configuration</legend>" "<legend>IP Configuration</legend>"
"<div><input type=\"radio\" name=\"ip_config\" value=\"0\" " + dhcpChecked + ">DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\" " + staticChecked + ">Static</div>" "<div><input type=\"radio\" name=\"ip_config\" value=\"0\" checked>DHCP <input type=\"radio\" name=\"ip_config\" value=\"1\">Static</div>"
"<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (conf.ip == 0 ? WiFi.localIP().toString() : IPAddress(conf.ip).toString()) + "\" /> </div>" "<div><label for=\"ip\">Ip :</label><br/><input type=\"text\" name=\"ip\" value=\"" + (conf.ip == 0 ? "192.168.0.123": IPAddress(conf.ip).toString()) + "\" /> </div>"
"<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? WiFi.gatewayIP().toString() : IPAddress(conf.gw).toString()) + "\" /> </div>" "<div><label for=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? "192.168.0.250": IPAddress(conf.gw).toString()) + "\" /> </div>"
"<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (conf.mask == 0 ? WiFi.subnetMask().toString() : IPAddress(conf.mask).toString()) + "\" /> </div>" "<div><label for=\"mask\">Netmask :</label><br/><input type=\"text\" name=\"mask\" value=\"" + (conf.mask == 0 ? "255.255.255.0": IPAddress(conf.mask).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? (WiFi.dnsIP().isSet() ? WiFi.dnsIP().toString():"") : IPAddress(conf.dns).toString()) + "\" /> </div>" "<div><label for=\"mask\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? "192.168.0.250": IPAddress(conf.dns).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? (WiFi.dnsIP(1).isSet() ? WiFi.dnsIP(1).toString():"") : IPAddress(conf.dns2).toString()) + "\" /> </div>" "<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? "": IPAddress(conf.dns2).toString()) + "\" /> </div>"
"</fieldset>" "</fieldset>"
"<fieldset>" "<fieldset>"
"<legend>MQTT:</legend>" "<legend>MQTT:</legend>"
"<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(conf.mqttServer) + "\" /> </div>" "<div><label for=\"mqttServer\">Server :</label><br/><input type=\"text\" name=\"mqttServer\" value=\"" + String(conf.mqttServer) + "\" /> </div>"
"<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(conf.mqttUser) + "\" /> </div>" "<div><label for=\"mqttUser\">Username :</label><br/><input type=\"text\" name=\"mqttUser\" value=\"" + String(conf.mqttUser) + "\" /> </div>"
"<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" value=\"" + String(conf.mqttPasswd) + "\" /> </div>" "<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" /> </div>"
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>" "<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>"
"</fieldset>" "</fieldset>"
"<fieldset>"
"<legend>Sensor:</legend>"
"<div><label for=\"samplingPeriod\">Sampling Period (ms): </label><br/><input type=\"text\" name=\"samplingPeriod\" value=\"" + String(conf.samplingPeriod) + "\" /> </div>"
"</fieldset>"
"<div class=\"button\"> <button type=\"submit\">Save</button></div>" "<div class=\"button\"> <button type=\"submit\">Save</button></div>"
"</form>"); "</form>");
} }
@ -182,7 +169,7 @@ void WebHandleSave() {
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd") || !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
|| !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip") || !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip")
|| !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns") || !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns")
|| !server.hasArg("dns2") || !server.hasArg("channel") || !server.hasArg("bssid") || !server.hasArg("samplingPeriod")) { || !server.hasArg("dns2") || !server.hasArg("channel") || ! server.hasArg("bssid")) {
server.send(500, "text/plain", "Bad arguments\r\n"); server.send(500, "text/plain", "Bad arguments\r\n");
return; return;
} }
@ -197,35 +184,22 @@ void WebHandleSave() {
} }
} }
productConfig newConf = {BOOTMODE_NORMAL, productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").c_str()),
strdup(server.arg("ssid").c_str()), strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").c_str()), strdup(server.arg("mqttUser").c_str()),
strdup(server.arg("password").c_str()), strdup(server.arg("mqttPasswd").c_str()), server.arg("mqttPort").toInt(),
strdup(server.arg("host").c_str()), server.arg("ip_config").toInt(), static_cast<uint32_t>(ip), static_cast<uint32_t>(gw),
strdup(server.arg("mqttServer").c_str()), static_cast<uint32_t>(mask), static_cast<uint32_t>(dns), static_cast<uint32_t>(dns2), server.arg("channel").toInt(), strdup(server.arg("bssid").c_str())};
strdup(server.arg("mqttUser").c_str()),
strdup(server.arg("mqttPasswd").c_str()),
server.arg("mqttPort").toInt(),
server.arg("ip_config").toInt(),
static_cast<uint32_t>(ip),
static_cast<uint32_t>(gw),
static_cast<uint32_t>(mask),
static_cast<uint32_t>(dns),
static_cast<uint32_t>(dns2),
static_cast<uint8_t>(server.arg("channel").toInt()),
strdup(server.arg("bssid").c_str()),
static_cast<uint32_t>(server.arg("samplingPeriod").toInt())};
if (EepromSaveConfig(newConf) < 0) { if (EepromSaveConfig(newConf) < 0) {
WebSendError("Cannot Save configuration ( Credentials too long ?Contains \";\"?)\r\n"); WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
return; return;
} }
samplingPeriod = newConf.samplingPeriod; if(WiFi.softAPIP() != IPAddress((uint32_t)0)){
if (WiFi.softAPIP() != IPAddress((uint32_t)0)) {
//In STA mode, we can test the AP connection //In STA mode, we can test the AP connection
WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str()); WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str());
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>" server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
"<a href=\"/wifiStatus\">Check Wifi Configuration</a><br/>" "<a href=\"/wifiStatus\">Check Wifi Configuration</a><br/>"
"<a href=\"/reboot\">Reboot</a><br/>"); "<a href=\"/reboot\">Reboot</a><br/>");
} else { }else{
server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>" server.send(200, "text/html", "<h1>Configuration Saved</h1><br/>"
"<a href=\"/reboot\">Reboot</a><br/>"); "<a href=\"/reboot\">Reboot</a><br/>");
} }
@ -261,7 +235,7 @@ void WebHandleReboot() {
} }
String statusToString(wl_status_t status) { String statusToString(wl_status_t status) {
switch (status) { switch(status){
case WL_IDLE_STATUS: return String("Idle"); case WL_IDLE_STATUS: return String("Idle");
case WL_NO_SSID_AVAIL: return String("Wifi not found"); case WL_NO_SSID_AVAIL: return String("Wifi not found");
case WL_CONNECTED: return String("Connected"); case WL_CONNECTED: return String("Connected");
@ -275,7 +249,7 @@ String statusToString(wl_status_t status) {
void WebHandleWifiStatus() { void WebHandleWifiStatus() {
String message; String message;
if (WiFi.status() == WL_DISCONNECTED) if(WiFi.status() == WL_DISCONNECTED)
message += "<head><meta http-equiv=\"refresh\" content=\"1\" ></head>"; message += "<head><meta http-equiv=\"refresh\" content=\"1\" ></head>";
message += "<h1>Wifi Connection Status</h1><br/>"; message += "<h1>Wifi Connection Status</h1><br/>";
message += "Connection to "; message += "Connection to ";
@ -283,19 +257,19 @@ void WebHandleWifiStatus() {
message += ":<br/>"; message += ":<br/>";
message += statusToString(WiFi.status()); message += statusToString(WiFi.status());
message += "<br/>"; message += "<br/>";
if (mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED) { if(mode == BOOTMODE_SETUP && WiFi.status() == WL_CONNECTED){
message += "Wifi correctly setup! You can reboot now<br/>"; message += "Wifi correctly setup! You can reboot now<br/>";
message += "<a href=\"/reboot\">Reboot</a><br/>"; message += "<a href=\"/reboot\">Reboot</a><br/>";
} }
if (WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL) { if(WiFi.status() == WL_CONNECT_FAILED || WiFi.status() == WL_NO_SSID_AVAIL){
message += "Try to reconfigure you WiFi details<br/>"; message += "Try to reconfigure you WiFi details<br/>";
message += "<a href=\"/setup\">Enter Setup</a><br/>"; message += "<a href=\"/setup\">Enter Setup</a><br/>";
} }
server.send(200, "text/html", message); server.send(200, "text/html", message);
} }
void WebBuildGpioControl() { void WebBuildGpioControl(){
if (NB_ELEMENTS(gpioControlled) > 0) { if (NB_ELEMENTS(gpioControlled) > 0){
gpioControlHTML += "<fieldset>" gpioControlHTML += "<fieldset>"
"<legend>Relay</legend>"; "<legend>Relay</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) ; i++) {
@ -306,8 +280,8 @@ void WebBuildGpioControl() {
} }
} }
void WebBuildPwmControl() { void WebBuildPwmControl(){
if (NB_ELEMENTS(pwmControlled) > 0) { if (NB_ELEMENTS(pwmControlled) > 0){
pwmControlHTML += "<fieldset>" pwmControlHTML += "<fieldset>"
"<legend>PWM</legend>"; "<legend>PWM</legend>";
for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) { for (uint i = 0 ; i < NB_ELEMENTS(pwmControlled) ; i++) {
@ -347,12 +321,12 @@ void WebSetupServer(int ) {
} }
#else #else
void WebHandleRoot() {} void WebHandleRoot(){}
void WebHandleSetupPreConfig() {} void WebHandleSetupPreConfig(){}
void WebHandleSetup() {} void WebHandleSetup(){}
void WebHandleGpio() {} void WebHandleGpio(){}
void WebHandleSave() {} void WebHandleSave(){}
void WebHandleOTA() {} void WebHandleOTA(){}
void WebHandleNotFound() {} void WebHandleNotFound(){}
void WebSetupServer(int bootmode) {} void WebSetupServer(int bootmode){}
#endif #endif

View File

@ -28,7 +28,7 @@
#include <vector>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <WiFiClient.h> #include <WiFiClient.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
@ -43,17 +43,12 @@
#include "debug_sketch.h" #include "debug_sketch.h"
#include "BMP180.h" #include "BMP180.h"
#include "BMP280.h"
#include "BME680.h"
#include "BME680_BSEC.h"
#include "sensor_DHT.h" #include "sensor_DHT.h"
#include "dry_sensor.h" #include "dry_sensor.h"
#include "MQTT.h" #include "MQTT.h"
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h" #include "Adafruit_MQTT_Client.h"
#include "EEPROM.h" #include "EEPROM.h"
#include "Teleinfo.h"
#include "SCD4X.h"
extern "C" { extern "C" {
#include <user_interface.h> #include <user_interface.h>
@ -66,22 +61,15 @@ extern "C" {
double temp, pressure; double temp, pressure;
float dhtTemp, dhtHumidity; float dhtTemp, dhtHumidity;
float bme680T, bme680P, bme680H, bme680G, bme680A;
float bme680BSECT, bme680BSECP, bme680BSECH, bme680BSECIaq, bme680BSECIaqAcc;
float teleIinst, telePapp, teleBase;
float SCD4xT, SCD4xH;
uint16_t SCD4xCo2;
int dryness; int dryness;
uint8_t mode; uint8_t mode;
int reconfig = 0; int reconfig = 0;
productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL, CONFIG_SAMPLING_PERIOD_MS}; productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 1883, 0, 0, 0, 0, 0, 0, 0, ""};
// Should have less that MAXSUBSCRIPTIONS elements // Should have less that MAXSUBSCRIPTIONS elements
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; const int gpioControlled[] = CONFIG_CONTROLLED_GPIO;
const int gpioObserved[] = CONFIG_OBSERVED_GPIO; const int gpioObserved[] = CONFIG_OBSERVED_GPIO;
const int pwmControlled[] = CONFIG_CONTROLLED_PWM; const int pwmControlled[] = CONFIG_CONTROLLED_PWM;
uint samplingPeriod = CONFIG_SAMPLING_PERIOD_MS;
uint nbCycle = UINT_MAX - 1;
/* Set these to your desired credentials. */ /* Set these to your desired credentials. */
const char *ssid = CONFIG_SSID_NAME; const char *ssid = CONFIG_SSID_NAME;
@ -99,8 +87,8 @@ void WebSetupServer(int bootmode);
#ifdef CONFIG_SETUP_BUTTON #ifdef CONFIG_SETUP_BUTTON
void onLongButtonPressed(uint8_t pin) { void onLongButtonPressed(uint8_t pin){
if (pin == CONFIG_SETUP_BUTTON) { if(pin == CONFIG_SETUP_BUTTON){
reconfig = 1; reconfig = 1;
SKETCH_DEBUG_PRINTLN("Putting device in setup mode"); SKETCH_DEBUG_PRINTLN("Putting device in setup mode");
mode = BOOTMODE_SETUP; mode = BOOTMODE_SETUP;
@ -122,7 +110,7 @@ void WifiSetup(productConfig conf) {
//Disable previous AP mode //Disable previous AP mode
WiFi.softAPdisconnect(true); WiFi.softAPdisconnect(true);
SKETCH_DEBUG_PRINTLN("Connecting to Wifi..."); SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
if (conf.ip_mode == 1) { if(conf.ip_mode == 1){
SKETCH_DEBUG_PRINTLN("Use static ip configuration"); SKETCH_DEBUG_PRINTLN("Use static ip configuration");
WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2)); WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2));
} }
@ -130,27 +118,25 @@ void WifiSetup(productConfig conf) {
uint8_t bssid[6]; uint8_t bssid[6];
if (conf.bssid[0] != '\0') { if (conf.bssid[0] != '\0') {
String bssidStr = conf.bssid; String bssidStr = conf.bssid;
bssid[0] = strtoul(bssidStr.substring(0, 2).c_str(), NULL, 16); bssid[0] = strtoul(bssidStr.substring(0,2).c_str(), NULL, 16);
bssid[1] = strtoul(bssidStr.substring(3, 5).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[2] = strtoul(bssidStr.substring(6,8).c_str(), NULL, 16);
bssid[3] = strtoul(bssidStr.substring(9, 11).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[4] = strtoul(bssidStr.substring(12,14).c_str(), NULL, 16);
bssid[5] = strtoul(bssidStr.substring(15, 17).c_str(), NULL, 16); bssid[5] = strtoul(bssidStr.substring(15,17).c_str(), NULL, 16);
SKETCH_DEBUG_PRINTF("Using BSSID: %02x:%02x:%02x:%02x:%02x:%02x\n", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]); SKETCH_DEBUG_PRINTF("Using BSSID: %x:%x:%x:%x:%x:%x\n", bssid[5], bssid[4], bssid[3], bssid[2], bssid[1], bssid[0]);
bssidConf = bssid; bssidConf = bssid;
} }
SKETCH_DEBUG_PRINTF("Using channel %u (0==auto)\n", conf.channel );
WiFi.begin(conf.ssid, conf.password, conf.channel, bssidConf); WiFi.begin(conf.ssid, conf.password, conf.channel, bssidConf);
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(50); delay(500);
SKETCH_DEBUG_PRINT("."); SKETCH_DEBUG_PRINT(".");
if (reconfig == 1) { if (reconfig == 1){
reconfig = 0; reconfig = 0;
return; return;
} }
if (connectionTry == 120) { if(connectionTry == 10){
SKETCH_DEBUG_PRINTLN("Cannot connect to wifi. Try without BSSID and channel"); SKETCH_DEBUG_PRINTLN("Cannot connect to wifi. Try withour BSSID and channel");
WiFi.begin(conf.ssid, conf.password); WiFi.begin(conf.ssid, conf.password);
} }
connectionTry++; connectionTry++;
@ -223,8 +209,7 @@ void OTASetup() {
#endif #endif
} }
void setup() void setup() {
{
#ifdef CONFIG_SETUP_BUTTON #ifdef CONFIG_SETUP_BUTTON
new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, NULL, onLongButtonPressed); new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, NULL, onLongButtonPressed);
#endif #endif
@ -235,15 +220,11 @@ void setup()
SKETCH_DEBUG_PRINTLN(); SKETCH_DEBUG_PRINTLN();
// Get GPIO 3 Status // Get GPIO 3 Status
#if CONFIG_SERIAL_SHOULD_SWAP Serial.swap(); //Switch Serial on GPIO 13 & 15
SKETCH_DEBUG_PRINTLN("SWAP UART");
Serial.swap(); // Switch Serial on GPIO 13 & 15
#endif
pinMode(CONFIG_SETUP_GPIO, INPUT_PULLUP); pinMode(CONFIG_SETUP_GPIO, INPUT_PULLUP);
int txStatus = digitalRead(CONFIG_SETUP_GPIO); int txStatus = digitalRead(CONFIG_SETUP_GPIO);
#if !defined(CONFIG_ENABLE_EXTRA_GPIO) && CONFIG_SERIAL_SHOULD_SWAP #ifndef CONFIG_ENABLE_EXTRA_GPIO
Serial.swap(); // Switch back on GPIO 1 & 3 Serial.swap(); // Switch back on GPIO 1 & 3
SKETCH_DEBUG_PRINTLN("SWAP UART");
#endif #endif
EEPROM.begin(CONFIG_EEPROM_SIZE); EEPROM.begin(CONFIG_EEPROM_SIZE);
@ -264,8 +245,8 @@ void setup()
SKETCH_DEBUG_PRINTLN("No configuration saved"); SKETCH_DEBUG_PRINTLN("No configuration saved");
} }
SKETCH_DEBUG_PRINTF("Force Setup Mode ? : %s\n", txStatus ? "No" : "Yes"); SKETCH_DEBUG_PRINTF("Force Setup Mode ? : %s\n",txStatus ? "No" : "Yes");
if (txStatus == 0) { if(txStatus == 0){
mode = BOOTMODE_SETUP; mode = BOOTMODE_SETUP;
} }
@ -278,121 +259,59 @@ void setup()
if (mode == BOOTMODE_OTA) { if (mode == BOOTMODE_OTA) {
OTASetup(); OTASetup();
} else { } else {
if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)) { if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)){
SKETCH_DEBUG_PRINTLN("BMP180 init success"); SKETCH_DEBUG_PRINTLN("BMP180 init success");
} }
if (!BMP280Setup()) { if (!DHTSetup(CONFIG_DHT_PIN)){
SKETCH_DEBUG_PRINTLN("BMP280 init success");
}
if (!DHTSetup(CONFIG_DHT_PIN)) {
SKETCH_DEBUG_PRINTLN("DHT init success"); SKETCH_DEBUG_PRINTLN("DHT init success");
} }
if (!DrySetup(CONFIG_DRY_POWER_PIN)) { if (!DrySetup(CONFIG_DRY_POWER_PIN)){
SKETCH_DEBUG_PRINTLN("DRY init success"); SKETCH_DEBUG_PRINTLN("DRY init success");
} }
if(!BME680Setup()){
SKETCH_DEBUG_PRINTLN("BME680 init success");
}
if(!BME680BSECSetup()){
SKETCH_DEBUG_PRINTLN("BME680 with BSEC init success");
}
if(!TeleinfoSetup()){
SKETCH_DEBUG_PRINTLN("Teleinfo init success");
}
if(!SCD4XSetup()){
SKETCH_DEBUG_PRINTLN("SCD4X init success");
}
WebSetupServer(mode); WebSetupServer(mode);
} }
samplingPeriod = conf.samplingPeriod;
#ifdef CONFIG_ENABLE_POWER_SAVE #ifdef CONFIG_ENABLE_POWER_SAVE
wifi_set_sleep_type(LIGHT_SLEEP_T); wifi_set_sleep_type(LIGHT_SLEEP_T);
#endif #endif
} }
uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS;
void loop() { void loop() {
if (mode == BOOTMODE_OTA) { if (mode == BOOTMODE_OTA) {
ArduinoOTA.handle(); ArduinoOTA.handle();
} else { } else {
server.handleClient(); server.handleClient();
if (mode == BOOTMODE_NORMAL) { if (mode == BOOTMODE_NORMAL){
MqttCheckSubscription(); MqttCheckSubscription();
MqttCheckIRQ(); MqttCheckIRQ();
} }
delay(CONFIG_WEB_DELAY_MS); delay(CONFIG_WEB_DELAY_MS);
TeleinfoRetrieve(teleIinst, telePapp, teleBase);
nbCycle++; nbCycle++;
if (nbCycle > samplingPeriod / CONFIG_WEB_DELAY_MS) { if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
std::vector<struct mqttInfo> batchInfo;
if (!BMP180GetTempAndPressure(temp, pressure)) { if (!BMP180GetTempAndPressure(temp, pressure)) {
SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure); SKETCH_DEBUG_PRINT("Current T°C ");
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0}); SKETCH_DEBUG_PRINT(temp);
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0}); SKETCH_DEBUG_PRINT(" Pressure mB ");
} SKETCH_DEBUG_PRINTLN(pressure);
if (!BMP280GetTempAndPressure(temp, pressure)) { if (mode == BOOTMODE_NORMAL)
SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure); MqttPublishBMP180(temp, pressure);
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0});
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0});
} }
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) { if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
SKETCH_DEBUG_PRINTF("Current %f°C %f%% Humidity\n", dhtTemp, dhtHumidity); SKETCH_DEBUG_PRINT("Current T°C ");
batchInfo.push_back({dhtTemp, TEMPERATURE_DHT_FEED_FORMAT, 0, 0}); SKETCH_DEBUG_PRINT(dhtTemp);
batchInfo.push_back({dhtHumidity, HUMIDITY_DHT_FEED_FORMAT, 0, 0}); SKETCH_DEBUG_PRINT(" Humidity ");
} SKETCH_DEBUG_PRINTLN(dhtHumidity);
if (!SCD4XGetMeasure(SCD4xT, SCD4xH, SCD4xCo2)) {
SKETCH_DEBUG_PRINTF("Current %f°C %f Humidity, Current C02 %d\n", SCD4xT, SCD4xH, SCD4xCo2);
batchInfo.push_back({SCD4xT, SCD4X_TEMPERATURE_FEED_FORMAT, 0, 0});
batchInfo.push_back({SCD4xH, SCD4X_HUMIDITY_FEED_FORMAT, 0, 0});
batchInfo.push_back({(float)SCD4xCo2, SCD4X_CO2_FEED_FORMAT, 0, 0});
}
if (!DryGetMeasure(dryness)) {
SKETCH_DEBUG_PRINTF("Current dryness %d %%\n", (dryness * 100) / 1024);
batchInfo.push_back({(float)dryness, DRY_FEED_FORMAT, 0, 0});
}
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});
}
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});
}
TeleinfoRetrieve(teleIinst, telePapp, teleBase, batchInfo);
if (mode == BOOTMODE_NORMAL) if (mode == BOOTMODE_NORMAL)
if (MqttBatchPublish(batchInfo, conf.mqttUser, conf.host)) MqttPublishDHT(dhtTemp, dhtHumidity);
WifiSetup(conf); }
if (!DryGetMeasure(dryness)){
SKETCH_DEBUG_PRINT("Current dryness ");
SKETCH_DEBUG_PRINT((dryness*100)/1024);
SKETCH_DEBUG_PRINTLN("%");
if (mode == BOOTMODE_NORMAL)
MqttPublishDry(dryness);
}
nbCycle = 0; nbCycle = 0;
} }
} }

View File

@ -9,16 +9,8 @@
#define CONFIG_WEB_DELAY_MS 100 #define CONFIG_WEB_DELAY_MS 100
#endif #endif
#ifndef CONFIG_SAMPLING_PERIOD_MS #ifndef CONFIG_SAMPLING_PERIODE_MS
#define CONFIG_SAMPLING_PERIOD_MS 60000 #define CONFIG_SAMPLING_PERIODE_MS 60000
#endif
#if defined(CONFIG_ENABLE_BME680) && defined(CONFIG_BME680_BSEC_ENABLE)
#error "BME680 and BME680 BSEC cannot be enabled together"
#endif
#ifndef CONFIG_BME680_BSEC_I2C_ADDR
#define CONFIG_BME680_BSEC_I2C_ADDR 0x76
#endif #endif
#if defined(CONFIG_ENABLE_BMP180) && !defined(CONFIG_BMP180_SDA) #if defined(CONFIG_ENABLE_BMP180) && !defined(CONFIG_BMP180_SDA)
@ -35,7 +27,7 @@
#if defined(CONFIG_ENABLE_DHT) && !defined(CONFIG_DHT_PIN) #if defined(CONFIG_ENABLE_DHT) && !defined(CONFIG_DHT_PIN)
#error "When enabling DHT, you should configure SDA pin" #error "When enabling DHT, you should configure SDA pin"
#elif !defined(CONFIG_ENABLE_DHT) && !defined(CONFIG_DHT_PIN) #elif !defined(CONFIG_ENABLE_DHT)
#define CONFIG_DHT_PIN 0 #define CONFIG_DHT_PIN 0
#endif #endif
@ -64,24 +56,9 @@
#endif #endif
#ifndef CONFIG_SETUP_GPIO #ifndef CONFIG_SETUP_GPIO
#define CONFIG_SETUP_GPIO 14 #define CONFIG_SETUP_GPIO 3
#endif #endif
#ifndef CONFIG_DHT_TYPE #ifndef CONFIG_DHT_TYPE
#define CONFIG_DHT_TYPE DHT11 #define CONFIG_DHT_TYPE DHT11
#endif #endif
#if CONFIG_SETUP_GPIO == 3 || CONFIG_SETUP_GPIO == 1
#define CONFIG_SERIAL_SHOULD_SWAP
#endif
#if defined(CONFIG_ENABLE_TELEINFO)
#warning "TELEINFO is using SERIAL for communication. Debug will be on Serial1 (D4)"
#define DEBUG_PRINTER_WIFICONTROLSENSOR Serial1
#if defined(CONFIG_SERIAL_SHOULD_SWAP)
#error "When enabling TELEINFO, SERIAL_SHOULD_SWAP cannot be enabled (SETUP_GPIO == 1 or 3)"
#endif
#if defined(CONFIG_ENABLE_EXTRA_GPIO)
#error "When enabling TELEINFO, ENABLE_EXTRA_CPIO cannot be enabled"
#endif
#endif

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "pins_arduino.h"
// Enable Serial Console (Disable to save space and power) // Enable Serial Console (Disable to save space and power)
#define CONFIG_SKETCH_DEBUG #define CONFIG_SKETCH_DEBUG
@ -12,33 +12,20 @@
//#define CONFIF_DISABLE_OTA //#define CONFIF_DISABLE_OTA
//#define CONFIG_DISABLE_MQTT //#define CONFIG_DISABLE_MQTT
// Enable the temperature, pressure, humidity and gaz Sensor BME680 on standard i2c esp8266 pins
// It use default i2c pin GPIO4(D2): SDA, GPIO5(D1):SCL
// Should be powered by 3.3v and sampled every 3sec or 300s (Or you should adapt bsec_config_iaq in BME680_BSEC.ino )
//#define CONFIG_BME680_BSEC_ENABLE
#define CONFIG_BME680_BSEC_I2C_ADDR 0x77
// Enable the temperature, pressure, humidity and gaz Sensor BME680 on standard i2c esp8266 pins
// It use default i2c pin GPIO4(D2): SDA, GPIO5(D1):SCL
//#define CONFIG_ENABLE_BME680
// Enable the temperatue and pressure Sensor BMP180 // Enable the temperatue and pressure Sensor BMP180
// (CONFIG_BMP180_SDA and CONFIG_BMP180_SDA should be defined as well) // (CONFIG_BMP180_SDA and CONFIG_BMP180_SDA should be defined as well)
//#define CONFIG_ENABLE_BMP180 #define CONFIG_ENABLE_BMP180
//#define CONFIG_BMP180_SDA SDA //D2 #define CONFIG_BMP180_SDA 12
//#define CONFIG_BMP180_SCL SCL //D1 #define CONFIG_BMP180_SCL 14
//
//#define CONFIG_ENABLE_DHT #define CONFIG_ENABLE_DHT
//#define CONFIG_DHT_PIN 2 #define CONFIG_DHT_PIN 2
//#define CONFIG_DHT_TYPE DHT22 #define CONFIG_DHT_TYPE DHT22
//#define CONFIG_ENABLE_BMP280
//#define CONFIG_ENABLE_DRY_SENSOR //#define CONFIG_ENABLE_DRY_SENSOR
//If the dry sensor is powered by a GPIO, this GPIO could be defined here //If the dry sensor is powered by a GPIO, this GPIO could be defined here
//#define CONFIG_DRY_POWER_PIN 13 //#define CONFIG_DRY_POWER_PIN 13
//Advised CONFIG_SAMPLING_PERIOD_MS is 5s for this sensor
//#define CONFIG_ENABLE_SCD4X
// Enable light sleep to save some power (http://bbs.espressif.com/viewtopic.php?f=6&t=133&p=485&hilit=sleep+modem#p485) // Enable light sleep to save some power (http://bbs.espressif.com/viewtopic.php?f=6&t=133&p=485&hilit=sleep+modem#p485)
#define CONFIG_ENABLE_POWER_SAVE #define CONFIG_ENABLE_POWER_SAVE
@ -48,18 +35,16 @@
// Long press on this button will put device in setup mode at runtime // Long press on this button will put device in setup mode at runtime
#define CONFIG_SETUP_BUTTON 0 #define CONFIG_SETUP_BUTTON 0
// Teleinfo https://github.com/hallard/LibTeleinfo/
#define CONFIG_ENABLE_TELEINFO
/* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */ /* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */
//If this GPIO is LOW at boot, device will enter setup mode //If this GPIO is LOW at boot, device will enter setup mode
#define CONFIG_SETUP_GPIO 14 //D5 #define CONFIG_SETUP_GPIO 5
// Time to sleep between 2 webserver request (increase it reduce battery usage but increase latency) // Time to sleep between 2 webserver request (increase it reduce battery usage but increase latency)
#define CONFIG_WEB_DELAY_MS 800 //#define CONFIG_WEB_DELAY_MS 100
// Get sensors value every X ms // Get sensors value every X ms
#define CONFIG_SAMPLING_PERIOD_MS 30000 #define CONFIG_SAMPLING_PERIODE_MS 300000
// Name of the SSID when in AP mode for configuration // Name of the SSID when in AP mode for configuration
#define CONFIG_SSID_NAME "ESPConfiguratorBureau" #define CONFIG_SSID_NAME "ESPConfiguratorBureau"

View File

@ -2,15 +2,13 @@
//#define CONFIG_SKETCH_DEBUG //#define CONFIG_SKETCH_DEBUG
// Set where debug messages will be printed. // Set where debug messages will be printed.
#ifndef DEBUG_PRINTER_WIFICONTROLSENSOR #define DEBUG_PRINTER Serial
#define DEBUG_PRINTER_WIFICONTROLSENSOR Serial
#endif
#ifdef CONFIG_SKETCH_DEBUG #ifdef CONFIG_SKETCH_DEBUG
#define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER_WIFICONTROLSENSOR.begin(speed); } #define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); }
#define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.print(__VA_ARGS__); } #define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
#define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.printf(__VA_ARGS__); } #define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); }
#define SKETCH_DEBUG_PRINTLN(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.println(__VA_ARGS__); } #define SKETCH_DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
#else #else
#define SKETCH_DEBUG_INIT(speed) #define SKETCH_DEBUG_INIT(speed)
#define SKETCH_DEBUG_PRINT(...) {} #define SKETCH_DEBUG_PRINT(...) {}