[請益] NodeMCU ESP8266 wifi 連線不穩(代po)
(已更新)
各位先進大家好~
請問我想將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
02/11 22:27, 1F
→
02/12 00:04,
1年前
, 2F
02/12 00:04, 2F
→
02/12 00:04,
1年前
, 3F
02/12 00:04, 3F
→
02/12 00:04,
1年前
, 4F
02/12 00:04, 4F
→
02/12 00:04,
1年前
, 5F
02/12 00:04, 5F
推
02/12 01:47,
1年前
, 6F
02/12 01:47, 6F
→
02/12 01:47,
1年前
, 7F
02/12 01:47, 7F
→
02/12 01:48,
1年前
, 8F
02/12 01:48, 8F
→
02/12 01:48,
1年前
, 9F
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
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