Skip to content

The HTTP Server

Tinkerspy edited this page Nov 20, 2016 · 17 revisions

The HTTP server

The Atm_esp8266_httpd_simple component provides an asynchronous event driven web server with a simple Automaton interface.

Synopsis

#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();
}

Atm_esp8266_httpd_simple & begin( void )

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();
}

Atm_esp8266_httpd_simple & reply( const char * s )

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();
    });

Atm_esp8266_httpd_simple & onRequest( url, {connector}, {connector-arg} )

Sets a handler for a certain url.

Atm_esp8266_httpd_simple & onRequest( {connector}, {connector-arg} )

Sets a handler for all urls. Also useful to create a 404 page.

Atm_esp8266_httpd_simple & onRequest()

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!" );   
    });

Atm_esp8266_httpd_simple & send( int result, String content_type, String content )

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!" );   
    });

Atm_esp8266_httpd_simple & send( String content )

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>" );   
    });

Atm_esp8266_httpd_simple & start()

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

int args()

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();
}

String arg( int idx )

Returns the value of the argument indicated by idx.

String arg( String name )

Returns the value of the name argument.

  Serial.println( server.arg( "id" ) );

String argName()

Returns the name of the argument indicated by idx.

String uri()

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'

EVT_START

Same as the start() method.

  server.trigger( server.EVT_START );

  button.begin( D2 )
    .onPress( server, server.EVT_START );

Atm_esp8266_httpd_simple & trace()

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