WiFiConfig/examples/WiFiConfig_simple_test/WiFiConfig_simple_test.ino

144 lines
3.2 KiB
Arduino
Raw Permalink Normal View History

2020-04-06 17:37:23 +02:00
#if defined(ARDUINO_ARCH_ESP8266)
# include <ESP8266WiFi.h>
# include <ESP8266WebServer.h>
# include <ESP8266NetBIOS.h>
#else
# include <WiFi.h>
# include <WebServer.h>
# include <NetBIOS.h>
#endif
2018-04-16 15:00:49 +02:00
#include <ArduinoOTA.h>
2018-07-04 15:33:58 +02:00
#include <WiFiConfig.h>
2021-03-05 07:41:14 +01:00
#include <sysvars.hpp>
2020-04-06 17:37:23 +02:00
2018-04-16 15:00:49 +02:00
// Plati pro desticku Witty s modulem ESP-12E
#define PIN_FORCE_CONFIG 4
//#define DEBUG_OUT(a) {}
#define DEBUG_OUT(a) Serial.print(a)
2021-03-05 07:41:14 +01:00
char WiFiDeviceName[32]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
2020-04-06 17:37:23 +02:00
#if defined(ARDUINO_ARCH_ESP8266)
2018-04-16 15:00:49 +02:00
ESP8266WebServer wwwserver(80); // webovy server
2020-04-06 17:37:23 +02:00
#else
WebServer wwwserver(80); // webovy server
#endif
2021-03-05 07:41:14 +01:00
static char MAINPAGE[] PROGMEM = R"=====(<!DOCTYPE HTML>
<html>
Hello world from ESP8266
<p>
</html>
)=====";
2018-04-16 15:00:49 +02:00
static void handleRoot(void)
{
2021-03-05 07:41:14 +01:00
wwwserver.send_P(200, "text/html", MAINPAGE);
2018-04-16 15:00:49 +02:00
}
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
{
2020-04-06 17:37:23 +02:00
switch (state) {
2018-04-16 15:00:49 +02:00
case WCS_CONNECTSTART:
2020-04-06 17:37:23 +02:00
DEBUG_OUT(F("Starting connect...\r\n"));
break;
2018-04-16 15:00:49 +02:00
case WCS_CONNECTING:
2020-04-06 17:37:23 +02:00
break;
2018-04-16 15:00:49 +02:00
case WCS_CONNECTED:
2020-04-06 17:37:23 +02:00
DEBUG_OUT(F("Connected.\r\n"));
break;
2018-04-16 15:00:49 +02:00
case WCS_CONFIGSTART:
2020-04-06 17:37:23 +02:00
DEBUG_OUT(F("Starting config...\r\n"));
break;
2018-04-16 15:00:49 +02:00
case WCS_CONFIGWAIT:
2020-04-06 17:37:23 +02:00
break;
2020-04-07 08:38:52 +02:00
case WCS_CONFIGTIMEOUT:
DEBUG_OUT(F("Config timeout...\r\n"));
break;
2020-04-06 17:37:23 +02:00
}
2018-04-16 15:00:49 +02:00
}
void ICACHE_FLASH_ATTR saveDevname(const char *param)
2018-04-16 15:00:49 +02:00
{
2020-04-06 17:37:23 +02:00
String p = param;
2018-04-16 15:00:49 +02:00
2021-03-05 07:41:14 +01:00
svSetV(F("devname"), p);
2018-04-16 15:00:49 +02:00
}
void ICACHE_FLASH_ATTR setup()
{
2020-04-06 17:37:23 +02:00
WiFiConfig wifi; // konfigurace ESP modulu
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
2018-04-16 15:00:49 +02:00
2021-03-05 07:41:14 +01:00
LittleFS.begin();
2018-04-16 15:00:49 +02:00
2020-04-06 17:37:23 +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
2020-04-07 08:38:52 +02:00
Serial.begin(115200);
2021-03-05 07:41:14 +01:00
String dn = svGetV<String>(F("devname"));
2020-04-06 17:37:23 +02:00
strcpy(WiFiDeviceName, dn.c_str());
wifi.addParameter(&devname);
2021-03-05 07:41:14 +01:00
if (WCR_OK != wifi.begin(fc, wcb)) // startujeme pripojeni
2020-04-06 17:37:23 +02:00
ESP.restart();
2018-04-16 15:00:49 +02:00
2020-04-06 17:37:23 +02:00
wwwserver.on("/", handleRoot);
wwwserver.begin(); // startujeme webovy server
if (strlen(WiFiDeviceName) > 0) {
NBNS.begin(WiFiDeviceName);
ArduinoOTA.setHostname(WiFiDeviceName);
2018-04-16 15:00:49 +02:00
}
2020-04-06 17:37:23 +02:00
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();
2018-04-16 15:00:49 +02:00
}
void loop()
{
2020-04-06 17:37:23 +02:00
wwwserver.handleClient(); // osetrujeme praci serveru
ArduinoOTA.handle();
2018-04-16 15:00:49 +02:00
}