-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi.c
135 lines (118 loc) · 2 KB
/
wifi.c
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
/*
void setupWiFi()
{
restartWiFi();
boolean connected = false;
while (!connected)
{
if (connectWiFi())
{
connected = true;
break;
}
}
delay(2000);
printer("AT+CIPMUX=0");
}
void restartWiFi()
{
while (!Serial)
;
while (!esp8266)
;
while (esp8266.available() > 0)
{
esp8266.read();
}
esp8266.println("AT");
boolean reset = true;
while (reset)
{
esp8266.flush();
esp8266.println("AT+RST");
if (esp8266.find("Ready") || esp8266.find("ready"))
{
Serial.println("Module is ready");
reset = false;
}
else
{
Serial.println(esp8266.read());
Serial.println("Module have no response.");
}
}
delay(1000);
}
void updateRemoteOptoState(boolean state)
{
String valueToSend = state ? "true " : "false";
long length = state ? 65 : 66;
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
esp8266.println(cmd);
Serial.println(cmd);
if (esp8266.find("Error")) return;
cmd = apiRequest(valueToSend);
String cip_cmp = "AT+CIPSEND=";
cip_cmp += length;
printer(cip_cmp);
while (!esp8266.find(">"))
{
Serial.println("No response...");
reset();
}
printer(cmd);
printer("host:domain.com");
printer("");
printer("");
delay(1500);
while (esp8266.available())
{
char c = esp8266.read();
Serial.write(c);
if (c == '\r') Serial.print('\n');
}
Serial.println("====");
}
String apiRequest(String valueToSend)
{
String cmd = "GET /toxic.php?room=";
cmd += roomNumber;
cmd += "&status=";
cmd += valueToSend;
cmd += " HTTP/1.0";
return cmd;
}
boolean connectWiFi()
{
esp8266.println("AT+CWMODE=1");
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASSWORD;
cmd += "\"";
printer(cmd);
delay(3000);
if (esp8266.find("OK"))
{
Serial.println("OK, Connected to WiFi.");
return true;
}
else
{
Serial.println("Can not connect to the WiFi.");
return false;
}
}
void reset()
{
Serial.println("Reset");
digitalWrite(resetPin, LOW);
}
void printer(String cmd)
{
Serial.println(cmd);
esp8266.println(cmd);
}
*/