Skip to content

Commit 741d22e

Browse files
committed
Add ESP32 output driver, and some convience defines to allow using the same example code on all platforms.
1 parent a4038f5 commit 741d22e

10 files changed

+177
-67
lines changed

EmbAJAX.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
44
*
5-
* Copyright (C) 2018 Thomas Friedrichsmeier
5+
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
66
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU Lesser General Public License as published
@@ -19,6 +19,9 @@
1919
*
2020
**/
2121

22+
// Avoid auto-including an output driver, here.
23+
#define EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION
24+
2225
#include "EmbAJAX.h"
2326

2427
#define ITOA_BUFLEN 8

EmbAJAX.h

+12-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
*
33
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
44
*
5-
* Copyright (C) 2018 Thomas Friedrichsmeier
5+
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
66
*
77
* This program is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU Lesser General Public License as published
@@ -18,8 +18,8 @@
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*
2020
**/
21-
#ifndef ARDUJAX_H
22-
#define ARDUJAX_H
21+
#ifndef EMBAJAX_H
22+
#define EMBAJAX_H
2323

2424
#include <Arduino.h>
2525

@@ -163,34 +163,15 @@ class EmbAJAXOutputDriverBase {
163163
uint16_t next_revision;
164164
};
165165

166-
#if defined (ESP8266) // TODO: Move this to extra header
167-
#include <ESP8266WebServer.h>
168-
/** @brief Output driver implementation for ESP8266WebServer */
169-
class EmbAJAXOutputDriverESP8266 : public EmbAJAXOutputDriverBase {
170-
public:
171-
/** To register an ESP8266WebServer with EmbAJAX, simply create a (globaL) instance of this class. */
172-
EmbAJAXOutputDriverESP8266(ESP8266WebServer *server) {
173-
EmbAJAXBase::setDriver(this);
174-
_server = server;
175-
}
176-
void printHeader(bool html) override {
177-
_server->setContentLength(CONTENT_LENGTH_UNKNOWN);
178-
if (html) {
179-
_server->send(200, "text/html", "");
180-
} else {
181-
_server->send(200, "text/json", "");
182-
}
183-
}
184-
void printContent(const char *content) override {
185-
if (content[0] != '\0') _server->sendContent(content); // NOTE: There seems to be a bug in the server when sending empty string.
186-
}
187-
const char* getArg(const char* name, char* buf, int buflen) override {
188-
_server->arg(name).toCharArray (buf, buflen);
189-
return buf;
190-
};
191-
private:
192-
ESP8266WebServer *_server;
193-
};
166+
// If the user has not #includ'ed a specific output driver implementation, make a good guess, here
167+
#if not defined (EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION)
168+
#if defined (ESP8266)
169+
#include <EmbAJAXOutputDriverESP8266.h>
170+
#elif defined (ESP32)
171+
#include <EmbAJAXOutputDriverESP32.h>
172+
#else
173+
#error No output driver available for this hardware (yet). Please implement your own (it is easy!) and submit a patch.
174+
#endif
194175
#endif
195176

196177
/** Convenience macro to set up an EmbAJAXPage, without counting the number of elements for the template. See EmbAJAXPage::EmbAJAXPage()

EmbAJAXOutputDriverESP32.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
*
3+
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
4+
*
5+
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or (at
10+
* your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
**/
21+
22+
#ifndef EMBAJAXOUTPUTDRIVERESP32_H
23+
#define EMBAJAXOUTPUTDRIVERESP32_H
24+
25+
#include <WebServer.h>
26+
#define EmbAJAXOutputDriverWebserverClass Webserver
27+
28+
#include <WiFi.h> // Makes the examples work cross-platform; not strictly needed
29+
30+
// the actual implementation
31+
#include "EmbAJAXOutputDriverGeneric.h"
32+
33+
#endif

EmbAJAXOutputDriverESP8266.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
4+
*
5+
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or (at
10+
* your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
**/
21+
22+
#ifndef EMBAJAXOUTPUTDRIVERESP8266_H
23+
#define EMBAJAXOUTPUTDRIVERESP8266_H
24+
25+
#include <ESP8266WebServer.h>
26+
#define EmbAJAXOutputDriverWebServerClass ESP8266WebServer
27+
#include <ESP8266WiFi.h> // Makes the examples work cross-platform; not strictly needed
28+
29+
// the actual implementation
30+
#include "EmbAJAXOutputDriverGeneric.h"
31+
32+
// for compatibility with early code examples
33+
#define EmbAJAXOutputDriverESP8266 EmbAJAXOutputDriver
34+
35+
#endif

EmbAJAXOutputDriverGeneric.h

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
*
3+
* EmbAJAX - Simplistic framework for creating and handling displays and controls on a WebPage served by an Arduino (or other small device).
4+
*
5+
* Copyright (C) 2018-2019 Thomas Friedrichsmeier
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as published
9+
* by the Free Software Foundation, either version 3 of the License, or (at
10+
* your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
**/
21+
22+
#ifndef EMBAJAXOUTPUTDRIVERGENERIC_H
23+
#define EMBAJAXOUTPUTDRIVERGENERIC_H
24+
25+
#if defined (EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION)
26+
#error Duplicate definition of output driver. Fix your include-directives.
27+
#endif
28+
#define EMBAJAX_OUTUPUTDRIVER_IMPLEMENTATION
29+
30+
#if not defined EmbAJAXOutputDriverWebServerClass
31+
#error Please define EmbAJAXOutputDriverWebServerClass or #include appropriate hardware specific driver
32+
#endif
33+
34+
/** @brief Output driver implementation. This implementation should work for most arduino web servers with minimal adjustmnets. */
35+
class EmbAJAXOutputDriver : public EmbAJAXOutputDriverBase {
36+
public:
37+
/** To register an WebServer with EmbAJAX, simply create a (globaL) instance of this class.
38+
@param server pointer to the server. The class of this is usually an auto-detected sensible default for the platform, e.g. ESP8266WebServer on ESP8266. */
39+
EmbAJAXOutputDriver(EmbAJAXOutputDriverWebServerClass *server) {
40+
EmbAJAXBase::setDriver(this);
41+
_server = server;
42+
}
43+
void printHeader(bool html) override {
44+
_server->setContentLength(CONTENT_LENGTH_UNKNOWN);
45+
if (html) {
46+
_server->send(200, "text/html", "");
47+
} else {
48+
_server->send(200, "text/json", "");
49+
}
50+
}
51+
void printContent(const char *content) override {
52+
if (content[0] != '\0') _server->sendContent(content); // NOTE: There seems to be a bug in the ESP8266 server when sending empty string.
53+
}
54+
const char* getArg(const char* name, char* buf, int buflen) override {
55+
_server->arg(name).toCharArray (buf, buflen);
56+
return buf;
57+
};
58+
private:
59+
EmbAJAXOutputDriverWebServerClass *_server;
60+
};
61+
62+
#endif

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,27 @@ The following additional features may be of interest (supported as of now):
5353
- Allows to insert your own custom CSS for styling (no styling in the lib).
5454
- Elements can be hidden, inputs can be disabled from the server (EmbAJAXBase::setVisible(), setEnabled()).
5555

56-
## Example sketch (compilable on ESP8266)
56+
### Hardware support
57+
58+
Currently there are output drivers for ESP8266 and ESP32. However, drivers are really easy to add. All that is needed is a very
59+
basic abstraction across some web server calls.
60+
61+
ESP8266 support is solid. ESP32-support is currently plagued by a bug in the ESP32's networking code, that causes incoming connections
62+
to fail under some circumstances (https://github.com/espressif/arduino-esp32/issues/1921).
63+
64+
65+
## Example sketch
5766

5867
Not really useful, but you know what to really do with a slider, and a display, right?
5968
Some further examples can be found in the examples folder.
6069

6170
```cpp
62-
#include <ESP8266WiFi.h>
63-
#include <ESP8266WebServer.h>
6471
#include <EmbAJAX.h>
6572

66-
// Set up web server, and register it with EmbAJAX
67-
ESP8266WebServer server(80);
68-
EmbAJAXOutputDriverESP8266 driver(&server);
73+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDirverWebServerClass is a
74+
// converience #define to allow using the same example code across platforms
75+
EmbAJAXOutputDriverWebServerClass server(80);
76+
EmbAJAXOutputDriver driver(&server);
6977

7078
// Define the main elements of interest as variables, so we can access them later in our sketch.
7179
EmbAJAXSlider slider("slider", 0, 500, 400); // slider, from 0 to 500, initial value 400

examples/Blink/Blink.ino

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
/* Basic usage example for EmbAJAX library:
22
* Provide a web interface to set built-in LED on, off, or blinking.
33
*
4-
* This example is based on an ESP8266 with Arduino core (https://github.com/esp8266/Arduino).
5-
*
64
* Note that ESP boards seems to be no real standard on which pin the builtin LED is on, and
75
* there is a very real chance that LED_BUILTIN is not defined, correctly, for your board.
86
* If you see no blinking, try changing the LEDPIN define (with an externally connected LED
9-
* on a known pin being the safest option).
7+
* on a known pin being the safest option). Similarly, on and off states are sometimes reversed.
108
*
119
* This example code is in the public domain (CONTRARY TO THE LIBRARY ITSELF). */
1210

13-
#include <ESP8266WiFi.h>
14-
#include <ESP8266WebServer.h>
1511
#include <EmbAJAX.h>
1612

1713
#define LEDPIN LED_BUILTIN
1814

19-
// Set up web server, and register it with EmbAJAX
20-
ESP8266WebServer server(80);
21-
EmbAJAXOutputDriverESP8266 driver(&server);
15+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDirverWebServerClass is a
16+
// converience #define to allow using the same example code across platforms
17+
EmbAJAXOutputDriverWebServerClass server(80);
18+
EmbAJAXOutputDriver driver(&server);
2219

2320
// Define the main elements of interest as variables, so we can access to them later in our sketch.
2421
const char* modes[] = {"On", "Blink", "Off"};

examples/Inputs/Inputs.ino

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,15 @@
33
* of this writing. The right hand side has displays to show some sort of value for each (after a
44
* round-trip to the server).
55
*
6-
* This example is based on an ESP8266 with Arduino core (https://github.com/esp8266/Arduino).
7-
*
86
* This example code is in the public domain (CONTRARY TO THE LIBRARY ITSELF). */
97

10-
#include <ESP8266WiFi.h>
11-
#include <ESP8266WebServer.h>
128
#include <EmbAJAX.h>
139
#include <EmbAJAXValidatingTextInput.h> // Fancier text input in a separate header file
1410

15-
// Set up web server, and register it with EmbAJAX
16-
ESP8266WebServer server(80);
17-
EmbAJAXOutputDriverESP8266 driver(&server);
11+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDirverWebServerClass is a
12+
// converience #define to allow using the same example code across platforms
13+
EmbAJAXOutputDriverWebServerClass server(80);
14+
EmbAJAXOutputDriver driver(&server);
1815

1916
#define BUFLEN 30
2017

examples/TwoPages/TwoPages.ino

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
/* Basic usage example for EmbAJAX library:
22
* Demonstrate using two pages with shared controls
33
*
4-
* This example is based on an ESP8266 with Arduino core (https://github.com/esp8266/Arduino).
5-
*
64
* This example code is in the public domain (CONTRARY TO THE LIBRARY ITSELF). */
75

8-
#include <ESP8266WiFi.h>
9-
#include <ESP8266WebServer.h>
106
#include <EmbAJAX.h>
117

128
#define LEDPIN LED_BUILTIN
139

14-
// Set up web server, and register it with EmbAJAX
15-
ESP8266WebServer server(80);
16-
EmbAJAXOutputDriverESP8266 driver(&server);
10+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDirverWebServerClass is a
11+
// converience #define to allow using the same example code across platforms
12+
EmbAJAXOutputDriverWebServerClass server(80);
13+
EmbAJAXOutputDriver driver(&server);
1714

1815
// Define the main elements of interest as variables, so we can access to them later in our sketch.
1916
EmbAJAXSlider shared_slider("shared_slider", 0, 1000, 100);

examples/Visibility/Visibility.ino

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@
33
* This example shows the semantics of calling setVisible() and setEnabled() on the various
44
* elements.
55
*
6-
* This example is based on an ESP8266 with Arduino core (https://github.com/esp8266/Arduino).
7-
*
86
* This example code is in the public domain (CONTRARY TO THE LIBRARY ITSELF). */
97

10-
#include <ESP8266WiFi.h>
11-
#include <ESP8266WebServer.h>
128
#include <EmbAJAX.h>
139

14-
// Set up web server, and register it with EmbAJAX
15-
ESP8266WebServer server(80);
16-
EmbAJAXOutputDriverESP8266 driver(&server);
10+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDirverWebServerClass is a
11+
// converience #define to allow using the same example code across platforms
12+
EmbAJAXOutputDriverWebServerClass server(80);
13+
EmbAJAXOutputDriver driver(&server);
1714

1815
// The radio groups will be used to control the state
1916
const char* visibility_opts[] = {"Normal", "Hidden", "Disabled"};

0 commit comments

Comments
 (0)