-
Notifications
You must be signed in to change notification settings - Fork 7
The HTTP Client
The Atm_esp8266_httpc_simple component provides an asynchronous event driven web client with a simple Automaton interface.
- The HTTP Client
- begin()
- onStart()
- onFinish()
- start()
- get()
- post()
- responseCode()
- content()
- headers()
- is_success()
- is_redirect()
- is_error()
- EVT_START
- trace()
#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();
}
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 );
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.
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() );
}
});
}
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
}
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" );
}
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" );
}
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() );
});
}
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() );
});
}
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() );
});
}
Returns true if the previous request was a success (responseCode 2xx).
Returns true if the previous request returned a redirect (responseCode 3xx).
Returns true if the previous request returned an error (responseCode 4xx).
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 );
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 );