Domotique/WifiControlSensor/WifiControlSensor.ino

284 lines
8.5 KiB
Arduino
Raw Normal View History

/* --------------------- 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 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 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 */
2016-06-01 01:22:47 +02:00
/* subscribing to /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO] and */
/* modified by publishin to */
/* /feeds/MQTTUSER/[HOSTNAME]/gpio/[GPIO]/set */
/* BMP180 will be published to /feeds/[HOSTNAME]/temperature and */
/* /feeds/[HOSTNAME]/pressure */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <EEPROM.h>
#include <ArduinoOTA.h>
#include <errno.h>
#include <HIB.h>
2016-04-01 01:06:13 +02:00
#include "config.h"
#include "utils.h"
2016-03-28 23:28:33 +02:00
#include "debug_sketch.h"
#include "BMP180.h"
#include "sensor_DHT.h"
2016-04-14 01:15:42 +02:00
#include "MQTT.h"
2016-03-11 01:31:03 +01:00
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
2016-06-01 15:21:32 +02:00
#include "EEPROM.h"
2016-11-20 18:08:15 +01:00
#include "sensors.h"
2016-03-11 01:31:03 +01:00
2016-04-01 01:06:13 +02:00
extern "C" {
#include <user_interface.h>
}
#define BOOTMODE_SETUP 0
#define BOOTMODE_NORMAL 1
#define BOOTMODE_OTA 2
2016-03-16 00:54:13 +01:00
double temp, pressure;
float dhtTemp, dhtHumidity;
2016-03-14 17:18:36 +01:00
uint8_t mode;
2016-11-20 00:06:31 +01:00
productConfig conf = {BOOTMODE_SETUP, "", "", "", "", "", "", 0, 0, 0, 0, 0, 0, 0};
/* Set these to your desired credentials. */
const char *ssid = CONFIG_SSID_NAME;
ESP8266WebServer server(80);
/* WebServer decl*/
void WebHandleRoot();
void WebHandleSetup();
void WebHandleGpio();
void WebHandleSave();
void WebHandleOTA();
void WebHandleNotFound();
void WebSetupServer(int bootmode);
#ifdef CONFIG_SETUP_BUTTON
void onLongButtonPressed(uint8_t pin){
if(pin == CONFIG_SETUP_BUTTON && mode == BOOTMODE_NORMAL){
SKETCH_DEBUG_PRINTLN("Putting device in setup mode");
mode = BOOTMODE_SETUP;
WiFi.disconnect();
WiFi.softAP(ssid);
}
}
#endif
2016-11-20 00:06:31 +01:00
void WifiSetup(productConfig conf) {
IPAddress myIP;
2016-11-20 00:06:31 +01:00
if (conf.bootMode == BOOTMODE_SETUP) {
2016-04-07 02:02:51 +02:00
SKETCH_DEBUG_PRINT("Configuring access point...");
2016-11-20 00:06:31 +01:00
SKETCH_DEBUG_PRINTLN(conf.ssid);
/* You can set a password to the AP here */
2016-11-20 00:06:31 +01:00
WiFi.softAP(conf.ssid);
myIP = WiFi.softAPIP();
} else {
2016-11-20 00:23:16 +01:00
//Disable previous AP mode
WiFi.softAPdisconnect(true);
SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
2016-11-20 00:06:31 +01:00
if(conf.ip_mode == 1){
SKETCH_DEBUG_PRINTLN("Use static ip configuration");
2016-11-20 00:06:31 +01:00
WiFi.config(IPAddress(conf.ip), IPAddress(conf.gw), IPAddress(conf.mask), IPAddress(conf.dns), IPAddress(conf.dns2));
2016-04-07 02:02:51 +02:00
}
2016-11-20 00:06:31 +01:00
WiFi.begin(conf.ssid, conf.password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
SKETCH_DEBUG_PRINT(".");
}
2016-11-20 00:23:16 +01:00
SKETCH_DEBUG_PRINTF("\nWiFi connected\n");
2016-06-01 01:22:47 +02:00
#ifdef CONFIG_ENABLE_MDNS
2016-11-20 00:06:31 +01:00
if (!MDNS.begin(conf.host)) {
SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!");
2016-11-20 00:23:16 +01:00
} else {
SKETCH_DEBUG_PRINTLN("mDNS responder started");
}
#endif
myIP = WiFi.localIP();
}
SKETCH_DEBUG_PRINT("My IP address: ");
SKETCH_DEBUG_PRINTLN(myIP);
}
2016-11-20 00:23:16 +01:00
void OTASetup() {
2016-10-28 18:03:36 +02:00
#ifndef CONFIF_DISABLE_OTA
// Port defaults to 8266
// ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
// ArduinoOTA.setHostname("myesp8266");
// No authentication by default
// ArduinoOTA.setPassword((const char *)"123");
//Disable OTA mode to avoid forever loop
//Force BOOTMODE_SETUP in case eeprom layout have changed
EepromSaveBootMode(BOOTMODE_SETUP);
ArduinoOTA.onStart([]() {
SKETCH_DEBUG_PRINTLN("Start");
});
ArduinoOTA.onEnd([]() {
SKETCH_DEBUG_PRINTLN("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
2016-06-04 18:45:11 +02:00
SKETCH_DEBUG_PRINTF("Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
SKETCH_DEBUG_PRINTF("Error[%u]: ", error);
2016-04-01 01:05:29 +02:00
if (error == OTA_AUTH_ERROR) {
SKETCH_DEBUG_PRINTLN("Auth Failed");
}
else if (error == OTA_BEGIN_ERROR) {
SKETCH_DEBUG_PRINTLN("Begin Failed");
}
else if (error == OTA_CONNECT_ERROR) {
SKETCH_DEBUG_PRINTLN("Connect Failed");
}
else if (error == OTA_RECEIVE_ERROR) {
SKETCH_DEBUG_PRINTLN("Receive Failed");
}
else if (error == OTA_END_ERROR) {
SKETCH_DEBUG_PRINTLN("End Failed");
}
});
ArduinoOTA.begin();
SKETCH_DEBUG_PRINTLN("Ready");
2016-11-20 18:08:15 +01:00
SKETCH_DEBUG_PRINT("IP address: ");
SKETCH_DEBUG_PRINTLN(WiFi.localIP());
SKETCH_DEBUG_PRINTF("Free Space: %d\n", ESP.getFreeSketchSpace());
2016-10-28 18:03:36 +02:00
#endif
}
void setup() {
pinMode(3, OUTPUT);
delay(1000);
SKETCH_DEBUG_INIT(115200);
SKETCH_DEBUG_PRINTLN();
// Get GPIO 3 Status
Serial.swap(); //Switch Serial on GPIO 13 & 15
pinMode(CONFIG_SETUP_GPIO, INPUT_PULLUP);
int txStatus = digitalRead(CONFIG_SETUP_GPIO);
#ifndef CONFIG_ENABLE_EXTRA_GPIO
Serial.swap(); // Switch back on GPIO 1 & 3
#endif
2016-03-30 00:49:57 +02:00
EEPROM.begin(CONFIG_EEPROM_SIZE);
2016-11-20 00:06:31 +01:00
EepromReadConfig(conf);
2016-03-25 00:03:25 +01:00
2016-11-20 00:06:31 +01:00
mode = conf.bootMode;
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
SKETCH_DEBUG_PRINTLN("Configuration Found !:");
2016-11-20 00:06:31 +01:00
SKETCH_DEBUG_PRINTLN(conf.bootMode);
SKETCH_DEBUG_PRINTLN(conf.ssid);
SKETCH_DEBUG_PRINTLN(conf.password);
SKETCH_DEBUG_PRINTLN(conf.host);
SKETCH_DEBUG_PRINTLN(conf.mqttServer);
SKETCH_DEBUG_PRINTLN(conf.mqttUser);
SKETCH_DEBUG_PRINTLN(conf.mqttPasswd);
SKETCH_DEBUG_PRINTLN(conf.mqttPort);
SKETCH_DEBUG_PRINTLN();
} else {
SKETCH_DEBUG_PRINTLN("No configuration saved");
}
2016-11-20 00:23:16 +01:00
SKETCH_DEBUG_PRINTF("Force Setup Mode ? : %s\n",txStatus ? "No" : "Yes");
if(txStatus == 0){
mode = BOOTMODE_SETUP;
}
2016-11-20 00:06:31 +01:00
WifiSetup(conf);
if (mode == BOOTMODE_NORMAL) {
2016-11-20 00:06:31 +01:00
MqttSetup(conf.mqttServer, conf.mqttUser, conf.mqttPasswd, conf.mqttPort, conf.host);
MqttPublishIP(WiFi.localIP().toString());
}
if (mode == BOOTMODE_OTA) {
OTASetup();
} else {
if (!BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)){
SKETCH_DEBUG_PRINTLN("BMP180 init success");
}
if (!DHTSetup(CONFIG_DHT_PIN)){
SKETCH_DEBUG_PRINTLN("DHT init success");
}
WebSetupServer(mode);
}
#ifdef CONFIG_ENABLE_POWER_SAVE
2016-04-01 01:06:13 +02:00
wifi_set_sleep_type(LIGHT_SLEEP_T);
#endif
#ifdef CONFIG_SETUP_BUTTON
new HIB(CONFIG_SETUP_BUTTON, HIGH, NULL, NULL, onLongButtonPressed);
#endif
}
2016-06-01 01:23:23 +02:00
uint nbCycle = CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS;
void loop() {
if (mode == BOOTMODE_OTA) {
ArduinoOTA.handle();
} else {
server.handleClient();
2016-11-15 22:49:04 +01:00
if (mode == BOOTMODE_NORMAL)
MqttCheckSubscription();
delay(CONFIG_WEB_DELAY_MS);
2016-03-21 00:56:27 +01:00
nbCycle++;
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
2016-11-20 18:08:15 +01:00
uint i;
if (!BMP180GetTempAndPressure(temp, pressure)) {
SKETCH_DEBUG_PRINT("Current T°C ");
SKETCH_DEBUG_PRINT(temp);
SKETCH_DEBUG_PRINT(" Pressure mB ");
SKETCH_DEBUG_PRINTLN(pressure);
2016-11-15 22:49:04 +01:00
if (mode == BOOTMODE_NORMAL)
MqttPublishBMP180(temp, pressure);
}
if (!DHTGetTempAndHumidity(dhtTemp, dhtHumidity)) {
SKETCH_DEBUG_PRINT("Current T°C ");
SKETCH_DEBUG_PRINT(dhtTemp);
SKETCH_DEBUG_PRINT(" Humidity ");
SKETCH_DEBUG_PRINTLN(dhtHumidity);
2016-11-15 22:49:04 +01:00
if (mode == BOOTMODE_NORMAL)
MqttPublishDHT(dhtTemp, dhtHumidity);
}
2016-11-20 18:08:15 +01:00
for (i = 0; i < NB_ELEMENTS(gpioSensors); i++){
int val;
gpioSensors[i]->getMeasure(val);
2016-06-02 01:31:57 +02:00
}
2016-11-20 18:08:15 +01:00
if (mode == BOOTMODE_NORMAL)
MqttPublishGPIO();
nbCycle = 0;
}
}
}