forked from fustyles/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP01_UNO_Station_getResponse.ino
155 lines (132 loc) · 4.02 KB
/
ESP01_UNO_Station_getResponse.ino
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
/*
Arduino Uno + ESP-01 (AT Command)
Author : ChungYi Fu (Kaohsiung, Taiwan) 2018-08-07 22:00
https://www.facebook.com/francefu
Expanding Arduino Serial Port Buffer Size
https://internetofhomethings.com/homethings/?p=927
https://www.facebook.com/francefu/videos/10211231615856099/
*/
// Enter your WiFi ssid and password
String WIFI_SSID = ""; //your network SSID
String WIFI_PWD = ""; //your network password
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // ESP01 TX->D10, RX->D11
String ReceiveData="",STAIP="",STAMAC="",ResponseData="";
void setup()
{
Serial.begin(9600);
//You must change uart baud rate of ESP-01 to 9600.
mySerial.begin(115200); //Default uart baud rate
SendData("AT+UART_CUR=9600,8,1,0,0",2000); //Change uart baud rate to 9600
mySerial.begin(9600); // you will get more stable data without junk chars.
mySerial.setTimeout(10);
SendData("AT+CIPSERVER=0",2000);
SendData("AT+CWMODE_CUR=1",2000);
SendData("AT+CIPMUX=0",2000);
if (WIFI_SSID!="")
SendData("AT+CWJAP_CUR=\""+WIFI_SSID+"\",\""+WIFI_PWD+"\"",5000);
else
Serial.print("Please check your network SSID and password settings");
}
void loop()
{
if (STAIP=="")
{
getSTAIP();
if (STAIP!="")
{
pinMode(13,1);
for (int i=0;i<20;i++)
{
digitalWrite(13,1);
delay(50);
digitalWrite(13,0);
delay(50);
}
}
}
else
{
ResponseData="";
String Domain="www.arduino.cc";
String request = "GET / HTTP/1.1\r\nHost: "+Domain+"\r\n\r\n";
/*
If request length is too long, it can't work! (Arduino Uno)
If you change buffer size to 256 bytes, request length must be less than or equal to 128.
*/
SendData("AT+CIPSTART=\"TCP\",\""+Domain+"\",80", 4000);
SendData("AT+CIPSEND=" + String(request.length()+2), 2000); //Serial.println(request) -> request+"\r\n" -> request.length()+2
SendData(request, 6000); // Adjust the waiting time until you get the complete response data.
SendData("AT+CIPCLOSE",2000);
Serial.println(ResponseData); // If response length is too long, it will get junk chars because of serial buffer size.
delay(10000);
}
}
void SendData(String data,int TimeLimit)
{
mySerial.println(data);
mySerial.flush();
delay(10);
WaitReply(TimeLimit);
}
String WaitReply(long int TimeLimit)
{
ReceiveData="";
long int StartTime=millis();
while( (StartTime+TimeLimit) > millis())
{
if (mySerial.available())
{
delay(4);
while(mySerial.available())
{
if (ReceiveData.indexOf("SEND OK")!=-1)
ResponseData=ResponseData+String(char(mySerial.read()));
else
ReceiveData=ReceiveData+String(char(mySerial.read()));
}
if ((ReceiveData.indexOf("OK")!=-1)&&(ReceiveData.indexOf("SEND OK")==-1))
return ReceiveData;
}
}
return ReceiveData;
}
void getSTAIP()
{
ReceiveData="";
if (mySerial.available())
{
while (mySerial.available())
{
char c=mySerial.read();
ReceiveData=ReceiveData+String(c);
}
if (ReceiveData.indexOf("WIFI GOT IP")!=-1)
{
while(!mySerial.find('OK')){}
delay(10);
int staipreadstate=0,stamacreadstate=0,j=0;
mySerial.println("AT+CIFSR");
mySerial.flush();
delay(6);
while(mySerial.available())
{
char c=mySerial.read();
String t=String(c);
if (t=="\"") j++;
if (j==1)
staipreadstate=1;
else if (j==2)
staipreadstate=0;
if ((staipreadstate==1)&&(t!="\"")) STAIP=STAIP+t;
if (j==3)
stamacreadstate=1;
else if (j==4)
stamacreadstate=0;
if ((stamacreadstate==1)&&(t!="\"")) STAMAC=STAMAC+t;
delay(1);
}
Serial.println("STAIP: "+STAIP+"\nSTAMAC: "+STAMAC+"\n");
}
}
}