|
| 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