Compare commits

...

6 Commits

Author SHA1 Message Date
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
1b52a80b15 Fix teleinfo configuration 2022-09-10 21:53:06 +02:00
Mathieu Maret
8f471fc0bf WIP: working teleinfo 2021-02-18 23:50:54 +01:00
Mathieu Maret
8fed8bedf4 Add missing files 2021-02-18 15:51:49 +01:00
Mathieu Maret
9110ae3589 Add teleinfo module 2021-02-18 00:25:45 +01:00
8 changed files with 141 additions and 16 deletions

View File

@ -18,6 +18,9 @@
#define BME680_ALT_FEED_FORMAT "/feeds/%s/%s/bme680/alt" #define BME680_ALT_FEED_FORMAT "/feeds/%s/%s/bme680/alt"
#define BME680_IAQ_FEED_FORMAT "/feeds/%s/%s/bme680/iaq" #define BME680_IAQ_FEED_FORMAT "/feeds/%s/%s/bme680/iaq"
#define BME680_IAQ_ACC_FEED_FORMAT "/feeds/%s/%s/bme680/iaqAcc" #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"
#ifndef CONFIG_DISABLE_MQTT #ifndef CONFIG_DISABLE_MQTT
#include "Adafruit_MQTT.h" #include "Adafruit_MQTT.h"

View File

@ -197,14 +197,17 @@ 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));
} }
@ -212,7 +215,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

@ -0,0 +1,21 @@
#pragma once
#include "debug_sketch.h"
#ifdef CONFIG_ENABLE_TELEINFO
#include <LibTeleinfo.h>
int TeleinfoSetup();
int TeleinfoProcess(std::vector<struct mqttInfo> &batchInfo);
int TeleinfoProcess(float &iinst, float &papp, float &base);
#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 &, float &, float &){
return 0;
};
#endif

View File

@ -0,0 +1,70 @@
#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 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 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) {
base = atof(me->value);
}
me = me->next;
}
return 0;
}
#endif

View File

@ -51,6 +51,7 @@
#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"
extern "C" { extern "C" {
#include <user_interface.h> #include <user_interface.h>
@ -68,7 +69,7 @@ float bme680BSECT, bme680BSECP, bme680BSECH, bme680BSECIaq, bme680BSECIaqAcc;
int dryness; int dryness;
uint8_t mode; uint8_t mode;
int reconfig = 0; int reconfig = 0;
productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 1883, 0, 0, 0, 0, 0, 0, 0, ""}; productConfig conf = {BOOTMODE_SETUP, NULL, NULL, NULL, NULL, NULL, NULL, 1883, 0, 0, 0, 0, 0, 0, 0, NULL};
// 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;
@ -215,7 +216,8 @@ 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
@ -226,11 +228,15 @@ void setup() {
SKETCH_DEBUG_PRINTLN(); SKETCH_DEBUG_PRINTLN();
// Get GPIO 3 Status // Get GPIO 3 Status
Serial.swap(); //Switch Serial on GPIO 13 & 15 #if CONFIG_SERIAL_SHOULD_SWAP
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);
#ifndef CONFIG_ENABLE_EXTRA_GPIO #if !defined(CONFIG_ENABLE_EXTRA_GPIO) && CONFIG_SERIAL_SHOULD_SWAP
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);
@ -280,6 +286,9 @@ void setup() {
if(!BME680BSECSetup()){ if(!BME680BSECSetup()){
SKETCH_DEBUG_PRINTLN("BME680 with BSEC init success"); SKETCH_DEBUG_PRINTLN("BME680 with BSEC init success");
} }
if(!TeleinfoSetup()){
SKETCH_DEBUG_PRINTLN("Teleinfo init success");
}
WebSetupServer(mode); WebSetupServer(mode);
} }
#ifdef CONFIG_ENABLE_POWER_SAVE #ifdef CONFIG_ENABLE_POWER_SAVE
@ -352,7 +361,7 @@ void loop() {
batchInfo.push_back({bme680BSECIaq, BME680_IAQ_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}); batchInfo.push_back({bme680BSECIaqAcc, BME680_IAQ_ACC_FEED_FORMAT, 0, 0});
} }
TeleinfoProcess(batchInfo);
if (mode == BOOTMODE_NORMAL) if (mode == BOOTMODE_NORMAL)
MqttBatchPublish(batchInfo, conf.mqttUser, conf.host); MqttBatchPublish(batchInfo, conf.mqttUser, conf.host);
nbCycle = 0; nbCycle = 0;

View File

@ -64,9 +64,24 @@
#endif #endif
#ifndef CONFIG_SETUP_GPIO #ifndef CONFIG_SETUP_GPIO
#define CONFIG_SETUP_GPIO 3 #define CONFIG_SETUP_GPIO 14
#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

@ -15,7 +15,7 @@
// Enable the temperature, pressure, humidity and gaz Sensor BME680 on standard i2c esp8266 pins // 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 // 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 ) // 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_ENABLE
#define CONFIG_BME680_BSEC_I2C_ADDR 0x77 #define CONFIG_BME680_BSEC_I2C_ADDR 0x77
// Enable the temperature, pressure, humidity and gaz Sensor BME680 on standard i2c esp8266 pins // Enable the temperature, pressure, humidity and gaz Sensor BME680 on standard i2c esp8266 pins
@ -44,6 +44,8 @@
// 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
@ -53,7 +55,7 @@
//#define CONFIG_WEB_DELAY_MS 100 //#define CONFIG_WEB_DELAY_MS 100
// Get sensors value every X ms // Get sensors value every X ms
#define CONFIG_SAMPLING_PERIODE_MS 3000 #define CONFIG_SAMPLING_PERIODE_MS 30000
// 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,13 +2,15 @@
//#define CONFIG_SKETCH_DEBUG //#define CONFIG_SKETCH_DEBUG
// Set where debug messages will be printed. // Set where debug messages will be printed.
#define DEBUG_PRINTER Serial #ifndef DEBUG_PRINTER_WIFICONTROLSENSOR
#define DEBUG_PRINTER_WIFICONTROLSENSOR Serial
#endif
#ifdef CONFIG_SKETCH_DEBUG #ifdef CONFIG_SKETCH_DEBUG
#define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); } #define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER_WIFICONTROLSENSOR.begin(speed); }
#define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } #define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.print(__VA_ARGS__); }
#define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); } #define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.printf(__VA_ARGS__); }
#define SKETCH_DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); } #define SKETCH_DEBUG_PRINTLN(...) { DEBUG_PRINTER_WIFICONTROLSENSOR.println(__VA_ARGS__); }
#else #else
#define SKETCH_DEBUG_INIT(speed) #define SKETCH_DEBUG_INIT(speed)
#define SKETCH_DEBUG_PRINT(...) {} #define SKETCH_DEBUG_PRINT(...) {}