Register callback for wifi
This commit is contained in:
parent
6890a87825
commit
49415c4173
1 changed files with 40 additions and 11 deletions
|
@ -15,28 +15,52 @@ fw::API *firewallApi;
|
|||
|
||||
WiFiServer wifiServer(80);
|
||||
|
||||
void setup_wifi()
|
||||
void connectToWifi()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, psk);
|
||||
Serial.printf("Attempting to connect to WPA SSID: %s\n", ssid);
|
||||
while (WiFi.status() != WL_CONNECTED)
|
||||
{
|
||||
Serial.print('.');
|
||||
delay(1000);
|
||||
delay(200);
|
||||
}
|
||||
Serial.println();
|
||||
Serial.print("IP Address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
wifiServer.begin();
|
||||
}
|
||||
|
||||
void handle_wifi_events(WiFiEvent_t event, WiFiEventInfo_t info)
|
||||
{
|
||||
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()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
setup_wifi();
|
||||
firewall = new fw::Firewall();
|
||||
firewallApi = new fw::API(firewall, cert, key, username, password);
|
||||
wifiServer.begin();
|
||||
}
|
||||
|
||||
void loop()
|
||||
|
@ -46,9 +70,14 @@ void loop()
|
|||
if (client)
|
||||
{
|
||||
if (firewall->is_client_allowed(client))
|
||||
{
|
||||
Serial.println("allowed");
|
||||
client.stop();
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("rejected");
|
||||
client.stop();
|
||||
client.flush();
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue