RSeries astromech firmware
WifiAccess.h
Go to the documentation of this file.
1 #ifndef WifiAccess_h
2 #define WifiAccess_h
3 
4 #include "ReelTwo.h"
5 #include "core/AnimatedEvent.h"
6 #include <WiFi.h>
7 #include <WiFiAP.h>
8 #include <vector>
9 
25 class WifiAccess : public AnimatedEvent
26 {
27 public:
32  WifiAccess(uint32_t interval = 30000) :
33  fWifiAP(""),
34  fWifiPassword(""),
35  fWifiInterval(interval),
36  fWifiSoftAP(false),
37  fWifiEnabled(false),
39  fWifiActive(false)
40  {
41  }
42 
47  WifiAccess(const String& wifiAP, const String& wifiPassword, bool accessPoint = true, bool enabled = true, uint32_t interval = 30000) :
48  fWifiAP(wifiAP),
49  fWifiPassword(wifiPassword),
50  fWifiInterval(interval),
51  fWifiSoftAP(accessPoint),
54  fWifiActive(false)
55  {
56  }
57 
58  void setNetworkCredentials(const String& wifiAP, const String& wifiPassword, bool accessPoint, bool enabled)
59  {
60  fWifiAP = wifiAP;
61  fWifiPassword = wifiPassword;
62  fWifiSoftAP = accessPoint;
64  }
65 
66  bool isConnected()
67  {
68  bool connected = false;
69  if (fWifiActive)
70  {
71  connected = (fWifiSoftAP) ? true : WiFi.isConnected();
72  }
73  return connected;
74  }
75 
76  IPAddress getIPAddress()
77  {
78  if (isConnected())
79  {
80  return (fWifiSoftAP) ? WiFi.softAPIP() : WiFi.localIP();
81  }
82  return IPAddress();
83  }
84 
85  String getMacAddress()
86  {
87  return WiFi.macAddress();
88  }
89 
90  String getWifiAP()
91  {
92  return fWifiAP;
93  }
94 
95  String getWifiPassword()
96  {
97  return fWifiPassword;
98  }
99 
100  bool isSoftAP()
101  {
102  return fWifiSoftAP;
103  }
104 
105  void setReconnect(bool reconnect)
106  {
107  fWifiAttemptReconnect = reconnect;
108  }
109 
110  virtual void animate()
111  {
112  if (!fWifiEnabled)
113  return;
114  if (fWifiSoftAP)
115  {
116  if (!fWifiActive)
117  {
118  DEBUG_PRINTLN("Wifi Access Point: "+fWifiAP);
119  DEBUG_PRINTLN("Password: "+fWifiPassword);
120  WiFi.softAP(fWifiAP.c_str(), fWifiPassword.c_str());
121  IPAddress myIP = WiFi.softAPIP();
122  fWifiActive = true;
123  notifyConnected();
124  }
125  }
126  else if (!fWifiStarted)
127  {
128  DEBUG_PRINTLN("Joining: "+fWifiAP);
129  WiFi.begin(fWifiAP.c_str(), fWifiPassword.c_str());
130  fWifiStarted = true;
131  }
132  else if (!WiFi.isConnected())
133  {
134  uint32_t now = millis();
135  if (fWifiActive)
136  {
138  fWifiActive = false;
139  }
141  {
142  DEBUG_PRINTLN("Reconnecting: "+fWifiAP);
143  WiFi.disconnect();
144  WiFi.reconnect();
145  fWifiPrevMillis = now;
146  }
147  }
148  else if (!fWifiWasConnected || !fWifiActive)
149  {
150  fWifiWasConnected = true;
151  fWifiActive = true;
152  notifyConnected();
153  }
154  }
155 
156  inline bool enabled()
157  {
158  return fWifiEnabled;
159  }
160 
161  void setEnabled(bool enabled = true)
162  {
164  }
165 
166  class Notify
167  {
168  public:
169  virtual void wifiConnected(WifiAccess &access) = 0;
170  virtual void wifiDisconnected(WifiAccess &access) = 0;
171  };
172 
174  {
175  fWifiNotify.push_back(client);
176  }
177 
178  void notifyWifiConnected(void (*notify)(WifiAccess& wifi))
179  {
180  fNotifyConnectedCallback = notify;
181  }
182 
183  void notifyWifiDisconnected(void (*notify)(WifiAccess& wifi))
184  {
186  }
187 
188 protected:
189  String fWifiAP;
191  uint32_t fWifiInterval;
192  uint32_t fWifiPrevMillis = 0;
194  bool fWifiStarted = false;
195  bool fWifiEnabled = true;
197  bool fWifiActive = false;
198  bool fWifiWasConnected = false;
199  std::vector<Notify*> fWifiNotify;
200  void (*fNotifyConnectedCallback)(WifiAccess& wifi) = nullptr;
201  void (*fNotifyDisconnectedCallback)(WifiAccess& wifi) = nullptr;
202 
204  {
205  for (auto client: fWifiNotify)
206  {
207  client->wifiConnected(*this);
208  }
209  if (fNotifyConnectedCallback != nullptr)
211  }
212 
214  {
215  if (fNotifyDisconnectedCallback != nullptr)
217  for (auto client: fWifiNotify)
218  {
219  client->wifiDisconnected(*this);
220  }
221  }
222 };
223 
224 #endif
WifiAccess::notifyDisconnected
void notifyDisconnected()
Definition: WifiAccess.h:213
WifiAccess::setNetworkCredentials
void setNetworkCredentials(const String &wifiAP, const String &wifiPassword, bool accessPoint, bool enabled)
Definition: WifiAccess.h:58
WifiAccess::fWifiAttemptReconnect
bool fWifiAttemptReconnect
Definition: WifiAccess.h:196
WifiAccess::WifiAccess
WifiAccess(const String &wifiAP, const String &wifiPassword, bool accessPoint=true, bool enabled=true, uint32_t interval=30000)
Constructor.
Definition: WifiAccess.h:47
WifiAccess
Definition: WifiAccess.h:25
WifiAccess::fWifiPassword
String fWifiPassword
Definition: WifiAccess.h:190
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
WifiAccess::Notify
Definition: WifiAccess.h:166
AnimatedEvent.h
WifiAccess::notifyConnected
void notifyConnected()
Definition: WifiAccess.h:203
WifiAccess::fWifiWasConnected
bool fWifiWasConnected
Definition: WifiAccess.h:198
WifiAccess::fWifiAP
String fWifiAP
Definition: WifiAccess.h:189
WifiAccess::fWifiSoftAP
bool fWifiSoftAP
Definition: WifiAccess.h:193
WifiAccess::fNotifyConnectedCallback
void(* fNotifyConnectedCallback)(WifiAccess &wifi)
Definition: WifiAccess.h:200
WifiAccess::getWifiAP
String getWifiAP()
Definition: WifiAccess.h:90
WifiAccess::getMacAddress
String getMacAddress()
Definition: WifiAccess.h:85
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
WifiAccess::isSoftAP
bool isSoftAP()
Definition: WifiAccess.h:100
WifiAccess::setReconnect
void setReconnect(bool reconnect)
Definition: WifiAccess.h:105
WifiAccess::fWifiStarted
bool fWifiStarted
Definition: WifiAccess.h:194
WifiAccess::notifyWifiConnected
void notifyWifiConnected(void(*notify)(WifiAccess &wifi))
Definition: WifiAccess.h:178
WifiAccess::fWifiEnabled
bool fWifiEnabled
Definition: WifiAccess.h:195
WifiAccess::getIPAddress
IPAddress getIPAddress()
Definition: WifiAccess.h:76
WifiAccess::setEnabled
void setEnabled(bool enabled=true)
Definition: WifiAccess.h:161
WifiAccess::enabled
bool enabled()
Definition: WifiAccess.h:156
WifiAccess::fWifiActive
bool fWifiActive
Definition: WifiAccess.h:197
WifiAccess::fWifiPrevMillis
uint32_t fWifiPrevMillis
Definition: WifiAccess.h:192
WifiAccess::notifyWifiDisconnected
void notifyWifiDisconnected(void(*notify)(WifiAccess &wifi))
Definition: WifiAccess.h:183
WifiAccess::addNotify
void addNotify(WifiAccess::Notify *client)
Definition: WifiAccess.h:173
WifiAccess::animate
virtual void animate()
Subclasses must implement this function to run through a single frame of animation/activity.
Definition: WifiAccess.h:110
WifiAccess::WifiAccess
WifiAccess(uint32_t interval=30000)
Constructor.
Definition: WifiAccess.h:32
WifiAccess::fWifiInterval
uint32_t fWifiInterval
Definition: WifiAccess.h:191
WifiAccess::isConnected
bool isConnected()
Definition: WifiAccess.h:66
WifiAccess::Notify::wifiConnected
virtual void wifiConnected(WifiAccess &access)=0
WifiAccess::getWifiPassword
String getWifiPassword()
Definition: WifiAccess.h:95
WifiAccess::fWifiNotify
std::vector< Notify * > fWifiNotify
Definition: WifiAccess.h:199
WifiAccess::fNotifyDisconnectedCallback
void(* fNotifyDisconnectedCallback)(WifiAccess &wifi)
Definition: WifiAccess.h:201
WifiAccess::Notify::wifiDisconnected
virtual void wifiDisconnected(WifiAccess &access)=0