Browse Source

Use template for MqttPublish functions

And rename Mqtt*Publish function into MqttPublish*
gpiosensors
Mathieu Maret 5 years ago
parent
commit
6f6c0f7525
  1. 18
      WifiControlSensor/MQTT.h
  2. 40
      WifiControlSensor/MQTT.ino
  3. 8
      WifiControlSensor/WifiControlSensor.ino

18
WifiControlSensor/MQTT.h

@ -6,10 +6,11 @@ Adafruit_MQTT_Publish *MqttCreatePublisher(const char *fmt, ...);
int MqttConnect();
int MqttIsConnected();
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname);
int MqttPublish(double temp, double pressure);
int MqttDhtPublish(float temp, float humidity);
int MqttDryPublish(int dry);
int MqttIPPublish(const String &ip);
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);
void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value);
@ -18,10 +19,11 @@ bool MqttIsConfigured();
int MqttConnect(){return 0;}
int MqttIsConnected(){return 0;}
int MqttSetup(char *server, char *user, char *passwd, int port, char * hostname){return 0;}
int MqttPublish(double temp, double pressure){return 0;}
int MqttDhtPublish(float temp, float humidity){return 0;}
int MqttDryPublish(int dry){return 0;}
int MqttIPPublish(const String &ip){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;}
void MqttCheckSubscription(){}
void MqttChangeGpioValue(int gpio, int value){}
void MqttChangePWMValue(int gpio, int value){}

40
WifiControlSensor/MQTT.ino

@ -127,38 +127,28 @@ int MqttConnect() {
return 0;
}
int MqttPublish(double temp, double pressure) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing !");
mqtt_temp->publish(temp);
mqtt_pressure->publish(pressure);
template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){
if(MqttConnect() == 0){
publisher->publish(value);
return 0;
}
return 0;
return -1;
}
int MqttDryPublish(int dry) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing dry!");
mqtt_dry->publish((dry*100)/1024);
}
return 0;
int MqttPublishBMP180(double temp, double pressure) {
return MqttPublish(mqtt_temp, temp) + MqttPublish(mqtt_pressure, pressure);
}
int MqttIPPublish(const String &ip) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing IP!");
mqtt_ip->publish(ip.c_str());
}
return 0;
int MqttPublishDry(int dry) {
return MqttPublish(mqtt_dry, (dry*100)/1024);
}
int MqttDhtPublish(float temp, float humidity) {
if (MqttConnect() == 0) {
SKETCH_DEBUG_PRINTLN("publishing DHT!");
mqtt_dht_temp->publish(temp);
mqtt_dht_humidity->publish(humidity);
}
return 0;
int MqttPublishIP(const String &ip) {
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) {

8
WifiControlSensor/WifiControlSensor.ino

@ -238,7 +238,7 @@ void setup() {
WifiSetup(mode, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2);
if (mode == BOOTMODE_NORMAL) {
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
MqttIPPublish(WiFi.localIP().toString());
MqttPublishIP(WiFi.localIP().toString());
}
if (mode == BOOTMODE_OTA) {
@ -281,7 +281,7 @@ void loop() {
SKETCH_DEBUG_PRINT(" Pressure mB ");
SKETCH_DEBUG_PRINTLN(pressure);
if (mode == BOOTMODE_NORMAL)
MqttPublish(temp, pressure);
MqttPublishBMP180(temp, pressure);
}
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
SKETCH_DEBUG_PRINT("Current T°C ");
@ -289,14 +289,14 @@ void loop() {
SKETCH_DEBUG_PRINT(" Humidity ");
SKETCH_DEBUG_PRINTLN(dhtHumidity);
if (mode == BOOTMODE_NORMAL)
MqttDhtPublish(dhtTemp, dhtHumidity);
MqttPublishDHT(dhtTemp, dhtHumidity);
}
if (!DryGetMeasure(dryness)){
SKETCH_DEBUG_PRINT("Current dryness ");
SKETCH_DEBUG_PRINT((dryness*100)/1024);
SKETCH_DEBUG_PRINTLN("%");
if (mode == BOOTMODE_NORMAL)
MqttDryPublish(dryness);
MqttPublishDry(dryness);
}
nbCycle = 0;
}

Loading…
Cancel
Save