diff --git a/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino b/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino index 25489ec9..b359f1c1 100644 --- a/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino +++ b/examples/ESP8266_HTTP_Get/ESP8266_HTTP_Get.ino @@ -17,13 +17,15 @@ ESP8266 wifi = Kite.ModuleA; // create HTTP client instance using the wifi module -HTTPClient http(&wifi); +// the default port used for HTTP is 80 +HTTPClient http(&wifi, 80); void setup() { Serial.begin(9600); - // initialize ESP8266 with baudrate 9600 + // initialize ESP8266 Serial.print(F("[ESP8266] Initializing ... ")); + // baudrate: 9600 baud byte state = wifi.begin(9600); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -33,9 +35,11 @@ void setup() { while(true); } - // join AP named "SSID" using the password "password" + // join access point Serial.print(F("[ESP8266] Joining AP ... ")); - state = wifi.join("Tenda", "Student20-X13"); + // name: SSID + // password: password + state = wifi.join("SSID", "password"); if(state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -51,6 +55,7 @@ void loop() { // the response will contain origin IP address of the request String response; Serial.print(F("[ESP8266] Sending HTTP GET request ... ")); + // URL: www.httpbin.org/ip int http_code = http.get("www.httpbin.org/ip", response); if(http_code == 200) { Serial.println(F("success!")); diff --git a/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino b/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino index 3f9f0d0a..58aa5a2b 100644 --- a/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino +++ b/examples/ESP8266_HTTP_Post/ESP8266_HTTP_Post.ino @@ -17,13 +17,15 @@ ESP8266 wifi = Kite.ModuleA; // create HTTP client instance using the wifi module -HTTPClient http(&wifi); +// the default port used for HTTP is 80 +HTTPClient http(&wifi, 80); void setup() { Serial.begin(9600); - // initialize ESP8266 with baudrate 9600 + // initialize ESP8266 Serial.print(F("[ESP8266] Initializing ... ")); + // baudrate: 9600 baud byte state = wifi.begin(9600); if(state == ERR_NONE) { Serial.println(F("success!")); @@ -33,9 +35,11 @@ void setup() { while(true); } - // join AP named "SSID" using the password "password" + // join access point Serial.print(F("[ESP8266] Joining AP ... ")); - state = wifi.join("Tenda", "Student20-X13"); + // name: SSID + // password: password + state = wifi.join("SSID", "password"); if(state == ERR_NONE) { Serial.println(F("success!")); } else { @@ -47,11 +51,14 @@ void setup() { } void loop() { - // send HTTP POST request to www.httpbin.org/status/200 - // Content: str - // Content-Type: text/plain + // send HTTP POST request to www.httpbin.org/status/404 + // the server doesn't process the posted data, it just returns + // response with the status code 404 String response; Serial.print(F("[ESP8266] Sending HTTP POST request ... ")); + // URL: www.httpbin.org/status/404 + // content: str + // content type: text/plain int http_code = http.post("www.httpbin.org/status/404", "str", response); if(http_code >= 100) { Serial.print(F("HTTP code ")); diff --git a/src/protocols/HTTP.h b/src/protocols/HTTP.h index 18e4d277..8339360a 100644 --- a/src/protocols/HTTP.h +++ b/src/protocols/HTTP.h @@ -6,13 +6,16 @@ class HTTPClient { public: + // constructor HTTPClient(TransportLayer* tl, uint16_t port = 80); + // basic methods uint16_t get(const char* url, String& response); uint16_t post(const char* url, const char* content, String& response, const char* contentType = "text/plain"); private: TransportLayer* _tl; + uint16_t _port; };