Example i pro ESP32

This commit is contained in:
Pavel Brychta 2020-04-06 17:37:23 +02:00
parent 26a65f00ac
commit 8a95f6d69b

View File

@ -1,16 +1,21 @@
#include <ESP8266WiFi.h> #if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WebServer.h> # include <ESP8266WiFi.h>
# include <ESP8266WebServer.h>
# include <ESP8266NetBIOS.h>
#else
# include <WiFi.h>
# include <WebServer.h>
# include <NetBIOS.h>
#endif
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
#include <EEPROM.h> #include <EEPROM.h>
#include <ESP8266NetBIOS.h>
#include <WiFiConfig.h> #include <WiFiConfig.h>
#include <encipheredEEPROMStrings.h> #include <encipheredEEPROMStrings.h>
#define MAGIC 0xdeadbeef
// Plati pro desticku Witty s modulem ESP-12E // Plati pro desticku Witty s modulem ESP-12E
#define PIN_FORCE_CONFIG 4 #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) {}
#define DEBUG_OUT(a) Serial.print(a) #define DEBUG_OUT(a) Serial.print(a)
@ -18,8 +23,8 @@
// Definice obsazeni EEPROM // Definice obsazeni EEPROM
#define elementSize(type, element) sizeof(((type *)0)->element) #define elementSize(type, element) sizeof(((type *)0)->element)
#define countof(a) (sizeof(a) / sizeof(a[0])) #define countof(a) (sizeof(a) / sizeof(a[0]))
typedef struct typedef struct {
{ uint32_t magic; // kontrola spravneho obsahu EEPROM
char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku) char devname[32 + 2]; // jmeno zarizeni (pro NBNS plati jen 16 znaku)
} eepromconfig_t; } eepromconfig_t;
@ -28,7 +33,11 @@ typedef struct
#define EEPROM_CONFIG_ORIGIN (0) #define EEPROM_CONFIG_ORIGIN (0)
char WiFiDeviceName[elementSize(eepromconfig_t, devname)]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...) char WiFiDeviceName[elementSize(eepromconfig_t, devname)]; // misto pro jmeno zarizeni (dodane do DNS, DHCP NBNS apod...)
#if defined(ARDUINO_ARCH_ESP8266)
ESP8266WebServer wwwserver(80); // webovy server ESP8266WebServer wwwserver(80); // webovy server
#else
WebServer wwwserver(80); // webovy server
#endif
String content; String content;
static void handleRoot(void) static void handleRoot(void)
@ -44,8 +53,7 @@ static void handleRoot(void)
void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state) void ICACHE_FLASH_ATTR wcb(wificonfigstate_t state)
{ {
switch (state) switch (state) {
{
case WCS_CONNECTSTART: case WCS_CONNECTSTART:
DEBUG_OUT(F("Starting connect...\r\n")); DEBUG_OUT(F("Starting connect...\r\n"));
break; break;
@ -79,6 +87,19 @@ void ICACHE_FLASH_ATTR setup()
WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname); WiFiConfigUsrParameter devname("devname", "Jméno zařízení", (const char *)WiFiDeviceName, 32, saveDevname);
EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM EEPROM.begin(EEPROM_SIZE); // zahajujeme praci s EEPROM
uint32_t magic;
EEPROM.get(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic);
if (EEPROM_MAGIC != magic) {
// EEPROM ma spatny obsah
for (unsigned int i = 0; i < EEPROM_SIZE; i++)
EEPROM.write(i, 0);
magic = EEPROM_MAGIC;
EEPROM.put(EEPROM_CONFIG_ORIGIN + offsetof(eepromconfig_t, magic), magic);
EEPROM.commit();
}
pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka pinMode(PIN_FORCE_CONFIG, INPUT_PULLUP); // pin, co slouzi jako vstup tlacitka
int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace int fc = digitalRead(PIN_FORCE_CONFIG); // pozadavek na vynucene vyvolani konfigurace
Serial.begin(9600); Serial.begin(9600);
@ -92,8 +113,7 @@ void ICACHE_FLASH_ATTR setup()
wwwserver.on("/", handleRoot); wwwserver.on("/", handleRoot);
wwwserver.begin(); // startujeme webovy server wwwserver.begin(); // startujeme webovy server
if (strlen(WiFiDeviceName) > 0) if (strlen(WiFiDeviceName) > 0) {
{
NBNS.begin(WiFiDeviceName); NBNS.begin(WiFiDeviceName);
ArduinoOTA.setHostname(WiFiDeviceName); ArduinoOTA.setHostname(WiFiDeviceName);
} }
@ -108,8 +128,7 @@ void ICACHE_FLASH_ATTR setup()
DEBUG_OUT(F("Error[")); DEBUG_OUT(F("Error["));
DEBUG_OUT(error); DEBUG_OUT(error);
DEBUG_OUT(F("]: ")); DEBUG_OUT(F("]: "));
switch (error) switch (error) {
{
case OTA_AUTH_ERROR: case OTA_AUTH_ERROR:
DEBUG_OUT(F("Auth Failed\r\n")); DEBUG_OUT(F("Auth Failed\r\n"));
break; break;