実現したいこと
プログラミング初心者になります。
・ESP32を用いて、定期的なデータ送信をしたい
前提
ソフト:Aruduino IDE
マイコン:ESP32
ESP32からIFTTTを使用して、あるデータをスプレッドシートへ蓄積させたいと考えています
発生している問題・エラーメッセージ
WI-FIへ接続し、IPアドレスも確認でき、PINGの送信も確認しましたが、データが送信できません。
WI-FIの問題かと考えますが、どのようにしてエラーを特定すれば良いでしょうか?
お手数ですが、ご教授をお願い致します。
KEY等は伏せており、URLを入力すると正常につながることは確認しています。
シリアルモニタのメッセージ Connecting to *** .. WiFi connected IP address: 192.168.**.** connecting to maker.ifttt.com Requesting URL: /trigger/ESP32/with/key/***?value1=15&value2=0&value3=0
該当のソースコード
Aruduino
1ソースコード 2 3#include <WiFi.h> 4 5const char* ssid = "SSID"; //Your SSID 6const char* password = "PASS"; //Your Password 7 8const char* host = "maker.ifttt.com"; 9const char* event = "ESP32"; 10const char* secretkey = "***"; 11int val1 = 0; 12int val2 = 0; 13int val3 = 0; 14 15void setup() { 16 Serial.begin(115200); 17 delay(10); 18 19 Serial.println(); 20 Serial.println(); 21 Serial.print("Connecting to "); 22 Serial.println(ssid); 23 24 WiFi.begin(ssid, password); 25 26 while (WiFi.status() != WL_CONNECTED) { 27 delay(500); 28 Serial.print("."); 29 } 30 31 Serial.println(""); 32 Serial.println("WiFi connected"); 33 Serial.println("IP address: "); 34 Serial.println(WiFi.localIP()); 35} 36 37int value = 0; 38 39void loop() { 40 delay(5000); 41 val1 = value; 42 ++value; 43 44 Serial.print("connecting to "); 45 Serial.println(host); 46 47 // Use WiFiClient class to create TCP connections 48 WiFiClient client; 49 const int httpPort = 80; 50 if (!client.connect(host, httpPort)) { 51 Serial.println("connection failed"); 52 return; 53 } 54 55 // We now create a URI for the request 56 String url = "/trigger/"; 57 url += event; 58 url += "/with/key/"; 59 url += secretkey; 60 url += "?value1="; 61 url += String(val1); 62 url += "&value2="; 63 url += String(val2); 64 url += "&value3="; 65 url += String(val3); 66 67 Serial.print("Requesting URL: "); 68 Serial.println(url); 69 70 // This will send the request to the server 71 client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); 72 delay(10); 73 74 // Read all the lines of the reply from server and print them to Serial 75 while(client.available()){ 76 String line = client.readStringUntil('\r'); 77 Serial.print(line); 78 } 79 80 Serial.println(); 81 Serial.println("closing connection"); 82}
試したこと
PINGでのWI-FI確認 192.168.** に ping を送信しています 32 バイトのデータ: 要求がタイムアウトしました。 192.168.** からの応答: バイト数 =32 時間 =145ms TTL=255 192.168.** からの応答: バイト数 =32 時間 =43ms TTL=255 要求がタイムアウトしました。 192.168.** の ping 統計: パケット数: 送信 = 4、受信 = 2、損失 = 2 (50% の損失)、 ラウンド トリップの概算時間 (ミリ秒): 最小 = 43ms、最大 = 145ms、平均 = 94ms
Aruduino
1回答にてお教えいただいた方法にてトライ 2#include <WiFi.h> 3#include <HTTPClient.h> 4// 自分の環境に合わせて適宜変更 5 6const char* ssid = "*"; //Your SSID 7const char* password = "*"; //Your Password 8const String maker_Event = "ESP32"; // Maker Webhooks 9const String maker_Key = "*"; // Maker Webhooks 10// 環境設定ここまで 11String url = "https://maker.ifttt.com/trigger/" + maker_Event + "/with/key/" + maker_Key; 12HTTPClient http; 13void setup() { 14 Serial.begin(115200); 15 WiFi.begin(ssid, password); 16 Serial.print("connecting to WiFi."); 17 while (WiFi.status() != WL_CONNECTED) { 18 delay(100); 19 Serial.print("."); 20 } 21 Serial.println("Connected!!"); 22 http.begin(url); 23 pinMode(4, INPUT_PULLUP); // 4ピンをスイッチ検出に使う 24 Serial.println("Waiting..."); 25} 26void loop() { 27 if (digitalRead(4) == LOW) { // ピンとGNDとの間に繋いだスイッチがONになったら... 28 Serial.println("press!"); 29 http.addHeader("Content-Type", "application/json"); 30 String val = "{\"value1\":\"123\",\"value2\":\"abc\",\"value3\":\"あいう\"}"; 31 int http_code = http.POST(val); 32 if (!(http_code > 0)) { 33 Serial.println("request failed!! try again"); 34 } else { 35 Serial.println("Send to IFTTT"); 36 } 37 unsigned long counter = millis(); 38 while (millis() - counter < 10000) 39 ; // 一度反応したら10秒休み 40 Serial.println("Waiting..."); 41 } 42} 43 44
....Connected!! Waiting... press! -1(http code エラー値) request failed!! try again Waiting...
WIFIにはつながったが、何らかの形でデータを送信することができでいない?と考えます。
ご教授いただいた、esp32にwebserverを立てる方法をトライしてみました。
参考url
https://qiita.com/northVil/items/f5cfee1cfd38ece59bb5
#include <WiFi.h> #include <WebServer.h> const char *ssid = "Buffalo-A-aaaa"; // WifiのSSID const char *pass = "abcdefghijklm"; // Wifiのパスワード WebServer Server(80); // ポート番号(HTTP) int Counter = 0; // カウンタ void SendMessage() { // レスポンス文字列の生成('\n' は改行; '\' は行継続) Counter++; Serial.println("SendMessage"); String message = "\ <html lang=\"ja\">\n\ <meta charset=\"utf-8\">\n\ <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\ <head>\n\ <title>HTML</title>\n\ </head>\n\ <body style=\"font-family: sans-serif; background-color: #008080;\" >\n\ <h1>North Vilによるウェブサーバー</h1>\n\ <h3>あなたはこのサーバの " + String(Counter) + "人目のお客さまです!</h3>\n\ </body>\n\ </html>\n"; String celebrate = "\ <html lang=\"ja\">\n\ <meta charset=\"utf-8\">\n\ <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n\ <head>\n\ <title>HTML</title>\n\ </head>\n\ <body style=\"font-family: sans-serif; background-color: #008080;\" >\n\ <h1>North Vilによるウェブサーバー</h1>\n\ <h2>あなたはこのサーバの記念すべき " + String(Counter) + "人目のお客さまです!!!!!</h2>\n\ </body>\n\ </html>\n"; // クライアントにレスポンスを返す if (Counter % 10 != 0) { Server.send(200, "text/html", message); } else { Server.send(200, "text/html", celebrate); } } void SendNotFound() { Serial.println("SendNotFound"); Server.send(404, "text/plain", "404 not found..."); } // メインプログラム void setup() { // シリアルモニタ(動作ログ) Serial.begin(115200); // ESP 標準の通信速度 115200 delay(100); Serial.println("\n*** Starting ***"); // 無線 LAN に接続 WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); Serial.println("Connecting..."); while (WiFi.status() != WL_CONNECTED) { delay(1000); if (WiFi.status() == WL_CONNECT_FAILED) { Serial.println("Can't connect"); } } Serial.println("Connected"); Serial.println(WiFi.localIP()); // ESP 自身の IP アドレスをログ出力 // ウェブサーバの設定 Server.on("/", SendMessage); // ルートアクセス時の応答 Server.onNotFound(SendNotFound); // 不正アクセス時の応答 Server.begin(); // ウェブサーバ開始 } void loop() { Server.handleClient(); }
シリアルモニタ
*** Starting *** Connecting... Connected 192.168.65.21 SendMessage
webserverは立ち上がりました。
ただ3回に1回程度しかつながらず、ERR_CONNECTION_TIMED_OUTになります。
wifiの接続は非常に悪いということが分かりました。
ping結果
192.168.65.** の ping 統計:
パケット数: 送信 = 142、受信 = 13、損失 = 129 (90% の損失)、
ラウンド トリップの概算時間 (ミリ秒):
最小 = 54ms、最大 = 174ms、平均 = 104ms

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/10/31 12:03
2023/11/01 03:12 編集
2023/11/01 23:25 編集
2023/11/01 23:26 編集
2023/11/03 01:28
2023/11/06 03:39
2023/11/06 12:33
2023/11/07 03:09 編集
2023/11/07 10:24
2023/11/07 13:40