Register callback for wifi

This commit is contained in:
Florian Hoss 2022-07-19 13:07:31 +02:00
parent 6890a87825
commit 49415c4173

View file

@ -15,28 +15,52 @@ fw::API *firewallApi;
WiFiServer wifiServer(80); WiFiServer wifiServer(80);
void setup_wifi() void connectToWifi()
{ {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, psk); WiFi.begin(ssid, psk);
Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid);
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
Serial.print('.'); delay(200);
delay(1000);
} }
Serial.println(); }
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); void handle_wifi_events(WiFiEvent_t event, WiFiEventInfo_t info)
wifiServer.begin(); {
Serial.print("[WiFi] ");
switch (event)
{
case ARDUINO_EVENT_WIFI_STA_START:
Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid);
break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.println("connected");
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("disconnected");
break;
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
break;
default:
Serial.println(event);
}
}
void setup_wifi()
{
WiFi.mode(WIFI_STA);
WiFi.onEvent(handle_wifi_events, ARDUINO_EVENT_MAX);
connectToWifi();
} }
void setup() void setup()
{ {
Serial.begin(115200);
setup_wifi(); setup_wifi();
firewall = new fw::Firewall(); firewall = new fw::Firewall();
firewallApi = new fw::API(firewall, cert, key, username, password); firewallApi = new fw::API(firewall, cert, key, username, password);
wifiServer.begin();
} }
void loop() void loop()
@ -46,9 +70,14 @@ void loop()
if (client) if (client)
{ {
if (firewall->is_client_allowed(client)) if (firewall->is_client_allowed(client))
{
Serial.println("allowed"); Serial.println("allowed");
client.stop();
}
else else
{
Serial.println("rejected"); Serial.println("rejected");
client.stop(); client.flush();
}
} }
} }