-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespatserial2.ino
171 lines (138 loc) · 9.97 KB
/
espatserial2.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
#include <SoftwareSerial.h>
#define DEBUG true
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
SoftwareSerial esp8266(10,11);
char a;
int flag=0,temp;
float tempc;
void setup()
{
Serial.begin(115200);
esp8266.begin(115200);
sensors.begin();
pinMode (4,OUTPUT);
pinMode (5,OUTPUT);
pinMode (6,OUTPUT);
sendCommand("AT+RST\r\n",2000,DEBUG);
sendCommand("AT+CWMODE=1\r\n",1000,DEBUG);
sendCommand("AT+CWJAP=\"TP-LINK_40D216\",\"36780389\"\r\n",3000,DEBUG);
delay(1000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG);
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG);
sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG);
Serial.println("Server Ready");
}
void loop()
{
if(esp8266.available())
{
if(esp8266.find("+IPD,"))
{
delay(1000);
int connectionId = esp8266.read()-48;
esp8266.find("func=");
a = esp8266.read();
if(a == '0')
digitalWrite(4,LOW);
if(a == '1')
digitalWrite(5,LOW);
if(a == '2')
digitalWrite(6,LOW);
flag=1;
if(a == '3')
flag = 2;
if(a == '4')
{
analogRead(0);
}
flag = 3;
sendHTTPResponse(connectionId,flag);
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId;
closeCommand+="\r\n";
sendCommand(closeCommand,1000,DEBUG);
}
}
sensors.requestTemperatures();
tempc = sensors.getTempCByIndex(0);
temp = (int)tempc;
}
String sendData(String command,const int timeout, boolean debug)
{
String response = "";
int dataSize = command.length();
char data[dataSize];
command.toCharArray(data,dataSize);
esp8266.write(data,dataSize);
if(debug)
{
Serial.println("Response from arduino>>>>");
Serial.write(data,dataSize);
}
long int time = millis();
while((time+timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read();
response+=c;
}
}
if(debug)
{
Serial.print(response);
}
return response;
}
void sendHTTPResponse(int connectionId, int res)
{
String httpResponse;
String httpHeader;
String content;
if(res == 1)
{
content = "OK";
}
if(res == 2)
{
content = String(temp);
}
httpHeader = "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\n";
httpHeader += "Content-Length";
httpHeader += content.length();
httpHeader += "\r\n";
httpHeader = " Connection: close\r\n\r\n";
httpResponse = httpHeader + content + " " ;
sendCIPData(connectionId,httpResponse);
}
void sendCIPData(int connectionId,String data)
{
String cipSend = "AT+CIPSEND=";
cipSend += connectionId;
cipSend += ",";
cipSend += data.length();
cipSend += "\r\n";
sendCommand(cipSend,1000,DEBUG);
sendData(data,1000,DEBUG);
}
String sendCommand(String command, const int timeout, boolean debug)
{
String response = "";
esp8266.print(command);
long int time= millis();
while((time +timeout) > millis())
{
while(esp8266.available())
{
char c = esp8266.read();
response += c;
}
}
if(debug)
Serial.print(response);
return response;
}