-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compiled_working_test.ino
364 lines (322 loc) · 9.63 KB
/
Compiled_working_test.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include <XBee.h>
#define ARRAYSIZE 1024
int encrypt(String text, int keyln, char *key, int *ciphercode) ;
char *key = "98765abcd";
int keyln = 9;
int reqid = 2682;
char *clientid = "test01";
String timestamp = "";
String remarks1 = "";
int onModulePin = 2;
String str = "";
String Split_str[7];
int data_analog[64];
int addrss[64];
int No=6;
int encrypt(int textln, char *text, int keyln, char *key, int *ciphercode);
void acquire_data();
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
ZBRxResponse rx = ZBRxResponse();
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();
char ch;
void setup()
{
pinMode(onModulePin, OUTPUT);
Serial.begin(9600);
Serial1.begin(115200);
Serial2.begin(9600);
Serial3.begin(9600);
xbee.setSerial(Serial3);
//initialisegprs();
}
void loop()
{
int textln;
int i;
int x = 0;
int answer = 0;
String output;
int cipher[ARRAYSIZE];
for (int p = 1; p <= 64; p++)
{
data_analog[p] = 0;
if (p <= 8)
Split_str[p] = "";
}
//Time data : Start
String str = "";
while (Serial2.available())
Serial.write("Cleaning input buffer");
Serial2.write(No);
Serial.println(No);
delay(250);
if (Serial2.available())
{
while (Serial2.available())
{
ch = Serial2.read();
if (ch == '\n');
else
str = str + ch;
}
}
//Time data : End
remarks1="";
remarks1 = remarks1 + reqid;
remarks1 = remarks1 + ",";
remarks1 = remarks1 + 7;
acquire_data();//Get xbee data into dataanalog[]
String pqr="";
//Splits XBee data into 8 parts and stores in 8 strings :Start
for (int p = 1; p <= 64; p++)
{
pqr=pqr+data_analog[p]+",";
if (p <= 8)
Split_str[1] = Split_str[1] + data_analog[p] + ",";
else if (p <= 16)
Split_str[2] = Split_str[2] + data_analog[p] + ",";
else if (p <= 24)
Split_str[3] = Split_str[3] + data_analog[p] + ",";
else if (p <= 32)
Split_str[4] = Split_str[4] + data_analog[p] + ",";
else if (p <= 40)
Split_str[5] = Split_str[5] + data_analog[p] + ",";
else if (p <= 48)
Split_str[6] = Split_str[6] + data_analog[p] + ",";
else if (p <= 56)
Split_str[7] = Split_str[7] + data_analog[p] + ",";
else if (p <= 64)
Split_str[8] = Split_str[8] + data_analog[p] + ",";
}
Serial.println("________________________________________________________________________________________________________________________________");
Serial.println(Split_str[7]);
//Splits XBee data into 8 parts and stores in 8 strings :End
Serial.println(Split_str[7]);
String text = "#test01#" + str;
text = reqid + text;
text = text + "#";
//Serial.println(Split_str[7]);
text = text + Split_str[7];
text = text + "#";
text = text + remarks1;
reqid++;
textln = text.length();
Serial.println(str);
Serial.println(remarks1);
Serial.println(text);
Serial.println("________________________________________________________________________________________________________________________________");
/*
encrypt(text, keyln, key, cipher);
output = "data=";
for (i = 0; i < textln; i ++)// Print the entire encrypted data into a string separated by space
{
output = output + cipher[i] + ",";
}
output = "http://sensors.bennyraphael.in/cgi-bin/update.cgi?" + output; //Adding the URL to string
output = "AT+HTTPPARA=\"URL\",\"" + output;
output = output + "\"";
answer = sendATcommand2("AT+HTTPINIT", "OK", "ERROR", 1000);
Serial.print("\n Answer ="); Serial.print(answer); Serial.println("\n");
if (answer == 1)
{
answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
Serial.print("\n Answer ="); Serial.print(answer); Serial.println("\n");
if (answer == 1)
{
while ( Serial1.available() > 0) Serial.println(Serial1.read());
Serial.println(output);
Serial1.println(output); Serial.println("\n");
char response[100];
unsigned long previous;
memset(response, '\0', 100); // Initialize the string
delay(100);
do {
if (Serial1.available() != 0)
{
response[x] = Serial1.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(response, "OK") != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while ((answer == 0) && ((millis() - previous) < 5000));
Serial.println(response);
Serial.print("\n Answer ="); Serial.print(answer); Serial.println("\n");
delay(5000);
memset(response, '\0', 100);
answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 5000);
Serial.print("\n Answer ="); Serial.print(answer);
answer = 0;
while (answer == 0)
{
answer = sendATcommand("AT+HTTPTERM", "OK", 5000);
Serial.print("\n Answer ="); Serial.print(answer);
Serial.println("\n");
}
}
}*/
text = "";
remarks1 = "";
}
int encrypt(String text, int keyln, char *key, int *ciphercode)
{
int textln;
int i ;
int ind1 ;
int prev ;
int val2 ;
int ch1 ;
int ch2 ;
textln = text.length();
prev = 0;
for (i = 0; i < textln; i++) {
ch1 = text.charAt(i);
ind1 = i % keyln;
ch2 = key[ind1];
val2 = prev % 16;
ciphercode[i] = i + ch1 + ch2 + val2;
prev = (prev + ch1) % 1024;
}
return 1;
}
void initialisegprs()
{
Serial.println("Initialising the GSM Module");
power_on();
//delay(3000);
while (sendATcommand2("AT+CREG?", "+CREG: 1,1", "+CREG: 1,5", 2000) == 0 );
//delay(2000);
sendATcommand("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"", "OK", 2000);
sendATcommand("AT+SAPBR=3,1,\"APN\",\"bsnlnet\"", "OK", 2000);
while ( sendATcommand("AT+SAPBR=1,1", "OK", 3000) == 0);
sendATcommand("AT+SAPBR=2,1", "OK", 2000);
}
void power_on()
{
uint8_t answer = 0;
// checks if the module is started
answer = sendATcommand("AT", "OK", 2000);
Serial.println("AT");
if (answer == 0)
{
// power on pulse
digitalWrite(onModulePin, HIGH);
delay(3000);
digitalWrite(onModulePin, LOW);
// waits for an answer from the module
while (answer == 0) {
// Send AT every two seconds and wait for the answer
answer = sendATcommand("AT", "OK", 2000);
Serial.println("AT");
Serial.println(answer);
}
}
}
int8_t sendATcommand(char * ATcommand, char * expected_answer1, unsigned int timeout)
{
uint8_t x = 0, answer = 0;
char responsea[100];
unsigned long previous;
memset(responsea, '\0', 100); // Initialize the string
delay(100);
while ( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do {
if (Serial1.available() != 0)
{
responsea[x] = Serial1.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(responsea, expected_answer1) != NULL)
{
answer = 1;
}
}
// Waits for the asnwer with time out
}
while ((answer == 0) && ((millis() - previous) < timeout));
Serial.println(responsea);
//Serial.println("Flow of control :Left sendATcommand");
return answer;
}
int8_t sendATcommand2(char * ATcommand, char * expected_answer1, char * expected_answer2, unsigned int timeout)
{
//Serial.println("Flow of control :Entered sendATcommand2");
uint8_t x = 0, answer = 0;
char responseb[100];
unsigned long previous;
memset(responseb, '\0', 100); // Initialize the string
delay(100);
while ( Serial1.available() > 0) Serial1.read(); // Clean the input buffer
Serial1.println(ATcommand); // Send the AT command
x = 0;
previous = millis();
// this loop waits for the answer
do {
if (Serial1.available() != 0) {
responseb[x] = Serial1.read();
x++;
// check if the desired answer is in the response of the module
if (strstr(responseb, expected_answer1) != NULL)
{
answer = 1;
}
if (strstr(responseb, expected_answer2) != NULL)
{
answer = 2;
}
}
// Waits for the asnwer with time out
}
while ((answer == 0) && ((millis() - previous) < timeout));
Serial.println(responseb);
// Serial.println("Flow of control :Left sendATcommand 2");
return answer;
}
//Handles XBEE communication
void acquire_data()
{
Serial.println("Flow of control :Entered Acquiredata");
int xtimeout = 5000;
int start = millis();
do
{
// delay(5000);
xbee.readPacket();
if (xbee.getResponse().isAvailable()) //Check whether there is an xbee response
{
if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) //Check whether response is an IO sample
{
xbee.getResponse().getZBRxIoSampleResponse(ioSample);
uint16_t deviceID = ioSample.getRemoteAddress16();//Getting the 16 bit address of the transmitting module
int flag = 0;
for (int p = 1; p <= 64; p++)
{
//if(deviceID == addrss[p]) //Mapping the address to the correct module
//{
flag = 1;
if (ioSample.containsAnalog())
data_analog[p] = ioSample.getAnalog(1);
else
Serial.println("Error: No analog data");
//}
}
if (flag == 0)
Serial.println("Error: Device not recognised/found");
}
else
Serial.println("Error: Not an IO response");
}
//else
//Serial.println("No XBee response");
} while ((millis() - start) <= xtimeout);
Serial.println("Flow of control :Left Acquiredata");
}