RadioLib/examples/HTTP/HTTP_Post/HTTP_Post.ino

87 wiersze
2.3 KiB
Arduino
Czysty Zwykły widok Historia

2018-07-11 11:44:43 +00:00
/*
2019-02-08 14:58:29 +00:00
RadioLib HTTP POST Example
2019-06-02 13:02:19 +00:00
2018-10-07 10:18:34 +00:00
This example sends HTTP POST request using ESP8266 WiFi module.
2019-06-02 13:02:19 +00:00
2018-10-07 10:18:34 +00:00
Please note that the response will be saved including header. HTTP header size
can easily exceed Arduino resources and cause the program to behave erratically.
2019-06-02 13:02:19 +00:00
2018-10-07 10:18:34 +00:00
IMPORTANT: Before uploading this example, make sure that the ESP8266 module is running
AT firmware (can be found in the /extras folder of the library)!
2019-06-02 13:02:19 +00:00
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
2018-07-11 11:44:43 +00:00
*/
// include the library
2019-02-08 14:58:29 +00:00
#include <RadioLib.h>
2018-07-11 11:44:43 +00:00
2019-06-02 13:02:19 +00:00
// ESP8266 has the following connections:
// TX pin: 9
// RX pin: 8
ESP8266 wifi = new Module(9, 8);
// or using RadioShield
// https://github.com/jgromes/RadioShield
//ESP8266 wifi = RadioShield.ModuleA;
2018-07-11 11:44:43 +00:00
// create HTTP client instance using the wifi module
2018-07-11 15:39:23 +00:00
// the default port used for HTTP is 80
HTTPClient http(&wifi, 80);
2018-07-11 11:44:43 +00:00
void setup() {
Serial.begin(9600);
2019-06-02 13:02:19 +00:00
2018-07-11 15:39:23 +00:00
// initialize ESP8266
2018-07-11 11:44:43 +00:00
Serial.print(F("[ESP8266] Initializing ... "));
2018-07-11 15:39:23 +00:00
// baudrate: 9600 baud
2018-07-23 10:41:48 +00:00
int state = wifi.begin(9600);
2018-07-11 11:44:43 +00:00
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
2018-07-23 10:41:48 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
2018-07-11 11:44:43 +00:00
while(true);
}
2018-07-11 15:39:23 +00:00
// join access point
2018-07-11 11:44:43 +00:00
Serial.print(F("[ESP8266] Joining AP ... "));
2018-07-11 15:39:23 +00:00
// name: SSID
// password: password
state = wifi.join("SSID", "password");
2018-07-11 11:44:43 +00:00
if(state == ERR_NONE) {
Serial.println(F("success!"));
} else {
2018-07-23 10:41:48 +00:00
Serial.print(F("failed, code "));
Serial.println(state);
2018-07-11 11:44:43 +00:00
while(true);
}
}
void loop() {
2018-07-11 15:39:23 +00:00
// 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
2018-07-11 11:44:43 +00:00
String response;
Serial.print(F("[ESP8266] Sending HTTP POST request ... "));
2018-07-11 15:39:23 +00:00
// URL: www.httpbin.org/status/404
// content: str
// content type: text/plain
2018-07-11 11:44:43 +00:00
int http_code = http.post("www.httpbin.org/status/404", "str", response);
2018-07-23 10:41:48 +00:00
if(http_code > 0) {
2018-07-11 11:44:43 +00:00
Serial.print(F("HTTP code "));
Serial.println(http_code);
Serial.print(F("[ESP8266] Response is "));
Serial.print(response.length());
Serial.println(F(" bytes long."));
Serial.println(response);
} else {
2018-07-23 10:41:48 +00:00
Serial.print(F("failed, code "));
Serial.println(http_code);
2018-07-11 11:44:43 +00:00
}
// wait for a second before sending new request
delay(1000);
}