forked from veonik/arduino-cc1101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.ino
116 lines (99 loc) · 3.28 KB
/
example.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
#include <Arduino.h>
#include <cc1101.h>
#include <ccpacket.h>
// Attach CC1101 pins to their corresponding SPI pins
// Uno pins:
// CSN (SS) => 10
// MOSI => 11
// MISO => 12
// SCK => 13
// GD0 => A valid interrupt pin for your platform (defined below this)
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__)
#define CC1101Interrupt 4 // Pin 19
#define CC1101_GDO0 19
#elif defined(__MK64FX512__)
// Teensy 3.5
#define CC1101Interrupt 9 // Pin 9
#define CC1101_GDO0 9
#else
#define CC1101Interrupt 0 // Pin 2
#define CC1101_GDO0 2
#endif
CC1101 radio;
byte syncWord[2] = {199, 10};
bool packetWaiting;
unsigned long lastSend = 0;
unsigned int sendDelay = 5000;
void messageReceived() {
packetWaiting = true;
}
void setup() {
radio.init();
radio.setSyncWord(syncWord);
radio.setCarrierFreq(CFREQ_433);
radio.disableAddressCheck();
radio.setTxPowerAmp(PA_LongDistance);
Serial.begin(9600);
Serial.print(F("CC1101_PARTNUM "));
Serial.println(radio.readReg(CC1101_PARTNUM, CC1101_STATUS_REGISTER));
Serial.print(F("CC1101_VERSION "));
Serial.println(radio.readReg(CC1101_VERSION, CC1101_STATUS_REGISTER));
Serial.print(F("CC1101_MARCSTATE "));
Serial.println(radio.readReg(CC1101_MARCSTATE, CC1101_STATUS_REGISTER) & 0x1f);
Serial.println(F("CC1101 radio initialized."));
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
}
// Get signal strength indicator in dBm.
// See: http://www.ti.com/lit/an/swra114d/swra114d.pdf
int rssi(char raw) {
uint8_t rssi_dec;
// TODO: This rssi_offset is dependent on baud and MHz; this is for 38.4kbps and 433 MHz.
uint8_t rssi_offset = 74;
rssi_dec = (uint8_t) raw;
if (rssi_dec >= 128)
return ((int)( rssi_dec - 256) / 2) - rssi_offset;
else
return (rssi_dec / 2) - rssi_offset;
}
// Get link quality indicator.
int lqi(char raw) {
return 0x3F - raw;
}
void loop() {
if (packetWaiting) {
detachInterrupt(CC1101Interrupt);
packetWaiting = false;
CCPACKET packet;
if (radio.receiveData(&packet) > 0) {
Serial.println(F("Received packet..."));
if (!packet.crc_ok) {
Serial.println(F("crc not ok"));
}
Serial.print(F("lqi: "));
Serial.println(lqi(packet.lqi));
Serial.print(F("rssi: "));
Serial.print(rssi(packet.rssi));
Serial.println(F("dBm"));
if (packet.crc_ok && packet.length > 0) {
Serial.print(F("packet: len "));
Serial.println(packet.length);
Serial.println(F("data: "));
Serial.println((const char *) packet.data);
}
}
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
}
unsigned long now = millis();
if (now > lastSend + sendDelay) {
detachInterrupt(CC1101Interrupt);
lastSend = now;
const char *message = "hello world";
CCPACKET packet;
// We also need to include the 0 byte at the end of the string
packet.length = strlen(message) + 1;
strncpy((char *) packet.data, message, packet.length);
radio.sendData(packet);
Serial.println(F("Sent packet..."));
attachInterrupt(CC1101Interrupt, messageReceived, FALLING);
}
}