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 __OmWebServer__
32 #define __OmWebServer__
33 
34 #include <vector>
35 #ifdef ARDUINO_ARCH_ESP8266
36 #include <ESP8266WiFi.h>
37 #endif
38 #ifdef ARDUINO_ARCH_ESP32
39 #include <WiFi.h>
40 #endif
41 
42 #include "OmWebPages.h"
43 #include "OmNtp.h"
44 #include "OmXmlWriter.h" // for the streaming api
45 
46 typedef const char *(* OmRequestHandler)(const char *request);
47 typedef void (* OmConnectionStatus)(const char *ssid, bool trying, bool failure, bool success);
48 
49 #ifdef LED_BUILTIN
50 #define OM_DEFAULT_LED LED_BUILTIN
51 #else
52 #define OM_DEFAULT_LED -1
53 #endif
54 
56 
58 class OmWebServer : public OmIByteStream
59 {
60  OmWebServerPrivates *p = NULL;
61  void initiateConnectionTry(String wifi, String password);
62  void maybeStatusCallback(bool trying, bool failure, bool success);
63 public:
64  OmWebServer(int port = 80);
65  ~OmWebServer();
66 
68  void setVerbose(int verbose); // turn off to print less stuff
69 
76  void setAccessPoint(String ssid, String password);
77 
79  void addWifi(String ssid, String password);
81  void clearWifis();
82 
84  void setBonjourName(String bonjourName);
86  String getBonjourName();
87 
88  void setHandler(OmRequestHandler requestHandler);
89 
91  void setHandler(OmWebPages &requestHandler);
93  void setStatusCallback(OmConnectionStatus statusCallback);
94 
96  void setNtp(OmNtp *ntp);
97 
99  void setPort(int port);
100 
102  void setStatusLedPin(int statusLedPin);
103 
104  void end();
105 
107  void glitch(int k);
108 
114  int tick();
115 
116  const char *getSsid();
117  int getPort();
118  unsigned int getIp();
119  int getClientPort();
120  unsigned int getClientIp();
121  unsigned int getTicks();
122 
124  bool isWifiConnected();
126  long long uptimeMillis();
127 
128  bool put(uint8_t) override;
129  bool done() override;
130  bool put(const char *s); // helpers to send longer amounts & strings
131  bool put(uint8_t *d, int size); // helpers to send longer amounts & strings
132 private:
133  /* public for callback purposes, not user-useful */
134  void handleRequest(String request, WiFiClient &client);
135 
136  /* state machine business. */
137  void owsBegin();
138 
139  int pollForClient();
140 
141 
142 };
143 
144 #endif // __OmWebServer__
OmWebServer::setStatusLedPin
void setStatusLedPin(int statusLedPin)
changes or disables the blinking status LED. Use -1 to disable.
Definition: OmWebServer.cpp:612
OmWebServer::setPort
void setPort(int port)
defaults to 80
Definition: OmWebServer.cpp:172
OmWebServer::setBonjourName
void setBonjourName(String bonjourName)
advertises on local network as bonjourName.local
Definition: OmWebServer.cpp:130
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:135
OmWebServer::glitch
void glitch(int k)
simulate a network trouble. 1==disconnect the wifi
Definition: OmWebServer.cpp:283
OmWebServer::setStatusCallback
void setStatusCallback(OmConnectionStatus statusCallback)
receive notifications of changes to wifi status
Definition: OmWebServer.cpp:166
OmWebServer::isWifiConnected
bool isWifiConnected()
is it?
Definition: OmWebServer.cpp:787
OmNtp
Definition: OmNtp.h:48
OmWebServer::addWifi
void addWifi(String ssid, String password)
add to the list of known networks to try.
Definition: OmWebServer.cpp:141
OmWebServer::setVerbose
void setVerbose(int verbose)
OmWebServer by default prints much status to serial; set to 0 to cut that out.
Definition: OmWebServer.cpp:782
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:431
OmWebServer::setAccessPoint
void setAccessPoint(String ssid, String password)
Definition: OmWebServer.cpp:122
OmIByteStream
Definition: OmXmlWriter.h:53
OmWebServer::uptimeMillis
long long uptimeMillis()
arduino's millis() will overflow after 50 days. Not this baby.
Definition: OmWebServer.cpp:793
OmWebServer
Manages wifi connection, and forwarding http requests to a handler, typically OmWebPages.
Definition: OmWebServer.h:59
OmWebServer::clearWifis
void clearWifis()
reset the list of known networks to try, to empty again.
Definition: OmWebServer.cpp:148
OmWebPages
A class that routes and serves web pages, and manages control values, typically works with OmWebServe...
Definition: OmWebPages.h:122
OmWebServer::setNtp
void setNtp(OmNtp *ntp)
Introduce an NTP object to the server.
Definition: OmWebServer.cpp:777
OmWebServer::put
bool put(uint8_t) override
emit a single byte, overridden by any implementation
Definition: OmWebServer.cpp:617