RFM12B Example

Dependencies:   RFM12B TextLCD mbed-rtos mbed

/media/uploads/hajesusrodrigues/img_3299.jpg

mBed RFM12B module Hello World example

The main purpose of this example was to implement the RFM12B module in order to be able to establish communication with the Moteino (arduino clone that uses the RFM12B).

In this example I’m able to test the communication between:

1) mBed -> mBed (2 RFM12B modules sharing the same mBed board)

2) mBed -> Moteino

3) Moteino -> mBed

Who/What is Moteino? (http://lowpowerlab.com/moteino/)

Committer:
hajesusrodrigues
Date:
Thu May 30 22:10:28 2013 +0000
Revision:
0:9457949d5d00
Child:
1:e30050868e55
mBed RFM12B module example. ; mBed -> mBed (2 RFM12B modules using the same mBed); mbed -> Moteino ; Moteino -> mBed; ; Who/What is Moteino? (http://lowpowerlab.com/blog/2012/12/20/moteino-the-wireless-low-power-low-cost-arduino-clone/)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hajesusrodrigues 0:9457949d5d00 1 #include "mbed.h"
hajesusrodrigues 0:9457949d5d00 2 #include "rtos.h"
hajesusrodrigues 0:9457949d5d00 3 #include "RFM12B/RFM12B.h"
hajesusrodrigues 0:9457949d5d00 4 #include "TextLCD/TextLCD.h"
hajesusrodrigues 0:9457949d5d00 5
hajesusrodrigues 0:9457949d5d00 6 extern "C" void mbed_reset();
hajesusrodrigues 0:9457949d5d00 7
hajesusrodrigues 0:9457949d5d00 8 TextLCD lcd(p22, p23, p24, p25, p26, p27, p28,TextLCD::LCD16x2); // rs, rw, e, d4-d7
hajesusrodrigues 0:9457949d5d00 9
hajesusrodrigues 0:9457949d5d00 10 RFM12B rfm12b_sender(p5, p6, p7, p10, p9, LED1); //RFM12B(PinName SDI, PinName SDO, PinName SCK, PinName SEL, PinName IRQ, , PinName IRQ_LED);
hajesusrodrigues 0:9457949d5d00 11 RFM12B rfm12b_receiver(p11, p12, p13, p14, p15, LED4); //RFM12B(PinName SDI, PinName SDO, PinName SCK, PinName SEL, PinName IRQ, , PinName IRQ_LED);
hajesusrodrigues 0:9457949d5d00 12
hajesusrodrigues 0:9457949d5d00 13 DigitalOut rfm12b_sender_led(p21);
hajesusrodrigues 0:9457949d5d00 14 DigitalOut rfm12b_receiver_led(p18);
hajesusrodrigues 0:9457949d5d00 15
hajesusrodrigues 0:9457949d5d00 16 DigitalOut activity_ledOn(LED2);
hajesusrodrigues 0:9457949d5d00 17 DigitalOut activity_ledOff(LED3);
hajesusrodrigues 0:9457949d5d00 18
hajesusrodrigues 0:9457949d5d00 19 Serial pc(USBTX, USBRX);
hajesusrodrigues 0:9457949d5d00 20
hajesusrodrigues 0:9457949d5d00 21 char send_message[255] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
hajesusrodrigues 0:9457949d5d00 22
hajesusrodrigues 0:9457949d5d00 23 uint8_t KEY[] = "ABCDABCDABCDABCD";
hajesusrodrigues 0:9457949d5d00 24
hajesusrodrigues 0:9457949d5d00 25 #define ACK_TIME 15000 //R of ms to wait for an ack (usually between 600ms and 1300ms)
hajesusrodrigues 0:9457949d5d00 26 #define CLIENT_MOTEINO_NODE 1
hajesusrodrigues 0:9457949d5d00 27 #define CLIENT_MBED_NODE 2
hajesusrodrigues 0:9457949d5d00 28
hajesusrodrigues 0:9457949d5d00 29 #define SERVER_MOTEINO_NODE 10
hajesusrodrigues 0:9457949d5d00 30 #define SERVER_MBED_NODE 11
hajesusrodrigues 0:9457949d5d00 31
hajesusrodrigues 0:9457949d5d00 32 #define NETWORD_ID 5 //GROUP / NETWORK ID
hajesusrodrigues 0:9457949d5d00 33
hajesusrodrigues 0:9457949d5d00 34 #define MBED_TO_MBED 0
hajesusrodrigues 0:9457949d5d00 35 #define MBED_TO_ARDUINO 1
hajesusrodrigues 0:9457949d5d00 36 #define ARDUINO_TO_MBED 2
hajesusrodrigues 0:9457949d5d00 37
hajesusrodrigues 0:9457949d5d00 38 #define TEST_TYPE MBED_TO_MBED
hajesusrodrigues 0:9457949d5d00 39
hajesusrodrigues 0:9457949d5d00 40 void ReceiverThread(void const *args) {
hajesusrodrigues 0:9457949d5d00 41 Timer receiverTimer;
hajesusrodrigues 0:9457949d5d00 42 rfm12b_receiver.Initialize(SERVER_MBED_NODE, RF12_433MHZ, NETWORD_ID); //id = 10, band 433, group 5
hajesusrodrigues 0:9457949d5d00 43 rfm12b_receiver.SetEncryptionKey((uint8_t*) KEY);
hajesusrodrigues 0:9457949d5d00 44 rfm12b_receiver.ReceiveStart();
hajesusrodrigues 0:9457949d5d00 45
hajesusrodrigues 0:9457949d5d00 46 while (true) {
hajesusrodrigues 0:9457949d5d00 47
hajesusrodrigues 0:9457949d5d00 48 if (rfm12b_receiver.ReceiveComplete()) {
hajesusrodrigues 0:9457949d5d00 49 // Do something with received data
hajesusrodrigues 0:9457949d5d00 50
hajesusrodrigues 0:9457949d5d00 51 receiverTimer.start();
hajesusrodrigues 0:9457949d5d00 52 printf("RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\nRECEIVER- beginning receiving message\n");
hajesusrodrigues 0:9457949d5d00 53 lcd.locate(0,1);
hajesusrodrigues 0:9457949d5d00 54 lcd.printf("RECEIVER- BRM");
hajesusrodrigues 0:9457949d5d00 55
hajesusrodrigues 0:9457949d5d00 56 if (rfm12b_receiver.CRC_Pass()) {
hajesusrodrigues 0:9457949d5d00 57
hajesusrodrigues 0:9457949d5d00 58 printf("RECEIVER- Data received from [%d] : ", rfm12b_receiver.GetSender());
hajesusrodrigues 0:9457949d5d00 59 for (int i = 0; i < int(rfm12b_receiver.GetDataLen()); i++) {
hajesusrodrigues 0:9457949d5d00 60 printf("%c", (rfm12b_receiver.GetData())[i]);
hajesusrodrigues 0:9457949d5d00 61 }
hajesusrodrigues 0:9457949d5d00 62 printf("\n");
hajesusrodrigues 0:9457949d5d00 63
hajesusrodrigues 0:9457949d5d00 64 if (rfm12b_receiver.ACKRequested()) {
hajesusrodrigues 0:9457949d5d00 65 printf("RECEIVER- ACK sent to [%d] \n", rfm12b_receiver.GetSender());
hajesusrodrigues 0:9457949d5d00 66 rfm12b_receiver.SendACK("ACK sent from mBed server");
hajesusrodrigues 0:9457949d5d00 67 }
hajesusrodrigues 0:9457949d5d00 68
hajesusrodrigues 0:9457949d5d00 69 } else {
hajesusrodrigues 0:9457949d5d00 70 printf("RECEIVER- !!! BAD-CRC !!! \n");
hajesusrodrigues 0:9457949d5d00 71 }
hajesusrodrigues 0:9457949d5d00 72
hajesusrodrigues 0:9457949d5d00 73 printf("RECEIVER- finishing receiving message (time since start receiving message %d ms, %4.2f s)\nRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\n", receiverTimer.read_ms(), receiverTimer.read());
hajesusrodrigues 0:9457949d5d00 74 lcd.locate(0,1);
hajesusrodrigues 0:9457949d5d00 75 lcd.printf("RECEIVER- finishing receiving message");
hajesusrodrigues 0:9457949d5d00 76
hajesusrodrigues 0:9457949d5d00 77 receiverTimer.stop();
hajesusrodrigues 0:9457949d5d00 78 receiverTimer.reset();
hajesusrodrigues 0:9457949d5d00 79
hajesusrodrigues 0:9457949d5d00 80 rfm12b_receiver_led = 1;
hajesusrodrigues 0:9457949d5d00 81 wait(0.2);
hajesusrodrigues 0:9457949d5d00 82 rfm12b_receiver_led = 0;
hajesusrodrigues 0:9457949d5d00 83 }
hajesusrodrigues 0:9457949d5d00 84 }
hajesusrodrigues 0:9457949d5d00 85 }
hajesusrodrigues 0:9457949d5d00 86
hajesusrodrigues 0:9457949d5d00 87 void SenderThread(void const *args) {
hajesusrodrigues 0:9457949d5d00 88 Timer ackTimer;
hajesusrodrigues 0:9457949d5d00 89 Timer senderTimer;
hajesusrodrigues 0:9457949d5d00 90
hajesusrodrigues 0:9457949d5d00 91 bool requestACK = true;
hajesusrodrigues 0:9457949d5d00 92 rfm12b_sender.Initialize(CLIENT_MBED_NODE, RF12_433MHZ, NETWORD_ID); //id = 2, band 866, group 5
hajesusrodrigues 0:9457949d5d00 93 rfm12b_sender.SetEncryptionKey((uint8_t*) KEY);
hajesusrodrigues 0:9457949d5d00 94
hajesusrodrigues 0:9457949d5d00 95 int i = 1;
hajesusrodrigues 0:9457949d5d00 96
hajesusrodrigues 0:9457949d5d00 97 while (true) {
hajesusrodrigues 0:9457949d5d00 98 senderTimer.start();
hajesusrodrigues 0:9457949d5d00 99
hajesusrodrigues 0:9457949d5d00 100 printf("\n\n\n\n");
hajesusrodrigues 0:9457949d5d00 101 printf("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\nSENDER- beginning sending the message\n");
hajesusrodrigues 0:9457949d5d00 102
hajesusrodrigues 0:9457949d5d00 103 #if TEST_TYPE == MBED_TO_MBED
hajesusrodrigues 0:9457949d5d00 104 rfm12b_sender.SendStart(SERVER_MBED_NODE, send_message, i, requestACK, false);
hajesusrodrigues 0:9457949d5d00 105 #elif TEST_TYPE == MBED_TO_ARDUINO
hajesusrodrigues 0:9457949d5d00 106 rfm12b_sender.SendStart(SERVER_MOTEINO_NODE, send_message, i, requestACK, false);
hajesusrodrigues 0:9457949d5d00 107 #endif
hajesusrodrigues 0:9457949d5d00 108 if (requestACK) {
hajesusrodrigues 0:9457949d5d00 109 printf("SENDER- Waiting for ACK... after sending message \n");
hajesusrodrigues 0:9457949d5d00 110
hajesusrodrigues 0:9457949d5d00 111 ackTimer.start();
hajesusrodrigues 0:9457949d5d00 112 while (ackTimer.read_ms() <= ACK_TIME) {
hajesusrodrigues 0:9457949d5d00 113
hajesusrodrigues 0:9457949d5d00 114 #if TEST_TYPE == MBED_TO_MBED
hajesusrodrigues 0:9457949d5d00 115
hajesusrodrigues 0:9457949d5d00 116 if (rfm12b_sender.ACKReceived(SERVER_MBED_NODE) == true) {
hajesusrodrigues 0:9457949d5d00 117 printf("SENDER- ACK... received YEAHHHHHHHHH I FEEL GOOD \n");
hajesusrodrigues 0:9457949d5d00 118 break;
hajesusrodrigues 0:9457949d5d00 119 }
hajesusrodrigues 0:9457949d5d00 120
hajesusrodrigues 0:9457949d5d00 121 #elif TEST_TYPE == MBED_TO_ARDUINO
hajesusrodrigues 0:9457949d5d00 122
hajesusrodrigues 0:9457949d5d00 123 if (rfm12b_sender.ACKReceived(SERVER_MOTEINO_NODE) == true) {
hajesusrodrigues 0:9457949d5d00 124 printf("SENDER- ACK... received YEAHHHHHHHHH I FEEL GOOD \n");
hajesusrodrigues 0:9457949d5d00 125 break;
hajesusrodrigues 0:9457949d5d00 126 }
hajesusrodrigues 0:9457949d5d00 127 #endif
hajesusrodrigues 0:9457949d5d00 128
hajesusrodrigues 0:9457949d5d00 129 }
hajesusrodrigues 0:9457949d5d00 130 printf("SENDER- time since Waiting for ACK... %d ms, %4.2f s \n", ackTimer.read_ms(), ackTimer.read());
hajesusrodrigues 0:9457949d5d00 131
hajesusrodrigues 0:9457949d5d00 132 }
hajesusrodrigues 0:9457949d5d00 133
hajesusrodrigues 0:9457949d5d00 134 printf("SENDER- finishing sending the message (time since start sending message %d ms, %4.2f s)\nSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\n \n\n", senderTimer.read_ms(), senderTimer.read());
hajesusrodrigues 0:9457949d5d00 135
hajesusrodrigues 0:9457949d5d00 136 senderTimer.stop();
hajesusrodrigues 0:9457949d5d00 137 senderTimer.reset();
hajesusrodrigues 0:9457949d5d00 138
hajesusrodrigues 0:9457949d5d00 139 ackTimer.stop();
hajesusrodrigues 0:9457949d5d00 140 ackTimer.reset();
hajesusrodrigues 0:9457949d5d00 141
hajesusrodrigues 0:9457949d5d00 142 rfm12b_sender_led = 1;
hajesusrodrigues 0:9457949d5d00 143 wait(0.2);
hajesusrodrigues 0:9457949d5d00 144 rfm12b_sender_led = 0;
hajesusrodrigues 0:9457949d5d00 145
hajesusrodrigues 0:9457949d5d00 146 (i >= 60) ? i = 0 : i++;
hajesusrodrigues 0:9457949d5d00 147
hajesusrodrigues 0:9457949d5d00 148 wait(20);
hajesusrodrigues 0:9457949d5d00 149 }
hajesusrodrigues 0:9457949d5d00 150 }
hajesusrodrigues 0:9457949d5d00 151
hajesusrodrigues 0:9457949d5d00 152 int main() {
hajesusrodrigues 0:9457949d5d00 153
hajesusrodrigues 0:9457949d5d00 154 #if TEST_TYPE == MBED_TO_MBED
hajesusrodrigues 0:9457949d5d00 155
hajesusrodrigues 0:9457949d5d00 156 printf("TEST TYPE: MBED_TO_MBED \n");
hajesusrodrigues 0:9457949d5d00 157 Thread b_thread(ReceiverThread);
hajesusrodrigues 0:9457949d5d00 158 printf("Receive thread \n");
hajesusrodrigues 0:9457949d5d00 159 wait(20);
hajesusrodrigues 0:9457949d5d00 160 Thread a_thread(SenderThread);
hajesusrodrigues 0:9457949d5d00 161 printf("Send thread \n");
hajesusrodrigues 0:9457949d5d00 162
hajesusrodrigues 0:9457949d5d00 163 #elif TEST_TYPE == MBED_TO_ARDUINO
hajesusrodrigues 0:9457949d5d00 164
hajesusrodrigues 0:9457949d5d00 165 printf("TEST TYPE: MBED_TO_ARDUINO \n");
hajesusrodrigues 0:9457949d5d00 166 Thread a_thread(SenderThread);
hajesusrodrigues 0:9457949d5d00 167 printf("Send thread \n");
hajesusrodrigues 0:9457949d5d00 168
hajesusrodrigues 0:9457949d5d00 169 #elif TEST_TYPE == ARDUINO_TO_MBED
hajesusrodrigues 0:9457949d5d00 170
hajesusrodrigues 0:9457949d5d00 171 printf("TEST TYPE: MBED_TO_ARDUINO \n");
hajesusrodrigues 0:9457949d5d00 172 Thread b_thread(ReceiverThread);
hajesusrodrigues 0:9457949d5d00 173 printf("Receive thread \n");
hajesusrodrigues 0:9457949d5d00 174
hajesusrodrigues 0:9457949d5d00 175 #endif
hajesusrodrigues 0:9457949d5d00 176
hajesusrodrigues 0:9457949d5d00 177 while (true) {
hajesusrodrigues 0:9457949d5d00 178 activity_ledOn = 1;
hajesusrodrigues 0:9457949d5d00 179 activity_ledOff = 0;
hajesusrodrigues 0:9457949d5d00 180 wait(0.2);
hajesusrodrigues 0:9457949d5d00 181 activity_ledOn = 0;
hajesusrodrigues 0:9457949d5d00 182 activity_ledOff = 1;
hajesusrodrigues 0:9457949d5d00 183 wait(0.2);
hajesusrodrigues 0:9457949d5d00 184
hajesusrodrigues 0:9457949d5d00 185 if(pc.readable()) {
hajesusrodrigues 0:9457949d5d00 186 if (pc.getc() == 'r') {
hajesusrodrigues 0:9457949d5d00 187 mbed_reset();
hajesusrodrigues 0:9457949d5d00 188 }
hajesusrodrigues 0:9457949d5d00 189
hajesusrodrigues 0:9457949d5d00 190 }
hajesusrodrigues 0:9457949d5d00 191 }
hajesusrodrigues 0:9457949d5d00 192 }