-
Notifications
You must be signed in to change notification settings - Fork 7
The HTTP Server
The Atm_esp8266_httpd_simple component provides an asynchronous event driven web server with a simple Automaton interface.
#include <Automaton.h>
#include <Atm_esp8266.h>
// Control a led on pin D5 with this simple webserver
Atm_esp8266_httpd_simple server( 80 );
Atm_led led;
char ap_ssid[] = "MySSID";
char ap_password[] = "MyPASSWORD";
void setup() {
Serial.begin( 9600 );
Serial.println( "Connecting to WIFI" );
// The led to be controlled
led.begin( D5 );
// The Wifi machine manages the wifi connection
wifi.begin( ap_ssid, ap_password )
.onChange( true, [] ( int idx, int v, int up ) {
Serial.print( "Connected to Wifi, browse to http://");
Serial.println( wifi.ip() );
server.start(); // Time to start the web server
})
.onChange( false, [] ( int idx, int v, int up ) {
Serial.println( "Lost Wifi connection");
})
.led( LED_BUILTIN, true ) // Esp8266 built in led shows wifi status
.start();
// The Http server machine handles incoming requests
server.begin()
.onRequest( "/on", led, led.EVT_ON )
.onRequest( "/off", led, led.EVT_OFF )
.onRequest( "/blink", led, led.EVT_START )
.onRequest( "/", [] ( int idx, int v, int up ) {
server.send(
"<!DOCTYPE html><html><body>"
"<a href='on'>On</a><br>"
"<a href='off'>Off</a><br>"
"<a href='blink'>Blink</a><br>"
"</body></html>"
);
});
}
void loop() {
automaton.run();
}
Initializes the HTTP server state machine. Note that the web server will only start listening for incoming connections after an EVT_START event (or the start() method).
#include <Automaton.h>
#include <Atm_esp8266.h>
Atm_esp8266_httpd_simple server( 80 );
Atm_led led;
void setup() {
led.begin( D5 );
wifi.begin( "MySSID", "MyPASSWORD" )
.onChange( true, server, server.EVT_START )
.start();
server.begin()
.onRequest( "/toggle", led, led.EVT_TOGGLE );
}
void loop() {
automaton.run();
}
Sets the content that is returned when a URL is accessed. The default reply is a two character string: 'OK'.
// Reply 'Hello world' to every request to every URL
server.begin()
.reply( "Hello world" )
.onRequest();
The reply() content is only used if no other content is sent by the handler via the send() method.
// Reply 'Hello world' to every request to every URL except /on
server.begin()
.reply( "Hello world" )
.onRequest()
.onRequest( "/on", [] ( int idx, int v, int up ) {
server.send( "Led is now on" );
led.on();
});
Sets a handler for a certain url.
Sets a handler for all urls. Also useful to create a 404 page.
Catchall for all urls. This form of onRequest() makes sure all incoming HTTP requests receive the installed reply(). Specific onRequest calls take precedence.
server.begin()
.onRequest( "/on", led, led.EVT_ON )
.onRequest( "/off", led, led.EVT_OFF )
.onRequest( [] ( int idx, int v, int up ) {
server.send( 404, "text/plain", "Page not found!" );
});
Send a result code, content type and content string to the client web browser.
server.begin()
.onRequest( "/on", led, led.EVT_ON )
.onRequest( "/off", led, led.EVT_OFF )
.onRequest( [] ( int idx, int v, int up ) {
server.send( 404, "text/plain", "Page not found!" );
});
Send a content string to the web browser using the default text/html
content-type and result code 200
.
server.begin()
.onRequest( "/hello", [] ( int idx, int v, int up ) {
server.send( "<!DOCTYPE html><html><body>Hello world!</body></html>" );
});
Starts the webserver (which is not started by begin
).
Triggers an EVT_START event.
wifi.begin( ap_ssid, ap_password )
.onChange( true, [] ( int idx, int v, int up ) {
Serial.print( "Connected to Wifi, browse to http://");
Serial.println( wifi.ip() );
server.start();
})
.start();
server.begin()
.onRequest( "/on", led, led.EVT_ON );
Returns the number of GET or POST arguments sent in the request.
#include <Automaton.h>
#include <Atm_esp8266.h>
Atm_esp8266_httpd_simple server( 80 );
void setup() {
Serial.begin( 9600 );
wifi.begin( "MySSID", "MyPASSWORD" )
.onChange( true, server, server.EVT_START )
.start();
server.begin()
.onRequest( [] ( int idx, int v, int up) {
Serial.print( "Request: " );
Serial.println( server.uri() );
for ( int i = 0; i < server.args(); i++ ) {
Serial.print( server.argName( i ) );
Serial.print( " = " );
Serial.println( server.arg( i ) );
}
server.send( "<!DOCTYPE html><html><body>OK!</body></html>" );
});
}
void loop() {
automaton.run();
}
Returns the value of the argument indicated by idx
.
Returns the value of the name
argument.
Serial.println( server.arg( "id" ) );
Returns the name of the argument indicated by idx
.
Returns the uri of the current request.
// for http://12.23.34.45/on?id=6
Serial.println( server.uri() ); // Prints "/on"
Serial.println( server.arg( "id" ) ); // Prints '6'
Same as the start() method.
server.trigger( server.EVT_START );
button.begin( D2 )
.onPress( server, server.EVT_START );
To monitor the behavior of this machine you may log state change events to a Stream object like Serial.
Serial.begin( 9600 );
server.trace( Serial );