-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy patha_FixedPackets.ino
51 lines (41 loc) · 1.85 KB
/
a_FixedPackets.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
/*
The first task of the base station is to receive data packets from the wireless nodes.
This example shows the simplest way to receive packets it uses a struct: "a structured record
type that aggregates a fixed set of labelled objects, possibly of different types, into a single object" - Wikipedia
This struct is used to tell the program how to extract individual elements from the packet data received.
It tells the program that the first variable is an integer or second could be a float for example.
It requires that the same struct definition is used on the transmitting node.
Use the emonTxFirmware > Guide > g_TransmittingData example to send data to be recieved by this sketch.
-----------------------------------------
Part of the openenergymonitor.org project
Licence: GNU GPL V3
*/
#define RF69_COMPAT 0 // Set to 1 if using RFM69CW or 0 is using RFM12B
#include <JeeLib.h> //https://github.com/jcw/jeelib
// Define the data structure of the packet to be received
typedef struct { int power, voltage; } PayloadTX;
// Create a variable to hold the received data of the defined structure .
PayloadTX emontx;
void setup ()
{
Serial.begin(9600);
Serial.println("01 fixed packets");
rf12_initialize(15,RF12_433MHZ,210); // NodeID, Frequency, Group
}
void loop ()
{
if (rf12_recvDone() && rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
{
int node_id = (rf12_hdr & 0x1F);
// Emontx node id is set to 10
if (node_id == 10)
{
// The packet data is contained in rf12_data, the *(PayloadTX*) part tells the compiler
// what the format of the data is so that it can be copied correctly
emontx = *(PayloadTX*) rf12_data;
Serial.print(emontx.power);
Serial.print(' ');
Serial.println(emontx.voltage);
}
}
}