-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi.cpp
172 lines (136 loc) · 3.82 KB
/
wifi.cpp
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
#include <NTPClient.h>
#include <WiFiUdp.h>
#include "ledcontroller.h"
#include "wifi.h"
#include "storage.h"
String ssid = "";
String password = "";
String otaPassword = "";
String hostname = "";
WiFiUDP ntpUDP;
int16_t utc = -3; //UTC -3:00 Brazil
uint32_t currentMillis = 0;
uint32_t previousMillis = 0;
NTPClient timeClient(ntpUDP, "a.st1.ntp.br", utc*3600, 60000);
bool inOTA = false;
int getHours() {
return timeClient.getHours();
}
int getMinutes() {
return timeClient.getMinutes();
}
int getSeconds() {
return timeClient.getSeconds();
}
void SetupWiFi() {
ssid = GetWifiSSID();
password = GetWifiPassword();
hostname = GetHostname();
inOTA = false;
Serial.println("\r\n");
Serial.print("Chip ID: 0x");
Serial.println((long int)ESP.getEfuseMac(), HEX);
// Set Hostname.
if (hostname == "") {
hostname = "WIMATRIX-";
hostname += String((long int)ESP.getEfuseMac(), HEX);
}
Serial.print("Hostname: ");
Serial.println(hostname);
Serial.println("Setting up WiFi");
Serial.print("SSID: ");
Serial.println(ssid);
Serial.print("Password: ");
Serial.println(password);
String tmp = "WiFi (";
tmp += ssid;
tmp += ")";
LedPrint(tmp.c_str(), CRGB::Red);
if (WiFi.getMode() != WIFI_STA) {
WiFi.mode(WIFI_STA);
LedLoop();
delay(10);
}
if (WiFi.SSID() != ssid || WiFi.psk() != password) {
Serial.println("WiFi config changed.");
// ... Try to connect to WiFi station.
WiFi.begin(ssid.c_str(), password.c_str());
// ... Pritn new SSID
Serial.print("new SSID: ");
Serial.println(WiFi.SSID());
} else {
WiFi.begin();
}
WiFi.setHostname(hostname.c_str());
Serial.print("Hostname: ");
Serial.println(WiFi.getHostname());
unsigned long startTime = millis();
while (WiFi.status() != WL_CONNECTED && millis() - startTime < 10000) {
delay(100);
Serial.write('.');
LedLoop();
yield();
}
Serial.println();
// Check connection
if (WiFi.status() == WL_CONNECTED) {
// ... print IP Address
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
tmp = "WiFi (";
tmp += ssid;
tmp += ")";
LedPrint(tmp.c_str(), CRGB::Green);
delay(1500);
} else {
LedPrint("WiFi", CRGB::Yellow);
Serial.println("Can not connect to WiFi station. Go into AP mode.");
// Go into software AP mode.
WiFi.mode(WIFI_AP);
delay(10);
WiFi.softAP("WIMATRIX", "1234567890");
delay(1500);
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
LedPrint(WiFi.softAPIP().toString().c_str(), CRGB::Yellow);
}
// Start OTA server.
ArduinoOTA.setHostname((const char *)hostname.c_str());
ArduinoOTA.onStart([]() {
SetMode(BACKGROUND_STRING_DISPLAY);
SetBrightness(0.5);
inOTA = true;
Serial.println("OTA Update Start");
LedPrint("OTA", CRGB::Yellow);
});
ArduinoOTA.onEnd([]() {
Serial.println("OTA Update End");
LedPrint("OTA", CRGB::Green);
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
String progressStr;
progressStr = String((progress / (total / 100))) + String("%");
Serial.print("Progress: ");
Serial.println(progressStr);
LedPrint(progressStr.c_str(), CRGB::Yellow);
});
ArduinoOTA.onError([](ota_error_t error) {
LedPrint("OTA", CRGB::Red);
Serial.printf("Erro [%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Start Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connection failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Error");
else if (error == OTA_END_ERROR) Serial.println("End Fail");
});
ArduinoOTA.begin();
// NTP
timeClient.begin();
timeClient.update();
}
void WiFiLoop() {
timeClient.update();
}
bool InOTA() {
return inOTA;
}