-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathluxmart-esp.ino
98 lines (73 loc) · 2.22 KB
/
luxmart-esp.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*********
Rui Santos
Complete instructions at https://RandomNerdTutorials.com/esp32-wi-fi-manager-asyncwebserver/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <ESPAsyncTCP.h>
#include <stdlib.h>
#include <EEPROM.h>
#define EEPROM_SIZE 130
// Config
#define DEBUG
//Variables to save values from HTML form
String ssid;
String password;
String ip;
String gateway;
struct WifiData {
char ssid[33];
char password[64];
char ip[16];
char gateway[16];
};
#include "api/pwm_controller_commands.h"
#include "common/pwm_controller_esp.h"
#include "common/pwm_controller_user.h"
#include "html.h"
#include "WebUserControl.h"
WebUserControl* webServer;
#include "PwmControllerWebUserControl.h"
#include "WifiSetupWebUserControl.h"
bool setupSuccessful = false;
void start_web_server() {
if(WiFi.status() == WL_CONNECTED) {
DBGLOG("Connected successfully. Starting web server");
webServer = new PwmControllerWebUserControl();
setupSuccessful = true;
} else {
DBGLOG("Setting AP");
WiFi.softAP("ESP-WIFI-MANAGER", NULL);
DBGLOG("Starting Web Server");
webServer = new WifiSetupWebUserControl();
}
webServer->start();
}
void pwm_controller_setup() {
EEPROM.begin(EEPROM_SIZE);
// Load saved WiFi Data
WifiData savedWifiData;
EEPROM.get(0, savedWifiData);
ssid = savedWifiData.ssid;
password = savedWifiData.password;
ip = savedWifiData.ip;
gateway = savedWifiData.gateway;
DBGLOG("Connecting to WiFi...");
setupWiFi();
start_web_server();
}
#define RESET_COUNT 3
int resetCounter = 0;
void pwm_controller_loop() {
webServer->iterate();
if(setupSuccessful) {
if(WiFi.status() != WL_CONNECTED && resetCounter++ < RESET_COUNT) {
setupWiFi();
}
if(WiFi.status() == WL_CONNECTED) resetCounter = 0;
if(resetCounter >= RESET_COUNT) ESP.reset();
}
}