作者lavender19 (lavender)
看板Electronics
標題[請益] NodeMCU ESP8266 wifi 不穩 (更新) 代po
時間Sat Feb 11 17:43:26 2023
(已更新)
各位先進大家好~
請問我想將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://webptt.com/m.aspx?n=bbs/Electronics/M.1676108608.A.B8B.html
1F:推 god145145: 硬體都驗證過了嗎? 連上時RSSI多少02/11 22:27
2F:→ lavender19: WIFI 有連到,硬體應該沒問題02/12 00:04
3F:→ lavender19: RRSI: -6802/12 00:04
4F:→ lavender19: 看起來好像是資料POST有問題02/12 00:04
5F:→ lavender19: 不曉得程式碼是否需要調整?02/12 00:04
6F:推 mmonkeyboyy: 先看一下是不是一睡不醒的問題....nodemcu一直都有02/12 01:47
7F:→ mmonkeyboyy: 這類問題 另外看一下power是不是夠02/12 01:47
8F:→ mmonkeyboyy: 如果你的wifi是用別人的code 通常沒有問題02/12 01:48
9F:→ mmonkeyboyy: 先看供電行不行 這個inrush current很大的02/12 01:48
10F:→ mmonkeyboyy: 他有可能醒來 或是要傳東西要大量電流 一沒拿到就02/12 01:49
11F:→ mmonkeyboyy: 卡住 就會死掉02/12 01:50
感謝回復~會再多試試
12F:→ god145145: 沒看到post的code 沒貼完?02/12 11:56
內文已更新,謝謝~
※ 編輯: 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