esp8266(1) 手機+Arduino+esp8266通信


 

ESP8266 Android與Arduino通信

功能描述:

1 Arduino上電,它通過軟串口(Arduino的 2號和3號腳)發送命令,配置espson8266為 AP模式,wifi名 DDD 密碼 123456 

   通過硬串口向電腦打印調試信息,以及espson8266收到指令后發出的信息。

2 等待Esp8266初始化成功,Arduino不斷監聽軟和硬串口

  1. 監聽電腦從硬串口發來的命令,轉發給軟串口讓Esp8266通過WIFI發送出去。
  • 電腦向硬串口發送命令沒什么格式,但是通過串口命令讓esp8266向wifi發命令,特定格式
  • AT+CIPSEND=0,15  //  申請向第0 個TCP鏈接,發送15個字符

    //收到OK,出現 > 輸入要發的 15個字符
    OK
    >
    Recv 15 bytes

    SEND OK

3 手機連接wifi,打開調試軟件,輸入地址 192.168.4.1,端口 6000 ,發送命令,格式隨意。反之接收來自電腦發來的命令。

 

 

手機端           電腦端   

特別說明:

1 arduino軟串口讀取ESP8266命令,出現亂碼,只要重新設置下其波特率。AT+UART=9600,8,1,0,0

2 我目前使用的IDE,雖然程序里設置串口波特率是 9600,但是,實際運行只有,4800. 所以,需要設置為9600*2=19200. 更換IDE可能會解決這個問題。

  • 但是IDE串口調試設置為9600就是9600

3 esp8266從串口收到數據格式結尾應該添加 “\r\n”    樣例;

  •  sendCommand("AT+CWMODE=2\r\n",2000,DEBUG);
  • String s="AT+CWSAP=\"DDD\",\"12345678\",11,0\r\n";
    sendCommand(s,2000,DEBUG);

4 為何做這個實驗。 其實單獨手機連接WIFI直接就通信了,為何中間加一個arduino單片機? 浪費成本和中轉的復雜性?

   因為,我目前不想費精力使用esp8266的專用SDK開發這個單片機,時間和精力成本不值得。

   我目前需要的功能,用Arduino當控制板,esp8266低成本的wifi轉發命令,這樣把開發又轉回arduino,

   目前針對 esp8266使用arduino ide 開發的 esp-12f板,已經出現,我已經購買,正在路上,以后可以直接使用arduino ide開發esp8266。

http://blog.csdn.net/gnf_cc/article/details/53667312

ARuino程序燒錄:

#include <SoftwareSerial.h>

#define DEBUG true

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                             // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                             // and the RX line from the esp to the Arduino's pin 3
void setup()
{
  Serial.begin(19200);
  esp8266.begin(19200); // your esp's baud rate might be different
   
  sendCommand("AT+CWMODE=2\r\n",2000,DEBUG); // reset module
 // delay(20000);
 String s="AT+CWSAP=\"DDD\",\"12345678\",11,0\r\n";
 sendCommand(s,2000,DEBUG);// reset module
    Serial.println("1");
 // delay(30000);
  sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
  delay(3000);
  sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
//   delay(20000);
  sendCommand("AT+CIPSERVER=1,6000\r\n",1000,DEBUG); // turn on server on port 80
  Serial.println("Server Ready");
}

void loop() { // run over and over
  if (esp8266.available()) {
    Serial.write(esp8266.read());
  }
   if (Serial.available()) {
    esp8266.write(Serial.read());
  }
}
String sendCommand(String command, const int timeout, boolean debug)
{
    String response = "";

    esp8266.print(command); // send the read character to the esp8266

    long int time = millis();

    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {

        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }

    if(debug)
    {
      Serial.print(response);
    }

    return response;
}

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM