ENTS-tock
Environmental NeTworked Sensor firmware on Tock OS
Loading...
Searching...
No Matches
wifi.cpp
Go to the documentation of this file.
1#include "modules/wifi.hpp"
2
3#include <ArduinoLog.h>
4
5#include "http.hpp"
6
8 // set module type
9 type = Esp32Command_wifi_command_tag;
10
11 // create ntp client
12 timeClient = new NTPClient(ntpUDP);
13}
14
16 // delete timeClient
17 delete timeClient;
18}
19
20void ModuleWiFi::OnReceive(const Esp32Command& cmd) {
21 Log.traceln("ModuleWiFi::OnReceive");
22
23 // check if WiFi command
24 if (cmd.which_command != Esp32Command_wifi_command_tag) {
25 return;
26 }
27
28 Log.traceln("WiFiCommand type: %d", cmd.command.wifi_command.type);
29
30 // switch for command types
31 switch (cmd.command.wifi_command.type) {
32 case WiFiCommand_Type_CONNECT:
33 Log.traceln("Calling CONNECT");
34 Connect(cmd);
35 break;
36
37 case WiFiCommand_Type_POST:
38 Log.traceln("Calling POST");
39 Post(cmd);
40 break;
41
42 case WiFiCommand_Type_CHECK:
43 Log.traceln("Calling CHECK_REQUEST");
44 CheckRequest(cmd);
45 break;
46
47 case WiFiCommand_Type_TIME:
48 Log.traceln("Calling TIME");
49 Time(cmd);
50 break;
51
52 case WiFiCommand_Type_DISCONNECT:
53 Log.traceln("Calling DISCONNECT");
54 Disconnect(cmd);
55 break;
56
57 case WiFiCommand_Type_CHECK_WIFI:
58 Log.traceln("Calling CHECK_WIFI");
59 CheckWiFi(cmd);
60 break;
61
62 case WiFiCommand_Type_CHECK_API:
63 Log.traceln("Calling CHECK_API");
64 CheckApi(cmd);
65 break;
66
67 case WiFiCommand_Type_NTP_SYNC:
68 Log.traceln("Calling NTP_SYNC");
69 NtpSync(cmd);
70 break;
71
72 case WiFiCommand_Type_HOST:
73 Log.traceln("Calling HOST");
74 Host(cmd);
75 break;
76
77 case WiFiCommand_Type_STOP_HOST:
78 Log.traceln("Calling STOP_HOST");
79 StopHost();
80 break;
81
82 case WiFiCommand_Type_HOST_INFO:
83 Log.traceln("Calling HOST_INFO");
84 HostInfo();
85 break;
86
87 default:
88 Log.warningln("wifi command type %d not found!",
89 cmd.command.wifi_command.type);
90 break;
91 }
92}
93
94size_t ModuleWiFi::OnRequest(uint8_t* buffer) {
95 Log.traceln("ModuleWiFi::OnRequest");
96 memcpy(buffer, request_buffer, request_buffer_len);
97 return request_buffer_len;
98}
99
100void ModuleWiFi::Connect(const Esp32Command& cmd) {
101 // init return command
102 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
103 wifi_cmd.type = WiFiCommand_Type_CONNECT;
104
105 Log.traceln("ModuleWiFi::Connect");
106
107 // print the mac address
108 uint8_t mac[6];
109 WiFi.macAddress(mac);
110 Log.noticeln("MAC: %x:%x:%x:%x:%x:%x", mac[0], mac[1], mac[2], mac[3], mac[4],
111 mac[5]);
112
113 Log.noticeln("Connecting to WiFI...");
114 Log.noticeln("ssid: %s", cmd.command.wifi_command.ssid);
115 Log.noticeln("passwd: %s", cmd.command.wifi_command.passwd);
116
117 // TODO(jmadden173) update hostname to something sane
118 // WiFi.setHostname("esp32");
119
120 // connect to WiFi
121 // WiFi.disconnect();
122 int status = WiFi.begin(cmd.command.wifi_command.ssid,
123 cmd.command.wifi_command.passwd);
124
125 Log.noticeln("WiFi connection status: %d", status);
126
127 // set status
128 wifi_cmd.rc = status;
129
130 request_buffer_len =
131 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
132}
133
134void ModuleWiFi::Post(const Esp32Command& cmd) {
135 // init return WiFi command
136 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
137 wifi_cmd.type = WiFiCommand_Type_POST;
138
139 Log.traceln("ModuleWiFI::Post");
140
141 // send measurement
142 const uint8_t* meas = cmd.command.wifi_command.resp.bytes;
143 const size_t meas_len = cmd.command.wifi_command.resp.size;
144 dirtviz.SendMeasurement(meas, meas_len);
145
146 // encode wifi command in buffer
147 this->request_buffer_len =
148 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
149}
150
151void ModuleWiFi::CheckRequest(const Esp32Command& cmd) {
152 Log.traceln("ModuleWiFi::CheckRequest");
153
154 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
155 wifi_cmd.type = WiFiCommand_Type_CHECK;
156
157 // set url and port
158 HttpClient resp_msg = dirtviz.GetResponse();
159
160 const uint8_t* resp =
161 reinterpret_cast<const uint8_t*>(resp_msg.Data().c_str());
162 size_t resp_len = resp_msg.Data().length();
163 unsigned int status_code = resp_msg.ResponseCode();
164
165 Log.noticeln("Resp length: %d", resp_len);
166
167 // save status code and response
168 wifi_cmd.rc = status_code;
169 if (resp_len > 0) {
170 wifi_cmd.resp.size = resp_len;
171 memcpy(wifi_cmd.resp.bytes, resp, resp_len);
172 }
173
174 request_buffer_len =
175 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
176}
177
178void ModuleWiFi::NtpSync(const Esp32Command& cmd) {
179 Log.traceln("ModuleWiFi::NtpSync");
180
181 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
182 wifi_cmd.type = WiFiCommand_Type_NTP_SYNC;
183
184 // force update
185 timeClient->forceUpdate();
186
187 request_buffer_len =
188 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
189}
190
191void ModuleWiFi::Time(const Esp32Command& cmd) {
192 Log.traceln("ModuleWiFi::Time");
193
194 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
195 wifi_cmd.type = WiFiCommand_Type_TIME;
196
197 if (timeClient->isTimeSet()) {
198 wifi_cmd.ts = timeClient->getEpochTime();
199 Log.noticeln("Current timestamp: %d", wifi_cmd.ts);
200 } else {
201 Log.errorln("Failed to get time from NTP server!");
202 }
203
204 request_buffer_len =
205 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
206}
207
208void ModuleWiFi::Disconnect(const Esp32Command& cmd) {
209 // init return command
210 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
211 wifi_cmd.type = WiFiCommand_Type_CONNECT;
212
213 Log.traceln("ModuleWiFi::Disconnect");
214
215 timeClient->end();
216 WiFi.disconnect();
217
218 request_buffer_len =
219 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
220}
221
222void ModuleWiFi::CheckWiFi(const Esp32Command& cmd) {
223 Log.traceln("ModuleWiFi::CheckWiFi");
224
225 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
226 wifi_cmd.type = WiFiCommand_Type_CHECK_WIFI;
227
228 // check if connected to WiFi connected
229 int wifi_status = WiFi.status();
230 wifi_cmd.rc = wifi_status;
231 Log.noticeln("WiFi status: %d", wifi_status);
232 if (wifi_status == WL_CONNECTED) {
233 Log.noticeln("IP Address: %p", WiFi.localIP());
234 Log.noticeln("Gateway IP: %p", WiFi.gatewayIP());
235 Log.noticeln("Subnet Mask: %p", WiFi.subnetMask());
236 Log.noticeln("DNS: %p", WiFi.dnsIP());
237
238 // start NTP
239 timeClient->begin();
240 }
241
242 request_buffer_len =
243 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
244}
245
246void ModuleWiFi::CheckApi(const Esp32Command& cmd) {
247 Log.traceln("ModuleWiFi::CheckApi");
248
249 WiFiCommand wifi_cmd = WiFiCommand_init_zero;
250 wifi_cmd.type = WiFiCommand_Type_CHECK_API;
251
252 // check API health
253 dirtviz.SetUrl(cmd.command.wifi_command.url);
254 dirtviz.Check();
255
256 request_buffer_len =
257 EncodeWiFiCommand(&wifi_cmd, request_buffer, sizeof(request_buffer));
258}
259
260void ModuleWiFi::Host(const Esp32Command& cmd) {
261 Log.traceln("ModuleWiFi::Host");
262
263 WiFiCommand resp = WiFiCommand_init_zero;
264 resp.type = WiFiCommand_Type_HOST;
265
266 // append wifi mac to the ssid to make unique
267 std::string ssid = cmd.command.wifi_command.ssid;
268 std::string pass = cmd.command.wifi_command.passwd;
269
270 WiFi.softAP(ssid.c_str(), pass.c_str());
271
272 std::string ip = WiFi.softAPIP().toString().c_str();
273 Log.noticeln("Access Point started");
274
275 request_buffer_len =
276 EncodeWiFiCommand(&resp, request_buffer, sizeof(request_buffer));
277}
278
279void ModuleWiFi::StopHost() {
280 Log.traceln("ModuleWiFi::StopHost");
281
282 WiFiCommand resp = WiFiCommand_init_zero;
283 resp.type = WiFiCommand_Type_STOP_HOST;
284
285 // stop hosting access point
286 WiFi.softAPdisconnect();
287
288 request_buffer_len =
289 EncodeWiFiCommand(&resp, request_buffer, sizeof(request_buffer));
290}
291
292void ModuleWiFi::HostInfo() {
293 Log.traceln("ModuleWiFi::HostInfo");
294
295 WiFiCommand resp = WiFiCommand_init_zero;
296 resp.type = WiFiCommand_Type_HOST_INFO;
297
298 // copy the ip address
299 strncpy(resp.ssid, WiFi.softAPSSID().c_str(), sizeof(resp.ssid));
300 strncpy(resp.url, WiFi.softAPIP().toString().c_str(), sizeof(resp.url));
301 strncpy(resp.mac, WiFi.softAPmacAddress().c_str(), sizeof(resp.mac));
302
303 // get number of connected clients
304 resp.clients = WiFi.softAPgetStationNum();
305
306 Log.noticeln("SSID: %s", resp.ssid);
307 Log.noticeln("IP: %s", resp.url);
308 Log.noticeln("MAC: %s", resp.mac);
309
310 request_buffer_len =
311 EncodeWiFiCommand(&resp, request_buffer, sizeof(request_buffer));
312}
unsigned int Check()
Health check for API endpoint.
Definition dirtviz.cpp:40
void SetUrl(const char *url)
Set new URL string.
Definition dirtviz.cpp:33
unsigned int SendMeasurement(const uint8_t *meas, size_t meas_len)
Send serialized measurement to the API.
Definition dirtviz.cpp:64
HttpClient GetResponse()
Get the response from the server.
Definition dirtviz.cpp:106
HTTP request parser library.
Definition http.hpp:28
unsigned int ResponseCode()
Get the http response code.
Definition http.cpp:31
std::string Data()
Get data from Http request.
Definition http.cpp:35
~ModuleWiFi(void)
Definition wifi.cpp:15
ModuleWiFi(void)
Definition wifi.cpp:7
size_t OnRequest(uint8_t *buffer)
Definition wifi.cpp:94
void OnReceive(const Esp32Command &cmd)
Definition wifi.cpp:20
const char ssid[]
const char pass[]
uint8_t buffer[32]
size_t EncodeWiFiCommand(const WiFiCommand *wifi_cmd, uint8_t *buffer, size_t size)
Encodes a WiFiCommand.
Definition transcoder.c:290
bool Disconnect(void)
Disconnect from WiFi.
Definition wifi.c:289
bool Connect(void)
Connect to WiFi.
Definition wifi.c:245
Module for WiFi communication.