ESP8266通过串行通信与Arduino通信

时间:2017-02-28 13:07:44

标签: firebase arduino firebase-realtime-database arduino-uno arduino-ide

我构建了一个可以使用带有firebase Arduino库的ESP8266与我的Firebase应用程序通信的系统,

一切都运转良好然而我现在有一个问题,让ESP8266说话,听一下StandAlone Atmega328P(Arduino Uno中使用的那个)的微控制器。

以下是要点 1-不能使用softwareSerial,因为所有数字引脚都被占用。 2-尝试了同样问题的不同波士顿。 3-只有一次工作,Arduino能够正确地收听Serial。但无法通过ESP读取连续剧。

我绝对能够从ESP写入串口并通过Arduino读取它但是当试图使用IF语句时,无法识别字符串,

所以我尝试将字符串打印4次,这就是我得到的。

锁门

锁门

锁门

锁门

如果有人能解决问题,我发布了Arduino和ESP的代码

ESP代码

void getData(){

FirebaseObject object = Firebase.get(EspMac);
String Door = object.getString("Door");
String Connection = object.getString("Connect");
if(Door == "Open Door"){
  Serial.println(Door);
  delay(100);
  Firebase.set(path3,"");
  Door = "";
}else if(Door == "Lock Door"){
  Serial.println(Door);
  delay(100);
  Firebase.set(path3,"");
  Door = "";
}else if (Connection == "Connect"){
  Firebase.set(path5,"Received");
  Connection = "";
  }

}

ARDUINO代码

do {
  if(digitalRead(wipeB) == LOW)
      servoDoorOpen ();
  while (Serial.available() > 0){
    String Door = Serial.readString();
    delay(100);
    Serial.println(Door);
    Serial.println(Door);
    Serial.println(Door);
    Serial.println(Door);


    if (Door.equals("Open Door")){
      //Serial.println("We open Doors");
      servoDoorOpen();
      Door = "";
    }
    else if (Door.equals("Lock Door")){
      //Serial.println("We Lock Doors");
        servoDoorLock();
        Door = "";
    }
  }

谢谢。

1 个答案:

答案 0 :(得分:0)

最后我解决了自己的问题, 仍然不太确定我是如何解决这个问题的,但是我在代码中添加了一些延迟,而不是一切都很好

void getData(){
    FirebaseObject object = Firebase.get(EspMac);
    String Door = object.getString("Door");
    String Connection = object.getString("Connect");
    if(Door == "Open Door"){
        Serial.println(Door);
        Firebase.set(path3,"");
        Door = "";
    }else if(Door == "Lock Door"){
        Serial.println(Door);
        Firebase.set(path3,"");
        Door = "";
    }else if (Connection == "Connect"){
        Firebase.set(path5,"Received");
        Connection = "";
    }  
}

void loop() {
   getData();
   while (Serial.available() > 0){
     delay (100);
     String h = Serial.readString();
     String DoorStat = h.substring(0,11);
     if (DoorStat.equals("Door Opened")){
        Firebase.set(path2, DoorStat.c_str());
        DoorStat="";
        delay(100);
     }else if (DoorStat.equals("Door Locked"))
        Firebase.set(path2, DoorStat.c_str());
   }
}

Android代码

  do {
  if(digitalRead(wipeB) == LOW)
      servoDoorOpen ();
  while (Serial.available() > 0){
    delay (100);
    String h = Serial.readString();
    String Door = h.substring(0,9);

    if (Door.equals("Open Door")){
      //Serial.println("We open Doors");
      servoDoorOpen();
      Door = "";
    }
    else if (Door.equals("Lock Door")){
      //Serial.println("We Lock Doors");
        servoDoorLock();
        Door = "";
    }
  }