From efe135a80176d8a34f3423b3d7d25f21fed314cd Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 30 Mar 2016 00:44:37 +0200 Subject: [PATCH] Move configuration into a dedicated file --- WifiControlSensor/BMP180.h | 4 +-- WifiControlSensor/BMP180.ino | 2 +- WifiControlSensor/MQTT.ino | 2 +- WifiControlSensor/WebServer.ino | 13 ++++--- WifiControlSensor/WifiControlSensor.ino | 47 ++++++++++++++----------- WifiControlSensor/config.h | 29 +++++++++++++++ WifiControlSensor/config_device.h | 28 +++++++++++++++ WifiControlSensor/debug_sketch.h | 4 +-- 8 files changed, 98 insertions(+), 31 deletions(-) create mode 100644 WifiControlSensor/config.h create mode 100644 WifiControlSensor/config_device.h diff --git a/WifiControlSensor/BMP180.h b/WifiControlSensor/BMP180.h index 3c01d56..d7d79d7 100644 --- a/WifiControlSensor/BMP180.h +++ b/WifiControlSensor/BMP180.h @@ -1,5 +1,5 @@ #pragma once -#ifdef ENABLE_BMP180 +#ifdef CONFIG_ENABLE_BMP180 #include #include "debug_sketch.h" @@ -13,7 +13,7 @@ int BMP180GetTempAndPressure(double &t, double &p); int BMP180Setup(int sda, int scl); bool BMP180IsConnected(); -#else //ENABLE_BMP80 +#else //CONFIG_ENABLE_BMP80 int BMP180GetTemperature(double &t){return 0;}; int BMP180GetTempAndPressure(double &t, double &p){return 0;}; int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;}; diff --git a/WifiControlSensor/BMP180.ino b/WifiControlSensor/BMP180.ino index 6320224..d51eac4 100644 --- a/WifiControlSensor/BMP180.ino +++ b/WifiControlSensor/BMP180.ino @@ -1,4 +1,4 @@ -#ifdef ENABLE_BMP180 +#ifdef CONFIG_ENABLE_BMP180 #include "BMP180.h" int BMP180Setup(int sda, int scl) { bmp180Connected = bmp180.begin(sda, scl); diff --git a/WifiControlSensor/MQTT.ino b/WifiControlSensor/MQTT.ino index d35a78a..691a3cd 100644 --- a/WifiControlSensor/MQTT.ino +++ b/WifiControlSensor/MQTT.ino @@ -14,7 +14,7 @@ char pressureFeed[FEED_MAX_SIZE] = {}; // Should have less that MAXSUBSCRIPTIONS elements // MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h -const int gpioWatched[] = {2, 13}; +const int gpioWatched[] = CONFIG_MQTT_CONTROLLED_GPIO; #define GPIO_FEED_FORMAT "/feeds/%s/gpio/%d" #define GPIO_SET_FEED_FORMAT "/feeds/%s/gpio/%d/set" diff --git a/WifiControlSensor/WebServer.ino b/WifiControlSensor/WebServer.ino index 590253f..0d7382f 100644 --- a/WifiControlSensor/WebServer.ino +++ b/WifiControlSensor/WebServer.ino @@ -1,6 +1,9 @@ +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) + void WebHandleRoot() { server.send(200, "text/html", - "" + "" "

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

" "
" "Sensors" @@ -8,10 +11,12 @@ void WebHandleRoot() { "Pressure " + String(pressure, 2) + "mB
" "
" "
" - "Pump" - "Power PUMP ON
" - "Power PUMP OFF
" +#ifdef CONFIG_WEB_CONTROLLED_GPIO + "Relay" + "Relay ON
" + "Relay OFF
" "
" +#endif "
" "Settings" "Enter Setup
" diff --git a/WifiControlSensor/WifiControlSensor.ino b/WifiControlSensor/WifiControlSensor.ino index 5317345..15d63ac 100644 --- a/WifiControlSensor/WifiControlSensor.ino +++ b/WifiControlSensor/WifiControlSensor.ino @@ -1,16 +1,32 @@ +/* --------------------- CONFIG ---------------------------------- */ +/* Adapt this sketch to your needs by modifying the config_device.h*/ +/* --------------------- GPIO ------------------------------------ */ /* Generic GPIO Control by HTTP REST interface */ /* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */ +/* -------------------- SETUP ------------------------------------ */ /* At first boot it creates a WiFi access point */ /* and provide a web server on it, so you can configure Wifi ssid */ -/* Wifi passwd, and a mDNS hostname */ +/* Wifi passwd, and a mDNS HOSTNAME and a mqtt server */ /* In this mode, device will be available at 192.168.4.1 */ +/* --------------------- OTA ------------------------------------- */ /* Device can also be put in OTA Mode: In this case, if you have */ /* a little flash (512K), it better to disable SPIFFS in */ -/* "Flash Size" Menu. Use espota.py to upload OTA */ +/* "Flash Size" Menu to save some place for the sketch to upload */ +/* Use espota.py to upload OTA */ /* After passing in OTA mode, next boot will be in setup mode */ +/* --------------------- BMP180 -----------------------------------*/ +/* if BMP180 is available temperature will be published by mqtt */ +/* and on web server interface */ +/* --------------------- MQTT ------------------------------------ */ +/* Send information to mqtt server configured in the setup mode */ +/* GPIO value configured in config_device.h can be get by */ +/* subscribing to /feeds/[HOSTNAME]/gpio/[GPIO] and modified by */ +/* publishing to /feeds/[HOSTNAME]/gpio/[GPIO]/set */ +/* BMP180 will be published to /feeds/[HOSTNAME]/temperature and */ +/* /feeds/[HOSTNAME]/pressure */ + + -/* To Use GPIO 3 And 1, uncomment #define ENABLE_EXTRA_GPIO */ -/* but Serial will be available on GPIO 15 and 13 */ #include #include @@ -19,9 +35,7 @@ #include #include #include - -#define ENABLE_BMP180 -#define SKETCH_DEBUG +#include "config.h" #include "debug_sketch.h" #include "BMP180.h" @@ -29,18 +43,9 @@ #include "Adafruit_MQTT.h" #include "Adafruit_MQTT_Client.h" -//#define ENABLE_EXTRA_GPIO - #define EEPROM_SIZE 256 char eeprom[EEPROM_SIZE]; -#define WEB_DELAY_MS 100 -#define SAMPLING_PERIODE_MS 60000 - -/* I2C pin used*/ -#define SDA 12 -#define SCL 14 - #define BOOTMODE_SETUP 0 #define BOOTMODE_NORMAL 1 #define BOOTMODE_OTA 2 @@ -50,7 +55,7 @@ uint8_t mode; char *hostName = ""; /* Set these to your desired credentials. */ -const char *ssid = "ESPConfigurator"; +const char *ssid = CONFIG_SSID_NAME; ESP8266WebServer server(80); @@ -165,7 +170,7 @@ void setup() { Serial.swap(); //Switch Serial on GPIO 13 & 15 pinMode(3, INPUT_PULLUP); int txStatus = digitalRead(3); -#ifndef ENABLE_EXTRA_GPIO +#ifndef CONFIG_ENABLE_EXTRA_GPIO Serial.swap(); // Switch back on GPIO 1 & 3 #endif @@ -198,7 +203,7 @@ void setup() { if (mode == BOOTMODE_OTA) { OTASetup(); } else { - if (BMP180Setup(SDA, SCL)) + if (BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)) SKETCH_DEBUG_PRINTLN("BMP180 init success"); WebSetupServer(mode); } @@ -211,10 +216,10 @@ void loop() { } else { server.handleClient(); MqttCheckSubscription(); - delay(WEB_DELAY_MS); + delay(CONFIG_WEB_DELAY_MS); nbCycle++; - if (nbCycle > SAMPLING_PERIODE_MS / WEB_DELAY_MS) { + if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) { if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) { SKETCH_DEBUG_PRINT("Current T°C "); SKETCH_DEBUG_PRINT(temp); diff --git a/WifiControlSensor/config.h b/WifiControlSensor/config.h new file mode 100644 index 0000000..f0fac82 --- /dev/null +++ b/WifiControlSensor/config.h @@ -0,0 +1,29 @@ +#pragma once +#include "config_device.h" + +/* DO NOT CHANGE THIS FILE */ +/* Contains values that SHOULD be defined to have the sketch working */ +/* Modify value in config_device.h */ + +#ifndef CONFIG_WEB_DELAY_MS +#define CONFIG_WEB_DELAY_MS 100 +#endif + +#ifndef CONFIG_SAMPLING_PERIODE_MS +#define CONFIG_SAMPLING_PERIODE_MS 60000 +#endif + +#ifdef CONFIG_ENABLE_BMP180 +#ifndef CONFIG_BMP180_SDA +#define CONFIG_BMP180_SDA 12 +#endif +#ifndef CONFIG_BMP180_SLC +#define CONFIG_BMP180_SLC 14 +#endif +#endif + +#define CONFIG_SSID_NAME "ESPConfigurator" + +#define CONFIG_MQTT_CONTROLLED_GPIO {2, 13} + +#define CONFIG_WEB_CONTROLLED_GPIO 2 diff --git a/WifiControlSensor/config_device.h b/WifiControlSensor/config_device.h new file mode 100644 index 0000000..5616050 --- /dev/null +++ b/WifiControlSensor/config_device.h @@ -0,0 +1,28 @@ +#pragma once + +// Enable Serial Console (Disable to save space and power) +#define CONFIG_SKETCH_DEBUG + +// Switch Serial console on gpio 13 and 15 (So you can use GPIO 1 and 3 for other things) +//#define CONFIG_ENABLE_EXTRA_GPIO + +// Enable the temperatue and pressure Sensor BMP180 +// (CONFIG_BMP180_SDA and CONFIG_BMP180_SDA should be defined as well) +#define CONFIG_ENABLE_BMP180 +#define CONFIG_BMP180_SDA 12 +#define CONFIG_BMP180_SCL 14 + +/* DEFAULT VALUE ALSO DEFINED IN CONFIG.H */ + +// Time to sleep between 2 webserver request (increase it reduce battery usage but increase latency) +//#define CONFIG_WEB_DELAY_MS 100 + +// Get sensors value every X ms +//#define CONFIG_SAMPLING_PERIODE_MS 60000 + +// Name of the SSID when in AP mode for configuration +//#define CONFIG_SSID_NAME "ESPConfigurator" + +// GPIO that can be set or get by mqtt +// Should have less value than MAXSUBSCRIPTIONS +//#define CONFIG_MQTT_CONTROLLED_GPIO {2, 13} diff --git a/WifiControlSensor/debug_sketch.h b/WifiControlSensor/debug_sketch.h index 69f3d27..10b6f21 100644 --- a/WifiControlSensor/debug_sketch.h +++ b/WifiControlSensor/debug_sketch.h @@ -1,10 +1,10 @@ #pragma once -//#define SKETCH_DEBUG +//#define CONFIG_SKETCH_DEBUG // Set where debug messages will be printed. #define DEBUG_PRINTER Serial -#ifdef SKETCH_DEBUG +#ifdef CONFIG_SKETCH_DEBUG #define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); } #define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); } #define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); }