Mqtt: use batch publish to generalize code
And fix indentation
This commit is contained in:
parent
eab447abc6
commit
caf88dcd98
@ -1,15 +1,33 @@
|
|||||||
#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"
|
||||||
#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;
|
||||||
|
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(char *server, char *user, char *passwd, int port, 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();
|
||||||
@ -18,13 +36,11 @@ 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(char *server, char *user, char *passwd, int port, 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(){}
|
||||||
|
@ -6,11 +6,6 @@
|
|||||||
#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] = {};
|
||||||
@ -19,18 +14,6 @@ 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;
|
char *mqttId;
|
||||||
|
|
||||||
bool isMqttConfigured = false;
|
bool isMqttConfigured = false;
|
||||||
@ -52,11 +35,6 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname)
|
|||||||
#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_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);
|
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) {
|
||||||
@ -97,6 +75,26 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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, ...) {
|
Adafruit_MQTT_Subscribe *MqttCreateSubscribe(const char *fmt, ...) {
|
||||||
char buf[FEED_MAX_SIZE];
|
char buf[FEED_MAX_SIZE];
|
||||||
va_list args;
|
va_list args;
|
||||||
@ -145,22 +143,10 @@ 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)
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
#include <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
#include <ESP8266WebServer.h>
|
#include <ESP8266WebServer.h>
|
||||||
@ -289,29 +289,23 @@ void loop() {
|
|||||||
|
|
||||||
nbCycle++;
|
nbCycle++;
|
||||||
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / 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_PRINT("Current T°C ");
|
SKETCH_DEBUG_PRINTF("Current %f°C Pressure %fmB\n", temp, pressure);
|
||||||
SKETCH_DEBUG_PRINT(temp);
|
batchInfo.push_back({(float)temp, TEMPERATURE_FEED_FORMAT, 0, 0});
|
||||||
SKETCH_DEBUG_PRINT(" Pressure mB ");
|
batchInfo.push_back({(float)pressure, PRESSURE_FEED_FORMAT, 0, 0});
|
||||||
SKETCH_DEBUG_PRINTLN(pressure);
|
|
||||||
if (mode == BOOTMODE_NORMAL)
|
|
||||||
MqttPublishBMP180(temp, pressure);
|
|
||||||
}
|
}
|
||||||
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
|
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
|
||||||
SKETCH_DEBUG_PRINT("Current T°C ");
|
SKETCH_DEBUG_PRINTF("Current %f°C %f%% Humidity\n", dhtTemp, dhtHumidity);
|
||||||
SKETCH_DEBUG_PRINT(dhtTemp);
|
batchInfo.push_back({dhtTemp, TEMPERATURE_DHT_FEED_FORMAT, 0, 0});
|
||||||
SKETCH_DEBUG_PRINT(" Humidity ");
|
batchInfo.push_back({dhtHumidity, HUMIDITY_DHT_FEED_FORMAT, 0, 0});
|
||||||
SKETCH_DEBUG_PRINTLN(dhtHumidity);
|
|
||||||
if (mode == BOOTMODE_NORMAL)
|
|
||||||
MqttPublishDHT(dhtTemp, dhtHumidity);
|
|
||||||
}
|
}
|
||||||
if (!DryGetMeasure(dryness)) {
|
if (!DryGetMeasure(dryness)) {
|
||||||
SKETCH_DEBUG_PRINT("Current dryness ");
|
SKETCH_DEBUG_PRINTF("Current dryness %d %%\n", (dryness * 100) / 1024);
|
||||||
SKETCH_DEBUG_PRINT((dryness*100)/1024);
|
batchInfo.push_back({dryness, DRY_FEED_FORMAT, 0, 0});
|
||||||
SKETCH_DEBUG_PRINTLN("%");
|
|
||||||
if (mode == BOOTMODE_NORMAL)
|
|
||||||
MqttPublishDry(dryness);
|
|
||||||
}
|
}
|
||||||
|
if (mode == BOOTMODE_NORMAL)
|
||||||
|
MqttBatchPublish(batchInfo, conf.mqttUser, conf.host);
|
||||||
nbCycle = 0;
|
nbCycle = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user