使用物联网实现数据的传输与下载,需要借助第三方数据采集平台,中国移动的onenet平台是免费的,给初学者测试肯定是够了,其实大多数平台的数据传输协议代码都差不多。
#include <Arduino.h>
#include<WiFi.h>
#include<HTTPClient.h>
const char* ssid = "****"; //wifi名称
const char* passwd = "****";//wifi密码
const char* apiKey = "************";// 这里是你创建的设备的apikey
const char* deviceId = "***********"; //创建设备的ID号
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, passwd);
while(WiFi.status() != WL_CONNECTED){
delay(2000);
Serial.println("Connecting to WiFi");
}
Serial.println("Connected to WiFi");
HTTPClient http;
http.begin("http://api.heclouds.com/devices/1102999876/datapoints"); //这个链接里面的设备号要改。
http.addHeader("api-key", apiKey);
http.addHeader("Content-Type", "application/json");
String data= "{\"datastreams\":[{\"id\":\"temperature\",\"datapoints\":[{\"value\":25.0}]}]}";
int httpResponseCode = http.POST(data);
if (httpResponseCode > 0){
Serial.print("HTTP Response code:");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code:");
Serial.println(httpResponseCode);
http.end();
}
}
void loop() {
}