diff --git a/lamp.ino b/lamp.ino index 7072988..1ca6994 100644 --- a/lamp.ino +++ b/lamp.ino @@ -1,6 +1,8 @@ #include "settings.h" +#include "wifi.h" Settings const* settings = nullptr; +Wifi * wifi = nullptr; bool ok = true; @@ -18,8 +20,12 @@ void setup() { } else Serial.println(" OK"); + + wifi = new Wifi(settings->wifi); } void loop() { if(!ok) {delay(3600000); return;} + + wifi->keep_alive_connection(); } diff --git a/wifi.cpp b/wifi.cpp new file mode 100644 index 0000000..6527664 --- /dev/null +++ b/wifi.cpp @@ -0,0 +1,19 @@ +#include "wifi.h" + +Wifi::Wifi(WifiSettings const& settings): + m_wifi() +{ + for(auto it = settings.networks_cbegin(); it != settings.networks_cend(); it = it->next()) + m_wifi.addAP(it->ssid(), it->pass()); + Serial.setDebugOutput(true); +} + +void Wifi::keep_alive_connection() +{ + m_wifi.run(); +} + +void Wifi::is_connected() const +{ + return m_wifi.status() == WL_CONNECTED; +} diff --git a/wifi.h b/wifi.h new file mode 100644 index 0000000..16d5e38 --- /dev/null +++ b/wifi.h @@ -0,0 +1,20 @@ +#ifndef WIFI_H +#define WIFI_H + +#include + +#include "wifi_settings.h" + +class Wifi +{ + public: + Wifi(WifiSettings const& settings); + + void keep_alive_connection(); + bool is_connected() const; + + protected: + ESP8266WiFiMulti m_wifi; +}; + +#endif //WIFI_H