-
Notifications
You must be signed in to change notification settings - Fork 0
/
txt_messaging.c
91 lines (66 loc) · 1.69 KB
/
txt_messaging.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
#include <SPI.h>
#include <Ethernet.h>
// Push button pins
int emailPin = 2;
int textPin = 3;
//Ethernet Sheild Configuration
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xA8, 0xDF };
byte myIP[] = { 192, 168, 1, 8 };
byte gateway[] = { 192, 168, 1, 1 };
// Server name
char server[] = "http://www.niskyponics.appspot.com";
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, myIP, gateway);
delay(1000);
pinMode(emailPin, INPUT);
pinMode(textPin, INPUT);
Serial.println("Setup Complete");
}
void loop() {
int email = digitalRead(emailPin);
if (email == HIGH) {
sendAlert("email");
}
delay(200);
}
void sendAlert(String type) {
Serial.println("Sending" + type = "alert.");
String response = GAE("hhtp://niskyponics.appspot.com/alerts?Type=" + type);
Serial.println(response);
Serial.println("Ready");
}
String GAE(String link) {
httpRequest(link);
delay(10000);
String readString = ""; //Reset string
while (client.available() > 0) {
char s = client.read();
//Serial.print(s); //Complete response w/ headers. For dev mode.
if (s== '\n') {
char c = client.read();
//Serial.print(c); //Parsed response. For dev mode.
if (c == '\r') {
while (client.connected()) {
char z = client.read();
readString += z;
}
}
}
}
client.stop();
return(readString);
}
// This method makes an HTTP connection to the server
void httpRequest(String link) {
//if there is a successful connection
if (client.connect(server, 80)) {
client.println("Get " + link + " HTTP/1.0 ");
client.println();
} else {
// You couldn't make the connection
Serial.println("Connection Failed");
Serial.println("Disconnecting.");
}
client.stop(); }