Arduino 通过api接口 post 数据

1.url编码形式Post数据

  • 示例代码
#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include "arduino_secrets.h"

///please enter your sensitive data in the Secret tab/arduino_secrets.h
/// Wifi Settings ///
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

char serverAddress[] = "192.168.0.3";  // server address
int port = 8080;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded";
  String postData = "name=Alice&age=12";

  client.post("/", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}

2.JSON格式Post数据

  • 示例代码
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

void setup() {
  Serial.begin(115200);                            //Serial connection
  WiFi.begin("UU", "1212312121");   //WiFi connection
  while (WiFi.status() != WL_CONNECTED) {  //Wait for the WiFI connection completion
    delay(500);
    Serial.println("Waiting for connection");
 }
} 

void loop() {
  int sensor_value = 45;
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
  String json = "{\"sensor\":\"gps\",\"value\":\""+ String(sensor_value) +"\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  DynamicJsonBuffer  jsonBuffer;
  char postMessage[300]; 
  JsonObject& root = jsonBuffer.createObject();
  JsonObject& Jarray = jsonBuffer.parseObject(json); 
  root["name"] = "lll";
  root["content"] = Jarray;
  root.printTo(postMessage,sizeof(postMessage));
  Serial.print(postMessage);
  
//----------------------------------------------------------
         HTTPClient http;
         http.begin("http://iot.data.ubu.ac.th/api/save" );      
         http.addHeader("Content-Type", "application/json");
         int httpCode = http.POST(postMessage);

    String payload = http.getString();                                        //Get the response payload
    Serial.println(httpCode);   //Print HTTP return code
    Serial.println(payload);    //Print request response payload
    http.end();  //Close connection
  }
 else {
    Serial.println("Error in WiFi connection");
  }
  delay(3000);  //Send a request every 30 seconds
}
  • 通过api接口post数据,数据格式可以有url编码,json格式,text文本格式,表单参数等各种形式,有些接口不限制数据格式,有些会限制,比如只允许JSON格式
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Arduino Restful程序是一个基于RESTful架构的Arduino程序,可以通过HTTP协议发送请求和获取响应,实现与外部设备进行通信和数据交换的功能。RESTful架构是一种面向Web资源的架构风格,具有统一的接口、客户端-服务器、无状态、可缓存、分层系统等特点,通过HTTP协议实现与Web应用程序之间的通信,广泛应用于Web服务和移动应用程序中。 Arduino Restful程序可以使用HTTP GET方法获取传感器数据,使用HTTP POST方法发送控制命令,通过HTTP PUT和DELETE方法实现数据的更新和删除。在Restful程序中,可以使用Arduino Ethernet或WiFi模块建立和维护网络连接,创建HTTP服务器或客户端来处理HTTP请求和响应,使用Arduino编程语言和RESTful API库实现对Web资源的访问和处理。 对于传感器数据的获取,Arduino Restful程序可以通过HTTP GET方法请求传感器数据,并将数据封装成JSON格式返回给客户端。客户端可以解析JSON数据,获取传感器数据并进行展示。对于控制命令的发送,客户端可以使用HTTP POST方法发送命令到Arduino Restful程序,指定控制参数和操作类型,Restful程序根据获取到的参数进行相应的操作,比如控制LED灯的开和关。 总之,Arduino Restful程序是一个基于RESTful架构的Arduino程序,可以通过HTTP协议实现与外部设备的通信和数据交换,具有灵活、可扩展和易于维护的优点,被广泛应用于物联网、智能家居等领域。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值