Compare commits

...

17 Commits

Author SHA1 Message Date
mathieu ec2e28a5b6 Merge pull request 'scd4x' (#1) from scd4x into master
Reviewed-on: #1
2023-11-28 23:12:28 +01:00
Mathieu Maret 06f94a2ee7 SCD4X is disabled by default 2023-11-28 23:10:50 +01:00
Mathieu Maret ccfc148e31 SCD4X: add information on html page 2023-11-28 22:57:29 +01:00
Mathieu Maret 346d7927d8 SCD4X: Setup and MQTT 2023-11-27 23:47:53 +01:00
Mathieu Maret 0f8c14e51e Add basic sensor files for SCD4x 2023-11-27 23:32:45 +01:00
Mathieu Maret a6281c5581 BME680: remove uneeded includes 2023-11-27 23:32:13 +01:00
Mathieu Maret efc7331f25 Default cfg for BMP280 2023-04-17 22:01:21 +02:00
Mathieu Maret 5bef410167 Add BMP280 support 2023-04-14 09:23:32 +02:00
Mathieu Maret f9985cc2c1 Fix wifi reconnect on wifi on/off 2023-03-28 14:27:45 +02:00
Mathieu Maret ef446ebd19 Teleinfo: pull data at every loop
to avoid overflow in libteleinfo
2023-03-28 14:05:14 +02:00
Mathieu Maret 5987bc28de Teleinfo: fix signature 2023-03-28 14:01:46 +02:00
Mathieu Maret 0847e89960 Display more teleinfo information 2022-09-25 00:09:04 +02:00
Mathieu Maret 7cac6f89f1 make sampling periode configurable 2022-09-10 23:18:52 +02:00
Mathieu Maret 7b1d9ae63e Avoid some mqtt server connection
Do not check subscription when we do not subscribe
2022-09-10 22:48:37 +02:00
Mathieu Maret 73d6e17dc1 Merge branch 'teleinfo' 2022-09-10 21:53:45 +02:00
Mathieu Maret cbce043f30 BME680 fix hPA unit 2022-09-07 22:58:44 +02:00
Mathieu Maret e68eec8955 Increase Wifi connection time before reconfig 2022-09-07 22:58:23 +02:00
16 changed files with 288 additions and 77 deletions

View File

@ -14,8 +14,6 @@
//Use Default i2c pin GPIO4(D2): SDA, GPIO5(D1):SCL
#define SEALEVELPRESSURE_HPA (1013.25)
#include <SFE_BMP180.h>
#include "debug_sketch.h"
int BME680GetMeasure(float &t, float &p, float &h, float &g, float &a);
int BME680Setup();

View File

@ -16,12 +16,12 @@
generic_33v_300s_28d
*/
const uint8_t bsec_config_iaq[] = {
#if CONFIG_SAMPLING_PERIODE_MS == 3000
#if CONFIG_SAMPLING_PERIOD_MS == 3000
#include "config/generic_33v_3s_4d/bsec_iaq.txt"
#elif CONFIG_SAMPLING_PERIODE_MS == 300000
#elif CONFIG_SAMPLING_PERIOD_MS == 300000
#include "config/generic_33v_300s_4d/bsec_iaq.txt"
#else
#error "Unsupport CONFIG_SAMPLING_PERIODE_MS (3000 and 300000 are supported)"
#error "Unsupport CONFIG_SAMPLING_PERIOD_MS (3000 and 300000 are supported)"
#endif
};
@ -94,7 +94,7 @@ 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;
p = iaqSensor.pressure/100;
h = iaqSensor.humidity;
iaq = iaqSensor.iaq;
iaqAcc = iaqSensor.iaqAccuracy;

View File

@ -0,0 +1,15 @@
#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

@ -0,0 +1,44 @@
#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

@ -17,6 +17,7 @@ typedef struct productConfig_t {
uint32_t dns2;
uint8_t channel;
char *bssid;
uint32_t samplingPeriod;
} productConfig;

View File

@ -24,7 +24,7 @@ int EepromSaveConfig(productConfig &config) {
+ String(config.ip_mode) + ";"
+ config.ip + ";" + config.gw + ";" + config.mask + ";"
+ config.dns + ";" + config.dns2 + ";" + config.channel + ";"
+ config.bssid + ";";
+ config.bssid + ";" + config.samplingPeriod + ";";
if (eeprom.length() > CONFIG_EEPROM_SIZE )
return -EMSGSIZE;
@ -104,6 +104,8 @@ void EepromReadConfig(productConfig &config) {
readConfElement(&tmpString, i);
config.channel = atoi(tmpString);
readConfElement(&config.bssid, i);
readConfElement(&tmpString, i);
config.samplingPeriod = atoi(tmpString);
}

View File

@ -21,6 +21,9 @@
#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
#include "Adafruit_MQTT.h"

View File

@ -197,14 +197,17 @@ void MqttCheckIRQ() {
}
void MqttCheckSubscription() {
void MqttCheckSubscription()
{
if (mqtt->getSubscriptionCount() == 0)
return;
if (MqttConnect() == 0) {
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt->readSubscription(0))) {
int gpio = getGpioFromSubscription(subscription, "/gpio/");
if (gpio > 0 && findIndex(gpio, gpioControlled) >= 0) {
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);
MqttChangeGpioValue(gpio, atoi(value));
}
@ -212,7 +215,7 @@ void MqttCheckSubscription() {
gpio = getGpioFromSubscription(subscription, "/pwm/");
if (gpio > 0 && findIndex(gpio, pwmControlled) >= 0) {
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);
MqttChangePWMValue(gpio, atoi(value));
}

23
WifiControlSensor/SCD4X.h Normal file
View File

@ -0,0 +1,23 @@
#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

@ -0,0 +1,61 @@
#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

@ -4,18 +4,20 @@
#ifdef CONFIG_ENABLE_TELEINFO
#include <LibTeleinfo.h>
int TeleinfoSetup();
int TeleinfoProcess(std::vector<struct mqttInfo> &batchInfo);
int TeleinfoProcess(float &iinst, float &papp, float &base);
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;
};
inline int TeleinfoProcess(std::vector<struct mqttInfo> &){
return 0;
};
int TeleinfoProcess(float &iinst, float &papp, float &base){
return 0;
};
int TeleinfoRetrieve(float &, float &, float &){
return 0;
}
int TeleinfoRetrieve(float &, float &, float &, std::vector<struct mqttInfo> &)
{
return 0;
}
#endif

View File

@ -13,37 +13,8 @@ int TeleinfoSetup()
return 0;
}
int TeleinfoProcess(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) {
float val = atof(me->value);
batchInfo.push_back({val, TELEINFO_IINST_FEED_FORMAT, 0, 0});
}
if (strcmp(me->name, "PAPP") == 0) {
float val = atof(me->value);
batchInfo.push_back({val, TELEINFO_PAPP_FEED_FORMAT, 0, 0});
}
if (strcmp(me->name, "BASE") == 0) {
float val = atof(me->value);
batchInfo.push_back({val, TELEINFO_BASE_FEED_FORMAT, 0, 0});
}
me = me->next;
}
return 0;
}
int TeleinfoProcess(float &iinst, float &papp, float &base )
{
int TeleinfoRetrieve(float &iinst, float &papp, float &base){
int c;
while ((c = TELESerial.read()) >= 0) {
@ -60,7 +31,43 @@ int TeleinfoProcess(float &iinst, float &papp, float &base )
papp = atof(me->value);
}
if (strcmp(me->name, "BASE") == 0) {
base = atof(me->value);
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;
}

View File

@ -27,16 +27,19 @@ void WebHandleRoot() {
}
server.send(200, "text/html",
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIOD_MS / 1000) + "\" ></head>"
"<h1>You are connected to " + String(conf.host) + "</h1>"
"<fieldset>"
"<legend>Sensors</legend>"
#ifdef CONFIG_ENABLE_BMP180
"" + (BMP180IsConnected() ? "<h6>BMP180</h6>Temperature " + String(temp, 2) + "C<br/> Pressure " + String(pressure, 2) + "hPa<br/>" : "BMP180 Disconnected" ) + ""
#if defined CONFIG_ENABLE_BMP180 || defined CONFIG_ENABLE_BMP280
"" + ((BMP180IsConnected() || BMP280IsConnected()) ? "<h6>BMP180/280</h6>Temperature " + String(temp, 2) + "C<br/> Pressure " + String(pressure, 2) + "hPa<br/>" : "BMP180/280 Disconnected" ) + ""
#endif
#ifdef CONFIG_ENABLE_DHT
"" + (DHTIsConnected() ? "<h6>DHT</h6>Temperature " + String(dhtTemp, 0) + "C<br/> Humidity " + String(dhtHumidity, 0) + "%<br/>" : "DHT Disconnected" ) + ""
#endif
#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
@ -45,6 +48,9 @@ void WebHandleRoot() {
#endif
#ifdef CONFIG_ENABLE_DRY_SENSOR
"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
"</fieldset>" + gpioControlHTML + gpioObserved + pwmControlHTML + "<fieldset>"
"<legend>Settings</legend>"
@ -119,8 +125,8 @@ void WebHandleSetup() {
"<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=\"gw\">Gateway :</label><br/><input type=\"text\" name=\"gw\" value=\"" + (conf.gw == 0 ? WiFi.gatewayIP().toString() : 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\">DNS :</label><br/><input type=\"text\" name=\"dns\" value=\"" + (conf.dns == 0 ? WiFi.dnsIP().toString() : IPAddress(conf.dns).toString()) + "\" /> </div>"
"<div><label for=\"mask\">DNS2 :</label><br/><input type=\"text\" name=\"dns2\" value=\"" + (conf.dns2 == 0 ? WiFi.dnsIP(1).toString() : IPAddress(conf.dns2).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\">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>"
"</fieldset>"
"<fieldset>"
"<legend>MQTT:</legend>"
@ -129,6 +135,10 @@ void WebHandleSetup() {
"<div><label for=\"mqttPasswd\">Password :</label><br/><input type=\"password\" name=\"mqttPasswd\" style=\"border-color:red\" value=\"" + String(conf.mqttPasswd) + "\" /> </div>"
"<div><label for=\"mqttPort\">Port :</label><br/><input type=\"text\" name=\"mqttPort\" value=\"" + String(conf.mqttPort) + "\" /> (8883 for secure Mqtts) </div>"
"</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>"
"</form>");
}
@ -172,7 +182,7 @@ void WebHandleSave() {
|| !server.hasArg("mqttServer") || !server.hasArg("mqttUser") || !server.hasArg("mqttPasswd")
|| !server.hasArg("mqttPort") || !server.hasArg("ip_config") || !server.hasArg("ip")
|| !server.hasArg("gw") || !server.hasArg("mask") || !server.hasArg("dns")
|| !server.hasArg("dns2") || !server.hasArg("channel") || ! server.hasArg("bssid")) {
|| !server.hasArg("dns2") || !server.hasArg("channel") || !server.hasArg("bssid") || !server.hasArg("samplingPeriod")) {
server.send(500, "text/plain", "Bad arguments\r\n");
return;
}
@ -187,16 +197,28 @@ void WebHandleSave() {
}
}
productConfig newConf = {BOOTMODE_NORMAL, strdup(server.arg("ssid").c_str()), strdup(server.arg("password").c_str()),
strdup(server.arg("host").c_str()), strdup(server.arg("mqttServer").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())
};
productConfig newConf = {BOOTMODE_NORMAL,
strdup(server.arg("ssid").c_str()),
strdup(server.arg("password").c_str()),
strdup(server.arg("host").c_str()),
strdup(server.arg("mqttServer").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) {
WebSendError("Cannot Save Credentials (Too long ?Contains \";\"?)\r\n");
WebSendError("Cannot Save configuration ( Credentials too long ?Contains \";\"?)\r\n");
return;
}
samplingPeriod = newConf.samplingPeriod;
if (WiFi.softAPIP() != IPAddress((uint32_t)0)) {
//In STA mode, we can test the AP connection
WiFi.begin(server.arg("ssid").c_str(), server.arg("password").c_str());

View File

@ -43,6 +43,7 @@
#include "debug_sketch.h"
#include "BMP180.h"
#include "BMP280.h"
#include "BME680.h"
#include "BME680_BSEC.h"
#include "sensor_DHT.h"
@ -52,6 +53,7 @@
#include "Adafruit_MQTT_Client.h"
#include "EEPROM.h"
#include "Teleinfo.h"
#include "SCD4X.h"
extern "C" {
#include <user_interface.h>
@ -66,15 +68,20 @@ double temp, pressure;
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;
uint8_t mode;
int reconfig = 0;
productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL};
productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL, CONFIG_SAMPLING_PERIOD_MS};
// 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;
uint samplingPeriod = CONFIG_SAMPLING_PERIOD_MS;
uint nbCycle = UINT_MAX - 1;
/* Set these to your desired credentials. */
const char *ssid = CONFIG_SSID_NAME;
@ -133,10 +140,7 @@ void WifiSetup(productConfig conf) {
bssidConf = bssid;
}
// 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);
SKETCH_DEBUG_PRINTF("Using channel %u (0==auto)\n", conf.channel );
WiFi.begin(conf.ssid, conf.password, conf.channel, bssidConf);
while (WiFi.status() != WL_CONNECTED) {
delay(50);
@ -145,7 +149,7 @@ void WifiSetup(productConfig conf) {
reconfig = 0;
return;
}
if (connectionTry == 60) {
if (connectionTry == 120) {
SKETCH_DEBUG_PRINTLN("Cannot connect to wifi. Try without BSSID and channel");
WiFi.begin(conf.ssid, conf.password);
}
@ -277,6 +281,9 @@ void setup()
if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)) {
SKETCH_DEBUG_PRINTLN("BMP180 init success");
}
if (!BMP280Setup()) {
SKETCH_DEBUG_PRINTLN("BMP280 init success");
}
if (!DHTSetup(CONFIG_DHT_PIN)) {
SKETCH_DEBUG_PRINTLN("DHT init success");
}
@ -292,14 +299,17 @@ void setup()
if(!TeleinfoSetup()){
SKETCH_DEBUG_PRINTLN("Teleinfo init success");
}
if(!SCD4XSetup()){
SKETCH_DEBUG_PRINTLN("SCD4X init success");
}
WebSetupServer(mode);
}
samplingPeriod = conf.samplingPeriod;
#ifdef CONFIG_ENABLE_POWER_SAVE
wifi_set_sleep_type(LIGHT_SLEEP_T);
#endif
}
uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS;
void loop() {
if (mode == BOOTMODE_OTA) {
ArduinoOTA.handle();
@ -311,19 +321,31 @@ void loop() {
}
delay(CONFIG_WEB_DELAY_MS);
TeleinfoRetrieve(teleIinst, telePapp, teleBase);
nbCycle++;
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
if (nbCycle > samplingPeriod / CONFIG_WEB_DELAY_MS) {
std::vector<struct mqttInfo> batchInfo;
if (!BMP180GetTempAndPressure(temp, pressure)) {
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});
}
if (!BMP280GetTempAndPressure(temp, pressure)) {
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});
}
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
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});
}
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});
@ -364,9 +386,13 @@ void loop() {
batchInfo.push_back({bme680BSECIaq, BME680_IAQ_FEED_FORMAT, 0, 0});
batchInfo.push_back({bme680BSECIaqAcc, BME680_IAQ_ACC_FEED_FORMAT, 0, 0});
}
TeleinfoProcess(batchInfo);
TeleinfoRetrieve(teleIinst, telePapp, teleBase, batchInfo);
if (mode == BOOTMODE_NORMAL)
MqttBatchPublish(batchInfo, conf.mqttUser, conf.host);
if (MqttBatchPublish(batchInfo, conf.mqttUser, conf.host))
WifiSetup(conf);
nbCycle = 0;
}
}

View File

@ -9,8 +9,8 @@
#define CONFIG_WEB_DELAY_MS 100
#endif
#ifndef CONFIG_SAMPLING_PERIODE_MS
#define CONFIG_SAMPLING_PERIODE_MS 60000
#ifndef CONFIG_SAMPLING_PERIOD_MS
#define CONFIG_SAMPLING_PERIOD_MS 60000
#endif
#if defined(CONFIG_ENABLE_BME680) && defined(CONFIG_BME680_BSEC_ENABLE)

View File

@ -30,11 +30,15 @@
//#define CONFIG_ENABLE_DHT
//#define CONFIG_DHT_PIN 2
//#define CONFIG_DHT_TYPE DHT22
//#define CONFIG_ENABLE_BMP280
//#define CONFIG_ENABLE_DRY_SENSOR
//If the dry sensor is powered by a GPIO, this GPIO could be defined here
//#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)
#define CONFIG_ENABLE_POWER_SAVE
@ -44,7 +48,7 @@
// Long press on this button will put device in setup mode at runtime
#define CONFIG_SETUP_BUTTON 0
// Teleinfo https://github.com/hallard/LibTeleinfo/
// Teleinfo https://github.com/hallard/LibTeleinfo/
#define CONFIG_ENABLE_TELEINFO
/* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */
@ -52,10 +56,10 @@
#define CONFIG_SETUP_GPIO 14 //D5
// Time to sleep between 2 webserver request (increase it reduce battery usage but increase latency)
//#define CONFIG_WEB_DELAY_MS 100
#define CONFIG_WEB_DELAY_MS 800
// Get sensors value every X ms
#define CONFIG_SAMPLING_PERIODE_MS 30000
#define CONFIG_SAMPLING_PERIOD_MS 30000
// Name of the SSID when in AP mode for configuration
#define CONFIG_SSID_NAME "ESPConfiguratorBureau"