Arduino ESP32 获取网络数据(HTTP PUT方式)
Posted perseverance52
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arduino ESP32 获取网络数据(HTTP PUT方式)相关的知识,希望对你有一定的参考价值。
Arduino ESP32 获取网络数据(HTTP PUT方式)
- 相关篇《Arduino ESP32 获取网络数据(HTTP POST方式)》
- 《Arduino ESP32 获取网络数据(HTTP GET方式)》
- 《Arduino ESP32 获取网络数据(HTTP PATCH方式)》
本实例通过PATCH方式请求获取网络数据,由于好多服务器访问不接受PUT方式,导致无法获取到数据。旨在展示获取网络数据的方法,针对获取具体的数据,需要根据具体的访问链接,选择合适的访问方式。
- 不需要要额外库,全部使用自带固件的库
- 访问对象:http://quan.suning.com/getSysTime.do
实例代码
/**
* ESP32 HTTP GET方式获取网络数据
*
*/
#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
//填写WIFI入网信息
const char* ssid = "MERCURY_D268G"; // WIFI账户
const char* password = "pba5ayzk"; // WIFI密码
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
for(uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\\n", t);
Serial.flush();
delay(1000);
}
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}
/* 开始访问指定服务器地址,获取数据 */
Serial.print("Connected to ");
Serial.println(ssid);
HTTPClient http;//创建 WiFiClient 实例化对象
Serial.print("[HTTP] begin...\\n");
// configure traged server and url
http.begin("http://quan.suning.com/getSysTime.do"); //HTTP
Serial.print("[HTTP] PUT...\\n");
// start connection and send HTTP header
int httpCode = http.PUT("http://quan.suning.com/getSysTime.do");//PUT请求
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_FOUND) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\\n", http.errorToString(httpCode).c_str());
}
http.end();
}
void loop() {
delay(5000);
}
- 串口打印访问网络,返回数据情况
返回的代码是:302
- HTTP_CODE代码
/// HTTP codes see RFC7231
typedef enum {
HTTP_CODE_CONTINUE = 100,
HTTP_CODE_SWITCHING_PROTOCOLS = 101,
HTTP_CODE_PROCESSING = 102,
HTTP_CODE_OK = 200,
HTTP_CODE_CREATED = 201,
HTTP_CODE_ACCEPTED = 202,
HTTP_CODE_NON_AUTHORITATIVE_INFORMATION = 203,
HTTP_CODE_NO_CONTENT = 204,
HTTP_CODE_RESET_CONTENT = 205,
HTTP_CODE_PARTIAL_CONTENT = 206,
HTTP_CODE_MULTI_STATUS = 207,
HTTP_CODE_ALREADY_REPORTED = 208,
HTTP_CODE_IM_USED = 226,
HTTP_CODE_MULTIPLE_CHOICES = 300,
HTTP_CODE_MOVED_PERMANENTLY = 301,
HTTP_CODE_FOUND = 302,
HTTP_CODE_SEE_OTHER = 303,
HTTP_CODE_NOT_MODIFIED = 304,
HTTP_CODE_USE_PROXY = 305,
HTTP_CODE_TEMPORARY_REDIRECT = 307,
HTTP_CODE_PERMANENT_REDIRECT = 308,
HTTP_CODE_BAD_REQUEST = 400,
HTTP_CODE_UNAUTHORIZED = 401,
HTTP_CODE_PAYMENT_REQUIRED = 402,
HTTP_CODE_FORBIDDEN = 403,
HTTP_CODE_NOT_FOUND = 404,
HTTP_CODE_METHOD_NOT_ALLOWED = 405,
HTTP_CODE_NOT_ACCEPTABLE = 406,
HTTP_CODE_PROXY_AUTHENTICATION_REQUIRED = 407,
HTTP_CODE_REQUEST_TIMEOUT = 408,
HTTP_CODE_CONFLICT = 409,
HTTP_CODE_GONE = 410,
HTTP_CODE_LENGTH_REQUIRED = 411,
HTTP_CODE_PRECONDITION_FAILED = 412,
HTTP_CODE_PAYLOAD_TOO_LARGE = 413,
HTTP_CODE_URI_TOO_LONG = 414,
HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415,
HTTP_CODE_RANGE_NOT_SATISFIABLE = 416,
HTTP_CODE_EXPECTATION_FAILED = 417,
HTTP_CODE_MISDIRECTED_REQUEST = 421,
HTTP_CODE_UNPROCESSABLE_ENTITY = 422,
HTTP_CODE_LOCKED = 423,
HTTP_CODE_FAILED_DEPENDENCY = 424,
HTTP_CODE_UPGRADE_REQUIRED = 426,
HTTP_CODE_PRECONDITION_REQUIRED = 428,
HTTP_CODE_TOO_MANY_REQUESTS = 429,
HTTP_CODE_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
HTTP_CODE_INTERNAL_SERVER_ERROR = 500,
HTTP_CODE_NOT_IMPLEMENTED = 501,
HTTP_CODE_BAD_GATEWAY = 502,
HTTP_CODE_SERVICE_UNAVAILABLE = 503,
HTTP_CODE_GATEWAY_TIMEOUT = 504,
HTTP_CODE_HTTP_VERSION_NOT_SUPPORTED = 505,
HTTP_CODE_VARIANT_ALSO_NEGOTIATES = 506,
HTTP_CODE_INSUFFICIENT_STORAGE = 507,
HTTP_CODE_LOOP_DETECTED = 508,
HTTP_CODE_NOT_EXTENDED = 510,
HTTP_CODE_NETWORK_AUTHENTICATION_REQUIRED = 511
} t_http_codes;
以上是关于Arduino ESP32 获取网络数据(HTTP PUT方式)的主要内容,如果未能解决你的问题,请参考以下文章
Arduino ESP32 获取网络数据(HTTP POST方式)
Arduino ESP32 获取网络数据(HTTP PATCH方式)
Arduino ESP32 通过getString方法获取网络时间和气象数据