1#include <SoftwareSerial.h>
2
3#define DEBUG true
4
5SoftwareSerial esp8266(2,3);
6
7boolean alreadyConnected = false;
8
9
10
11void setup()
12{
13
14 Serial.begin(9600);
15 esp8266.begin(9600);
16
17 pinMode(6,OUTPUT);
18 digitalWrite(6,LOW);
19
20 pinMode(7,OUTPUT);
21 digitalWrite(7,LOW);
22
23 sendData("AT+RST\
\
24",2000,DEBUG);
25 sendData("AT+CWMODE=2\
\
26",1000,DEBUG);
27 sendData("AT+CIFSR\
\
28",1000,DEBUG);
29 sendData("AT+CIPMUX=1\
\
30",1000,DEBUG);
31 sendData("AT+CIPSERVER=1,80\
\
32",1000,DEBUG);
33
34}
35
36void loop()
37{
38
39 String inString = "";
40 if(esp8266.available())
41 {
42 if (!alreadyConnected) {
43
44 esp8266.flush();
45 Serial.println("We have a new client");
46 esp8266.println("Hello, client!");
47 alreadyConnected = true;
48 }
49
50 if(esp8266.find("+IPD,"))
51 {
52 delay(10);
53
54 int pinNumber = 0;
55 String tempS = "";
56 esp8266.find("pin=");
57 char temp = esp8266.read();
58 tempS = temp;
59
60 pinNumber = tempS.toInt();
61 Serial.println(pinNumber);
62 digitalWrite(pinNumber, !digitalRead(pinNumber));
63
64 }
65 }
66
67}
68
69
70
76String sendData(String command, const int timeout, boolean debug)
77{
78 String response = "";
79
80 esp8266.print(command);
81
82 long int time = millis();
83
84 while( (time+timeout) > millis())
85 {
86 while(esp8266.available())
87 {
88
89
90 char c = esp8266.read();
91 response+=c;
92 }
93 }
94
95 if(debug)
96 {
97 Serial.print(response);
98 }
99
100 return response;
101 }