2018-04-16 15:00:49 +02:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESP8266WebServer.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <EEPROM.h>
|
2018-07-04 15:33:58 +02:00
|
|
|
#include <ESP8266NetBIOS.h>
|
|
|
|
#include <WiFiConfig.h>
|
2019-08-07 12:01:00 +02:00
|
|
|
#include <encipheredEEPROMStrings.h>
|
2018-04-16 15:00:49 +02:00
|
|
|
|
|
|
|
// Plati pro desticku Witty s modulem ESP-12E
|
|
|
|
#define PIN_FORCE_CONFIG 4
|
|
|
|
#define PIN_LED_RED 15
|
|
|
|
#define PIN_LED_GREEN 12
|
|
|
|
#define PIN_LED_BLUE 13
|
|
|
|
|
|
|
|
//#define DEBUG_OUT(a) {}
|
|
|
|
#define DEBUG_OUT(a) Serial.print(a)
|
|
|
|
|
2019-08-07 12:01:00 +02:00
|
|
|
// Definice obsazeni EEPROM
|
|
|
|
#define elementSize(type, element) sizeof(((type *)0)->element)
|
|
|
|
#define countof(a) (sizeof(a) / sizeof(a[0]))
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
|
|
|
|
} eepromconfig_t;
|
|
|
|
|
|
|
|
#define EEPROM_SIZE (sizeof(wificonfigarea_t) + sizeof(eepromconfig_t) + 10) // velikost EEPROM oblasti (POZOR!!! zbytecnym zvetsovanim se zaroven zmensuje velikost RAM kvuli zrcadlu!!!)
|
|
|
|
#define EEPROM_WIFICONFIG_ORIGIN (EEPROM_SIZE - sizeof(wificonfigarea_t)) // pocatek oblasti, pouzivane WiFiConfig (alokovano odzadu)
|
|
|
|
#define EEPROM_CONFIG_ORIGIN (0)
|
|
|
|
|
|
|
|
char WiFiDeviceName[elementSize(eepromconfig_t, devname)]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
|
2018-04-16 15:00:49 +02:00
|
|
|
ESP8266WebServer wwwserver(80); // webovy server
|
|
|
|
String content;
|
|
|
|
|
|
|
|
static void handleRoot(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
content = F("<!DOCTYPE HTML>\n<html>Hello world from ESP8266");
|
|
|
|
content += F("<p>");
|
|
|
|
content += F("</html>");
|
|
|
|
|
|
|
|
wwwserver.send(200, "text/html", content);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
|
|
|
|
{
|
|
|
|
|
|
|
|
switch (state)
|
|
|
|
{
|
|
|
|
case WCS_CONNECTSTART:
|
|
|
|
DEBUG_OUT(F("Starting connect...\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WCS_CONNECTING:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WCS_CONNECTED:
|
|
|
|
DEBUG_OUT(F("Connected.\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WCS_CONFIGSTART:
|
|
|
|
DEBUG_OUT(F("Starting config...\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WCS_CONFIGWAIT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 12:01:00 +02:00
|
|
|
void ICACHE_FLASH_ATTR saveDevname(const char *param)
|
2018-04-16 15:00:49 +02:00
|
|
|
{
|
2019-08-07 12:01:00 +02:00
|
|
|
String p = param;
|
2018-04-16 15:00:49 +02:00
|
|
|
|
2019-08-07 12:01:00 +02:00
|
|
|
EES_storeString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname), p);
|
2018-04-16 15:00:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ICACHE_FLASH_ATTR setup()
|
|
|
|
{
|
|
|
|
WiFiConfig wifi; // konfigurace ESP modulu
|
2019-08-07 12:01:00 +02:00
|
|
|
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
|
2018-04-16 15:00:49 +02:00
|
|
|
|
2019-08-07 12:01:00 +02:00
|
|
|
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM
|
2018-04-16 15:00:49 +02:00
|
|
|
pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka
|
|
|
|
int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace
|
|
|
|
Serial.begin(9600);
|
2019-08-07 12:01:00 +02:00
|
|
|
String dn = EES_readString(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, devname), elementSize(eepromconfig_t, devname));
|
|
|
|
strcpy(WiFiDeviceName, dn.c_str());
|
|
|
|
wifi.addParameter(&devname);
|
|
|
|
|
|
|
|
if (WCR_OK != wifi.begin(EEPROM_WIFICONFIG_ORIGIN, fc, wcb)) // startujeme pripojeni
|
2018-04-16 15:00:49 +02:00
|
|
|
ESP.restart();
|
|
|
|
|
|
|
|
wwwserver.on("/", handleRoot);
|
|
|
|
wwwserver.begin(); // startujeme webovy server
|
|
|
|
|
|
|
|
if (strlen(WiFiDeviceName) > 0)
|
|
|
|
{
|
2018-07-04 15:33:58 +02:00
|
|
|
NBNS.begin(WiFiDeviceName);
|
2018-04-16 15:00:49 +02:00
|
|
|
ArduinoOTA.setHostname(WiFiDeviceName);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
|
|
DEBUG_OUT(F("Start\r\n"));
|
|
|
|
});
|
|
|
|
ArduinoOTA.onEnd([]() {
|
|
|
|
DEBUG_OUT(F("End\r\n"));
|
|
|
|
});
|
|
|
|
ArduinoOTA.onError([](ota_error_t error) {
|
|
|
|
DEBUG_OUT(F("Error["));
|
|
|
|
DEBUG_OUT(error);
|
|
|
|
DEBUG_OUT(F("]: "));
|
|
|
|
switch (error)
|
|
|
|
{
|
|
|
|
case OTA_AUTH_ERROR:
|
|
|
|
DEBUG_OUT(F("Auth Failed\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_BEGIN_ERROR:
|
|
|
|
DEBUG_OUT(F("Begin Failed\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_CONNECT_ERROR:
|
|
|
|
DEBUG_OUT(F("Connect Failed\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_RECEIVE_ERROR:
|
|
|
|
DEBUG_OUT(F("Receive Failed\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OTA_END_ERROR:
|
|
|
|
DEBUG_OUT(F("End Failed\r\n"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
DEBUG_OUT(F("\r\n"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
wwwserver.handleClient(); // osetrujeme praci serveru
|
|
|
|
ArduinoOTA.handle();
|
|
|
|
}
|