From 1a7b3ec53aa358981b7d63ec9b0cac91a8b8d738 Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 16 Sep 2016 11:42:33 +0200 Subject: [PATCH] Use Array to define GPIO controlled by Web --- WifiControlSensor/MQTT.ino | 2 +- WifiControlSensor/WebServer.ino | 25 ++++++++++++++----------- WifiControlSensor/WifiControlSensor.ino | 1 + WifiControlSensor/config.h | 4 ++++ WifiControlSensor/config_device.h | 2 +- WifiControlSensor/utils.h | 6 ++++++ 6 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 WifiControlSensor/utils.h diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index 2191aff..7b9afba 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -1,4 +1,5 @@ #include +#include "utils.h" #include "MQTT.h" // Create an ESP8266 WiFiClient class to connect to the MQTT server. @@ -11,7 +12,6 @@ Adafruit_MQTT_Publish *mqtt_dht_humidity; Adafruit_MQTT_Publish *mqtt_dry; Adafruit_MQTT_Publish *mqtt_ip; -#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0])) #define FEED_MAX_SIZE 96 //FEED have the following formats /feeds/USER/DEVICE_NAME/.... diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index e278922..4085026 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -1,7 +1,18 @@ -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) +const int gpioWebConf[] = CONFIG_WEB_CONTROLLED_GPIO; void WebHandleRoot() { + String gpioWeb = ""; + + if (NB_ELEMENTS(gpioWebConf) > 0){ + gpioWeb += "
" + "Relay"; + for (uint i = 0 ; i < NB_ELEMENTS(gpioWebConf) ; i++) { + gpioWeb += "Relay " + String(gpioWebConf[i]) + " " + "ON/"; + gpioWeb += "OFF
"; + } + gpioWeb += "
"; + } + server.send(200, "text/html", "" "

You are connected to " + String(hostName) + "

" @@ -18,15 +29,7 @@ void WebHandleRoot() { #ifdef CONFIG_ENABLE_DRY_SENSOR "Dryness " + String((dryness*100)/1024) + "%
" #endif - "" -#ifdef CONFIG_WEB_CONTROLLED_GPIO - "
" - "Relay" - "Relay ON
" - "Relay OFF
" - "
" -#endif - "
" + "
" + gpioWeb + "
" "Settings" "Enter Setup
" "Put device in OTA mode
" diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 8e9db61..146c562 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -38,6 +38,7 @@ #include #include "config.h" +#include "utils.h" #include "debug_sketch.h" #include "BMP180.h" diff --git a/WifiControlSensor/config.h b/WifiControlSensor/config.h index fb70dd1..df3fdcb 100644 --- a/WifiControlSensor/config.h +++ b/WifiControlSensor/config.h @@ -39,6 +39,10 @@ #define CONFIG_SSID_NAME "ESPConfigurator" #endif +#ifndef CONFIG_WEB_CONTROLLED_GPIO +#define CONFIG_WEB_CONTROLLED_GPIO {} +#endif + #ifndef CONFIG_MQTT_CONTROLLED_GPIO #define CONFIG_MQTT_CONTROLLED_GPIO {} #endif diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h index 0e3481b..4119d6c 100644 --- a/WifiControlSensor/config_device.h +++ b/WifiControlSensor/config_device.h @@ -26,7 +26,7 @@ #define CONFIG_ENABLE_MDNS //Web controlled GPIO -#define CONFIG_WEB_CONTROLLED_GPIO 2 +#define CONFIG_WEB_CONTROLLED_GPIO {2} /* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */ diff --git a/WifiControlSensor/utils.h b/WifiControlSensor/utils.h new file mode 100644 index 0000000..0512751 --- /dev/null +++ b/WifiControlSensor/utils.h @@ -0,0 +1,6 @@ +#pragma once + +#define NB_ELEMENTS(x) (sizeof(x)/ sizeof(x[0])) + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x)