-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhub-xbee-code.ino
More file actions
68 lines (60 loc) · 2.05 KB
/
hub-xbee-code.ino
File metadata and controls
68 lines (60 loc) · 2.05 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
#include <SoftwareSerial.h> // Software based UART port to use Zigbee module
int a = 1;
float hum = 0, temp = 0; // Float Variable to store Temperature and Humidity
SoftwareSerial xbee(3, 2); // RX, TX
void setup() //One time preloading function
{
Serial.begin(9600); // Hardware Serial initialization to be connected to a bluetooth module or PC
xbee.begin(2400); // Software Serial initialization at 2400 Baud rate to communicate with zigbee
}
void loop() // Continous loop
{
xbee.print(a); // Sends (a) with "!" to Xbee -> "1!" Requests temperature data and vice versa
xbee.println("!");
while(xbee.available() > 0) //Checks is any data has been recieved from zigbee.
{
char aChar = xbee.read(); //reading the value from the Xbee serial port
if(aChar == 33) //If the first character is 33 ie) ! in ASCII
{
xbee.flush(); // Clear the buffer and
aChar = NULL;
}
if(aChar >= 100) // If it is more than 100 or random ASCII character flush the data
{
xbee.flush();
aChar = NULL;
}
Serial.print(aChar); //Printing the Read value
}
if(a == 3) // if a = 3 create new line or end of one set of data transmission
{
Serial.println(); //New line print
}
else
{
Serial.print(","); // if a not 3 then add ","
}
if(a> 3) // after a > 3 print the AES encryted data to xbee
{
a =1; // initialize a = 1 back
xbee.print("!f+F8YW+9W3+Cg0S1NVBexycQxz32biWTmzVsxO48+fk=!");
}
delay(100); // Wait for few ms for this to happen
xbee.flush(); // flush any data in Xbee serial port
a=a+1; //Increment data
if(Serial.available()); // Check if any data is sent from Hardware serial port
{
int r = Serial.parseInt(); // Recieving any integer data
if(r== 1) // if recieved data is 1. Send 4! which turns the LED on the Node.
{
xbee.print(4);
xbee.print("!");
delay(100);
}
if(r== 2)// if recieved data is 2. Send 5! which turns the LED off the Node.
{
xbee.print(5);
xbee.print("!");
}
}
}