[英]Arduino Uno r3 with wifi error when uploading string to database
我無法將字符串從我的 uno 傳輸到 ESP8266,然后傳輸到數據庫。 不是在 uno 程序中顯示“低”和定向,而是在我的網頁而不是整個世界的桌子上顯示低的每個字符。 (見附圖)。
Arduino Uno 的代碼:
// send as
String test = "Low";
void setup()
{
Serial.begin(115200);
// wait for the serial port to connect. Required for Leonardo/Micro native USB port only
while (!Serial) { ; }
}
void loop()
{
Serial.print(test);
}
板載 ESP8266 的代碼:
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
char c []= "";
// Replace with your network credentials
const char* ssid = "*****";
const char* password = "****";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "******";
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
String apiKeyValue = "****";
String humidityValue;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if(Serial.available())
{
char c = Serial.read();
delay(100);
humidityValue = c;
Serial.print(humidityValue);
}
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "api_key=" + apiKeyValue + "&humidity=" + humidityValue + "";
int httpResponseCode = http.POST(httpRequestData);
delay(1000);
http.end();
}
delay(1000);
}
您一次只讀取一個字符:
char c = Serial.read();
char 是一個字符,而不是整個字符串。
您可以讀取整個字符串,但隨后您只會得到已經到達的任何內容,這可能取決於時間。 您正在發送 LowLowLowLowLow ......所以接收者無法知道字符串在哪里結束。
也許發送“Low\\n”,然后在接收端繼續讀取字符,直到看到 '\\n' 然后進行處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.