Vous êtes sur la page 1sur 1

1 #include <SoftwareSerial.

h>
2
3 #define DEBUG true
4
5 SoftwareSerial esp8266(2,3); // rx, tx
6 // This means that you need to connect the TX pin of the esp to the Arduino's pin 2
7 // and the RX pin of the esp to the Arduino's pin 3
8 int relay1 = 9;
9 int relay2 = 10;
10 int relay3 = 11;
11 int relay4 = 12;
12 int relay5 = 13;
13
14
15 int flag1 = 0;
16 int flag2 = 0;
17 int flag3 = 0;
18 int flag4 = 0;
19 int flag5 = 0;
20
21 void setup()
22 {
23 Serial.begin(115200);
24 esp8266.begin(115200); // your esp's baud rate might be different
25 pinMode(relay1,OUTPUT);
26 pinMode(relay2,OUTPUT);
27 pinMode(relay3,OUTPUT);
28 pinMode(relay4,OUTPUT);
29 pinMode(relay5,OUTPUT);
30
31 digitalWrite(relay1, LOW);
32 digitalWrite(relay2, LOW);
33 digitalWrite(relay3, LOW);
34 digitalWrite(relay4, LOW);
35 digitalWrite(relay5, LOW);
36
37
38
39 sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
40 sendCommand("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
41 //sendCommand("AT+CWJAP=\"mySSID\",\"myPassword\"\r\n",3000,DEBUG);
42 //delay(10000);
43 sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
44 sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
45 sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
46 Serial.println("Server Ready");
47 }
48
49 void loop()
50 {
51 char pinNumber = '0';
52 char secondNumber = '0';
53 if(esp8266.available()) // check if the esp is sending a message
if
54 {
55
56
57 //delay(1000); // wait for the serial buffer to fill up (read all the serial data)
58
59
60 // get the connection id so that we can then disconnect
61 char connectionId = '0'; //esp8266.read(); // subtract 48 because the read() function returns
62 // the ASCII decimal value and 0 (the first decimal number) starts at 48
63 esp8266.find("pin="); // advance cursor to "pin="
64 char pinNumber = (esp8266.read()); // get first number i.e. if the pin 13 then the 1st number is 1
65 Serial.println("pin number:");
66 Serial.print(pinNumber);
67
68 char secondNumber = (esp8266.read());
69 Serial.println("second number:");
70 Serial.println(secondNumber);
71 String content ="" ;
72
73 // relay1
74 if
if((pinNumber == '1')&&(secondNumber == '1') && ( flag1 == 0))
75 {
76 digitalWrite(relay1, HIGH);
77 content = "relay1 is ON ";
78 flag1 = 1;
79
80 }
81
82 if
if((pinNumber == '1')&&(secondNumber == '8') && (flag1 == 1) )
83 {
84 digitalWrite(relay1, LOW);
85 content = "Relay1 is OFF ";
86 flag1 = 0;
87
88
89 }
90
91 // relay2
92 if
if((pinNumber == '2')&&(secondNumber == '3') && ( flag2 == 0))
93 {
94 digitalWrite(relay2, HIGH);
95 content = "relay2 is ON ";
96 flag2 = 1;
97
98 }
99
100 if
if((pinNumber == '6')&&(secondNumber == '6') && (flag2 == 1) )
101 {
102 digitalWrite(relay2, LOW);
103 content = "Relay2 is OFF ";
104 flag2 = 0;
105
106
107 }
108 // relay3
109 if
if((pinNumber == '4')&&(secondNumber == '4') && ( flag3 == 0))
110 {
111 digitalWrite(relay3, HIGH);
112 content = "relay3 is ON ";
113 flag3 = 1;
114
115 }
116
117 if
if((pinNumber == '7')&&(secondNumber == '7') && (flag3 == 1) )
118 {
119 digitalWrite(relay3, LOW);
120 content = "Relay3 is OFF ";
121 flag3 = 0;
122
123
124 }
125
126 // relay4
127 if
if((pinNumber == '9')&&(secondNumber == '9') && ( flag4 == 0))
128 {
129 digitalWrite(relay4, HIGH);
130 content = "relay4 is ON ";
131 flag4 = 1;
132
133 }
134
135 if
if((pinNumber == '6')&&(secondNumber == '4') && (flag4 == 1) )
136 {
137 digitalWrite(relay4, LOW);
138 content = "Relay4 is OFF ";
139 flag4 = 0;
140
141
142 }
143
144 // relay5
145 if
if((pinNumber == '3')&&(secondNumber == '3') && ( flag5 == 0))
146 {
147 digitalWrite(relay5, HIGH);
148 content = "relay5 is ON ";
149 flag5 = 1;
150
151 }
152
153 if
if((pinNumber == '9')&&(secondNumber == '5') && (flag5 == 1) )
154 {
155 digitalWrite(relay5, LOW);
156 content = "Relay5 is OFF ";
157 flag5 = 0;
158
159
160 }
161
162
163
164
165 // feedback
166 if
if((pinNumber == '5')&&(secondNumber == '5') )
167 {
168 if (digitalRead(relay1) )
169 content += "Relay1 = ON";
170 else
171 content += "Relay1 = OFF";
172
173
174 if (digitalRead(relay2) )
175 content += " Relay2 = ON";
176 else
177 content += " Relay2 = OFF";
178
179 if (digitalRead(relay3) )
180 content += " Relay3 = ON";
181 else
182 content += " Relay3 = OFF";
183
184 if (digitalRead(relay4) )
185 content += " Relay4 = ON";
186 else
187 content += " Relay4 = OFF";
188
189 if (digitalRead(relay5) )
190 content += " Relay5 = ON";
191 else
192 content += " Relay5 = OFF";
193
194
195 }
196 //******************************************
197
198
199 sendHTTPResponse(connectionId,content);
200 // make close command
201 String closeCommand = "AT+CIPCLOSE=";
202 closeCommand+=connectionId; // append connection id
203 closeCommand+="\r\n";
204 sendCommand(closeCommand,1000,DEBUG); // close connection
205
206 }
207 }
208
209 /*
210 * Name: sendData
211 * Description: Function used to send data to ESP8266.
212 * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes
213 * Returns: The response from the esp8266 (if there is a reponse)
214 */
215 String sendData(String
String command, const int timeout, boolean debug)
216 {
217 String response = "";
218 int dataSize = command.length();
219 char data[dataSize];
220 command.toCharArray(data,dataSize);
221 esp8266.write(data,dataSize); // send the read character to the esp8266
222 if
if(debug)
223 {
224 Serial.println("\r\n====== HTTP Response From Arduino ======");
225 Serial.write(data,dataSize);
226 Serial.println("\r\n========================================");
227 }
228 long int time = millis();
229 while
while( (time+timeout) > millis())
230 {
231 while
while(esp8266.available())
232 {
233 // The esp has data so display its output to the serial window
234 char c = esp8266.read(); // read the next character.
235 response+=c;
236 }
237 }
238 if
if(debug)
239 {
240 Serial.print(response);
241 }
242 return response;
243 }
244
245 /*
246 * Name: sendHTTPResponse
247 * Description: Function that sends HTTP 200, HTML UTF-8 response
248 */
249 void sendHTTPResponse(char
char connectionId, String content)
250 {
251 // build HTTP response
252 String httpResponse;
253 String httpHeader;
254 // HTTP Header
255 httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
256 httpHeader += "Content-Length: ";
257 httpHeader += content.length();
258 httpHeader += "\r\n";
259 httpHeader +="Connection: close\r\n\r\n";
260 httpResponse = httpHeader + content + " "; // There is a bug in this code: the last character of "content" is not sent, I cheated b
261 sendCIPData(connectionId,httpResponse);
262 }
263
264 /*
265 * Name: sendCIPDATA
266 * Description: sends a CIPSEND=<connectionId>,<data> command
267 *
268 */
269 void sendCIPData(char
char connectionId, String data)
270 {
271 String cipSend = "AT+CIPSEND=";
272 cipSend += connectionId;
273 cipSend += ",";
274 cipSend +=data.length();
275 cipSend +="\r\n";
276 sendCommand(cipSend,1000,DEBUG);
277 sendData(data,1000,DEBUG);
278 }
279
280 /*
281 * Name: sendCommand
282 * Description: Function used to send data to ESP8266.
283 * Params: command - the data/command to send; timeout - the time to wait for a response; debug - print to Serial window?(true = yes
284 * Returns: The response from the esp8266 (if there is a reponse)
285 */
286 String sendCommand(String
String command, const int timeout, boolean debug)
287 {
288 String response = "";
289 esp8266.print(command); // send the read character to the esp8266
290 long int time = millis();
291 while
while( (time+timeout) > millis())
292 {
293 while
while(esp8266.available())
294 {
295 // The esp has data so display its output to the serial window
296 char c = esp8266.read(); // read the next character.
297 response+=c;
298 }
299 }
300 if
if(debug)
301 {
302 Serial.print(response);
303 }
304 return response;
305 }

Vous aimerez peut-être aussi