当前位置:   article > 正文

Arduino UNO与ESP8266串口连接_arduino uno连接esp8266

arduino uno连接esp8266

1. ESP8266烧录AT固件

1.1 烧录软件下载

登录乐鑫官网下载地址,下载Flash 下载工具

1.2 烧录AT固件

首先,点击安信可AT固件下载地址,下载出厂默认 AT 固件。
然后打开Flash Download Tools,点击Download ESP8266,如图所示:
在这里插入图片描述
在COM处选择端口号,点击START连接。此时,DETECTED INFO会显示连接的ESP8266硬件信息,包括晶振频率和Flash Size。选择DoNotChgBin,并选择下载的AT固件文件,设置地址为0x00000,设置其余参数和ESP8266信号一致。点击ERASE清空FLASH,点击START开始写入直到完成。

2. ESP8266 AT指令

AT指令第二列
AT+CWMODE=2开启 AP 模式
AT+CWSAP=“ESP8266”,“0123456789”,11,0设置模块的 wifi 和密码
AT+CIPSERVER=1,8899设置模块服务器端口
AT+CIPSEND=0,11进入数据发送模式为11个字节
AT+CWMODE=1设置模组为STA模式
AT+CWLAP查询附近 WIFI
AT+CWJAP=“123123”,“12345678”连接 WIFI
AT+CIFSR查看路由器分配给模组的IP地址
AT+CIPMUX=0打开单连接
AT+CIPMUX=1打开多连接
AT+CIPMODE=1设置透传模式
AT+CIPSTART=“TCP”,“192.168.43.104”,8899连接手机端建立的TCP服务器
AT+CIPSEND开始发送数据
+++注意退出透传,直接发送。取消发送新行
AT+CIPSTART=0,“UDP”,“255.255.255.255”,50000,1000, 0是建立 UDP 连接,其中手机 UDP server 设置 50000,UDP client 设置的端口 1000
AT+CIPSTART=“UDP”,“192.168.43.104”,5000,2000,0连接网络调试助手UDP的IP和端口
  • 注意透传只能在单连接模式下进行,所以在建立连接之前一定要用(AT+CIPMUX=0设置单连接),但是模块处于服务器模式下时,必须要多链接,由于冲突,所以模块开启服务器模式不能做 TCP透传!
//模块通过数据外网透传
AT+CWMODE=3                        设置 AP 和 STA 共存模式
AT+CWLAP                            查询附近 WIFI
AT+CWJAP="HUAWEI-C4VTTJ","1234567890"           连接 wifi
AT+CIPMUX=0                          设置单连接
AT+CIPMODE=1                        设置透传模式
AT+CIPSTART="TCP","115.29.109.104",6602    连接外网服务器,请参考下面网址
AT+CIPSEND
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3. Arduino UNO 串口连接方式

Arduino UNO ESP8266 D2-TX D3-RX 5V-Vin 3.3V-3.3V GND-GND Arduino UNO ESP8266
  • D2、D3为arduino设置的软串口RX和TX。

4. Arduino UNO与ESP8266-12f串口连接

Arduino IDE官网下载连接,Arduino UNO端程序如下:

#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(9600);
  esp8266.begin(9600); // your esp's baud rate might be different

  pinMode(11,OUTPUT);
  digitalWrite(11,LOW);

  pinMode(12,OUTPUT);
  digitalWrite(12,LOW);

  pinMode(13,OUTPUT);
  digitalWrite(13,LOW);

  sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
  setAP("ESP8266","w12345678");
  Serial.println("Server Ready");
}

void loop()
{
  if(esp8266.available()) // check if the esp is sending a message 
  {
    if(esp8266.find("+IPD,"))
    {
     delay(1000); // wait for the serial buffer to fill up (read all the serial data)
     // get the connection id so that we can then disconnect
     int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48

     esp8266.find("pin="); // advance cursor to "pin="

     int pinNumber = (esp8266.read()-48); // get first number i.e. if the pin 13 then the 1st number is 1
     int secondNumber = (esp8266.read()-48);
     if(secondNumber>=0 && secondNumber<=9)
     {
      pinNumber*=10;
      pinNumber +=secondNumber; // get second number, i.e. if the pin number is 13 then the 2nd number is 3, then add to the first number
     }

     digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin    

     // build string that is send back to device that is requesting pin toggle
     String content;
     content = "Pin ";
     content += pinNumber;
     content += " is ";

     if(digitalRead(pinNumber))
     {
       content += "ON";
     }
     else
     {
       content += "OFF";
     }

     sendHTTPResponse(connectionId,content);

     // make close command
     String closeCommand = "AT+CIPCLOSE="; 
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";

     sendCommand(closeCommand,1000,DEBUG); // close connection
    }
  }
}

void setAP(String wifi,String passwd)
{
    sendCommand("AT+CWMODE=2\r\n",1000,DEBUG); // 开启 AP 模式
    sendCommand("AT+CWSAP=\""+wifi+"\",\""+passwd+"\",11,0\r\n",3000,DEBUG);
}

void setSTA(String wifi,String passwd)
{
    sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // 开启 STA 模式
    sendCommand("AT+CWJAP=\""+wifi+"\",\""+passwd+"\"\r\n",3000,DEBUG);
    delay(10000);
    sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
    sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
    sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
}

/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";

    int dataSize = command.length();
    char data[dataSize];
    command.toCharArray(data,dataSize);

    esp8266.write(data,dataSize); // send the read character to the esp8266
    if(debug)
    {
      Serial.println("\r\n====== HTTP Response From Arduino ======");
      Serial.write(data,dataSize);
      Serial.println("\r\n========================================");
    }

    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;
}

/*
* Name: sendHTTPResponse
* Description: Function that sends HTTP 200, HTML UTF-8 response
*/
void sendHTTPResponse(int connectionId, String content)
{

     // build HTTP response
     String httpResponse;
     String httpHeader;
     // HTTP Header
     httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n"; 
     httpHeader += "Content-Length: ";
     httpHeader += content.length();
     httpHeader += "\r\n";
     httpHeader +="Connection: close\r\n\r\n";
     httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated by adding this extra space
     sendCIPData(connectionId,httpResponse);
}

/*
* Name: sendCIPDATA
* Description: sends a CIPSEND=<connectionId>,<data> command
*
*/
void sendCIPData(int connectionId, String data)
{
   String cipSend = "AT+CIPSEND=";
   cipSend += connectionId;
   cipSend += ",";
   cipSend +=data.length();
   cipSend +="\r\n";
   sendCommand(cipSend,1000,DEBUG);
   sendData(data,1000,DEBUG);
}

/*
* Name: sendCommand
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes, false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203

5. 串口调试

打开Arduino IDE串口调试工具—>串口监视器,无需调试,静静地看着就好了。
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/610323
推荐阅读
相关标签
  

闽ICP备14008679号