M5Stack Core2: HTTP クライアント (Post)
2644 ワード
http_post/http_post.ino
// ---------------------------------------------------------------
/*
http_post.ino
Sep/03/2021
*/
// ---------------------------------------------------------------
#include <M5Core2.h>
#include <Arduino.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
WiFiMulti wifiMulti;
HTTPClient http;
const char *ssid = "some-ssid";
const char *password = "some-password";
int count = 0;
// ---------------------------------------------------------------
void setup()
{
M5.begin();
M5.Lcd.setBrightness(32);
wifiMulti.addAP(ssid, password);
M5.Lcd.print("\nConnecting Wifi...\n");
delay(3000);
Serial.println("*** setup *** aaa ***");
delay(1000);
String url_target = "https://httpbin.org/post";
http_post_proc(url_target);
delay(5000);
Serial.println("*** setup ***");
}
// ---------------------------------------------------------------
void loop()
{
Serial.println("*** loop *** " + String(count));
M5.Lcd.setTextSize(3);
M5.Lcd.setCursor(5,30);
M5.Lcd.println("*** loop *** " + String(count));
delay(3000);
M5.Lcd.clear();
delay(1000);
M5.Lcd.fillScreen(WHITE);
delay(1000);
count++;
}
// ---------------------------------------------------------------
void http_post_proc(String url_target)
{
DynamicJsonDocument doc(128);
char data_json[128];
doc["user"] = "jiro";
doc["password"] = "123456";
serializeJson(doc, data_json);
if((wifiMulti.run() == WL_CONNECTED)) {
M5.Lcd.print("[HTTP] begin...\n");
http.begin(url_target);
M5.Lcd.print("[HTTP] POST...\n");
int httpCode = http.POST((uint8_t*)data_json, strlen(data_json));
if(httpCode > 0) {
M5.Lcd.printf("[HTTP] POST... code: %d\n", httpCode);
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
M5.Lcd.println(payload);
Serial.println(payload);
}
}else {
M5.Lcd.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
else
{
M5.Lcd.print("connect failed");
}
}
// ---------------------------------------------------------------
シリアルポートの受信は次のコマンドで行います。
bash
cu -s 115200 -l /dev/ttyUSB0
Author And Source
この問題について(M5Stack Core2: HTTP クライアント (Post)), 我々は、より多くの情報をここで見つけました https://qiita.com/ekzemplaro/items/715990eb96ba19beac58著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .