This repository has been archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStation.cpp
153 lines (137 loc) · 4.33 KB
/
Station.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Station
* @author Curt Henrichs
* @date 1-23-2018
*
* Station module is responsable for initializing and maintaing a WiFi
* connection. Additionally, this module will maintain all network services
* for this device.
*
* For Wifi connection and management the WiFiManager library was selected.
* https://github.com/tzapu/WiFiManager
* This library preserves last configured wifi connection and hosts a captive
* web service when not connected to a wifi in station mode.
*
* MDNS is an optional service for this device if configured in the network
* configuration file. The following is pulled from the example documentation,
* - Install host software:
* - For Linux, install Avahi (http://avahi.org/).
* - For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
* - For Mac OSX and iOS support is built in through Bonjour already.
*
* The LED service REST API can be found in the appropriate project module.
*/
//==============================================================================
// Libraries
//==============================================================================
#include "Station.h"
#include "RestService.h"
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#if ST_USE_MDNS
#include <ESP8266mDNS.h>
#endif
//==============================================================================
// Private Function Prototypes
//==============================================================================
/**
* Attempt to connect to last configured Wifi network. If failed then block as
* an access point until connection is established.
* @return boolean that should always be true (legacy)
*/
static bool _connect(void);
/**
* Start all services for the project:
* - MDNS if enabled
* - LED Service REST API
*/
static void _start_services(void);
/**
* Update all services for the project:
* - MDNS if enabled
* - LED Service REST API
*/
static void _update_services(void);
/**
* Stop all services that require polling stop on Wifi failure
* - LED Service REST API
*/
static void _stop_services(void);
//==============================================================================
// Public Function Implementation
//==============================================================================
/**
* Initialize Station which will connect to last configured Wifi router. If
* no such router then block as an access point until user defines a router.
* When connected to a router, start all services.
*/
void st_init(void){
bool success = _connect();
if(success){
_start_services();
}
}
/**
* Update Station which will check current WiFi connection. If lost connection
* then stop all services and attempt to reconnect. (Blocking until new
* connection is established). Then restart all previously stopped services.
* Else update the services normally.
*/
void st_update(void){
if(WiFi.status() != WL_CONNECTED){
_stop_services();
bool success = _connect();
if(success){
_start_services();
}
}else{
_update_services();
}
}
//==============================================================================
// Private Function Implementation
//==============================================================================
/**
* Attempt to connect to last configured Wifi network. If failed then block as
* an access point until connection is established.
* @return boolean that should always be true (legacy)
*/
static bool _connect(void){
WiFiManager wifiManager;
wifiManager.autoConnect("Window_LEDs_AP",ST_CONFIGURATION_DEFAULT_PASSWORD);
return true;
}
/**
* Start all services for the project:
* - MDNS if enabled
* - LED Service REST API
*/
static void _start_services(void){
#if ST_USE_MDNS
bool success = MDNS.begin(ST_MDNS_NAME);
if(success){
MDNS.addService("http", "tcp", LED_SERVICE_PORT);
}
#endif
rs_init();
}
/**
* Update all services for the project:
* - MDNS if enabled
* - LED Service REST API
*/
static void _update_services(void){
#if ST_USE_MDNS
MDNS.update();
#endif
rs_update();
}
/**
* Stop all services that require polling stop on Wifi failure
* - LED Service REST API
*/
static void _stop_services(void){
rs_stop();
}