-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathesp8266Sample.ino
194 lines (168 loc) · 5.01 KB
/
esp8266Sample.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
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
#include <SoftwareSerial.h>
SoftwareSerial wifiSerial(2, 3); // RX, TX for ESP8266
bool DEBUG = true; //show more logs
int responseTime = 10; //communication timeout
void setup()
{
pinMode(13,OUTPUT); //set build in led as output
// Open serial communications and wait for port to open esp8266:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
wifiSerial.begin(115200);
while (!wifiSerial) {
; // wait for serial port to connect. Needed for Leonardo only
}
sendToWifi("AT+CWMODE=2",responseTime,DEBUG); // configure as access point
sendToWifi("AT+CIFSR",responseTime,DEBUG); // get ip address
sendToWifi("AT+CIPMUX=1",responseTime,DEBUG); // configure for multiple connections
sendToWifi("AT+CIPSERVER=1,80",responseTime,DEBUG); // turn on server on port 80
sendToUno("Wifi connection is running!",responseTime,DEBUG);
}
void loop()
{
if(Serial.available()>0){
String message = readSerialMessage();
if(find(message,"debugEsp8266:")){
String result = sendToWifi(message.substring(13,message.length()),responseTime,DEBUG);
if(find(result,"OK"))
sendData("\nOK");
else
sendData("\nEr");
}
}
if(wifiSerial.available()>0){
String message = readWifiSerialMessage();
if(find(message,"esp8266:")){
String result = sendToWifi(message.substring(8,message.length()),responseTime,DEBUG);
if(find(result,"OK"))
sendData("\n"+result);
else
sendData("\nErrRead"); //At command ERROR CODE for Failed Executing statement
}else
if(find(message,"HELLO")){ //receives HELLO from wifi
sendData("\\nHI!"); //arduino says HI
}else if(find(message,"LEDON")){
//turn on built in LED:
digitalWrite(13,HIGH);
}else if(find(message,"LEDOFF")){
//turn off built in LED:
digitalWrite(13,LOW);
}
else{
sendData("\nErrRead"); //Command ERROR CODE for UNABLE TO READ
}
}
delay(responseTime);
}
/*
* Name: sendData
* Description: Function used to send string to tcp client using cipsend
* Params:
* Returns: void
*/
void sendData(String str){
String len="";
len+=str.length();
sendToWifi("AT+CIPSEND=0,"+len,responseTime,DEBUG);
delay(100);
sendToWifi(str,responseTime,DEBUG);
delay(100);
sendToWifi("AT+CIPCLOSE=5",responseTime,DEBUG);
}
/*
* Name: find
* Description: Function used to match two string
* Params:
* Returns: true if match else false
*/
boolean find(String string, String value){
return string.indexOf(value)>=0;
}
/*
* Name: readSerialMessage
* Description: Function used to read data from Arduino Serial.
* Params:
* Returns: The response from the Arduino (if there is a reponse)
*/
String readSerialMessage(){
char value[100];
int index_count =0;
while(Serial.available()>0){
value[index_count]=Serial.read();
index_count++;
value[index_count] = '\0'; // Null terminate the string
}
String str(value);
str.trim();
return str;
}
/*
* Name: readWifiSerialMessage
* Description: Function used to read data from ESP8266 Serial.
* Params:
* Returns: The response from the esp8266 (if there is a reponse)
*/
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;
}
/*
* Name: sendToWifi
* 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 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.
response+=c;
}
}
if(debug)
{
Serial.println(response);
}
return response;
}
/*
* Name: sendToUno
* Description: Function used to send data to Arduino.
* 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 sendToUno(String command, const int timeout, boolean debug){
String response = "";
Serial.println(command); // send the read character to the esp8266
long int time = millis();
while( (time+timeout) > millis())
{
while(Serial.available())
{
// The esp has data so display its output to the serial window
char c = Serial.read(); // read the next character.
response+=c;
}
}
if(debug)
{
Serial.println(response);
}
return response;
}