-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi_utils.cpp
206 lines (174 loc) · 5.02 KB
/
wifi_utils.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* @file wifi_utils.cpp
* @brief Utilities for WiFi connection and UDP communication
*
* This file contains functions for managing WiFi connections,
* including connecting to networks, scanning for available networks,
* and sending UDP messages. It also provides utilities for setting
* and retrieving WiFi credentials.
*/
#include "wifi_utils.h"
#include "led.h"
#include "uart_utils.h"
#include <AsyncUDP.h>
#include <WiFi.h>
// Hold values for pass and ssid
static String ssid;
static String password;
// Create a UDP instance for network communication
extern AsyncUDP udp;
// WIFI Connection settings
const int MAX_RETRY_COUNT = 10; // Maximum number of retries
const int RETRY_DELAY_MS = 1000;
/**
* @brief Connect to the WiFi network
*
* This function attempts to connect to the WiFi network using the provided
* SSID and password. It will retry a few times before giving up.
*/
void connectToWiFi() {
// Check against empty SSID and password
if (ssid.isEmpty()) {
UART0.println("WIFI_ERROR: SSID is missing");
return;
}
if (password.isEmpty()) {
UART0.println("WIFI_ERROR: Password is missing");
return;
}
// Start up wifi
led_set_blue(255);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
// Retry counter
int retryCount = 0;
// Try and connect
led_set_blue(0);
while (WiFi.status() != WL_CONNECTED && retryCount < MAX_RETRY_COUNT) {
led_set_blue(255);
delay(RETRY_DELAY_MS);
retryCount++;
UART0.println("WIFI_CONNECT: Connecting to WiFi... try " +
String(retryCount) + "/" + String(MAX_RETRY_COUNT));
led_set_blue(0);
// Check for specific WiFi statuses
if (WiFi.status() == WL_CONNECT_FAILED) {
UART0.println("WIFI_ERROR: Failed to connect to WiFi: Incorrect password "
"or other issue.");
break;
}
}
// After retries still not connected ? Return error
if (WiFi.status() != WL_CONNECTED) {
led_error();
UART0.println("WIFI_ERROR: Failed to connect to WiFi");
return;
}
// Connected to WiFi
led_set_blue(0);
led_set_green(255);
UART0.println("WIFI_CONNECTED: Connected to " + ssid);
UART0.print("WIFI_INFO: IP Address: ");
UART0.println(WiFi.localIP());
led_set_green(0);
// Start listening for UDP packets
if (udp.listen(1234)) {
UART0.println("WIFI_INFO: UDP listening on port 1234");
udp.onPacket([](AsyncUDPPacket packet) {
String receivedData = String((char *)packet.data(), packet.length());
UART0.println("WIFI_UDP_INCOMING_DATA: " + receivedData);
String command;
String argument;
// Parse commands and arguments
int spaceIndex = receivedData.indexOf(' ');
if (spaceIndex != -1) {
command = receivedData.substring(0, spaceIndex);
argument = receivedData.substring(spaceIndex + 1);
} else {
command = receivedData;
}
// Special UDP Connection method
// Direct Message stream to uart from UDP
if (command == "MESSAGE") {
// Stream the message directly to UART and return
UART0.println("MESSAGE: " + argument);
return;
}
// Uart file command system
handleCommand(command, argument, &packet);
});
}
UART0.println("WIFI_SUCCESS: WiFi connected");
}
/**
* @brief Disconnect from the current WiFi network
*/
void disconnectFromWiFi() {
WiFi.disconnect();
UART0.println("WIFI_DISCONNECT: Wifi disconnected");
}
/**
* @brief List available WiFi networks
*
* @return String A comma-separated list of available WiFi SSIDs
*/
String listWiFiNetworks() {
UART0.println("WIFI_LIST: Scanning WiFi networks...");
led_set_blue(255);
int n = WiFi.scanNetworks();
String result = "";
for (int i = 0; i < n; ++i) {
result += WiFi.SSID(i);
if (i < n - 1) {
result += ", ";
}
}
led_set_blue(0);
return result;
}
/**
* @brief Set the SSID for WiFi connection
*
* @param newSSID The new SSID to set
*/
void setSSID(String newSSID) { ssid = newSSID; }
/**
* @brief Set the password for WiFi connection
*
* @param newPassword The new password to set
*/
void setPassword(String newPassword) { password = newPassword; }
/**
* @brief Get the current SSID
*
* @return const char* Pointer to the current SSID
*/
const char *getSSID() { return ssid.c_str(); }
/**
* @brief Get the current password
*
* @return const char* Pointer to the current password
*/
const char *getPassword() { return password.c_str(); }
/**
* @brief Send a UDP message
*
* @param message The message to send
* @param remoteIP The IP address of the remote device
* @param remotePort The port number of the remote device
*/
void sendUDPMessage(const char* message, IPAddress remoteIP, uint16_t remotePort) {
led_set_blue(255);
udp.writeTo((const uint8_t*)message, strlen(message), remoteIP, remotePort);
led_set_blue(0);
}
/**
* @brief Get the local IP address as a string
*
* @return String The local IP address
*/
String getLocalIpString() {
led_set_blue(255);
led_set_blue(0);
return WiFi.localIP().toString();
}