esp 8266 是 wifi 开发板
开发板连接
必须接 3.3v
en 口接 3.3v 为 enable 的意思
tx 接开发板的 rx 口
rx 接开发板的 tx 口
我接在开发板的 rx/tx 口时,不能工作,所以我接在了 4/5 口
当需要重置的时候,将 res 接下地
其它的端口在烧入固件的时候使用
工作方式
- 接在 usb to ttl 工作,然后使用 AiThinker_Serial_Tool_V1.2.3 进行测试。可以发送 AT 命令检测工作是否正常
> 注意 ub to ttl 的 vcc 与 3.3 在面包板上进行了短接。但我直接使用万用表测试 vcc 为 3.3v, 我不知道能不能直接 5v 与 vcc 和短接,然后 3.3v 输出给 8266。 所以我在面包板上将 vcc 与 3.3v 进行了短接。然后按上图的说明进行连接 - 可以直接使用 arduino 对 esp 8266 进行开发。我是使用 uno 主板对 8266 发送指令进行开发
- 我将 rx/tx 接在 uno 4、5 口,然后使用
``` SoftwareSerial wifiSerial(5, 4) ``` 串口流程操作类对 8266 进行通讯编程。比如发送 at 指令等 - 然后我找到了
```Adafruit_ESP8266``` 对 ``` SoftwareSerial ``` 进行包装。它包装了 wifi 连接等操作。但是很多指令没有包装,我还要再找找。 - 有一个
``` ESP8266Wifi ``` 好像很不错的样子。[ESP8266Wifi 官网](https://arduino.esp8266.com/stable/package_esp8266com_index.json)
AT 简易命令
https://zhuanlan.zhihu.com/p/55626114
ESP8266Wifi 安装
在 arduino 的设置中加入 ESP8266 的包地址
1. arduino 的首先项中的附加开发板管理中加入如下地址(如果有多个使用 ; 进行分隔 ) https://arduino.esp8266.com/stable/package_esp8266com_index.json
1. 在开发板管理中找到
```esp8266``` 进行安装, 可以安装 ESP8266WIFI 库,但是现在还不会用
1. 这里有一些调用例子 ```https://codeminions.github.io/2021/02/14/Arduino/```
使用串口流操作 ESP8266
在下面地址找到对 SoftwareSerial 的操作简单封装
```https://www.instructables.com/Add-WiFi-to-Arduino-UNO/ ````
封装的不好。有乱码,有空再研究
// 部分代码来自: https://www.instructables.com/Add-WiFi-to-Arduino-UNO/
#include "include.h"
#include "wifiTest.h"
#include <ArduinoLog.h>
#include <SoftwareSerial.h>
#include <Adafruit_ESP8266.h>
bool DEBUG = true; // 显示调试日志
int responseTime = 10; // 超时时间
// 我将 rx/tx 接在了 4/5 口
SoftwareSerial wifiSerial(5, 4); // RX, TX
/** 登录 wifi */
void loginWifi()
{
// auto result = esp8266.connectToAP(F("sinox624", F("1qaz2wsx3edc")));
// Log.infoln("登录完成, %t", result);
}
void intEsp8266()
{
wifiSerial.begin(115200);
Log.infoln("等待 wifi 串口连接");
while (!wifiSerial)
{
/* code */
}
Log.infoln("RST");
sendToWifi("AT+RST");
Log.infoln("查看版本");
sendToWifi("AT+GMR");
Log.infoln("列出 wifi 列表");
sendToWifi("AT+CWLAP");
// 连接 wifi , 这里我使用了 Adafruit_ESP8266 库
Log.infoln("连接 wifi ");
Adafruit_ESP8266 esp8266(&wifiSerial, &Serial);
auto result = esp8266.connectToAP(F("sinox624"), F("1qaz2wsx3edc"));
Log.infoln("连接 wifi 结果 %t", result);
if (result)
{
Log.infoln("当前连接 wifi");
sendToWifi("AT+CWJAP");
Log.infoln("当前 ip ");
wifiSerial.println("AT+CIFSR");
Log.infoln("关闭 wifi ");
esp8266.closeAP();
}
}
void updateEsp8266()
{
}
/** 通过 wifi 发送数据 */
void sendData(String str, const int timeout, boolean debug)
{
String len = "";
len += str.length();
sendToWifi("AT+CIPSEND=0," + len, timeout, debug);
delay(100);
sendToWifi(str, timeout, DEBUG);
delay(100);
sendToWifi("AT+CIPCLOSE=5", timeout, debug);
}
/** 从字符串中找到指定的值 */
boolean find(String string, String value)
{
if (string.indexOf(value) >= 0)
return true;
return false;
}
/** 从 wifi 串口中读取数据 */
String readWifiSerialMessage()
{
char value[100];
int index_count = 0;
while (wifiSerial.available() > 0)
{
value[index_count] = wifiSerial.read();
index_count++;
value[index_count] = '\0'; // Null terminate the string
}
String str(value);
str.trim();
return str;
}
String sendToWifi(String command)
{
return sendToWifi(command, responseTime, DEBUG);
}
/** 发送命令至 wifi 串口, 这里用到了 String */
String sendToWifi(String command, const int timeout, boolean debug)
{
String response = "";
wifiSerial.println(command); // send the read character to the esp8266
long int time = millis();
while ((time + timeout) > millis())
{
while (wifiSerial.available())
{
// The esp has data so display its output to the serial window
char c = wifiSerial.read(); // read the next character.
if( c == '\0'){
break;
}
response += c;
}
}
if (debug)
{
Serial.println(response);
// auto len = response.length();
// char buffer[len + 1];
// response.toCharArray(buffer, len);
// Log.info(buffer);
}
return response;
}