-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpachube_functions.pde
More file actions
164 lines (138 loc) · 4 KB
/
pachube_functions.pde
File metadata and controls
164 lines (138 loc) · 4 KB
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
// Functions to send data to Pachube
char pachube_data[16];
boolean found_status_200 = false;
boolean found_session_id = false;
char *found;
unsigned int successes = 0;
unsigned int failures = 0;
boolean ready_to_update = true;
boolean reading_pachube = false;
boolean request_pause = false;
boolean found_content = false;
unsigned long last_connect;
int content_length;
void pachube_in_out(){
// read data from CC128
if (currentcost.available()) {
cc_read();
}
if (millis() < last_connect) last_connect = millis();
if (request_pause){
if ((millis() - last_connect) > interval){
ready_to_update = true;
reading_pachube = false;
request_pause = false;
found_status_200 = false;
found_session_id = false;
Serial.flush();
DEBUG_PRINTLN("Ready to connect: ");
DEBUG_PRINTLN(millis());
Serial.flush();
}
}
if (ready_to_update){
Serial.flush();
DEBUG_PRINTLN("Connecting ...");
Serial.flush();
if (localClient.connect()) {
// here we assign comma-separated values to 'data', which will update Pachube datastreams
sprintf(pachube_data, " %lu, %lu.%lu, %s", PwrNum, TmprNum, TmprDecimal, TimeChr);
Serial.println("~~~~~~~~~~~ pachube_data ~~~~~~~~~~~~~~~");
Serial.println(pachube_data);
Serial.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
content_length = strlen(pachube_data);
Serial.flush();
DEBUG_PRINT("\nPUT, to update feed: ");
DEBUG_PRINT(PwrNum);
DEBUG_PRINT(",");
DEBUG_PRINT(int(TmprDouble));
DEBUG_PRINT(",");
DEBUG_PRINT(int(TimeChr));
DEBUG_PRINT("-");
DEBUG_PRINTLN(pachube_data);
Serial.flush();
localClient.print("PUT /api/");
localClient.print(SHARE_FEED_ID);
localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
localClient.print(PACHUBE_API_KEY);
localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
localClient.print("\nContent-Type: text/csv\nContent-Length: ");
localClient.print(content_length);
localClient.print("\nConnection: close\n\n");
localClient.print(pachube_data);
localClient.print("\n");
ready_to_update = false;
reading_pachube = true;
request_pause = false;
interval = UPDATE_INTERVAL;
Serial.flush();
DEBUG_PRINTLN("finished PUT");
Serial.flush();
}
else {
Serial.print("connection failed! ");
Serial.println(++failures);
found_status_200 = false;
found_session_id = false;
ready_to_update = false;
reading_pachube = false;
request_pause = true;
last_connect = millis();
interval = RESET_INTERVAL;
setupEthernet();
}
}
while (reading_pachube){
while (localClient.available()) {
checkForResponse();
}
if (!localClient.connected()) {
disconnect_pachube();
}
}
}
// disconnect after having sent data successfully
void disconnect_pachube(){
Serial.println("Disconnecting.\n=====\n");
localClient.stop();
ready_to_update = false;
reading_pachube = false;
request_pause = true;
last_connect = millis();
found_content = false;
resetEthernetShield();
}
void checkForResponse(){
char c = localClient.read();
//Serial.print(c);
buff[pointer] = c;
if (pointer < 64) pointer++;
if (c == '\n') {
// connection succeeded!
found = strstr(buff, "200 OK");
if (found != 0){
found_status_200 = true;
Serial.flush();
Serial.println(" ");
Serial.println("Status 200");
Serial.print(PwrNum);
Serial.print(",");
Serial.print(int(TmprDouble));
Serial.print(",");
Serial.print(TimeChr);
Serial.print("-");
Serial.println(pachube_data);
Serial.flush();
}
buff[pointer]=0;
found_content = true;
clean_buffer();
}
if (found_status_200){
found = strstr(buff, "_session=");
if (found != 0){
clean_buffer();
found_session_id = true;
}
}
}