Use template for MqttPublish functions

And rename Mqtt*Publish function into MqttPublish*
This commit is contained in:
Mathieu Maret 2016-11-16 16:59:17 +01:00
parent 3d82e2a200
commit 6f6c0f7525
3 changed files with 29 additions and 37 deletions

View File

@ -6,10 +6,11 @@ Adafruit_MQTT_Publish *MqttCreatePublisher(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);
int MqttPublish(double temp, double pressure); template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value);
int MqttDhtPublish(float temp, float humidity); int MqttPublishBMP180(double temp, double pressure);
int MqttDryPublish(int dry); int MqttPublishDHT(float temp, float humidity);
int MqttIPPublish(const String &ip); int MqttPublishDry(int dry);
int MqttPublishIp(const String &ip);
void MqttCheckSubscription(); void MqttCheckSubscription();
void MqttChangeGpioValue(int gpio, int value); void MqttChangeGpioValue(int gpio, int value);
void MqttChangePWMValue(int gpio, int value); void MqttChangePWMValue(int gpio, int value);
@ -18,10 +19,11 @@ bool MqttIsConfigured();
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;}
int MqttPublish(double temp, double pressure){return 0;} template<typename T> int MqttPublish(Adafruit_MQTT_Publish *publisher, T value){return 0;}
int MqttDhtPublish(float temp, float humidity){return 0;} int MqttPublishBMP180(double temp, double pressure){return 0;}
int MqttDryPublish(int dry){return 0;} int MqttPublishDHT(float temp, float humidity){return 0;}
int MqttIPPublish(const String &ip){return 0;} int MqttPublishDry(int dry){return 0;}
int MqttPublishIP(const String &ip){return 0;}
void MqttCheckSubscription(){} void MqttCheckSubscription(){}
void MqttChangeGpioValue(int gpio, int value){} void MqttChangeGpioValue(int gpio, int value){}
void MqttChangePWMValue(int gpio, int value){} void MqttChangePWMValue(int gpio, int value){}

View File

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

View File

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