-
Notifications
You must be signed in to change notification settings - Fork 4
/
horusbinary_radiolib.ino
271 lines (215 loc) · 8.09 KB
/
horusbinary_radiolib.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
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
/*
RadioLib Horus Binary FSK4 Packet Generation & Transmitter Example
This example sends an example FSK-4 'Horus Binary' message using SX1278's
FSK modem. This example uses 'stock' RadioLib - and makes calls into the radio's
lower-level functions.
This signal can be demodulated using a SSB demodulator (SDR or otherwise), and
horusdemodlib: https://github.com/projecthorus/horusdemodlib/wiki
Other modules that can be used for FSK4:
(Untested, but work with RTTY to are likely to work here too)
- SX127x/RFM9x
- RF69
- SX1231
- CC1101
- SX126x
- nRF24
- Si443x/RFM2x
- SX128x
*/
#include <RadioLib.h>
#include "horus_l2.h"
// RADIO SETUP
#define TX_FREQ 434.200
#define FSK4_BAUD 100
#define FSK4_SPACING 270 // NOTE: This results in a shift of 244 Hz due to the PLL Resolution of the SX127x
// SX1278 has the following connections:
// NSS pin: 10
// DIO0 pin: 2
// RESET pin: 9
// DIO1 pin: 3
SX1278 radio = new Module(10, 2, 9, 3);
// Horus Binary Structures & Variables
// Horus Binary Packet Structure - Version 1
struct HorusBinaryPacketV1
{
uint8_t PayloadID;
uint16_t Counter;
uint8_t Hours;
uint8_t Minutes;
uint8_t Seconds;
float Latitude;
float Longitude;
uint16_t Altitude;
uint8_t Speed; // Speed in Knots (1-255 knots)
uint8_t Sats;
int8_t Temp; // Twos Complement Temp value.
uint8_t BattVoltage; // 0 = 0v, 255 = 5.0V, linear steps in-between.
uint16_t Checksum; // CRC16-CCITT Checksum.
} __attribute__ ((packed));
// Horus v2 Mode 1 (32-byte) Binary Packet
struct HorusBinaryPacketV2
{
uint16_t PayloadID;
uint16_t Counter;
uint8_t Hours;
uint8_t Minutes;
uint8_t Seconds;
float Latitude;
float Longitude;
uint16_t Altitude;
uint8_t Speed; // Speed in Knots (1-255 knots)
uint8_t Sats;
int8_t Temp; // Twos Complement Temp value.
uint8_t BattVoltage; // 0 = 0v, 255 = 5.0V, linear steps in-between.
// The following 9 bytes (up to the CRC) are user-customizable. The following just
// provides an example of how they could be used.
uint8_t dummy1; // unsigned int
float dummy2; // Float
uint8_t dummy3; // battery voltage test
uint8_t dummy4; // divide by 10
uint16_t dummy5; // divide by 100
uint16_t Checksum; // CRC16-CCITT Checksum.
} __attribute__ ((packed));
// Buffers and counters.
char rawbuffer [128]; // Buffer to temporarily store a raw binary packet.
char codedbuffer [128]; // Buffer to store an encoded binary packet
char debugbuffer[256]; // Buffer to store debug strings
uint16_t packet_count = 1; // Packet counter
int build_horus_binary_packet_v1(char *buffer){
// Generate a Horus Binary v1 packet, and populate it with data.
// The assignments in this function should be replaced with real data
struct HorusBinaryPacketV1 BinaryPacket;
BinaryPacket.PayloadID = 0; // 0 = 4FSKTEST. Refer https://github.com/projecthorus/horusdemodlib/blob/master/payload_id_list.txt
BinaryPacket.Counter = packet_count;
BinaryPacket.Hours = 12;
BinaryPacket.Minutes = 34;
BinaryPacket.Seconds = 56;
BinaryPacket.Latitude = 0.0;
BinaryPacket.Longitude = 0.0;
BinaryPacket.Altitude = 0;
BinaryPacket.Speed = 0;
BinaryPacket.BattVoltage = 0;
BinaryPacket.Sats = 0;
BinaryPacket.Temp = 0;
BinaryPacket.Checksum = (uint16_t)crc16((unsigned char*)&BinaryPacket,sizeof(BinaryPacket)-2);
memcpy(buffer, &BinaryPacket, sizeof(BinaryPacket));
return sizeof(struct HorusBinaryPacketV1);
}
int build_horus_binary_packet_v2(char *buffer){
// Generate a Horus Binary v2 packet, and populate it with data.
// The assignments in this function should be replaced with real data
struct HorusBinaryPacketV2 BinaryPacketV2;
BinaryPacketV2.PayloadID = 256; // 0 = 4FSKTEST-V2. Refer https://github.com/projecthorus/horusdemodlib/blob/master/payload_id_list.txt
BinaryPacketV2.Counter = packet_count;
BinaryPacketV2.Hours = 12;
BinaryPacketV2.Minutes = 34;
BinaryPacketV2.Seconds = 56;
BinaryPacketV2.Latitude = 0.0;
BinaryPacketV2.Longitude = 0.0;
BinaryPacketV2.Altitude = 0;
BinaryPacketV2.Speed = 0;
BinaryPacketV2.BattVoltage = 0;
BinaryPacketV2.Sats = 0;
BinaryPacketV2.Temp = 0;
// Custom section. This is an example only, and the 9 bytes in this section can be used in other
// ways. Refer here for details: https://github.com/projecthorus/horusdemodlib/wiki/5-Customising-a-Horus-Binary-v2-Packet
BinaryPacketV2.dummy1 = 1; // uint8
BinaryPacketV2.dummy2 = 1.23456; // float32
BinaryPacketV2.dummy3 = 100; // uint8 - interpreted as a battery voltage 0-5V
BinaryPacketV2.dummy4 = 123; // uint8 - interpreted as a fixed-point value (div/10)
BinaryPacketV2.dummy5 = 1234; // uint16 - interpreted as a fixed-point value (div/100)
BinaryPacketV2.Checksum = (uint16_t)crc16((unsigned char*)&BinaryPacketV2,sizeof(BinaryPacketV2)-2);
memcpy(buffer, &BinaryPacketV2, sizeof(BinaryPacketV2));
return sizeof(struct HorusBinaryPacketV2);
}
void setup() {
Serial.begin(9600);
// initialize SX1278 with default settings
Serial.print(F("[SX1278] Initializing ... "));
int state = radio.beginFSK();
// when using one of the non-LoRa modules for FSK4
// (RF69, CC1101, Si4432 etc.), use the basic begin() method
// int state = radio.begin();
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
}
Serial.print(F("[FSK4] Initializing ... "));
// initialize FSK4 transmitter
// NOTE: FSK4 frequency shift will be rounded
// to the nearest multiple of frequency step size.
// The exact value depends on the module:
// SX127x/RFM9x - 61 Hz
// RF69 - 61 Hz
// CC1101 - 397 Hz
// SX126x - 1 Hz
// nRF24 - 1000000 Hz
// Si443x/RFM2x - 156 Hz
// SX128x - 198 Hz
state = fsk4_setup(&radio, TX_FREQ, FSK4_SPACING, FSK4_BAUD);
if(state == RADIOLIB_ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
}
}
void loop() {
// Horus Binary V1
Serial.println(F("Generating Horus Binary v1 Packet"));
// Generate packet
int pkt_len = build_horus_binary_packet_v1(rawbuffer);
// Debugging
Serial.print(F("Uncoded Length (bytes): "));
Serial.println(pkt_len);
Serial.print("Uncoded: ");
PrintHex(rawbuffer, pkt_len, debugbuffer);
Serial.println(debugbuffer);
// Apply Encoding
int coded_len = horus_l2_encode_tx_packet((unsigned char*)codedbuffer,(unsigned char*)rawbuffer,pkt_len);
// Debugging
Serial.print(F("Encoded Length (bytes): "));
Serial.println(coded_len);
Serial.print("Coded: ");
PrintHex(codedbuffer, coded_len, debugbuffer);
Serial.println(debugbuffer);
// Transmit!
Serial.println(F("Transmitting Horus Binary v1 Packet"));
// send out idle condition for 1000 ms
fsk4_idle(&radio);
delay(1000);
fsk4_preamble(&radio, 8);
fsk4_write(&radio, codedbuffer, coded_len);
// Horus Binary V2
Serial.println(F("Generating Horus Binary v2 Packet"));
// Generate packet
pkt_len = build_horus_binary_packet_v2(rawbuffer);
// Debugging
Serial.print(F("Uncoded Length (bytes): "));
Serial.println(pkt_len);
Serial.print("Uncoded: ");
PrintHex(rawbuffer, pkt_len, debugbuffer);
Serial.println(debugbuffer);
// Apply Encoding
coded_len = horus_l2_encode_tx_packet((unsigned char*)codedbuffer,(unsigned char*)rawbuffer,pkt_len);
// Debugging
Serial.print(F("Encoded Length (bytes): "));
Serial.println(coded_len);
Serial.print("Coded: ");
PrintHex(codedbuffer, coded_len, debugbuffer);
Serial.println(debugbuffer);
// Transmit!
Serial.println(F("Transmitting Horus Binary v2 Packet"));
// send out idle condition for 1000 ms
fsk4_idle(&radio);
delay(1000);
fsk4_preamble(&radio, 8);
fsk4_write(&radio, codedbuffer, coded_len);
Serial.println(F("done!"));
delay(1000);
packet_count++;
}