diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index 081621e..1e8210e 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -4,47 +4,39 @@ Adafruit_MQTT_Client *mqtt; Adafruit_MQTT_Publish *mqtt_temp; Adafruit_MQTT_Publish *mqtt_pressure; -Adafruit_MQTT_Subscribe *pumpButton; +#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0])) const char TEMPERATURE_FEED[] = "/feeds/temperature"; const char PRESSURE_FEED[] = "/feeds/pressure"; +// Should have less that MAXSUBSCRIPTIONS elements +// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h +const int gpioWatched[] = {2}; + +#define GPIO_BASE "/feeds/gpio/" +const char *GPIO_FEED[] = { GPIO_BASE"2"}; +const char *GPIO_SET_FEED[] = { GPIO_BASE"2/set"}; + +Adafruit_MQTT_Publish * mqttGpio[MAXSUBSCRIPTIONS] = {}; + const char PUMP_CMD[] = "/feeds/pump/set"; boolean mqttIsConfigured = false; -int publishMQTT(double temp, double pressure) { - if (MQTT_connect() == 0) { - Serial.println("publishing !"); - mqtt_temp->publish(temp); - mqtt_pressure->publish(pressure); - } - return 0; -} - -char *mqttGetSubData() { - return (char *)pumpButton->lastread; -} - -Adafruit_MQTT_Subscribe *subscription; -bool mqttSubAvailable() { - if (MQTT_connect() == 0) { - subscription = mqtt->readSubscription(0); - if (subscription == pumpButton) { - return true; - } - } - return false; -} int setupMQTT(char *server, char *user, char *passwd, int port) { // Setup the MQTT client class by passing in the WiFi client and MQTT server and login details. mqtt = new Adafruit_MQTT_Client(&client, server, port, user, passwd); mqtt_temp = new Adafruit_MQTT_Publish(mqtt, TEMPERATURE_FEED); mqtt_pressure = new Adafruit_MQTT_Publish(mqtt, PRESSURE_FEED); - pumpButton = new Adafruit_MQTT_Subscribe(mqtt, PUMP_CMD); + for (int i = 0 ; i < NB_ELEMENTS(gpioWatched); i++) { + Adafruit_MQTT_Subscribe *gpioSet = new Adafruit_MQTT_Subscribe(mqtt, GPIO_SET_FEED[i]); + mqtt->subscribe(gpioSet); + + Adafruit_MQTT_Publish *gpio = new Adafruit_MQTT_Publish(mqtt, GPIO_FEED[i]); + mqttGpio[i] = gpio; + } - mqtt->subscribe(pumpButton); return 0; } @@ -75,3 +67,56 @@ int MQTT_connect() { } return 0; } + +int publishMQTT(double temp, double pressure) { + if (MQTT_connect() == 0) { + Serial.println("publishing !"); + mqtt_temp->publish(temp); + mqtt_pressure->publish(pressure); + } + return 0; +} + +int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription) { + if(!strstr(subscription->topic, GPIO_BASE)) + return -1; + String gpioStr(subscription->topic+strlen(GPIO_BASE)); + int idx = gpioStr.indexOf("/"); + int gpio = gpioStr.substring(idx).toInt(); + + if(gpio >= 0 && gpio < 32 ) + return gpio; + else + return -1; +} + +bool isGpioWatched(int gpio) { + return false; +} + +void checkMqttSubscription() { + if (MQTT_connect() == 0) { + Adafruit_MQTT_Subscribe *subscription; + while (subscription = mqtt->readSubscription(0)) { + int gpio = getGpioFromSubscription(subscription); + if (gpio > 0 && isGpioWatched(gpio)) { + char *value = (char *) subscription->lastread; + } + } + } +} + +//char *mqttGetSubData() { +// return (char *)pumpButton->lastread; +//} +// +//Adafruit_MQTT_Subscribe *subscription; +//bool mqttSubAvailable() { +// if (MQTT_connect() == 0) { +// subscription = mqtt->readSubscription(0); +// if (subscription == pumpButton) { +// return true; +// } +// } +// return false; +//} diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index ea0a7e1..0e3323e 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -211,10 +211,10 @@ void loop() { ArduinoOTA.handle(); } else { server.handleClient(); - if(mqttSubAvailable()){ - Serial.println("Mqtt sub available: "); - Serial.println(mqttGetSubData()); - } +// if(mqttSubAvailable()){ +// Serial.println("Mqtt sub available: "); +// Serial.println(mqttGetSubData()); +// } delay(WEB_DELAY_MS); nbCycle++;