From 13c2d23330c2f2e928046b97313e4579f122ab18 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 9 Dec 2016 22:55:12 +0100 Subject: [PATCH] All controlled gpio have a single config entry They get be set/get by mqtt and set by http --- WifiControlSensor/MQTT.ino | 18 +++++++++--------- WifiControlSensor/WebServer.ino | 2 +- WifiControlSensor/config.h | 4 ++-- WifiControlSensor/config_device.h | 13 +++++-------- 4 files changed, 17 insertions(+), 20 deletions(-) diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index f3fcb2b..a545e55 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -29,7 +29,7 @@ Adafruit_MQTT_Publish *mqttPwm[MAXSUBSCRIPTIONS] = {}; // Should have less that MAXSUBSCRIPTIONS elements // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h -const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO; +const int gpioControlled[] = CONFIG_CONTROLLED_GPIO; const int pwmWatched[] = CONFIG_CONTROLLED_PWM; char *mqttId; @@ -60,18 +60,18 @@ int MqttSetup(char *server, char *user, char *passwd, int port, char *hostname) mqtt_dry = MqttCreatePublisher(DRY_FEED_FORMAT, user, mqttId); mqtt_ip = MqttCreatePublisher(IP_FEED_FORMAT, user, mqttId); - if( NB_ELEMENTS(gpioWatched) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){ + if( NB_ELEMENTS(gpioControlled) + NB_ELEMENTS(pwmWatched) > MAXSUBSCRIPTIONS){ SKETCH_DEBUG_PRINTF("Too much gpio/pwm to control\n Nb gpio %d Nb pwm %d Max is %d", - NB_ELEMENTS(gpioWatched), NB_ELEMENTS(pwmWatched), MAXSUBSCRIPTIONS); + NB_ELEMENTS(gpioControlled), NB_ELEMENTS(pwmWatched), MAXSUBSCRIPTIONS); return -1; } - for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) { - mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioWatched[i])); - mqttGpio[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioWatched[i]); + for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) && i < MAXSUBSCRIPTIONS; i++) { + mqtt->subscribe(MqttCreateSubscribe(GPIO_SET_FEED_FORMAT, user, mqttId, gpioControlled[i])); + mqttGpio[i] = MqttCreatePublisher(GPIO_FEED_FORMAT, user, mqttId, gpioControlled[i]); } - for (uint i = 0 ; i < NB_ELEMENTS(gpioWatched) && i < MAXSUBSCRIPTIONS; i++) { + for (uint i = 0 ; i < NB_ELEMENTS(gpioControlled) && i < MAXSUBSCRIPTIONS; i++) { mqtt->subscribe(MqttCreateSubscribe(PWM_SET_FEED_FORMAT, user, mqttId, pwmWatched[i])); mqttPwm[i] = MqttCreatePublisher(PWM_FEED_FORMAT, user, mqttId, pwmWatched[i]); } @@ -166,7 +166,7 @@ int getGpioFromSubscription(Adafruit_MQTT_Subscribe *subscription, const char *p } void MqttNofity(int gpio, int value){ - int watchIdx = findIndex(gpio, gpioWatched); + int watchIdx = findIndex(gpio, gpioControlled); if (watchIdx >= 0 && isMqttConfigured) { mqttGpio[watchIdx]->publish(value); } @@ -188,7 +188,7 @@ void MqttCheckSubscription() { Adafruit_MQTT_Subscribe *subscription; while ((subscription = mqtt->readSubscription(0))) { int gpio = getGpioFromSubscription(subscription, "/gpio/"); - if (gpio > 0 && findIndex(gpio, gpioWatched) >= 0) { + if (gpio > 0 && findIndex(gpio, gpioControlled) >= 0) { SKETCH_DEBUG_PRINTF("Got Subscription for GPIO %d\n", gpio); char *value = (char *) subscription->lastread; SKETCH_DEBUG_PRINTF("Receive data: %s\n", value); diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index f9a0335..3cc32cd 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -1,5 +1,5 @@ #ifndef CONFIG_DISABLE_WEB -const int gpioWebConf[] = CONFIG_WEB_CONTROLLED_GPIO; +const int gpioWebConf[] = CONFIG_CONTROLLED_GPIO; const int pwmWebConf[] = CONFIG_CONTROLLED_PWM; String gpioControlHTML = ""; diff --git a/WifiControlSensor/config.h b/WifiControlSensor/config.h index 27b4d02..0e3b5a5 100644 --- a/WifiControlSensor/config.h +++ b/WifiControlSensor/config.h @@ -47,8 +47,8 @@ #define CONFIG_WEB_CONTROLLED_GPIO {} #endif -#ifndef CONFIG_MQTT_CONTROLLED_GPIO -#define CONFIG_MQTT_CONTROLLED_GPIO {} +#ifndef CONFIG_CONTROLLED_GPIO +#define CONFIG_CONTROLLED_GPIO {} #endif #ifndef CONFIG_EEPROM_SIZE diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h index 71e44aa..3b8a59e 100644 --- a/WifiControlSensor/config_device.h +++ b/WifiControlSensor/config_device.h @@ -31,12 +31,6 @@ // Disable mDNS can also save power #define CONFIG_ENABLE_MDNS -// Web controlled GPIO -#define CONFIG_WEB_CONTROLLED_GPIO {2} - -// GPIO used in PWM -#define CONFIG_CONTROLLED_PWM {} - // Long press on this button will put device in setup mode at runtime #define CONFIG_SETUP_BUTTON 0 @@ -54,9 +48,12 @@ // Name of the SSID when in AP mode for configuration //#define CONFIG_SSID_NAME "ESPConfigurator" -// GPIO that can be set or get by mqtt +// GPIO that can be set or get by mqtt and set via http // Should have less value than MAXSUBSCRIPTIONS -#define CONFIG_MQTT_CONTROLLED_GPIO {2,13} +#define CONFIG_CONTROLLED_GPIO {2,13} + +// GPIO used in PWM +//#define CONFIG_CONTROLLED_PWM {} // EEPROM SIZE // Max is 4096, but this amount will be allocated in RAM for reading its content