Arduino ESP8266 D1 WiFi HTTPClient 範例程式

 

自己筆記記錄一下…

連上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);    
}

 

您可能也會喜歡…

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *