Move configuration into a dedicated file
This commit is contained in:
parent
45fac78797
commit
efe135a801
@ -1,5 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#ifdef ENABLE_BMP180
|
#ifdef CONFIG_ENABLE_BMP180
|
||||||
#include <SFE_BMP180.h>
|
#include <SFE_BMP180.h>
|
||||||
#include "debug_sketch.h"
|
#include "debug_sketch.h"
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ int BMP180GetTempAndPressure(double &t, double &p);
|
|||||||
int BMP180Setup(int sda, int scl);
|
int BMP180Setup(int sda, int scl);
|
||||||
bool BMP180IsConnected();
|
bool BMP180IsConnected();
|
||||||
|
|
||||||
#else //ENABLE_BMP80
|
#else //CONFIG_ENABLE_BMP80
|
||||||
int BMP180GetTemperature(double &t){return 0;};
|
int BMP180GetTemperature(double &t){return 0;};
|
||||||
int BMP180GetTempAndPressure(double &t, double &p){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;};
|
int BMP180Setup(int , int ){SKETCH_DEBUG_PRINTLN("BMP180 is disabled at build time"); return 0;};
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#ifdef ENABLE_BMP180
|
#ifdef CONFIG_ENABLE_BMP180
|
||||||
#include "BMP180.h"
|
#include "BMP180.h"
|
||||||
int BMP180Setup(int sda, int scl) {
|
int BMP180Setup(int sda, int scl) {
|
||||||
bmp180Connected = bmp180.begin(sda, scl);
|
bmp180Connected = bmp180.begin(sda, scl);
|
||||||
|
@ -14,7 +14,7 @@ char pressureFeed[FEED_MAX_SIZE] = {};
|
|||||||
|
|
||||||
// Should have less that MAXSUBSCRIPTIONS elements
|
// Should have less that MAXSUBSCRIPTIONS elements
|
||||||
// MAXSUBSCRIPTIONS is defined is Adafruit_mqtt.h
|
// 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_FEED_FORMAT "/feeds/%s/gpio/%d"
|
||||||
#define GPIO_SET_FEED_FORMAT "/feeds/%s/gpio/%d/set"
|
#define GPIO_SET_FEED_FORMAT "/feeds/%s/gpio/%d/set"
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
#define STRINGIFY(x) #x
|
||||||
|
#define TOSTRING(x) STRINGIFY(x)
|
||||||
|
|
||||||
void WebHandleRoot() {
|
void WebHandleRoot() {
|
||||||
server.send(200, "text/html",
|
server.send(200, "text/html",
|
||||||
"<head><meta http-equiv=\"refresh\" content=\"" + String(SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
"<head><meta http-equiv=\"refresh\" content=\"" + String(CONFIG_SAMPLING_PERIODE_MS / 1000) + "\" ></head>"
|
||||||
"<h1>You are connected to " + String(hostName) + "</h1>"
|
"<h1>You are connected to " + String(hostName) + "</h1>"
|
||||||
"<fieldset>"
|
"<fieldset>"
|
||||||
"<legend>Sensors</legend>"
|
"<legend>Sensors</legend>"
|
||||||
@ -8,10 +11,12 @@ void WebHandleRoot() {
|
|||||||
"Pressure " + String(pressure, 2) + "mB<br/>"
|
"Pressure " + String(pressure, 2) + "mB<br/>"
|
||||||
"</fieldset>"
|
"</fieldset>"
|
||||||
"<fieldset>"
|
"<fieldset>"
|
||||||
"<legend>Pump</legend>"
|
#ifdef CONFIG_WEB_CONTROLLED_GPIO
|
||||||
"<a href=\"/gpio?gpio=2&value=1\">Power PUMP ON</a><br/>"
|
"<legend>Relay</legend>"
|
||||||
"<a href=\"/gpio?gpio=2&value=0\">Power PUMP OFF</a><br/>"
|
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&value=1\">Relay ON</a><br/>"
|
||||||
|
"<a href=\"/gpio?gpio=" TOSTRING(CONFIG_WEB_CONTROLLED_GPIO) "&value=0\">Relay OFF</a><br/>"
|
||||||
"</fieldset>"
|
"</fieldset>"
|
||||||
|
#endif
|
||||||
"<fieldset>"
|
"<fieldset>"
|
||||||
"<legend>Settings</legend>"
|
"<legend>Settings</legend>"
|
||||||
"<a href=\"/setup\">Enter Setup</a><br/>"
|
"<a href=\"/setup\">Enter Setup</a><br/>"
|
||||||
|
@ -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 */
|
/* Generic GPIO Control by HTTP REST interface */
|
||||||
/* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */
|
/* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */
|
||||||
|
/* -------------------- SETUP ------------------------------------ */
|
||||||
/* At first boot it creates a WiFi access point */
|
/* At first boot it creates a WiFi access point */
|
||||||
/* and provide a web server on it, so you can configure Wifi ssid */
|
/* 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 */
|
/* 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 */
|
/* Device can also be put in OTA Mode: In this case, if you have */
|
||||||
/* a little flash (512K), it better to disable SPIFFS in */
|
/* 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 */
|
/* 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 <ESP8266WiFi.h>
|
#include <ESP8266WiFi.h>
|
||||||
#include <WiFiClient.h>
|
#include <WiFiClient.h>
|
||||||
@ -19,9 +35,7 @@
|
|||||||
#include <EEPROM.h>
|
#include <EEPROM.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include "config.h"
|
||||||
#define ENABLE_BMP180
|
|
||||||
#define SKETCH_DEBUG
|
|
||||||
|
|
||||||
#include "debug_sketch.h"
|
#include "debug_sketch.h"
|
||||||
#include "BMP180.h"
|
#include "BMP180.h"
|
||||||
@ -29,18 +43,9 @@
|
|||||||
#include "Adafruit_MQTT.h"
|
#include "Adafruit_MQTT.h"
|
||||||
#include "Adafruit_MQTT_Client.h"
|
#include "Adafruit_MQTT_Client.h"
|
||||||
|
|
||||||
//#define ENABLE_EXTRA_GPIO
|
|
||||||
|
|
||||||
#define EEPROM_SIZE 256
|
#define EEPROM_SIZE 256
|
||||||
char eeprom[EEPROM_SIZE];
|
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_SETUP 0
|
||||||
#define BOOTMODE_NORMAL 1
|
#define BOOTMODE_NORMAL 1
|
||||||
#define BOOTMODE_OTA 2
|
#define BOOTMODE_OTA 2
|
||||||
@ -50,7 +55,7 @@ uint8_t mode;
|
|||||||
char *hostName = "";
|
char *hostName = "";
|
||||||
|
|
||||||
/* Set these to your desired credentials. */
|
/* Set these to your desired credentials. */
|
||||||
const char *ssid = "ESPConfigurator";
|
const char *ssid = CONFIG_SSID_NAME;
|
||||||
|
|
||||||
ESP8266WebServer server(80);
|
ESP8266WebServer server(80);
|
||||||
|
|
||||||
@ -165,7 +170,7 @@ void setup() {
|
|||||||
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
||||||
pinMode(3, INPUT_PULLUP);
|
pinMode(3, INPUT_PULLUP);
|
||||||
int txStatus = digitalRead(3);
|
int txStatus = digitalRead(3);
|
||||||
#ifndef ENABLE_EXTRA_GPIO
|
#ifndef CONFIG_ENABLE_EXTRA_GPIO
|
||||||
Serial.swap(); // Switch back on GPIO 1 & 3
|
Serial.swap(); // Switch back on GPIO 1 & 3
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -198,7 +203,7 @@ void setup() {
|
|||||||
if (mode == BOOTMODE_OTA) {
|
if (mode == BOOTMODE_OTA) {
|
||||||
OTASetup();
|
OTASetup();
|
||||||
} else {
|
} else {
|
||||||
if (BMP180Setup(SDA, SCL))
|
if (BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL))
|
||||||
SKETCH_DEBUG_PRINTLN("BMP180 init success");
|
SKETCH_DEBUG_PRINTLN("BMP180 init success");
|
||||||
WebSetupServer(mode);
|
WebSetupServer(mode);
|
||||||
}
|
}
|
||||||
@ -211,10 +216,10 @@ void loop() {
|
|||||||
} else {
|
} else {
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
MqttCheckSubscription();
|
MqttCheckSubscription();
|
||||||
delay(WEB_DELAY_MS);
|
delay(CONFIG_WEB_DELAY_MS);
|
||||||
|
|
||||||
nbCycle++;
|
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) {
|
if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) {
|
||||||
SKETCH_DEBUG_PRINT("Current T°C ");
|
SKETCH_DEBUG_PRINT("Current T°C ");
|
||||||
SKETCH_DEBUG_PRINT(temp);
|
SKETCH_DEBUG_PRINT(temp);
|
||||||
|
29
WifiControlSensor/config.h
Normal file
29
WifiControlSensor/config.h
Normal file
@ -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
|
28
WifiControlSensor/config_device.h
Normal file
28
WifiControlSensor/config_device.h
Normal file
@ -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}
|
@ -1,10 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
//#define SKETCH_DEBUG
|
//#define CONFIG_SKETCH_DEBUG
|
||||||
|
|
||||||
// Set where debug messages will be printed.
|
// Set where debug messages will be printed.
|
||||||
#define DEBUG_PRINTER Serial
|
#define DEBUG_PRINTER Serial
|
||||||
|
|
||||||
#ifdef SKETCH_DEBUG
|
#ifdef CONFIG_SKETCH_DEBUG
|
||||||
#define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); }
|
#define SKETCH_DEBUG_INIT(speed){ DEBUG_PRINTER.begin(speed); }
|
||||||
#define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
#define SKETCH_DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
|
||||||
#define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); }
|
#define SKETCH_DEBUG_PRINTF(...) { DEBUG_PRINTER.printf(__VA_ARGS__); }
|
||||||
|
Loading…
Reference in New Issue
Block a user