-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.ino
89 lines (75 loc) · 1.73 KB
/
wifi.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
// WIFI
void setupWifi()
{
Serial.println("-setup WIFI");
// set mode
WiFi.mode(WIFI_STA);
// set hostname
WiFi.hostname(HOSTNAME);
// set ip
WiFi.config(ip, gateway, subnet);
if (WiFi.status() != WL_CONNECTED) { // FIX FOR USING 2.3.0 CORE (only .begin if not connected)
WiFi.begin(WIFI_SSID, WIFI_PASS); // connect to the network
}
boolean state = true;
int i = 0;
// Wait for connection
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
// try for 10 seconds max
if (i > 20) {
state = false;
break;
}
i++;
}
if (state)
{
Serial.println("");
Serial.print("Connected to ");
Serial.println(WIFI_SSID);
Serial.print("IP address: ");
Serial.println( WiFi.localIP() );
Serial.println("-setup WIFI - OK");
}
else
{
Serial.println("connection failed. retry in 60 seconds.");
blinkled(10);
// sleep 60 seconds
delay(60000);
// reboot
ESP.restart();
}
}
void scan_wifi_to_mqtt()
{
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0)
{
Serial.println("no networks found");
mqttPublish(topicStatusOut, "test requested! I´m alive!", true);
}
else
{
String msg = String(n);
msg += " networks found";
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
msg += String(i + 1);
msg += ": ";
msg += WiFi.SSID(i);
msg += " (";
msg += WiFi.RSSI(i);
msg += ")";
msg += (WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*";
}
mqttPublish(topicStatusOut, msg, true);
}
Serial.println("");
}