前提・実現したいこと
ESP32で、Open Weather Map (以下OWM)により、天気を取得し、次に、ウェブ アプリケーション化したGASを経由してDiscordにメッセージを送信したいです。そのために、OWMとGASのリンクにGETリクエストを送信して、返答を受け取りたいです。
ESP32でGASにGETリクエストを送信するときは、パラメータにvalue=12345をつけています。
GASがGETリクエストを受信すると、DiscordにWebhookを通してBOTによるメッセージが送信されます。
試したこと・問題
次のコードを基にして4種類試しました。
Arduino
#include <HTTPClient.h>#include <WiFi.h>const String url1 = "https://script.google.com/macros/s/*****/exec?value=12345";const String url2 = "http://api.openweathermap.org/data/2.5/weather?q=Japan, JP&APPID=*****"; void setup() { Serial.begin(115200); const char *ssid = "Buffalo-G-*****"; ////WiFi_SSID const char *password = "*****"; ////WiFi_パスワード Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.println("WiFi connected."); Serial.print("IP address: "); Serial.println(WiFi.localIP()); HTTPClient http; //1回目のGETリクエスト http.begin(url1); int httpCode = http.GET(); if (httpCode > 0) { String payload = http.getString(); Serial.println(httpCode); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end(); delay(1000); //2回目のGETリクエスト http.begin(url2); httpCode = http.GET(); if (httpCode > 0) { String payload = http.getString(); Serial.println(httpCode); Serial.println(payload); } else { Serial.println("Error on HTTP request"); } http.end();}void loop() {}
①url1をOWMのURLにし、url2をGASのURLにしました。結果、OWMは成功し、GASは失敗(ステータス・コードがこない)。
②url1をGASのURLにし、url2をOWMのURLにしました。結果、GASは成功し、OWMは失敗(ステータス・コードがこない)。
③url1と2をGASのURLにしました。結果、それぞれ成功
④url1と2をOWMのURLにしました。結果、それぞれ成功
2つのURLが異なると、2つ目は失敗し、2つが同じURLだと成功するようです。なので、URLがGAS、OWMだから失敗したわけではないように感じます。確証はありませんが。
ArduinoIDEのツール→Core Debug LevelはVerboseにしてあります。
①のシリアルモニターの内容
上略 IP address: 192.168.11.9 [ 3170][V][HTTPClient.cpp:252] beginInternal(): url: https://script.google.com/macros/s/*****/exec?value=12345 [ 3178][D][HTTPClient.cpp:263] beginInternal(): unexpected protocol: https, expected http [ 3186][V][HTTPClient.cpp:252] beginInternal(): url: https://script.google.com/macros/s/*****/exec?value=12345 [ 3202][D][HTTPClient.cpp:303] beginInternal(): protocol: https, host: script.google.com port: 443 url: /macros/s/*****/exec?value=12345 [ 3219][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0 [ 3227][V][ssl_client.cpp:62] start_ssl_client(): Free internal heap before TLS 251100 [ 3234][V][ssl_client.cpp:68] start_ssl_client(): Starting socket [ 3302][V][ssl_client.cpp:149] start_ssl_client(): Seeding the random number generator [ 3303][V][ssl_client.cpp:158] start_ssl_client(): Setting up the SSL/TLS structure... [ 3307][D][ssl_client.cpp:179] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE! [ 3315][V][ssl_client.cpp:257] start_ssl_client(): Setting hostname for TLS session... [ 3323][V][ssl_client.cpp:272] start_ssl_client(): Performing the SSL/TLS handshake... [ 4411][V][ssl_client.cpp:293] start_ssl_client(): Verifying peer X.509 certificate... [ 4411][V][ssl_client.cpp:301] start_ssl_client(): Certificate verified. [ 4414][V][ssl_client.cpp:316] start_ssl_client(): Free internal heap after TLS 200656 [ 4422][D][HTTPClient.cpp:1156] connect(): connected to script.google.com:443 [ 4429][V][ssl_client.cpp:369] send_ssl_data(): Writing HTTP request with 245 bytes... [ 5760][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'HTTP/1.1 302 Moved Temporarily' 省略 [ 5939][D][HTTPClient.cpp:1307] handleHeaderResponse(): code: 302 [ 5944][D][HTTPClient.cpp:1314] handleHeaderResponse(): Transfer-Encoding: chunked [ 5952][D][HTTPClient.cpp:628] sendRequest(): sendRequest code=302 [ 5958][D][HTTPClient.cpp:922] writeToStream(): read chunk len: 557 [ 5967][D][HTTPClient.cpp:1446] writeToStreamDataBlock(): connection closed or file end (written: 557). [ 5974][D][HTTPClient.cpp:922] writeToStream(): read chunk len: 0 [ 5979][D][HTTPClient.cpp:388] disconnect(): still data in buffer (2), clean up. [ 5987][D][HTTPClient.cpp:393] disconnect(): tcp keep open for reuse 302 <HTML> <HEAD> <TITLE>Moved Temporarily</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Moved Temporarily</H1> The document has moved <A HREF="https://script.googleusercontent.com/macros/echo?user_content_key=*****">here</A>. </BODY> </HTML> [ 6044][D][HTTPClient.cpp:388] disconnect(): still data in buffer (2), clean up. [ 6049][D][HTTPClient.cpp:393] disconnect(): tcp keep open for reuse [ 7055][V][HTTPClient.cpp:252] beginInternal(): url: http://api.openweathermap.org/data/2.5/weather?q=Japan, JP&APPID=***** [ 7057][D][HTTPClient.cpp:297] beginInternal(): switching host from 'script.google.com' to 'api.openweathermap.org'. disconnecting first [ 7069][D][HTTPClient.cpp:388] disconnect(): still data in buffer (2), clean up. [ 7076][D][HTTPClient.cpp:395] disconnect(): tcp stop [ 7082][V][ssl_client.cpp:324] stop_ssl_socket(): Cleaning SSL connection. [ 7089][D][HTTPClient.cpp:303] beginInternal(): protocol: http, host: api.openweathermap.org port: 80 url: /data/2.5/weather?q=Japan, JP&APPID=***** [ 7103][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0 [ 7111][V][ssl_client.cpp:62] start_ssl_client(): Free internal heap before TLS 249408 [ 7118][V][ssl_client.cpp:68] start_ssl_client(): Starting socket [ 7292][V][ssl_client.cpp:149] start_ssl_client(): Seeding the random number generator [ 7293][V][ssl_client.cpp:158] start_ssl_client(): Setting up the SSL/TLS structure... [ 7296][D][ssl_client.cpp:179] start_ssl_client(): WARNING: Skipping SSL Verification. INSECURE! [ 7305][V][ssl_client.cpp:257] start_ssl_client(): Setting hostname for TLS session... [ 7313][V][ssl_client.cpp:272] start_ssl_client(): Performing the SSL/TLS handshake... [ 7498][E][ssl_client.cpp:37] _handle_error(): [start_ssl_client():276]: (-29184) SSL - An invalid SSL record was received [ 7498][E][WiFiClientSecure.cpp:135] connect(): start_ssl_client: -29184 [ 7505][V][ssl_client.cpp:324] stop_ssl_socket(): Cleaning SSL connection. [ 7511][D][HTTPClient.cpp:1149] connect(): failed connect to api.openweathermap.org:80 [ 7518][W][HTTPClient.cpp:1469] returnError(): error(-1): connection refused Error on HTTP request [ 7536][D][HTTPClient.cpp:408] disconnect(): tcp is closed [ 7536][V][ssl_client.cpp:324] stop_ssl_socket(): Cleaning SSL connection. [ 7540][V][ssl_client.cpp:324] stop_ssl_socket(): Cleaning SSL connection.
②のシリアルモニターの内容
上略 IP address: 192.168.11.9 [ 2671][V][HTTPClient.cpp:252] beginInternal(): url: http://api.openweathermap.org/data/2.5/weather?q=Japan, JP&APPID=***** [ 2677][D][HTTPClient.cpp:303] beginInternal(): protocol: http, host: api.openweathermap.org port: 80 url: /data/2.5/weather?q=Japan, JP&APPID=***** [ 2692][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0 [ 2868][D][HTTPClient.cpp:1156] connect(): connected to api.openweathermap.org:80 [ 3080][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK' 省略 [ 3172][D][HTTPClient.cpp:628] sendRequest(): sendRequest code=200 [ 3179][D][HTTPClient.cpp:1446] writeToStreamDataBlock(): connection closed or file end (written: 472). [ 3188][D][HTTPClient.cpp:393] disconnect(): tcp keep open for reuse 200 //////天気のデータを取得することはできています {"coord":{"lon":139.7531,"lat":35.6854},"weather":[{"id":801,"main":"Clouds","description":"few clouds","icon":"02d"}],"base":"stations","main":{"temp":304.2,"feels_like":307.37,"temp_min":302.02,"temp_max":305.38,"pressure":1003,"humidity":57},"visibility":10000,"wind":{"speed":8.23,"deg":200},"clouds":{"all":20},"dt":1660893869,"sys":{"type":2,"id":268395,"country":"JP","sunrise":1660852963,"sunset":1660901225},"timezone":32400,"id":1861060,"name":"Japan","cod":200} [ 3245][D][HTTPClient.cpp:393] disconnect(): tcp keep open for reuse [ 4245][V][HTTPClient.cpp:252] beginInternal(): url: https://script.google.com/macros/s/*****/exec?value=12345 [ 4249][D][HTTPClient.cpp:263] beginInternal(): unexpected protocol: https, expected http [ 4257][V][HTTPClient.cpp:252] beginInternal(): url: https://script.google.com/macros/s/*****/exec?value=12345 [ 4273][D][HTTPClient.cpp:297] beginInternal(): switching host from 'api.openweathermap.org' to 'script.google.com'. disconnecting first [ 4285][D][HTTPClient.cpp:395] disconnect(): tcp stop [ 4291][D][HTTPClient.cpp:303] beginInternal(): protocol: https, host: script.google.com port: 443 url: /macros/s/*****/exec?value=12345 [ 4308][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0 [ 4361][D][HTTPClient.cpp:1156] connect(): connected to script.google.com:443 [ 4383][D][WiFiClient.cpp:545] connected(): Disconnected: RES: -1, ERR: 104 [ 4383][D][HTTPClient.cpp:628] sendRequest(): sendRequest code=-5 [ 4384][W][HTTPClient.cpp:1469] returnError(): error(-5): connection lost Error on HTTP request [ 4401][D][HTTPClient.cpp:408] disconnect(): tcp is closed
なぜ、下のように、なってしまうのでしょうか。原因と解決方法をご教授いただければ幸いです。
①の場合
[ 7076][D][HTTPClient.cpp:395] disconnect(): tcp stop [ 7082][V][ssl_client.cpp:324] stop_ssl_socket(): Cleaning SSL connection. [ 7089][D][HTTPClient.cpp:303] beginInternal(): protocol: http, host: api.openweathermap.org port: 80 url: /data/2.5/weather?q=Japan, JP&APPID=***** [ 7103][D][HTTPClient.cpp:598] sendRequest(): request type: 'GET' redirCount: 0
②の場合
[ 4361][D][HTTPClient.cpp:1156] connect(): connected to script.google.com:443 [ 4383][D][WiFiClient.cpp:545] connected(): Disconnected: RES: -1, ERR: 104 [ 4383][D][HTTPClient.cpp:628] sendRequest(): sendRequest code=-5
文字数制限によりシリアルモニターの内容は省略しています。
初心者であり、言葉足らずですがご回答を宜しくお願い致します。
0 コメント