-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunication2.ino
123 lines (114 loc) · 3.65 KB
/
communication2.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
// rf69_server.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing server
// with the RH_RF69 class. RH_RF69 class does not provide for addressing or
// reliability, so you should only use RH_RF69 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf69_client
// Demonstrates the use of AES encryption, setting the frequency and modem
// configuration.
// Tested on Moteino with RFM69 http://lowpowerlab.com/moteino/
// Tested on miniWireless with RFM69 www.anarduino.com/miniwireless
// Tested on Teensy 3.1 with RF69 on PJRC breakout board
#include <SPI.h>
#include <RH_RF69.h>
// Singleton instance of the radio driver
//RH_RF69 rf69;
//RH_RF69 rf69(15, 16); // For RF69 on PJRC breakout board with Teensy 3.1
//RH_RF69 rf69(4, 2); // For MoteinoMEGA https://lowpowerlab.com/shop/moteinomega
RH_RF69 rf69(8, 7); // Adafruit Feather 32u4
void setup()
{
Serial.begin(9600);
while (!Serial);
// pinMode(RFM69,OUTPUT);
// digitalWrite(RFM69_RST,LOW);
// delay(10);
// digitalWrite(RFM69_RST,HIGH);
// delay(10);
// digitalWrite(RFM69_RST,LOW);
if (!rf69.init())
Serial.println("init failed");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
// No encryption
if (!rf69.setFrequency(433.0))
Serial.println("setFrequency failed");
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
// ishighpowermodule flag set like this:
rf69.setTxPower(20, true);
// The encryption key has to be the same as the one in the client
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);
#if 0
// For compat with RFM69 Struct_send
rf69.setModemConfig(RH_RF69::GFSK_Rb250Fd250);
rf69.setPreambleLength(3);
uint8_t syncwords[] = { 0x2d, 0x64 };
rf69.setSyncWords(syncwords, sizeof(syncwords));
rf69.setEncryptionKey((uint8_t*)"thisIsEncryptKey");
#endif
}
uint8_t my_id =9;
uint8_t my_frame_counter=0;
uint8_t frame_counter[16];
uint8_t repeat_counter[16];
uint8_t data[128] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,'K','W','O','N',':'};
uint32_t byte_mask = 0 ;
uint32_t rcv_byte_mask = 0 ;
void loop()
{
if(Serial.available()){
byte_mask|=(1<<my_id);
data[0]=my_id;
my_frame_counter++;
my_frame_counter%=128;
data[1]=my_frame_counter;
data[2]=0;
uint32_t* tmp_pt = (uint32_t*)&data[3];
*tmp_pt=byte_mask;
int len_input =0 ;
while(Serial.available()){
data[12+len_input] = Serial.read();
len_input++;
}
data[12+len_input]=NULL;
len_input++;
rf69.send(data,len_input+12);
}
if (rf69.available())
{
// Should be a message for us now
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf69.recv(buf, &len))
{
// RH_RF69::printBuffer("request: ", buf, len);
int tmp_id=buf[0]-1;
Serial.print(tmp_id+1);
Serial.print(" ");
rcv_byte_mask = *((uint32_t*)&buf[3]);
Serial.println(rcv_byte_mask,BIN);
rcv_byte_mask |=(1<<my_id);
if(frame_counter[tmp_id]!=buf[1]){
Serial.print("got request: ");
Serial.println((char*)&buf[3]);
}else{
Serial.print("already recv!");
}
uint8_t tmp_repeatcounter = buf[2];
if(tmp_repeatcounter <10){
tmp_repeatcounter++;
buf[2]=tmp_repeatcounter;
rf69.send(buf,len + 1);
}
// Serial.print("RSSI: ");
// Serial.println(rf69.lastRssi(), DEC);
// Send a reply
}
else
{
Serial.println("recv failed");
}
}
}