-
Notifications
You must be signed in to change notification settings - Fork 4
/
template.ino
198 lines (152 loc) · 6.03 KB
/
template.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* * ESP8266 template with phone config web page
* based on BVB_WebConfig_OTA_V7 from Andreas Spiess https://github.com/SensorsIot/Internet-of-Things-with-ESP8266
*
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Ticker.h>
#include <EEPROM.h>
#include "global.h"
#include "NTP.h"
// Include the HTML, STYLE and Script "Pages"
#include "Page_Admin.h"
#include "Page_Script.js.h"
#include "Page_Style.css.h"
#include "Page_NTPSettings.h"
#include "Page_Information.h"
#include "Page_General.h"
#include "Page_NetworkConfiguration.h"
extern "C" {
#include "user_interface.h"
}
Ticker ticker;
os_timer_t myTimer;
//*** Normal code definition here ...
#define LED_esp 2
String chipID;
void setup() {
bool CFG_saved = false;
int WIFI_connected = false;
Serial.begin(115200);
//**** Network Config load
EEPROM.begin(512); // define an EEPROM space of 512Bytes to store data
CFG_saved = ReadConfig();
// Connect to WiFi acess point or start as Acess point
if (CFG_saved) //if no configuration yet saved, load defaults
{
// Connect the ESP8266 to local WIFI network in Station mode
Serial.println("Booting");
//printConfig();
WiFi.mode(WIFI_STA);
WiFi.begin(config.ssid.c_str(), config.password.c_str());
WIFI_connected = WiFi.waitForConnectResult();
if(WIFI_connected!= WL_CONNECTED )
Serial.println("Connection Failed! activating to AP mode...");
Serial.print("Wifi ip:");Serial.println(WiFi.localIP());
}
if ( (WIFI_connected!= WL_CONNECTED) or !CFG_saved){
// DEFAULT CONFIG
Serial.println("Setting AP mode default parameters");
config.ssid = "ESP8266-" + String(ESP.getChipId(),HEX); // SSID of access point
config.password = "" ; // password of access point
config.dhcp = true;
config.IP[0] = 192; config.IP[1] = 168; config.IP[2] = 1; config.IP[3] = 100;
config.Netmask[0] = 255; config.Netmask[1] = 255; config.Netmask[2] = 255; config.Netmask[3] = 0;
config.Gateway[0] = 192; config.Gateway[1] = 168; config.Gateway[2] = 1; config.Gateway[3] = 254;
config.ntpServerName = "0.ch.pool.ntp.org"; // to be adjusted to PT ntp.ist.utl.pt
config.Update_Time_Via_NTP_Every = 5;
config.timeZone = 1;
config.isDayLightSaving = true;
config.DeviceName = "Not Named";
WiFi.mode(WIFI_AP);
WiFi.softAP(config.ssid.c_str());
Serial.print("Wifi ip:");Serial.println(WiFi.softAPIP());
}
// Start HTTP Server for configuration
server.on ( "/", []() {
Serial.println("admin.html");
server.send_P ( 200, "text/html", PAGE_AdminMainPage); // const char top of page
} );
server.on ( "/favicon.ico", []() {
Serial.println("favicon.ico");
server.send( 200, "text/html", "" );
} );
// Network config
server.on ( "/config.html", send_network_configuration_html );
// Info Page
server.on ( "/info.html", []() {
Serial.println("info.html");
server.send_P ( 200, "text/html", PAGE_Information );
} );
server.on ( "/ntp.html", send_NTP_configuration_html );
//server.on ( "/appl.html", send_application_configuration_html );
server.on ( "/general.html", send_general_html );
// server.on ( "/example.html", []() { server.send_P ( 200, "text/html", PAGE_EXAMPLE ); } );
server.on ( "/style.css", []() {
Serial.println("style.css");
server.send_P ( 200, "text/plain", PAGE_Style_css );
} );
server.on ( "/microajax.js", []() {
Serial.println("microajax.js");
server.send_P ( 200, "text/plain", PAGE_microajax_js );
} );
server.on ( "/admin/values", send_network_configuration_values_html );
server.on ( "/admin/connectionstate", send_connection_state_values_html );
server.on ( "/admin/infovalues", send_information_values_html );
server.on ( "/admin/ntpvalues", send_NTP_configuration_values_html );
//server.on ( "/admin/applvalues", send_application_configuration_values_html );
server.on ( "/admin/generalvalues", send_general_configuration_values_html);
server.on ( "/admin/devicename", send_devicename_value_html);
server.onNotFound ( []() {
Serial.println("Page Not Found");
server.send ( 400, "text/html", "Page not Found" );
} );
server.begin();
Serial.println( "HTTP server started" );
// *********** OTA SETUP
//ArduinoOTA.setHostname(host);
ArduinoOTA.onStart([]() { // what to do before OTA download insert code here
Serial.println("Start");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end
for (int i=0;i<30;i++){
analogWrite(LED_esp,(i*100) % 1001);
delay(50);
}
digitalWrite(LED_esp,HIGH); // Switch OFF ESP LED to save energy
Serial.println("\nEnd");
ESP.restart();
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
ESP.restart();
});
/* setup the OTA server */
ArduinoOTA.begin();
Serial.println("Ready");
// start internal time update ISR
tkSecond.attach(1, ISRsecondTick);
//**** Normal Sketch code here...
}
// the loop function runs over and over again forever
void loop() {
// OTA request handling
ArduinoOTA.handle();
// WebServer requests handling
server.handleClient();
// feed de DOG :)
customWatchdog = millis();
//**** Normal Skecth code here ...
}