Arduino ESP8266 D1 WiFi HTTPClient 範例程式

文章分類:程式開發
發布日期:2019-01-13 00:00:00
自己筆記記錄一下…連上 WiFi 後,每隔 10 秒會進行 HTTP POST 動作#include <ArduinoJson.h>#include <ESP8266HTTPClient.h>#include <ESP8266WiFi.h>const char* ssid = "wifiname";const char* password...
封面圖片

自己筆記記錄一下…

連上 WiFi 後,每隔 10 秒會進行 HTTP POST 動作

#include <ArduinoJson.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
const char* ssid = "wifiname";
const char* password = "password";
String jsonStr = "";
HTTPClient http;
void httpSend(String url) {
jsonStr = "{\"type\": \"fall\" ,\"latitude\": 25.118210,\"longitude\": 121.521243 }";
Serial.println(jsonStr);

http.begin(url);
http.addHeader("Content-Type", "application/json");
http.POST(jsonStr);
String json = http.getString();
StaticJsonBuffer<80> jsonBuffer;
JsonObject& root = jsonBuffer.parse(json);

if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
 const char\* status = root["result"];
Serial.println(status);
http.end();
}

void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
}

void loop()
{
 if (WiFi.status() == WL_CONNECTED) {
httpSend("http://ip/post.php");
}
 delay(10000);
}