-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfm69-gw.cc
131 lines (111 loc) · 3.28 KB
/
rfm69-gw.cc
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
/**************************************************************************
|PC| <-serial-> |Moteino_Gw| <-RFM69W-> |Target_Moteino|
NOTE: Endian Conversion Required to properly read encoded data
https://docs.python.org/2/library/struct.html
Linux = Little Endian (Python conversion table)
Moteino = Big endian
Data Type Cross Reference Table
--------------------------------
Type Linux(python) Moteino
byte char(1) byte(1)
int short(2) int(2)
long long(4) long(4)
float float(4) float(4)
***************************************************************************/
#include <RFM69.h>
#include <SPI.h>
#include <NodeGwMsg.h> // Load Gateway Message structure:Task
#include <NodeConf.h>
// Initialize Redio
RFM69 radio;
byte buff[MAX_SERIAL_SIZE];
// BEGIN Function Prototypes
void Blink(byte PIN, int DELAY_MS);
// END Function Prototypes
void setup() {
Serial.begin(SERIAL_BAUD);
delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID);
radio.encrypt(KEY);
}
void loop() {
// READ Serial input if avaiable...
if (Serial.available() > 0) {
//Wait for serial buffer to fill
delay(100);
// Read serial data into buffer
// METHOD 1 Blind read, no limit (Risky?)
int count=0;
// read until the serial buffer is empty
while (Serial.available() > 0) {
buff[count] = (byte)Serial.read();
count++;
}
/*
Serial.print("Count = ");
Serial.println(count);
*/
// End read data from serial
// read Serial Message
sMsg = *(SerialMsg*)buff;
if (sMsg.SerialDelimiter == 0) {
/*
// Valid Message
Serial.print("NodeID = ");
Serial.println(sMsg.NodeID);
Serial.print("Message Size = ");
Serial.println(sMsg.SerialPayloadSize);
// Load Message
// Get Payload from Serial Payload
payload = *(Payload*)sMsg.SerialPayload;
Serial.print("Message ID = ");
Serial.println(payload.MsgID);
Serial.print("Message Type = ");
Serial.println(payload.MsgType);
Serial.print("LED Number = ");
Serial.println(sMsg.SerialPayload[2]);
Serial.print("LED State = ");
Serial.println(sMsg.SerialPayload[3]);
Turn on Light # 7
\x00\x2a\x04\x05\x0a\x07\x01
\x00\x2b\x04\x05\x0a\x06\x01
Turn off Light # 7
\x00\x2a\x04\x05\x0a\x07\x00
*/
//memcpy(o_payload.msg, &sMsg.SerialPayload, sizeof(sMsg.SerialPayloadSize));
radio.sendWithRetry(sMsg.NodeID, (const void*)(&sMsg.SerialPayload), sMsg.SerialPayloadSize);
// good Serial Message Blink Yellow Light
Blink(PASS_LED, 10);
} else {
Blink(FAIL_LED, 10);
/*
// !!!!!! Invalid message
Serial.println("DEBUG Invalid Message Discard!");
// Print debug info about message
Serial.print("[DEBUG]Serial Delimiter = ");
Serial.println(sMsg.SerialDelimiter);
Serial.print("[DEBUG]NodeID = ");
Serial.println(sMsg.NodeID);
Serial.print("[DEBUG]Message Size = ");
Serial.println(sMsg.SerialPayloadSize);
*/
}
}
// Get incomming messages
if ( radio.receiveDone() ) {
payload.msg = *(Payload*)radio.DATA;
nMsg.SerialDelimiter = 0x00;
nMsg.NodeID = radio.SENDERID;
nMsg.SerialPayloadSize = radio.DATALEN;
memcpy(nMsg.SerialPayload, &radio.DATA, nMsg.SerialPayloadSize);
Blink(LED,5);
Blink(PASS_LED, 10);
}
}
void Blink(byte PIN, int DELAY_MS)
{
pinMode(PIN, OUTPUT);
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
}