Skip to content

Commit 5ef32aa

Browse files
committed
Add example and documenation for using EmbAJAX with the EthernetWebServer library
1 parent c01f2d6 commit 5ef32aa

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ The following additional features may be of interest (supported as of now):
5757

5858
### Hardware support
5959

60-
Currently there are output drivers for ESP8266, ESP32, and Raspberry Pi Pico. However, drivers are really easy to add. All that is needed is a very
61-
basic abstraction across some web server calls.
60+
Currently EmbAJAX will work, without additional configuration using WiFi on ESP8266, ESP32, and Raspberry Pi Pico.
61+
62+
For the general approach on using EmbAJAX with different hardware, see the Blink_Ethernet example. This relies on the EthernetWebServer library,
63+
which supports a large number of different boards, including ATMEGA 2560, Teensy, etc. In a similar fashion, it should be possible to utilize the
64+
WiFiWebServer library for boards that do not include native WiFi (this latter claim has not currently been tested).
65+
66+
Should your hardware need more custom tweaking, or you wish to use a different webserver library, drivers are really easy to add.
67+
All that is needed is a very basic abstraction across some web server calls.
6268

6369
#### ESP32 quirks and workaround
6470

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* Demonstrate usage of with of EmbAJAX over an ethernet connection.
2+
*
3+
* This example is mostly identical to the basic Blink example, but shows how to specify
4+
* a different output driver. In this case the class EthernetWebServer from the library
5+
* of the same name is used (compatible with many different boards, including ATMEGA 2560).
6+
* In a similar fashion additionals boards can easily be supported, as long as a compatible
7+
* webserver implementation is available.
8+
*
9+
* For details on how to properly connect, and configure your ethernet shield, refer to the
10+
* documentation of the Ethernet library. The example used here should work in many cases,
11+
* however, and will create an EmbAJAX server listening on http://192.168.1.177 .
12+
*
13+
* This example code is in the public domain (CONTRARY TO THE LIBRARY ITSELF). */
14+
15+
#include <EthernetWebServer.h>
16+
#define EmbAJAXOutputDriverWebServerClass EthernetWebServer
17+
#include <EmbAJAXOutputDriverGeneric.h>
18+
#include <EmbAJAX.h>
19+
20+
// default for most Arduino ethernet shields
21+
#define ETHERNET_CS_PIN 10
22+
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
23+
IPAddress ip(192, 168, 1, 177);
24+
25+
#define LEDPIN LED_BUILTIN
26+
27+
// Set up web server, and register it with EmbAJAX. Note: EmbAJAXOutputDriverWebServerClass is a
28+
// convenience #define to allow using the same example code across platforms
29+
EmbAJAXOutputDriverWebServerClass server(80);
30+
EmbAJAXOutputDriver driver(&server);
31+
32+
// Define the main elements of interest as variables, so we can access to them later in our sketch.
33+
const char* modes[] = {"On", "Blink", "Off"};
34+
EmbAJAXRadioGroup<3> mode("mode", modes);
35+
EmbAJAXSlider blinkfreq("blfreq", 0, 1000, 100); // slider, from 0 to 1000, initial value 100
36+
37+
// Define a page (named "page") with our elements of interest, above, interspersed by some uninteresting
38+
// static HTML. Note: MAKE_EmbAJAXPage is just a convenience macro around the EmbAJAXPage<>-class.
39+
MAKE_EmbAJAXPage(page, "EmbAJAX example - Blink", "",
40+
new EmbAJAXStatic("<h1>Control the builtin LED</h1><p>Set the LED to: "),
41+
&mode,
42+
new EmbAJAXStatic("</p><p>Blink frequency: <i>SLOW</i>"),
43+
&blinkfreq,
44+
new EmbAJAXStatic("<i>FAST</i></p>")
45+
)
46+
47+
void setup() {
48+
// Init Ethernet. For many
49+
Ethernet.init(ETHERNET_CS_PIN);
50+
Ethernet.begin(mac, ip);
51+
52+
// Tell the server to serve our EmbAJAX test page on root
53+
// installPage() abstracts over the (trivial but not uniform) WebServer-specific instructions to do so
54+
driver.installPage(&page, "/", updateUI);
55+
server.begin();
56+
57+
pinMode(LEDPIN, OUTPUT);
58+
}
59+
60+
void updateUI() {
61+
// Enabled / disable the slider. Note that you could simply do this inside the loop. However,
62+
// placing it here makes the client UI more responsive (try it).
63+
blinkfreq.setEnabled(mode.selectedOption() == 1);
64+
}
65+
66+
void loop() {
67+
// handle network. loopHook() simply calls server.handleClient(), in most but not all server implementations.
68+
driver.loopHook();
69+
70+
// And these lines are all you have to write for the logic: Access the elements as if they were plain
71+
// local controls
72+
if (mode.selectedOption() == 1) { // blink
73+
digitalWrite(LEDPIN, (millis() / (1100 - blinkfreq.intValue())) % 2);
74+
} else { // on or off
75+
digitalWrite(LEDPIN, mode.selectedOption() != 0);
76+
}
77+
}

0 commit comments

Comments
 (0)