OmEspHelpers
OmWebServer.h
1 /*
2  * OmWebServer.h
3  * 2016-11-26 dvb
4  *
5  * This class implements a web server built on top of
6  * the excellent ESP8266WebServer.
7  *
8  * This code is for Arduino ESP8266, and uses ESP8266WebServer & ESP8266WiFi
9  *
10  * This class combines the Wifi Setup and retry loop
11  * and simplifies the web server API. The result is
12  * easier to use, but with less flexibility.
13  *
14  * It incorporates some feedback and status display on
15  * the built-in LED (can be disabled).
16  *
17  * It also attempts to guide the user with helpful
18  * serial messages and default served pages.
19  *
20  * EXAMPLE
21  *
22  * OmWebServer w;
23  *
24  * in setup(): w.addWifi("My Network", "password1234");
25  * in loop(): w.tick();
26  *
27  * The IP address will be printed to the Serial console.
28  * The IP address will also be "blinked" on the BUILTIN_LED.
29  */
30 
31 #ifndef NOT_ARDUINO
32 
33 #ifndef __OmWebServer__
34 #define __OmWebServer__
35 
36 #include <vector>
37 #ifdef ARDUINO_ARCH_ESP8266
38 #include <ESP8266WiFi.h>
39 #endif
40 #ifdef ARDUINO_ARCH_ESP32
41 #include <WiFi.h>
42 #endif
43 
44 #include "OmWebPages.h"
45 #include "OmNtp.h"
46 #include "OmXmlWriter.h" // for the streaming api
47 
48 typedef const char *(* OmRequestHandler)(const char *request);
49 typedef void (* OmConnectionStatus)(const char *ssid, bool trying, bool failure, bool success);
50 
51 #ifdef LED_BUILTIN
52 #define OM_DEFAULT_LED LED_BUILTIN
53 #else
54 #define OM_DEFAULT_LED -1
55 #endif
56 
58 
62 {
63 public:
64 };
65 
66 class OmWebServer : public OmIByteStream
67 {
68  OmWebServerPrivates *p = NULL;
69  void initiateConnectionTry(String wifi, String password);
70  void maybeStatusCallback(bool trying, bool failure, bool success);
71 public:
72  OmWebServer(int port = 80);
73  ~OmWebServer();
74 
76  void setVerbose(int verbose); // turn off to print less stuff
77 
93  void setAccessPoint(String ssid, String password, int secondsUntilReboot = 0);
94 
96  bool isAccessPoint();
97 
99  void addWifi(String ssid, String password);
101  void clearWifis();
102 
104  void setBonjourName(String bonjourName);
106  String getBonjourName();
107 
108  void setHandler(OmRequestHandler requestHandler);
109 
111  void setHandler(OmWebPages &requestHandler);
113  void setStatusCallback(OmConnectionStatus statusCallback);
114 
116  void setNtp(OmNtp *ntp);
117 
119  void setPort(int port);
120 
122  void setStatusLedPin(int statusLedPin);
123 
124  void end();
125 
127  void glitch(int k);
128 
134  int tick();
135 
136  const char *getSsid();
137  int getPort();
138  uint32_t getIp();
139  int getClientPort();
140  unsigned int getTicks();
141 
143  bool isWifiConnected();
145  long long uptimeMillis();
146 
147  bool put(uint8_t) override;
148  bool done() override;
149  bool put(const char *s); // helpers to send longer amounts & strings
150  bool put(uint8_t *d, int size); // helpers to send longer amounts & strings
151 
152  // queue up a system restart a little in the future
153  void rebootIn(int millis);
154 
155  // handy strings for esp wifi status
156  static const char *statusString(int wifiStatus);
157 
158  static OmWebServer *s; // most recent created. Really, the only one.
159 private:
160  /* public for callback purposes, not user-useful */
161  void handleRequest(String request, WiFiClient &client);
162 
163  /* state machine business. */
164  void owsBegin();
165 
166  int pollForClient();
167 
168 
169 };
170 
171 #endif // __OmWebServer__
172 
173 #endif
OmWebServer::setStatusLedPin
void setStatusLedPin(int statusLedPin)
changes or disables the blinking status LED. Use -1 to disable.
Definition: OmWebServer.cpp:681
OmWebServerStatus
Manages wifi connection, and forwarding http requests to a handler, typically OmWebPages.
Definition: OmWebServer.h:62
OmWebServer::setPort
void setPort(int port)
defaults to 80
Definition: OmWebServer.cpp:188
OmWebServer::setBonjourName
void setBonjourName(String bonjourName)
advertises on local network as bonjourName.local
Definition: OmWebServer.cpp:143
OmWebServerPrivates
This class reduces clutter in the public header file.
Definition: OmWebServer.cpp:36
OmWebServer::getBonjourName
String getBonjourName()
get the current bonjour name
Definition: OmWebServer.cpp:148
OmWebServer::glitch
void glitch(int k)
simulate a network trouble. 1==disconnect the wifi
Definition: OmWebServer.cpp:323
OmWebServer::setAccessPoint
void setAccessPoint(String ssid, String password, int secondsUntilReboot=0)
Definition: OmWebServer.cpp:130
OmWebServer::setStatusCallback
void setStatusCallback(OmConnectionStatus statusCallback)
receive notifications of changes to wifi status
Definition: OmWebServer.cpp:182
OmWebServer::isWifiConnected
bool isWifiConnected()
is it?
Definition: OmWebServer.cpp:844
OmNtp
Definition: OmNtp.h:74
OmWebServer::addWifi
void addWifi(String ssid, String password)
add to the list of known networks to try.
Definition: OmWebServer.cpp:154
OmWebServer::setVerbose
void setVerbose(int verbose)
OmWebServer by default prints much status to serial; set to 0 to cut that out.
Definition: OmWebServer.cpp:839
OmWebServer::tick
int tick()
You must call this in loop() to give time to run. This allows networks to be joined and rejoined,...
Definition: OmWebServer.cpp:469
OmIByteStream
Definition: OmXmlWriter.h:53
OmWebServer::uptimeMillis
long long uptimeMillis()
arduino's millis() will overflow after 50 days. Not this baby.
Definition: OmWebServer.cpp:850
OmWebServer
Definition: OmWebServer.h:67
OmWebServer::clearWifis
void clearWifis()
reset the list of known networks to try, to empty again.
Definition: OmWebServer.cpp:164
OmWebPages
A class that routes and serves web pages, and manages control values, typically works with OmWebServe...
Definition: OmWebPages.h:123
OmWebServer::setNtp
void setNtp(OmNtp *ntp)
Introduce an NTP object to the server.
Definition: OmWebServer.cpp:834
OmWebServer::isAccessPoint
bool isAccessPoint()
return true if we're currently in access point mode
Definition: OmWebServer.cpp:861
OmWebServer::put
bool put(uint8_t) override
emit a single byte, overridden by any implementation
Definition: OmWebServer.cpp:686