HTTP - Updated comments
This commit is contained in:
parent
2926f11620
commit
d9c553f7d0
3 changed files with 26 additions and 11 deletions
|
@ -17,13 +17,15 @@
|
||||||
ESP8266 wifi = Kite.ModuleA;
|
ESP8266 wifi = Kite.ModuleA;
|
||||||
|
|
||||||
// create HTTP client instance using the wifi module
|
// 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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
// initialize ESP8266 with baudrate 9600
|
// initialize ESP8266
|
||||||
Serial.print(F("[ESP8266] Initializing ... "));
|
Serial.print(F("[ESP8266] Initializing ... "));
|
||||||
|
// baudrate: 9600 baud
|
||||||
byte state = wifi.begin(9600);
|
byte state = wifi.begin(9600);
|
||||||
if(state == ERR_NONE) {
|
if(state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
|
@ -33,9 +35,11 @@ void setup() {
|
||||||
while(true);
|
while(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// join AP named "SSID" using the password "password"
|
// join access point
|
||||||
Serial.print(F("[ESP8266] Joining AP ... "));
|
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) {
|
if(state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -51,6 +55,7 @@ void loop() {
|
||||||
// the response will contain origin IP address of the request
|
// the response will contain origin IP address of the request
|
||||||
String response;
|
String response;
|
||||||
Serial.print(F("[ESP8266] Sending HTTP GET request ... "));
|
Serial.print(F("[ESP8266] Sending HTTP GET request ... "));
|
||||||
|
// URL: www.httpbin.org/ip
|
||||||
int http_code = http.get("www.httpbin.org/ip", response);
|
int http_code = http.get("www.httpbin.org/ip", response);
|
||||||
if(http_code == 200) {
|
if(http_code == 200) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
|
|
|
@ -17,13 +17,15 @@
|
||||||
ESP8266 wifi = Kite.ModuleA;
|
ESP8266 wifi = Kite.ModuleA;
|
||||||
|
|
||||||
// create HTTP client instance using the wifi module
|
// 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() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
|
|
||||||
// initialize ESP8266 with baudrate 9600
|
// initialize ESP8266
|
||||||
Serial.print(F("[ESP8266] Initializing ... "));
|
Serial.print(F("[ESP8266] Initializing ... "));
|
||||||
|
// baudrate: 9600 baud
|
||||||
byte state = wifi.begin(9600);
|
byte state = wifi.begin(9600);
|
||||||
if(state == ERR_NONE) {
|
if(state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
|
@ -33,9 +35,11 @@ void setup() {
|
||||||
while(true);
|
while(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// join AP named "SSID" using the password "password"
|
// join access point
|
||||||
Serial.print(F("[ESP8266] Joining AP ... "));
|
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) {
|
if(state == ERR_NONE) {
|
||||||
Serial.println(F("success!"));
|
Serial.println(F("success!"));
|
||||||
} else {
|
} else {
|
||||||
|
@ -47,11 +51,14 @@ void setup() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// send HTTP POST request to www.httpbin.org/status/200
|
// send HTTP POST request to www.httpbin.org/status/404
|
||||||
// Content: str
|
// the server doesn't process the posted data, it just returns
|
||||||
// Content-Type: text/plain
|
// response with the status code 404
|
||||||
String response;
|
String response;
|
||||||
Serial.print(F("[ESP8266] Sending HTTP POST request ... "));
|
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);
|
int http_code = http.post("www.httpbin.org/status/404", "str", response);
|
||||||
if(http_code >= 100) {
|
if(http_code >= 100) {
|
||||||
Serial.print(F("HTTP code "));
|
Serial.print(F("HTTP code "));
|
||||||
|
|
|
@ -6,13 +6,16 @@
|
||||||
|
|
||||||
class HTTPClient {
|
class HTTPClient {
|
||||||
public:
|
public:
|
||||||
|
// constructor
|
||||||
HTTPClient(TransportLayer* tl, uint16_t port = 80);
|
HTTPClient(TransportLayer* tl, uint16_t port = 80);
|
||||||
|
|
||||||
|
// basic methods
|
||||||
uint16_t get(const char* url, String& response);
|
uint16_t get(const char* url, String& response);
|
||||||
uint16_t post(const char* url, const char* content, String& response, const char* contentType = "text/plain");
|
uint16_t post(const char* url, const char* content, String& response, const char* contentType = "text/plain");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TransportLayer* _tl;
|
TransportLayer* _tl;
|
||||||
|
|
||||||
uint16_t _port;
|
uint16_t _port;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue