连接 Arduino Uno R3 和 WiFi ESP8266,按下按钮时将“1”发送到另一个 WiFi 板

connecting arduino uno r3 and wifi esp8266 and sending a "1" to another wifi board when button is pressed

提问人:user22921814 提问时间:11/15/2023 最后编辑:πάντα ῥεῖuser22921814 更新时间:11/15/2023 访问量:17

问:

问题是
按下按钮时没有运行。我可以看到“Pressed”这个词被发送到两个板子之间。但我觉得也许要么是 String msg = newPort.readStringUntil('\r');不工作或消息未完全格式化为“已按下”
if (msg.equals("Pressed")){ wasPressed = 1; }

WIFI代码:

#include <ESP8266WiFi.h>
#include "ESPAsyncWebSrv.h"
 
#include <SoftwareSerial.h>
SoftwareSerial newPort (D1,D2);
String msg;
// Set your access point network credentials
const char* ssid = "my wifi";
const char* password = "1234512345";
 
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
 
String greeting() {
  return String("Welcome to Dawei's WiFi");
  // return msg;
  //return String(1.8 * bme.readTemperature() + 32);
}

int wasPressed = 0;


String status() {
  if (wasPressed){
    return String("1");
  } else {
    return String("0");
  }
}
 
 
void setup(){
  // Serial port for debugging purposes
  Serial.begin(115200);
  newPort.begin(9600);
  Serial.println();
  
  // Setting the ESP as an access point
  Serial.print("Setting AP (Access Point)…");
  // Remove the password parameter, if you want the AP (Access Point) to be open
  WiFi.softAP(ssid, password);
 
  IPAddress IP = WiFi.softAPIP();
  IPAddress staticIP(192, 168, 4, 1);
  Serial.print("AP IP address: ");
  Serial.println(IP);
  delay(3000);
 
  // server.on("/welcome", HTTP_GET, [](AsyncWebServerRequest *request){
  //   request->send_P(200, "text/plain", greeting().c_str());
  // });

  server.on("/status", HTTP_GET, [](AsyncWebServerRequest *request){
    request->send_P(200, "text/plain", status().c_str());
  });
 
  
  
  // Start server
  server.begin();
}
 
 
 
void loop(){
  String msg = newPort.readStringUntil('\r');
  //Serial.print("Received message: ");
  Serial.println(msg);
    if (msg.equals("Pressed")){
      wasPressed = 1;
    }
  // delay(1000);
  // yield();
}

Arduino代码: //

void setup() {
  Serial.begin(9600);
  pinMode(13, INPUT_PULLUP);
}
int counter = 0;

void loop() {
  if (digitalRead(13)==LOW){
    Serial.println("Pressed");
  } else {
    Serial.println("Not Pressed");
  }  
  delay(1000);
}

我只是希望在按下按钮时看到“1”,但是如果条件给我带来麻烦并且它永远不会运行该代码块。另一个 WiFi 板只是不断得到“0” 连续剧节目 收到的消息: 未按下 收到的消息: 压

也许 msg 是空白的,因为我希望看到 收到消息:未按下 未按下

Arduino-uno arduino-esp8266 arduino-c++

评论

0赞 Juraj 11/16/2023
只做readString直到如果newPort.available()

答: 暂无答案