-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiWebClient.ino
256 lines (201 loc) · 5.2 KB
/
WiFiWebClient.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#ifndef __CC3200R1M1RGC__
// Do not include SPI for CC3200 LaunchPad
#include <SPI.h>
#endif
#include <WiFi.h>
#define PotPin 6
#define HalogenPin 38
// your network name also called SSID
char ssid[] = "AIRTIES-GUEST";
// your network password
char password[] = "wirelesslife";
int potValue;
int potValuePrev;
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(50,62,217,1); // numeric IP for Google (no DNS)
char server[] = "cis450.com"; // name address for Google (using DNS)
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
WiFiClient client;
int moduleToNum(char *module) {
if (strcmp(module,"FxHit4thSA")==0) {
return 1;
} else if (strcmp(module,"6LkEwn2IXM")==0) {
return 4;
} else if (strcmp(module,"ZRx0iKWXuM")==0) {
return 5;
} else if (strcmp(module,"0JEBuCn7WL")==0) {
return 7;
}
}
int numToPin(int num) {
Serial.println("!!!!**");
Serial.println(num);
Serial.println("!!!!**");
if (num==1) {
return 38;
} else if (num==3) {
return 6;
} else if (num==4) {
return 12;
} else if (num==7) {
return 10;
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void connection(char *r) {
if (client.connect(server, 80)) {
// Make a HTTP request:
char req[80];
bzero(req, 80);
strcat(req,"GET /");
strcat(req,r);
strcat(req," HTTP/1.1");
client.println(req);
Serial.println(req);
client.println("Host: cis450.com");
client.println("Connection: close");
client.println();
}
}
void setup() {
Serial.println("starting");
//Initialize serial and wait for port to open:
Serial.begin(115200);
pinMode(19, OUTPUT);
pinMode(12, OUTPUT); //fan
pinMode(10, OUTPUT); //led
pinMode(38, OUTPUT); //lamp
digitalWrite(12,LOW);
digitalWrite(10,LOW);
analogWrite(38,300);
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to Network named: ");
// print the network name (SSID);
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
}
void HalogenLamp() {
potValue = analogRead(PotPin) * (70.0 /1023) ;
if (abs(potValue - potValuePrev) > 10) {
analogWrite(HalogenPin, potValue);
}
}
void event(char *command, char *module, char *url, char *isAnalog) {
int pin = numToPin(moduleToNum(module));
int num;
if (strcmp(command,"on")==0) {
Serial.println("ON!!!!");
num = 1;
} else if (strcmp(command,"off")==0) {
Serial.println("OFF!!!!");
num = 0;
}
if (isAnalog[0]=='t') {
if (num==1) {
applyAnalog(pin, 0);
} else {
applyAnalog(pin, 300);
}
} else {
applyDigital(pin, num);
}
connection(url);
}
void applyDigital(int pin, int num) {
if (num==1) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
}
void applyAnalog(int pin, int num) {
potValuePrev = potValue;
Serial.println(num);
analogWrite(pin,num);
}
void echo() {
// if there are incoming bytes available
// from the server, read them and print them:
char string[500];
char *p;
int i = 0;
int j = 0;
char *url;
char *command;
char *module;
char *isAnalog;
int isparsed = 0;
bzero(string, 500);
while (client.available()) {
string[i] = client.read();
i++;
}
p = strtok(string, "**");
while(p != NULL) {
if (j==1) {
url = (char*)malloc(sizeof(char)*strlen(p)+1);
strcpy(url,p);
} else if (j==2) {
command = (char*)malloc(sizeof(char)*strlen(p)+1);
strcpy(command,p);
} else if (j==3) {
module = (char*)malloc(sizeof(char)*strlen(p)+1);
strcpy(module,p);
} else if (j==4) {
isAnalog = (char*)malloc(sizeof(char)*strlen(p)+1);
strcpy(isAnalog,p);
isparsed = 1;
}
p = strtok(NULL, "**");
j++;
}
if (isparsed) {
event(command, module, url, isAnalog);
}
if (!client.connected()) {
client.stop();
}
free(url);
free(command);
free(module);
free(isAnalog);
isparsed = 0;
}
void loop() {
connection("requests.php");
echo();
HalogenLamp();
}