-
Notifications
You must be signed in to change notification settings - Fork 0
/
Senior_Design_Team_2.ino
177 lines (158 loc) · 5.3 KB
/
Senior_Design_Team_2.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
/*
This code was modifty for the second senior design group by Tyler Adam Martinez
*/
#include <SPI.h>
#include <RH_RF95.h> // <-- Get the header file https://github.com/dragino/RadioHead/blob/master/RH_RF95.h
#include <Console.h>
#include <Process.h>
RH_RF95 rf95;
//If you use Dragino IoT Mesh Firmware, uncomment below lines.
//For product: LG01.
#define BAUDRATE 115200
String myWriteAPIString = "P07KVY59P5QEY6M6"; // <-- Remember to change to your person one.
uint16_t crcdata = 0;
uint16_t recCRCData = 0;
float frequency = 868.0;
String dataString = "";
void uploadData(); // Upload Data to ThingSpeak.
void setup()
{
Bridge.begin(BAUDRATE);
Console.begin();
// while(!Console);
if (!rf95.init())
Console.println("init failed");
;
// Setup ISM frequency
rf95.setFrequency(frequency);
// Setup Power,dBm
rf95.setTxPower(13);
// rf95.setSyncWord(0x34);
Console.println("LoRa Gateway Example --");
Console.println(" Upload Single Data to ThinkSpeak");
}
uint16_t calcByte(uint16_t crc, uint8_t b)
{
uint32_t i;
crc = crc ^ (uint32_t)b << 8;
for ( i = 0; i < 8; i++)
{
if ((crc & 0x8000) == 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
return crc & 0xffff;
}
uint16_t CRC16(uint8_t *pBuffer, uint32_t length)
{
uint16_t wCRC16 = 0;
uint32_t i;
if (( pBuffer == 0 ) || ( length == 0 ))
{
return 0;
}
for ( i = 0; i < length; i++)
{
wCRC16 = calcByte(wCRC16, pBuffer[i]);
}
return wCRC16;
}
uint16_t recdata( unsigned char* recbuf, int Length)
{
crcdata = CRC16(recbuf, Length - 2); //Get CRC code
recCRCData = recbuf[Length - 1]; //Calculate CRC Data
recCRCData = recCRCData << 8; //
recCRCData |= recbuf[Length - 2];
}
void loop()
{
if (rf95.waitAvailableTimeout(2000))// Listen Data from LoRa Node
{
uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];//receive data buffer
uint8_t len = sizeof(buf);//data buffer length
if (rf95.recv(buf, &len))//Check if there is incoming data
{
recdata( buf, len);
Console.print("Get LoRa Packet: ");
for (int i = 0; i < len; i++)
{
Console.print(buf[i],HEX);
Console.print(" ");
}
Console.println();
if(crcdata == recCRCData) //Check if CRC is correct
{
if(buf[0] == 1 && buf[1] == 1 && buf[2] ==1) //Check if the ID match the LoRa Node ID
{
uint8_t data[] = " Server ACK";//Reply
data[0] = buf[0];
data[1] = buf[1];
data[2] = buf[2];
rf95.send(data, sizeof(data));// Send Reply to LoRa Node
rf95.waitPacketSent();
int newData[4] = {0, 0, 0, 0}; //Store Sensor Data here
for (int i = 0; i < 4; i++)
{
newData[i] = buf[i + 3];
}
int hh = newData[0];
int hl = newData[1];
int th = newData[2];
int tl = newData[3];
Console.print("Get Temperature:");
Console.print(th);
Console.print(".");
Console.println(tl);
Console.print("Get Humidity:");
Console.print(hh);
Console.print(".");
Console.println(hl);
dataString ="field1=";
dataString += th;
dataString +=".";
dataString += tl;
dataString +="&field2=";
dataString += hh;
dataString +=".";
dataString += hl;
uploadData(); //
dataString="";
}
}
else
Console.println(" CRC Fail");
}
else
{
//Console.println("recv failed");
;
}
}
}
void uploadData() {//Upload Data to ThingSpeak
// form the string for the API header parameter:
// form the string for the URL parameter, be careful about the required "
String upload_url = "https://api.thingspeak.com/update?api_key=";
upload_url += myWriteAPIString;
upload_url += "&";
upload_url += dataString;
Console.println("Call Linux Command to Send Data");
Process p; // Create a process and call it "p", this process will execute a Linux curl command
p.begin("curl");
p.addParameter("-k");
p.addParameter(upload_url);
p.run(); // Run the process and wait for its termination
Console.print("Feedback from Linux: ");
// If there's output from Linux,
// send it out the Console:
while (p.available()>0)
{
char c = p.read();
Console.write(c);
}
Console.println("");
Console.println("Call Finished");
Console.println("####################################");
Console.println("");
}