Skip to content

The HTTP Client

Tinkerspy edited this page Mar 6, 2017 · 10 revisions

The HTTP Client

The Atm_esp8266_httpc_simple component provides an asynchronous event driven web client with a simple Automaton interface.

Synopsis

#include <Automaton.h>
#include <Atm_esp8266.h>

Atm_esp8266_httpc_simple client1, client2;
Atm_button button1, button2;

// Two buttons on D2 & D7 send out /on and /off web requests
// Could be used as a remote control for the esp_blink example

char ap_ssid[] = "MySSID";
char ap_password[] = "MyPASSWORD";
char client_host[] = "172.22.22.220";

void setup() {
  
  wifi.begin( ap_ssid, ap_password ) 
    .led( LED_BUILTIN, true ) // Esp8266 built in led shows wifi status
    .start();  

  client1.begin( client_host )
    .get( "/on" );        
  client2.begin( client_host )
    .get( "/off" );
        
  button1.begin( D2 )
    .onPress( client1, client1.EVT_START );
  button2.begin( D7 )
    .onPress( client2, client2.EVT_START );

}

void loop() {
  automaton.run();
}

Atm_esp8266_httpc_simple & begin( const char * host, int port = 80 )

Initializes a http client by setting the hostname (or ip address) and port to connect to. The connection is not actually created before an EVT_START event is received (or the start() method is called).

  client.begin( "172.22.22.100", 1080 );

Atm_esp8266_httpc_simple & onStart( {connector}, {connector-arg} )

This method is called just before the http request is executed. If the connector is a callback the EVT_START subcode will be passed in the v argument which allows us to prepare the URL's inside the component definition.

void setup() {

  client.begin( "172.22.22.100" )
    .onStart( []( int idx, int v, int ) {
      switch ( v ) {
        case 0:
          client.get( "/on" );
          return;
        case 1:
          client.get( "/off" );
          return;
      }
    });
    
  button1.begin( D2 )
    .onPress( client, client.EVT_START + 0 );
  button2.begin( D7 )
    .onPress( client, client.EVT_START + 1 );

}

This way the URL's to be called can be confined to the 'client' section. The actual URL to trigger is dependent on the EVT_START offset. This fits nicely within the Automaton event model.

Atm_esp8266_httpc_simple & onFinish( {connector}, {connector-arg} )

This method is called after the http request is finished. The (optional) subcode used to trigger the request is passed as the 'v' argument. In this handler the requested content, headers and result code are available.

void setup() {

  client.begin( "172.22.22.100" )
    .get( "/status" )
    .onFinish( []( int idx, int v, int ) {
       if ( client.is_success() ) {
         Serial.print( "Result: ");
         Serial.println( client.resultCode() );
         Serial.print( "Headers: ");
         Serial.println( client.headers() );
         Serial.print( "Content: ");
         Serial.println( client.content() );
       }
    });

}

Atm_esp8266_httpc_simple & start( int subCode = 0 )

Starts a client connection with an optional subcode (equivalent to EVT_START + subCode ).

enum { ON, OFF }; // This time we use enums instead of bare constants

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .onStart( []( int idx, int v, int ) {
      switch ( v ) {
        case ON:
          client.get( "/on" );
          return;
        case OFF:
          client.get( "/off" );
          return;
      }
    });
    

  client.start( OFF ); // Triggers the "/off" request
    
}

Atm_esp8266_httpc_simple & get( String path, String data = "", int maxResponseSize = 1024 )

Sets the URL, data and method (GET) to be used in the next HTTP request. The maxResponseSize argument sets the maximum bytes to store from the HTTP response. This bytecount includes the result code and headers.

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .get( "/on", "led=4&brightness=50" );
}

Atm_esp8266_httpc_simple & post( String path, String data = "", int maxResponseSize = 1024 )

Sets the URL, data and method (POST) to be used in the next HTTP request. The maxResponseSize argument sets the maximum bytes to store from the HTTP response. This bytecount includes the result code and headers.

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .post( "/on", "led=4&brightness=50" );
}

int responseCode()

Returns the responseCode of the last HTTP request as an int value.

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .get( "/status" )
    .onFinish( [] ( int idx, int v, int up ) {
      Serial.print( "HTTP result: ");
      Serial.println( client.responseCode() );
    });
    
}

String content()

Returns the response content of the last HTTP request as a String object.

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .get( "/status" )
    .onFinish( [] ( int idx, int v, int up ) {
      Serial.print( "HTTP response content: ");
      Serial.println( client.content() );
    });
    
}

String headers()

Returns the response content of the last HTTP request as a String object.

void setup() {

  client.begin( "lamp1.bedroom.tinkerspy.nl" )
    .get( "/status" )
    .onFinish( [] ( int idx, int v, int up ) {
      Serial.print( "HTTP response headers: ");
      Serial.println( client.headers() );
    });
    
}

bool is_success()

Returns true if the previous request was a success (responseCode 2xx).

bool is_redirect()

Returns true if the previous request returned a redirect (responseCode 3xx).

bool is_error()

Returns true if the previous request returned an error (responseCode 4xx).

EVT_START

Same as the start() method.

  server.trigger( client.EVT_START );

  button1.begin( D2 )
    .onPress( client, client.EVT_START + 0 );
  button2.begin( D3 )
    .onPress( client, client.EVT_START + 1 );

Atm_esp8266_httpc_simple & trace()

To monitor the behavior of this machine you may log state change events to a Stream object like Serial.

Serial.begin( 9600 );
client.trace( Serial );