[請益] NodeMCU ESP8266 wifi 連線不穩(代po)

看板Electronics作者 (lavender)時間1年前 (2023/02/11 17:43), 1年前編輯推噓2(2010)
留言12則, 3人參與, 1年前最新討論串1/1
(已更新) 各位先進大家好~ 請問我想將NodeMCU 上面的感測器資料Post到網路上,但在POST的過程中一直出現error: connection failed ,但wifi 連線是正常的(如圖:Connection successfully establi shed)。 請問如何修改才可以讓資料順利的丟出? 非常感謝! (已更新) #include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> #include <WiFiClient.h> const char* ssid = "xxxxx"; const char* password = "xxxxxx"; String iot_number = "xxxxx"; String iot_CK = "xxxxxx"; String sensor_name = "lxxxx"; const int trigPin = D6; const int echoPin = D7; long duration; float distanceCm; float distanceInch; unsigned long previousMillis = 0; unsigned long interval = 30000; void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); Serial.print("Connecting to WiFi .."); while (WiFi.status() != WL_CONNECTED) { Serial.print('.'); delay(1000); } Serial.println(WiFi.localIP()); WiFi.setAutoReconnect(true); WiFi.persistent(true); } void setup() { Serial.begin(115200); initWiFi(); Serial.print("RSSI: "); Serial.println(WiFi.RSSI()); pinMode(D8, INPUT_PULLUP); delay(1000); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >=interval) { switch (WiFi.status()) { case WL_NO_SSID_AVAIL: Serial.println("Configured SSID cannot be reached"); break; case WL_CONNECTED: Serial.println("Connection successfully established"); iot_upload(distanceCm);//New add break; case WL_CONNECT_FAILED: Serial.println("Connection failed"); break; } Serial.printf("Connection status: %d\n", WiFi.status()); Serial.print("RRSI: "); Serial.println(WiFi.RSSI()); previousMillis = currentMillis; } digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distanceCm = duration * SOUND_VELOCITY / 2; distanceInch = distanceCm * CM_TO_INCH; Serial.print("Distance (cm): "); Serial.println(distanceCm); delay(5000); Serial.print("Distance (inch): "); Serial.println(distanceInch); iot_upload(distanceCm); delay(250); } void iot_upload(float value) { WiFiClient client; HTTPClient http; Serial.print("[HTTP] begin...\n"); http.begin(client, "xxxxxxx" + iot_number + "/rawdata"); //HTTP http.addHeader("CK", iot_CK); http.addHeader("Content-Type", "application/json"); Serial.print("[HTTP] POST...\n"); String data = "[{\"id\":\"" + sensor_name + "\",\"save\":true,\"value\":[\"" + String(value) + "\"]}]"; int httpCode = http.POST(data); if (httpCode > 0) { Serial.printf("[HTTP] POST... code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { const String& payload = http.getString(); Serial.println("received payload:\n<<"); Serial.println(payload); Serial.println(">>"); } } else { Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpCod e).c_str()); } http.end(); } =========Serial Monitor=============== [HTTP] begin... [HTTP] POST... [HTTP] POST... failed, error: connection failed Distance (cm): 0.00 Distance (inch): 0.00 [HTTP] begin... [HTTP] POST... [HTTP] POST... failed, error: connection failed Connection successfully established [HTTP] begin... [HTTP] POST... [HTTP] POST... failed, error: connection failed Connection status: 3 RRSI: -67 -- Sent from nPTT on my iPhone 11 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.126.243 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Electronics/M.1676108608.A.B8B.html

02/11 22:27, 1年前 , 1F
硬體都驗證過了嗎? 連上時RSSI多少
02/11 22:27, 1F

02/12 00:04, 1年前 , 2F
WIFI 有連到,硬體應該沒問題
02/12 00:04, 2F

02/12 00:04, 1年前 , 3F
RRSI: -68
02/12 00:04, 3F

02/12 00:04, 1年前 , 4F
看起來好像是資料POST有問題
02/12 00:04, 4F

02/12 00:04, 1年前 , 5F
不曉得程式碼是否需要調整?
02/12 00:04, 5F

02/12 01:47, 1年前 , 6F
先看一下是不是一睡不醒的問題....nodemcu一直都有
02/12 01:47, 6F

02/12 01:47, 1年前 , 7F
這類問題 另外看一下power是不是夠
02/12 01:47, 7F

02/12 01:48, 1年前 , 8F
如果你的wifi是用別人的code 通常沒有問題
02/12 01:48, 8F

02/12 01:48, 1年前 , 9F
先看供電行不行 這個inrush current很大的
02/12 01:48, 9F

02/12 01:49, 1年前 , 10F
他有可能醒來 或是要傳東西要大量電流 一沒拿到就
02/12 01:49, 10F

02/12 01:50, 1年前 , 11F
卡住 就會死掉
02/12 01:50, 11F
感謝回復~會再多試試

02/12 11:56, 1年前 , 12F
沒看到post的code 沒貼完?
02/12 11:56, 12F
內文已更新,謝謝~ ※ 編輯: lavender19 (223.137.9.204 臺灣), 02/12/2023 14:24:51 ※ 編輯: lavender19 (223.137.9.204 臺灣), 02/12/2023 14:41:25 ※ 編輯: lavender19 (223.137.9.204 臺灣), 02/12/2023 14:44:26
文章代碼(AID): #1ZvsD0kB (Electronics)