-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.cpp
More file actions
295 lines (235 loc) · 9.07 KB
/
Main.cpp
File metadata and controls
295 lines (235 loc) · 9.07 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
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
/*
Raspberry Pi Sensor Framework
Main.cpp
Example program
This tests each type of node and their respective functions.
Uses SPI for communication and an MCP3008 ADC for reading data.
The developer can take ideas from this file, or modify it to suit his/her needs.
See README.md for further details about this program.
Author: Dayalan Nair
Date: January 2021
*/
#include "Main.hpp"
#include <pigpio.h>
//---------------- ADC setup -----------------------------
#include <wiringPi.h>
#include <mcp3004.h>
#include <stdio.h>
#include <stdlib.h>
#define BASE 100
#define SPI_CHAN 1
//--------------------------------------------------------
//Global variables to facilitate example on a single device
Control control(1);
Bridge bridge(2,1,0);
Sensor sensor(3, 'T', 10); //T = temperature
int bridge_c_handler(int nid, BYTE *ctr, unsigned int sz)//may not need sz here since pckt is fixed size
{
cout<<"Current node ID: "<<nid<<endl;
cout<<"control packet: "<<ctr[0]<<" "<<ctr[1]<<" "<<ctr[2]<<" "<<ctr[3]<<endl;
BYTE type = ctr[0];
int sensorID = ctr[1] - '0';//conv to int;
int bridgeID = ctr[2] - '0';
cout<<"Node ID from packet: "<<bridgeID<<endl;
//check if node ID of command matches the ID provided. this ID will be
if (bridgeID == nid){
cout<<"Node ID from received packet matches the bridge ID"<<endl;
if (type == 'c'){
cout<<"Forwarding to sensor node ID: "<<nid<<endl;
BYTE to_send[3];
//-----------------control packet--------------------
to_send[0] = 'c';
to_send[1] = sensorID + '0';
to_send[2] = ctr[3];
//port 1 is output to sensor/daisy chain
bridge.send_c(1,(BYTE*)to_send, 3);
return 2;
}
}
else{
cout<<"Forwarding to next bridge..."<<endl;
}
return 1;
return 0;
}
int sensor_c_handler(int nid, BYTE *ctr, unsigned int sz){
cout<<"control packet: "<<ctr[0]<<" "<<ctr[1]<<" "<<ctr[2]<<" "<<ctr[3]<<endl;
cout<<"Current node ID: "<<nid<<endl;
BYTE type = ctr[0];
int nodeID = ctr[1] - '0';//conv to int;
if (type == 'c'){
if (nodeID == nid){
//set the active command which will be executed by sensor
cout<<"Node ID from received packet matches the ID provided. Executing..."<<endl;
cout<<"Command: "<<ctr[2]<<endl;
sensor.executeCommand(ctr[2]);
//execute command
return 3;
}
}
return 0;
}
/*
=====================================================================================
Handler for a bridge receiving data from a sensor
=====================================================================================
*/
int bridge_d_handler(BYTE *ctr, unsigned int sz)
{
//cout<<"data packet: "<<ctr[0]<<" "<<ctr[1]<<" "<<ctr[2]<<endl;
BYTE type = ctr[0];
int size = ctr[2]-'0';
cout<<"Sensor ID of node that recorded data: "<<ctr[1]<<endl;
cout<<"size of data (from packet): "<<size<<endl<<"Received data: ";
for (int i = 0; i < size;i++){
cout<<ctr[i+3];
}
cout<<endl;
if (type == 'd'){
cout<<"Forwarding data to control node "<<endl;
//port 1 is output to sensor/daisy chain
bridge.send_c(0,ctr, sz);
return 2;
}
else{
cout<<"Data handler called on non-data packet."<<endl;
return 1;
}
return 0;
}
/*
=====================================================================================
Handler for a control node receiving data from a bridge
=====================================================================================
*/
int control_d_handler(BYTE *data, unsigned int sz)
{
//cout<<"data packet: "<<ctr[0]<<" "<<ctr[1]<<" "<<ctr[2]<<endl;
BYTE type = data[0];
int sensorID = data[1] - '0';
int size = data[2]-'0';
BYTE extracted_data[sz-3];
cout<<"Sensor ID of node that recorded data: "<<sensorID<<endl;
if (type == 'd'){
cout<<"size of data (from packet): "<<size<<endl<<"Received data: ";
for (int i = 0; i < size;i++){
cout<<data[i+3];
extracted_data[i] = data[i+3];
}
cout<<endl;
time_t now = time(0);
char* dt = ctime(&now);
cout<<"Storing... "<<endl;
control.saveData(sensorID,dt,extracted_data,sz-3);
return 2;
}
else{
cout<<"Data handler called on non-data packet."<<endl;
return 1;
}
return 0;
}
int main(){
char userInput;
control.addBridge(2,0);
bridge.addSensor(3,1);
cout<<"================= Control Node command send/recv test ======================="<<endl;
control.displayBridges();
//turn on sensors with bridge ID 456
control.command('1', 2, 3);
//char* rx = control.getRxBuffer();
control.recv_c_handler(0, bridge_c_handler);
//int* bridgeSIDs = bridge.getSensorIDs(); used if not using daisy chain
control.recv_c(control.getID());
//control.closeGPIO();
cout<<"================= Bridge Node command send/recv test ======================="<<endl;
//simulate incoming command
BYTE to_send[4];
//-----------------control packet--------------------
to_send[0] = 'c';
to_send[1] = sensor.getID() + '0'; //send to only sensor created
to_send[2] = bridge.getID() + '0'; //send to current bridge
to_send[3] = '1';//cmd = 1
bridge.send_c(0, (BYTE*)to_send, 4);
bridge.recv_c_handler(0, bridge_c_handler);
bridge.recv_c(bridge.getID());
cout<<endl<<"================= Sensor Node command send/recv test ======================="<<endl<<endl;
//thread for receiving data?
//simulate incoming command
//-----------------control packet--------------------
to_send[0] = 'c';
to_send[1] = sensor.getID() + '0';
//cout<<"sensor id: "<<sensorID<<endl;
to_send[2] = '1';//cmd = 1
to_send[3] = '.';
//---------------------------------------------------
//BYTE *ptr = to_send;
cout<< "Sent by bridge to sensor: "<<(BYTE*)to_send<<endl;
sensor.send_c(0, (BYTE*)to_send, 3);
sensor.recv_c_handler(0, sensor_c_handler);
sensor.recv_c(sensor.getID());//bridge sids for now
//sensor.setActiveCommand(command);
//cout<<"Active command: " << sensor.getActiveCommand()<<endl;
cout<<"---------------------Next command---------------------------"<<endl;
to_send[2] = '3';//cmd = 1
sensor.send_c(0, (BYTE*)to_send, 3); //send cmd to sensor using loop back
sensor.recv_c(sensor.getID());
cout<<"================= Bridge Node data send/recv test ======================="<<endl;
int data_size = 4; //4 bytes of data
BYTE to_send_d[data_size + 3];
BYTE data[data_size] = {'w','x','y','z'};
//-----------------data packet--------------------
to_send_d[0] = 'd';
to_send_d[1] = sensor.getID()+'0';
to_send_d[2] = data_size + '0';
for (int i = 3; i<=6;i++){
to_send_d[i] = data[i-3];
}
bridge.send_sd((BYTE*)to_send_d, data_size+3);
bridge.recv_sd_handler(0, bridge_d_handler);
bridge.recv_sd(data_size+3);
cout<<"================= Control Node data send/recv test ======================="<<endl;
control.send_sd((BYTE*)to_send_d, data_size+3);
control.recv_sd_handler(0, control_d_handler);
control.recv_sd(data_size+3);
cout<<"View data using the user interface below."<<endl;
cout<<"================= Sensor data polling test ======================="<<endl;
int setup = wiringPiSetup();
mcp3004Setup(BASE, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels
int x;
char in = '1';
while (setup != -1 && in != 'q'/*sensor.isOn()*/){
cout<<"Polling now (time between polls = 3s). Enter 'q' to stop or any key to continue."<<endl;
cin>>in;
cout<<"Channel readings: "<<endl;
for (int chan = 0 ; chan < 8 ; ++chan){
x = analogRead (BASE + chan);
cout<<x<<endl;
}
sleep(3);
}
cout<<endl<<"---------------------User interface-------------------------"<<endl<<endl;
bool nodeOn = true;
while (nodeOn)
{
cout << "Enter a command \n";
cout << "1 - start sampling \n";
cout << "2 - stop sampling \n";
cout << "3 - change sampling rate \n";
cout << "4 - view data \n";
cout << "q - quit \n";
cin >> userInput;
//pass command to the node's input handler. can add option for exiting program.
if (userInput == 'q')
{
nodeOn = false;
sensor.closeGPIO();
bridge.closeGPIO();
control.closeGPIO();
}
else
{
control.inputHandler(userInput); //handler must work with input as string
}
}
}