繁体   English   中英

Arduino Uno r3 将字符串上传到数据库时出现 wifi 错误

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM