http POSTを試していたら何故かうまく動かなかった
自分用のメモ的な部分が多い。
始まり
discordのwebhookを利用しようと色々試していました。 なんとかwebhookを利用してdiscordに投稿することができるようになった(まあなんでうまく行ったのか全くわかんなけど・・・)
しかし、loop関数の中に記載したはずが一回しか実行されなかった。
解決
なぜ治ったのかはわかりません。
void loop() {
// put your main code here, to run repeatedly:
int value;
value = digitalRead( DIN_PIN );
Serial.println("loop start");
Serial.println(value);
if (value != 0){
HTTPClient http;
WiFiClient client;
//WiFiClientSecure client;
//client.setCACert(discord_root_ca);
//http.begin(client, "https://www.forrexlapis.win/");
http.begin("https://discord.com/api/webhooks/");
http.addHeader("Content-Type", "application/json");
//メッセージ作成
char buffer[255];
StaticJsonDocument<JSON_OBJECT_SIZE(48)> json_request;
json_request["content"] = "あぶなーーーい!!";
serializeJson(json_request, buffer, sizeof(buffer));
int status_code = http.POST(buffer);
//int status_code = http.GET();
Serial.println("publication");
Serial.println( status_code );
//Serial.print(http.getString()); ここをコメントアウトしたら治った
delay( 1000 );
http.end();
delay( 1000 );
}
delay( 10000 );
Serial.println("endloop");
}
色々試しているうちに記載していた、
Serial.print(http.getString());
をコメントアウトしたら治った。
postしているのにhttp.getstringをしているのが悪かったりしたのかなぁ・・・?それとをhttp.getstringの中身が悪かったとか・・・?
それにそもそもどうしてwebhookがうまく動いたのかがよくわからないし!! httpsになっているから、証明書的なものを使えるようにしないと行けないのかとかとか調べたけど結局これで動いてしまった。
まだまだよくわからないことが多いのでのんびりながら頑張りたいですね