-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathespBasicExample.ino
150 lines (131 loc) · 2.88 KB
/
espBasicExample.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
#include <SoftwareSerial.h>
#define SSID "linksys" //your wifi ssid here
#define PASS "ffffffffff" //your wifi wep key here
#define DST_IP "220.181.111.85" //baidu.com
#define HOST "www.baidu.com"
SoftwareSerial dbgSerial(10, 11); // RX, TX
char myChar;
void setup()
{
pinMode(9, OUTPUT);
pinMode(13, OUTPUT);
//blink led13 to indicate power up
for(int i = 0; i<15; i++)
{
digitalWrite(13,HIGH);
delay(50);
digitalWrite(13,LOW);
delay(50);
}
// Open serial communications for WiFi module:
Serial.begin(115200);
// Set time to wait for response strings to be found
Serial.setTimeout(5000);
//Open software serial for debugging - must use serial to usb (FTDI) adapter
//dbgSerial.begin(19200); //can't be faster than 19200 for softserial
dbgSerial.begin(38400); //38400 softserial works fine for me
dbgSerial.println("ESP8266 Demo");
delay(100);
//test if the module is ready
Serial.println("AT+RST");
if(Serial.find("ready"))
{
dbgSerial.println("Module is ready");
delay(1000);
//connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected)
{
//die
while(1);
}
delay(5000);
//set the single connection mode
Serial.println("AT+CIPMUX=0");
}
else
{
dbgSerial.println("Module didn't respond.");
delay(100);
//serial loop mode for diag
while(1){
while (dbgSerial.available()) {
myChar = dbgSerial.read();
Serial.print(myChar);
}
while (Serial.available()) {
myChar = Serial.read();
delay(25);
dbgSerial.print(myChar);
}
}
}
}
void loop()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
Serial.println(cmd);
dbgSerial.println(cmd);
if(Serial.find("Error")) return;
cmd = "GET /status HTTP/1.0\r\nHost: ";
cmd += HOST;
cmd += "\r\n\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">"))
{
dbgSerial.print(">");
}
else
{
Serial.println("AT+CIPCLOSE");
dbgSerial.println("connect timeout");
delay(1000);
return;
}
Serial.print(cmd);
delay(500);
//Serial.find("+IPD");
while (Serial.available())
{
char c = Serial.read();
dbgSerial.write(c);
if(c=='\r') dbgSerial.print('\n');
delay(50);
}
dbgSerial.println();
dbgSerial.println("====");
delay(1000);
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
dbgSerial.println(cmd);
Serial.println(cmd);
delay(2000);
if(Serial.find("OK"))
{
dbgSerial.println("OK, Connected to WiFi.");
return true;
}
else
{
dbgSerial.println("Can not connect to the WiFi.");
return false;
}
}