AllWize Library
wifi.cpp
Go to the documentation of this file.
1 /*
2 
3 WIFI MODULE
4 
5 */
6 
7 #include "wifi.h"
8 #include "ntp.h"
9 #include "debug.h"
10 #include "configuration.h"
11 
12 #include <ESP8266WiFi.h>
13 #include <Ticker.h>
14 
15 WiFiEventHandler _wifi_connect_handler;
16 WiFiEventHandler _wifi_disconnect_handler;
17 Ticker _wifi_timer;
18 
19 bool wifiConnected() {
20  return (WiFi.status() == WL_CONNECTED);
21 }
22 
23 void wifiConnect() {
24  DEBUG_MSG("[WIFI] Connecting to '%s'\n", WIFI_SSID);
25  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
26 }
27 
28 void _wifiOnConnect(const WiFiEventStationModeGotIP& event) {
29  DEBUG_MSG("[WIFI] Connected!\n");
30  _wifi_timer.detach();
31  _wifi_timer.once(2, ntpConnect);
32 }
33 
34 void _wifiOnDisconnect(const WiFiEventStationModeDisconnected& event) {
35  DEBUG_MSG("[WIFI] Disconnected!\n");
36  _wifi_timer.detach();
37  _wifi_timer.once(2, wifiConnect);
38 }
39 
40 void wifiSetup() {
41  _wifi_connect_handler = WiFi.onStationModeGotIP(_wifiOnConnect);
42  _wifi_disconnect_handler = WiFi.onStationModeDisconnected(_wifiOnDisconnect);
43  WiFi.persistent(false);
44  WiFi.disconnect();
45  delay(1);
46 }
Ticker _wifi_timer
Definition: wifi.cpp:17
void _wifiOnConnect(const WiFiEventStationModeGotIP &event)
Definition: wifi.cpp:28
#define WIFI_SSID
Definition: configuration.h:20
#define DEBUG_MSG(...)
Definition: debug.h:12
void ntpConnect()
Definition: ntp.cpp:38
WiFiEventHandler _wifi_disconnect_handler
Definition: wifi.cpp:16
WiFiEventHandler _wifi_connect_handler
Definition: wifi.cpp:15
void wifiConnect()
Definition: wifi.cpp:23
bool wifiConnected()
Definition: wifi.cpp:19
void wifiSetup()
Definition: wifi.cpp:40
void _wifiOnDisconnect(const WiFiEventStationModeDisconnected &event)
Definition: wifi.cpp:34
#define WIFI_PASSWORD
Definition: configuration.h:21