-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwifi.ino
113 lines (100 loc) · 3.46 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/***********************************************************************************
* TBTracker-RX uses Wifi to send JSON data to Sondehub
*
* You can add multiple WiFi networks here.
* TBTracker-RX will choose the strongest one
***********************************************************************************/
#include "time.h"
#include <WiFiMulti.h>
WiFiMulti WiFiMulti;
/************************************************************************************
* WIFI setup
************************************************************************************/
void setupWifi()
{
Serial.println(); Serial.println();
#if defined(USE_SSD1306)
displayClear();
displayOled(0,0,"Searching for WiFi...");
#endif
// Set WiFi in Station mode
WiFi.mode(WIFI_STA);
// Add the WiFi networks as defined in the settings file
if (WIFI_SSID_1 != "") WiFiMulti.addAP(WIFI_SSID_1, WIFI_PASSWORD_1);
if (WIFI_SSID_2 != "") WiFiMulti.addAP(WIFI_SSID_2, WIFI_PASSWORD_2);
if (WIFI_SSID_3 != "") WiFiMulti.addAP(WIFI_SSID_3, WIFI_PASSWORD_3);
// WiFi network Scan
Serial.print("[WiFi] Scanning WiFi networks...");
// 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");
}
else
{
Serial.print(n); Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
}
}
// Try to connect to the stongest know access point
Serial.print(F("[WiFi} Connecting to WiFi, please wait."));
#if defined(USE_SSD1306)
displayClear();
displayOled(0,0,"Connecting to WiFi...");
#endif
int loopCounter = 0;
while (WiFiMulti.run() != WL_CONNECTED && loopCounter < 25)
{
delay(500);
Serial.print(".");
loopCounter++;
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("");
Serial.print(F("[WiFi} WiFi connected to: ")); Serial.println(WiFi.SSID());
Serial.print(F("[WiFi} IP address: ")); Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
}
else
{
Serial.println();
Serial.println(F("Could not connect to WiFi. No uploading possible."));
}
}
/************************************************************************************
* Format the local time in UTC
************************************************************************************/
void formatLocalTime()
{
if(!getLocalTime(&timeinfo))
{
Serial.println(F("Failed to obtain time"));
return;
}
strftime(Telemetry.time_received,sizeof(Telemetry.time_received),"%Y-%m-%dT%T.000000Z",&timeinfo);
}
/************************************************************************************
* Get time information from the NTP server
************************************************************************************/
void updateTime()
{
if (WiFi.status() == WL_CONNECTED)
{
//init and get the time. Use UTC
configTime(0, 0, ntpServer);
}
}