esp8266深入

本文最后更新于:2024年3月7日 下午


这里是esp8266初试的后续,主要是使用arduino ide来向esp8266中烧写程序。
esp8266不仅可以作为wifi模块来使用,同时还可以看作是一个小的自带wifi模块的单片机。
根据AT指令进行通讯的是出厂时烧录在里面的源程序,你自己写的程序烧录进去的时候会取代原有的程序,若是还想使用AT指令,需要重新刷固件
这里主要是介绍esp8266的自主编程,实现与服务器连接并接收服务器指令来操控led闪烁。


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
/*这篇把8266作为TCPcleint,加入手机创建的tcpServer中来控制一个继电器*/

#include <ESP8266WiFi.h>

#define relayPin 2 //继电器连接在8266的GPIO2上
//const char *ssid = "HiWiFi_cy1999";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
//const char *password = "zzuacmlab";//你要连接的wifi密码

const char *ssid = "OnePlus Ace";//这里是我的wifi,你使用时修改为你要连接的wifi ssid
const char *password = "16639193852";//你要连接的wifi密码
const char *host = "frp-bar.top";//修改为手机的的tcpServer服务端的IP地址,即手机在路由器上的ip
WiFiClient client;
const int tcpPort = 41491;//修改为你建立的Server服务端的端口号


void setup()
{
Serial.begin(115200);
delay(10);
Serial.println();
Serial.println();
Serial.print("Connecting to ");//写几句提示
Serial.println(ssid);
pinMode(2, OUTPUT);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)//WiFi.status() ,这个函数是wifi连接状态,返回wifi链接状态
//这里就不一一赘述它返回的数据了,有兴趣的到ESP8266WiFi.cpp中查看
{
delay(500);
Serial.print(".");
}//如果没有连通向串口发送.....

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());//WiFi.localIP()返回8266获得的ip地址
}


void loop()
{
if((!client.connected()))
{
while (!client.connected())//几个非连接的异常处理
{
if (!client.connect(host, tcpPort))
{
Serial.println("connection....");
//client.stop();
delay(500);

}
}
client.write("esp8266");
}
while (client.available())//改动的就这里啦,无线读取到的数据转发到到串口
{
uint8_t c = client.read();
Serial.write(c);
if (c == 'a') { //pc端发送a和b来控制
digitalWrite(2, HIGH);
client.write("已关灯");
}
if (c == 'b')
{
digitalWrite(2, LOW);
client.write("已开灯");
}
if (c == 'c')
{
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
client.write("已脉冲1");
}
if (c == 'd')
{
digitalWrite(2, LOW);
delay(200);
digitalWrite(2, HIGH);
client.write("已脉冲2");
}
break;
}


if (Serial.available())//串口读取到的转发到wifi,因为串口是一位一位的发送所以在这里缓存完再发送
{
delay(500);
size_t counti = Serial.available();
Serial.println(counti);
char sbuf[100];
int i=0;
while(Serial.available())
sbuf[i++]=Serial.read();
sbuf[i]='\n';
client.write(sbuf,counti);

}
}
/*int flasher_pin = 2; // GPIO2 for ESP-01S

void setup() {
Serial.begin(115200); // can use Serial and flash Led at the same time on ESP-01S
pinMode(2, OUTPUT);
for (int i = 10; i > 0; i--) {
Serial.print(i); Serial.print(' ');
delay(500);
}
Serial.println();
delay(1000);
}

unsigned long counter = 0;
void loop() {

delay(1000);
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
Serial.println(counter++);
}*/


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!