2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- CONFIG ---------------------------------- */
|
|
|
|
/* Adapt this sketch to your needs by modifying the config_device.h*/
|
|
|
|
/* --------------------- GPIO ------------------------------------ */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* Generic GPIO Control by HTTP REST interface */
|
|
|
|
/* Modify GPIO by accessing DEVICE_IP/gpio?gpio=[GPIO]&value=[0|1] */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* -------------------- SETUP ------------------------------------ */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* At first boot it creates a WiFi access point */
|
|
|
|
/* and provide a web server on it, so you can configure Wifi ssid */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* Wifi passwd, and a mDNS HOSTNAME and a mqtt server */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* In this mode, device will be available at 192.168.4.1 */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- OTA ------------------------------------- */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* Device can also be put in OTA Mode: In this case, if you have */
|
|
|
|
/* a little flash (512K), it better to disable SPIFFS in */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* "Flash Size" Menu to save some place for the sketch to upload */
|
|
|
|
/* Use espota.py to upload OTA */
|
2016-03-11 00:43:24 +01:00
|
|
|
/* After passing in OTA mode, next boot will be in setup mode */
|
2016-03-30 00:44:37 +02:00
|
|
|
/* --------------------- 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 */
|
|
|
|
|
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <WiFiClient.h>
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
#include <ESP8266mDNS.h>
|
|
|
|
#include <EEPROM.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <errno.h>
|
2016-04-01 01:06:13 +02:00
|
|
|
|
2016-03-30 00:44:37 +02:00
|
|
|
#include "config.h"
|
2016-03-28 23:28:33 +02:00
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
#include "debug_sketch.h"
|
2016-03-26 15:27:23 +01:00
|
|
|
#include "BMP180.h"
|
2016-04-07 15:26:01 +02:00
|
|
|
#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-04-01 01:06:13 +02:00
|
|
|
extern "C" {
|
|
|
|
#include <user_interface.h>
|
|
|
|
}
|
|
|
|
|
2016-03-30 00:49:57 +02:00
|
|
|
char eeprom[CONFIG_EEPROM_SIZE];
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
#define BOOTMODE_SETUP 0
|
|
|
|
#define BOOTMODE_NORMAL 1
|
|
|
|
#define BOOTMODE_OTA 2
|
|
|
|
|
2016-03-16 00:54:13 +01:00
|
|
|
double temp, pressure;
|
2016-04-07 15:26:01 +02:00
|
|
|
float dhtTemp, dhtHumidity;
|
2016-03-14 17:18:36 +01:00
|
|
|
uint8_t mode;
|
2016-04-07 16:14:19 +02:00
|
|
|
const char *hostName = "";
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
/* Set these to your desired credentials. */
|
2016-03-30 00:44:37 +02:00
|
|
|
const char *ssid = CONFIG_SSID_NAME;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
|
|
|
|
/* WebServer decl*/
|
2016-03-24 14:54:55 +01:00
|
|
|
void WebHandleRoot();
|
|
|
|
void WebHandleSetup();
|
|
|
|
void WebHandleGpio();
|
|
|
|
void WebHandleSave();
|
|
|
|
void WebHandleOTA();
|
|
|
|
void WebHandleNotFound();
|
|
|
|
void WebSetupServer(int bootmode);
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
/* EEPROM decl */
|
2016-04-06 19:34:29 +02:00
|
|
|
int EepromSaveConfig(uint8_t bootMode, String ssid, String password, String host,
|
|
|
|
String mqttServer, String mqttUser, String mqttPasswd,
|
2016-04-07 02:02:51 +02:00
|
|
|
int mqttPort, int ip_config, uint32_t ip, uint32_t gw,
|
|
|
|
uint32_t mask, uint32_t dns, uint32_t dns2);
|
2016-03-24 14:54:55 +01:00
|
|
|
int EepromSaveBootMode(uint8_t bootMode);
|
2016-04-06 19:34:29 +02:00
|
|
|
void EepromReadConfig(uint8_t &bootMode, char **ssid, char **password, char **host,
|
|
|
|
char **mqttServer, char **mqttUser, char **mqttPasswd,
|
2016-04-07 02:02:51 +02:00
|
|
|
int &mqttPort, int &ip_config, uint32_t &ip, uint32_t &gw,
|
|
|
|
uint32_t &mask, uint32_t &dns, uint32_t &dns2);
|
2016-03-14 17:18:36 +01:00
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
|
2016-04-10 02:01:25 +02:00
|
|
|
void WifiSetup(int bootmode, char *confSsid, char *confPassword, char *confHost, int ip_config, uint32_t ip, uint32_t gw, uint32_t mask, uint32_t dns, uint32_t dns2) {
|
2016-03-11 00:43:24 +01:00
|
|
|
IPAddress myIP;
|
2016-04-10 02:01:25 +02:00
|
|
|
if (bootmode == BOOTMODE_SETUP) {
|
2016-04-07 02:02:51 +02:00
|
|
|
SKETCH_DEBUG_PRINT("Configuring access point...");
|
|
|
|
SKETCH_DEBUG_PRINTLN(ssid);
|
2016-03-11 00:43:24 +01:00
|
|
|
/* You can set a password to the AP here */
|
|
|
|
WiFi.softAP(ssid);
|
|
|
|
myIP = WiFi.softAPIP();
|
|
|
|
} else {
|
2016-04-10 02:01:25 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("Disable previous AP mode ");
|
|
|
|
WiFi.softAPdisconnect(true);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Connecting to Wifi...");
|
2016-04-07 02:02:51 +02:00
|
|
|
if(ip_config == 1){
|
2016-04-07 12:05:37 +02:00
|
|
|
SKETCH_DEBUG_PRINTLN("Use static ip configuration");
|
2016-04-07 02:02:51 +02:00
|
|
|
WiFi.config(IPAddress(ip), IPAddress(gw), IPAddress(mask), IPAddress(dns), IPAddress(dns2));
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
WiFi.begin(confSsid, confPassword);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
delay(500);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT(".");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("");
|
|
|
|
SKETCH_DEBUG_PRINTLN("WiFi connected");
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-04-06 16:03:14 +02:00
|
|
|
#ifndef CONFIG_ENABLE_POWER_SAVE
|
2016-03-11 00:43:24 +01:00
|
|
|
if (!MDNS.begin(confHost)) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Error setting up MDNS responder!");
|
2016-03-11 00:43:24 +01:00
|
|
|
while (1) {
|
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
}
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("mDNS responder started");
|
2016-04-06 16:03:14 +02:00
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
myIP = WiFi.localIP();
|
|
|
|
}
|
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT("My IP address: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(myIP);
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 14:54:55 +01:00
|
|
|
void OTASetup() {
|
2016-03-11 00:43:24 +01:00
|
|
|
// 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
|
2016-03-24 14:54:55 +01:00
|
|
|
EepromSaveBootMode(BOOTMODE_SETUP);
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
ArduinoOTA.onStart([]() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Start");
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onEnd([]() {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("\nEnd");
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTF("Progress: %u%%\r", (progress / (total / 100)));
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
2016-03-26 15:07:15 +01:00
|
|
|
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");
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Ready");
|
|
|
|
SKETCH_DEBUG_PRINT("IP address: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(WiFi.localIP());
|
|
|
|
SKETCH_DEBUG_PRINT("Free Space: ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(ESP.getFreeSketchSpace());
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
pinMode(3, OUTPUT);
|
|
|
|
|
2016-04-06 19:34:29 +02:00
|
|
|
char *confSsid = "";
|
|
|
|
char *confPassword = "";
|
|
|
|
char *confHost = "";
|
|
|
|
char *mqttServer = "";
|
|
|
|
char *mqttUser = "";
|
|
|
|
char *mqttPasswd = "";
|
2016-04-14 01:17:37 +02:00
|
|
|
int mqttPort ;
|
|
|
|
int ip_mode = 0;
|
|
|
|
uint32_t ip = 0;
|
|
|
|
uint32_t gw = 0;
|
|
|
|
uint32_t mask = 0;
|
|
|
|
uint32_t dns = 0;
|
|
|
|
uint32_t dns2 = 0;
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
delay(1000);
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_INIT(115200);
|
|
|
|
SKETCH_DEBUG_PRINTLN();
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
// Get GPIO 3 Status
|
|
|
|
Serial.swap(); //Switch Serial on GPIO 13 & 15
|
|
|
|
pinMode(3, INPUT_PULLUP);
|
|
|
|
int txStatus = digitalRead(3);
|
2016-03-30 00:44:37 +02:00
|
|
|
#ifndef CONFIG_ENABLE_EXTRA_GPIO
|
2016-03-11 00:43:24 +01:00
|
|
|
Serial.swap(); // Switch back on GPIO 1 & 3
|
|
|
|
#endif
|
|
|
|
|
2016-03-30 00:49:57 +02:00
|
|
|
EEPROM.begin(CONFIG_EEPROM_SIZE);
|
2016-04-07 02:02:51 +02:00
|
|
|
EepromReadConfig(mode, &confSsid, &confPassword, &confHost, &mqttServer, &mqttUser, &mqttPasswd, mqttPort, ip_mode, ip, gw, mask, dns, dns2);
|
2016-03-25 00:03:25 +01:00
|
|
|
|
|
|
|
hostName = confHost;
|
2016-03-11 00:43:24 +01:00
|
|
|
if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Configuration Found !:");
|
|
|
|
SKETCH_DEBUG_PRINTLN(mode);
|
|
|
|
SKETCH_DEBUG_PRINTLN(confSsid);
|
|
|
|
SKETCH_DEBUG_PRINTLN(confPassword);
|
|
|
|
SKETCH_DEBUG_PRINTLN(confHost);
|
|
|
|
SKETCH_DEBUG_PRINTLN(mqttServer);
|
|
|
|
SKETCH_DEBUG_PRINTLN(mqttUser);
|
|
|
|
SKETCH_DEBUG_PRINTLN(mqttPasswd);
|
|
|
|
SKETCH_DEBUG_PRINTLN(mqttPort);
|
|
|
|
SKETCH_DEBUG_PRINTLN();
|
2016-03-11 00:43:24 +01:00
|
|
|
} else {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("No configuration saved");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT("Force Setup Mode ? :");
|
|
|
|
SKETCH_DEBUG_PRINT(txStatus ? "No" : "Yes");
|
|
|
|
SKETCH_DEBUG_PRINTLN();
|
2016-04-10 02:01:25 +02:00
|
|
|
if(txStatus == 0){
|
|
|
|
mode = BOOTMODE_SETUP;
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
|
2016-04-10 02:01:25 +02:00
|
|
|
WifiSetup(mode, confSsid, confPassword, confHost, ip_mode, ip, gw, mask, dns, dns2);
|
2016-04-07 12:24:30 +02:00
|
|
|
if (mode == BOOTMODE_NORMAL) {
|
|
|
|
MqttSetup(mqttServer, mqttUser, mqttPasswd, mqttPort, confHost);
|
|
|
|
}
|
2016-03-11 00:43:24 +01:00
|
|
|
|
|
|
|
if (mode == BOOTMODE_OTA) {
|
2016-03-24 14:54:55 +01:00
|
|
|
OTASetup();
|
2016-03-11 00:43:24 +01:00
|
|
|
} else {
|
2016-04-07 15:26:01 +02:00
|
|
|
if (BMP180Setup(CONFIG_BMP180_SDA, CONFIG_BMP180_SCL)){
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("BMP180 init success");
|
2016-04-07 15:26:01 +02:00
|
|
|
}
|
|
|
|
if (DHTSetup(CONFIG_DHT_PIN)){
|
|
|
|
SKETCH_DEBUG_PRINTLN("DHT init success");
|
|
|
|
}
|
2016-03-24 14:54:55 +01:00
|
|
|
WebSetupServer(mode);
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-04-06 16:03:14 +02:00
|
|
|
#ifdef CONFIG_ENABLE_POWER_SAVE
|
2016-04-01 01:06:13 +02:00
|
|
|
wifi_set_sleep_type(LIGHT_SLEEP_T);
|
2016-04-06 16:03:14 +02:00
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
digitalWrite(LED_BUILTIN, HIGH); //Active at low level
|
2016-04-01 01:06:13 +02:00
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint nbCycle = 0;
|
|
|
|
void loop() {
|
|
|
|
if (mode == BOOTMODE_OTA) {
|
|
|
|
ArduinoOTA.handle();
|
|
|
|
} else {
|
|
|
|
server.handleClient();
|
2016-04-01 00:14:12 +02:00
|
|
|
if (MqttIsConfigured())
|
|
|
|
MqttCheckSubscription();
|
2016-03-30 00:44:37 +02:00
|
|
|
delay(CONFIG_WEB_DELAY_MS);
|
2016-03-21 00:56:27 +01:00
|
|
|
|
2016-03-11 00:43:24 +01:00
|
|
|
nbCycle++;
|
2016-03-30 00:44:37 +02:00
|
|
|
if (nbCycle > CONFIG_SAMPLING_PERIODE_MS / CONFIG_WEB_DELAY_MS) {
|
2016-04-07 15:26:01 +02:00
|
|
|
#ifdef CONFIG_ENABLE_BMP180
|
2016-03-24 14:54:55 +01:00
|
|
|
if (BMP180IsConnected() && BMP180GetTempAndPressure(temp, pressure) == 0) {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINT("Current T°C ");
|
|
|
|
SKETCH_DEBUG_PRINT(temp);
|
2016-04-07 15:26:01 +02:00
|
|
|
SKETCH_DEBUG_PRINT(" Pressure mB ");
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN(pressure);
|
2016-04-01 00:14:12 +02:00
|
|
|
if (MqttIsConfigured())
|
|
|
|
MqttPublish(temp, pressure);
|
2016-03-11 01:31:03 +01:00
|
|
|
} else {
|
2016-03-26 15:07:15 +01:00
|
|
|
SKETCH_DEBUG_PRINTLN("Cannot get T°C");
|
2016-03-11 00:43:24 +01:00
|
|
|
}
|
2016-04-07 15:26:01 +02:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_ENABLE_DHT
|
|
|
|
if (DHTIsConnected() && DHTGetTempAndHumidity(dhtTemp, dhtHumidity) == 0) {
|
|
|
|
SKETCH_DEBUG_PRINT("Current T°C ");
|
|
|
|
SKETCH_DEBUG_PRINT(dhtTemp);
|
|
|
|
SKETCH_DEBUG_PRINT(" Humidity ");
|
|
|
|
SKETCH_DEBUG_PRINTLN(dhtHumidity);
|
|
|
|
if (MqttIsConfigured())
|
|
|
|
MqttDhtPublish(dhtTemp, dhtHumidity);
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-11 00:43:24 +01:00
|
|
|
nbCycle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|