Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: RFM12B TextLCD mbed-rtos mbed
main.cpp@1:e30050868e55, 2013-05-30 (annotated)
- Committer:
- hajesusrodrigues
- Date:
- Thu May 30 22:11:56 2013 +0000
- Revision:
- 1:e30050868e55
- Parent:
- 0:9457949d5d00
- Child:
- 4:03f3bdc02069
Code Format
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:e30050868e55 | 40 | void ReceiverThread(void const *args) |
hajesusrodrigues | 1:e30050868e55 | 41 | { |
hajesusrodrigues | 0:9457949d5d00 | 42 | Timer receiverTimer; |
hajesusrodrigues | 0:9457949d5d00 | 43 | rfm12b_receiver.Initialize(SERVER_MBED_NODE, RF12_433MHZ, NETWORD_ID); //id = 10, band 433, group 5 |
hajesusrodrigues | 0:9457949d5d00 | 44 | rfm12b_receiver.SetEncryptionKey((uint8_t*) KEY); |
hajesusrodrigues | 0:9457949d5d00 | 45 | rfm12b_receiver.ReceiveStart(); |
hajesusrodrigues | 0:9457949d5d00 | 46 | |
hajesusrodrigues | 0:9457949d5d00 | 47 | while (true) { |
hajesusrodrigues | 0:9457949d5d00 | 48 | |
hajesusrodrigues | 0:9457949d5d00 | 49 | if (rfm12b_receiver.ReceiveComplete()) { |
hajesusrodrigues | 0:9457949d5d00 | 50 | // Do something with received data |
hajesusrodrigues | 0:9457949d5d00 | 51 | |
hajesusrodrigues | 0:9457949d5d00 | 52 | receiverTimer.start(); |
hajesusrodrigues | 0:9457949d5d00 | 53 | printf("RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\nRECEIVER- beginning receiving message\n"); |
hajesusrodrigues | 0:9457949d5d00 | 54 | lcd.locate(0,1); |
hajesusrodrigues | 0:9457949d5d00 | 55 | lcd.printf("RECEIVER- BRM"); |
hajesusrodrigues | 0:9457949d5d00 | 56 | |
hajesusrodrigues | 0:9457949d5d00 | 57 | if (rfm12b_receiver.CRC_Pass()) { |
hajesusrodrigues | 0:9457949d5d00 | 58 | |
hajesusrodrigues | 0:9457949d5d00 | 59 | printf("RECEIVER- Data received from [%d] : ", rfm12b_receiver.GetSender()); |
hajesusrodrigues | 0:9457949d5d00 | 60 | for (int i = 0; i < int(rfm12b_receiver.GetDataLen()); i++) { |
hajesusrodrigues | 0:9457949d5d00 | 61 | printf("%c", (rfm12b_receiver.GetData())[i]); |
hajesusrodrigues | 0:9457949d5d00 | 62 | } |
hajesusrodrigues | 0:9457949d5d00 | 63 | printf("\n"); |
hajesusrodrigues | 0:9457949d5d00 | 64 | |
hajesusrodrigues | 0:9457949d5d00 | 65 | if (rfm12b_receiver.ACKRequested()) { |
hajesusrodrigues | 0:9457949d5d00 | 66 | printf("RECEIVER- ACK sent to [%d] \n", rfm12b_receiver.GetSender()); |
hajesusrodrigues | 0:9457949d5d00 | 67 | rfm12b_receiver.SendACK("ACK sent from mBed server"); |
hajesusrodrigues | 0:9457949d5d00 | 68 | } |
hajesusrodrigues | 0:9457949d5d00 | 69 | |
hajesusrodrigues | 0:9457949d5d00 | 70 | } else { |
hajesusrodrigues | 0:9457949d5d00 | 71 | printf("RECEIVER- !!! BAD-CRC !!! \n"); |
hajesusrodrigues | 0:9457949d5d00 | 72 | } |
hajesusrodrigues | 0:9457949d5d00 | 73 | |
hajesusrodrigues | 0:9457949d5d00 | 74 | 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 | 75 | lcd.locate(0,1); |
hajesusrodrigues | 0:9457949d5d00 | 76 | lcd.printf("RECEIVER- finishing receiving message"); |
hajesusrodrigues | 0:9457949d5d00 | 77 | |
hajesusrodrigues | 0:9457949d5d00 | 78 | receiverTimer.stop(); |
hajesusrodrigues | 0:9457949d5d00 | 79 | receiverTimer.reset(); |
hajesusrodrigues | 0:9457949d5d00 | 80 | |
hajesusrodrigues | 0:9457949d5d00 | 81 | rfm12b_receiver_led = 1; |
hajesusrodrigues | 0:9457949d5d00 | 82 | wait(0.2); |
hajesusrodrigues | 0:9457949d5d00 | 83 | rfm12b_receiver_led = 0; |
hajesusrodrigues | 0:9457949d5d00 | 84 | } |
hajesusrodrigues | 0:9457949d5d00 | 85 | } |
hajesusrodrigues | 0:9457949d5d00 | 86 | } |
hajesusrodrigues | 0:9457949d5d00 | 87 | |
hajesusrodrigues | 1:e30050868e55 | 88 | void SenderThread(void const *args) |
hajesusrodrigues | 1:e30050868e55 | 89 | { |
hajesusrodrigues | 0:9457949d5d00 | 90 | Timer ackTimer; |
hajesusrodrigues | 0:9457949d5d00 | 91 | Timer senderTimer; |
hajesusrodrigues | 0:9457949d5d00 | 92 | |
hajesusrodrigues | 0:9457949d5d00 | 93 | bool requestACK = true; |
hajesusrodrigues | 0:9457949d5d00 | 94 | rfm12b_sender.Initialize(CLIENT_MBED_NODE, RF12_433MHZ, NETWORD_ID); //id = 2, band 866, group 5 |
hajesusrodrigues | 0:9457949d5d00 | 95 | rfm12b_sender.SetEncryptionKey((uint8_t*) KEY); |
hajesusrodrigues | 0:9457949d5d00 | 96 | |
hajesusrodrigues | 0:9457949d5d00 | 97 | int i = 1; |
hajesusrodrigues | 0:9457949d5d00 | 98 | |
hajesusrodrigues | 0:9457949d5d00 | 99 | while (true) { |
hajesusrodrigues | 0:9457949d5d00 | 100 | senderTimer.start(); |
hajesusrodrigues | 0:9457949d5d00 | 101 | |
hajesusrodrigues | 0:9457949d5d00 | 102 | printf("\n\n\n\n"); |
hajesusrodrigues | 0:9457949d5d00 | 103 | printf("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\nSENDER- beginning sending the message\n"); |
hajesusrodrigues | 0:9457949d5d00 | 104 | |
hajesusrodrigues | 0:9457949d5d00 | 105 | #if TEST_TYPE == MBED_TO_MBED |
hajesusrodrigues | 0:9457949d5d00 | 106 | rfm12b_sender.SendStart(SERVER_MBED_NODE, send_message, i, requestACK, false); |
hajesusrodrigues | 0:9457949d5d00 | 107 | #elif TEST_TYPE == MBED_TO_ARDUINO |
hajesusrodrigues | 0:9457949d5d00 | 108 | rfm12b_sender.SendStart(SERVER_MOTEINO_NODE, send_message, i, requestACK, false); |
hajesusrodrigues | 0:9457949d5d00 | 109 | #endif |
hajesusrodrigues | 0:9457949d5d00 | 110 | if (requestACK) { |
hajesusrodrigues | 0:9457949d5d00 | 111 | printf("SENDER- Waiting for ACK... after sending message \n"); |
hajesusrodrigues | 0:9457949d5d00 | 112 | |
hajesusrodrigues | 0:9457949d5d00 | 113 | ackTimer.start(); |
hajesusrodrigues | 0:9457949d5d00 | 114 | while (ackTimer.read_ms() <= ACK_TIME) { |
hajesusrodrigues | 0:9457949d5d00 | 115 | |
hajesusrodrigues | 0:9457949d5d00 | 116 | #if TEST_TYPE == MBED_TO_MBED |
hajesusrodrigues | 0:9457949d5d00 | 117 | |
hajesusrodrigues | 0:9457949d5d00 | 118 | if (rfm12b_sender.ACKReceived(SERVER_MBED_NODE) == true) { |
hajesusrodrigues | 0:9457949d5d00 | 119 | printf("SENDER- ACK... received YEAHHHHHHHHH I FEEL GOOD \n"); |
hajesusrodrigues | 0:9457949d5d00 | 120 | break; |
hajesusrodrigues | 0:9457949d5d00 | 121 | } |
hajesusrodrigues | 0:9457949d5d00 | 122 | |
hajesusrodrigues | 0:9457949d5d00 | 123 | #elif TEST_TYPE == MBED_TO_ARDUINO |
hajesusrodrigues | 0:9457949d5d00 | 124 | |
hajesusrodrigues | 0:9457949d5d00 | 125 | if (rfm12b_sender.ACKReceived(SERVER_MOTEINO_NODE) == true) { |
hajesusrodrigues | 0:9457949d5d00 | 126 | printf("SENDER- ACK... received YEAHHHHHHHHH I FEEL GOOD \n"); |
hajesusrodrigues | 0:9457949d5d00 | 127 | break; |
hajesusrodrigues | 0:9457949d5d00 | 128 | } |
hajesusrodrigues | 0:9457949d5d00 | 129 | #endif |
hajesusrodrigues | 0:9457949d5d00 | 130 | |
hajesusrodrigues | 0:9457949d5d00 | 131 | } |
hajesusrodrigues | 0:9457949d5d00 | 132 | printf("SENDER- time since Waiting for ACK... %d ms, %4.2f s \n", ackTimer.read_ms(), ackTimer.read()); |
hajesusrodrigues | 0:9457949d5d00 | 133 | |
hajesusrodrigues | 0:9457949d5d00 | 134 | } |
hajesusrodrigues | 0:9457949d5d00 | 135 | |
hajesusrodrigues | 0:9457949d5d00 | 136 | 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 | 137 | |
hajesusrodrigues | 0:9457949d5d00 | 138 | senderTimer.stop(); |
hajesusrodrigues | 0:9457949d5d00 | 139 | senderTimer.reset(); |
hajesusrodrigues | 0:9457949d5d00 | 140 | |
hajesusrodrigues | 0:9457949d5d00 | 141 | ackTimer.stop(); |
hajesusrodrigues | 0:9457949d5d00 | 142 | ackTimer.reset(); |
hajesusrodrigues | 0:9457949d5d00 | 143 | |
hajesusrodrigues | 0:9457949d5d00 | 144 | rfm12b_sender_led = 1; |
hajesusrodrigues | 0:9457949d5d00 | 145 | wait(0.2); |
hajesusrodrigues | 0:9457949d5d00 | 146 | rfm12b_sender_led = 0; |
hajesusrodrigues | 0:9457949d5d00 | 147 | |
hajesusrodrigues | 0:9457949d5d00 | 148 | (i >= 60) ? i = 0 : i++; |
hajesusrodrigues | 0:9457949d5d00 | 149 | |
hajesusrodrigues | 0:9457949d5d00 | 150 | wait(20); |
hajesusrodrigues | 0:9457949d5d00 | 151 | } |
hajesusrodrigues | 0:9457949d5d00 | 152 | } |
hajesusrodrigues | 0:9457949d5d00 | 153 | |
hajesusrodrigues | 1:e30050868e55 | 154 | int main() |
hajesusrodrigues | 1:e30050868e55 | 155 | { |
hajesusrodrigues | 0:9457949d5d00 | 156 | |
hajesusrodrigues | 0:9457949d5d00 | 157 | #if TEST_TYPE == MBED_TO_MBED |
hajesusrodrigues | 0:9457949d5d00 | 158 | |
hajesusrodrigues | 0:9457949d5d00 | 159 | printf("TEST TYPE: MBED_TO_MBED \n"); |
hajesusrodrigues | 0:9457949d5d00 | 160 | Thread b_thread(ReceiverThread); |
hajesusrodrigues | 0:9457949d5d00 | 161 | printf("Receive thread \n"); |
hajesusrodrigues | 0:9457949d5d00 | 162 | wait(20); |
hajesusrodrigues | 0:9457949d5d00 | 163 | Thread a_thread(SenderThread); |
hajesusrodrigues | 0:9457949d5d00 | 164 | printf("Send thread \n"); |
hajesusrodrigues | 0:9457949d5d00 | 165 | |
hajesusrodrigues | 0:9457949d5d00 | 166 | #elif TEST_TYPE == MBED_TO_ARDUINO |
hajesusrodrigues | 0:9457949d5d00 | 167 | |
hajesusrodrigues | 0:9457949d5d00 | 168 | printf("TEST TYPE: MBED_TO_ARDUINO \n"); |
hajesusrodrigues | 0:9457949d5d00 | 169 | Thread a_thread(SenderThread); |
hajesusrodrigues | 0:9457949d5d00 | 170 | printf("Send thread \n"); |
hajesusrodrigues | 0:9457949d5d00 | 171 | |
hajesusrodrigues | 0:9457949d5d00 | 172 | #elif TEST_TYPE == ARDUINO_TO_MBED |
hajesusrodrigues | 0:9457949d5d00 | 173 | |
hajesusrodrigues | 0:9457949d5d00 | 174 | printf("TEST TYPE: MBED_TO_ARDUINO \n"); |
hajesusrodrigues | 0:9457949d5d00 | 175 | Thread b_thread(ReceiverThread); |
hajesusrodrigues | 0:9457949d5d00 | 176 | printf("Receive thread \n"); |
hajesusrodrigues | 0:9457949d5d00 | 177 | |
hajesusrodrigues | 0:9457949d5d00 | 178 | #endif |
hajesusrodrigues | 0:9457949d5d00 | 179 | |
hajesusrodrigues | 0:9457949d5d00 | 180 | while (true) { |
hajesusrodrigues | 0:9457949d5d00 | 181 | activity_ledOn = 1; |
hajesusrodrigues | 0:9457949d5d00 | 182 | activity_ledOff = 0; |
hajesusrodrigues | 0:9457949d5d00 | 183 | wait(0.2); |
hajesusrodrigues | 0:9457949d5d00 | 184 | activity_ledOn = 0; |
hajesusrodrigues | 0:9457949d5d00 | 185 | activity_ledOff = 1; |
hajesusrodrigues | 0:9457949d5d00 | 186 | wait(0.2); |
hajesusrodrigues | 1:e30050868e55 | 187 | |
hajesusrodrigues | 0:9457949d5d00 | 188 | if(pc.readable()) { |
hajesusrodrigues | 0:9457949d5d00 | 189 | if (pc.getc() == 'r') { |
hajesusrodrigues | 0:9457949d5d00 | 190 | mbed_reset(); |
hajesusrodrigues | 0:9457949d5d00 | 191 | } |
hajesusrodrigues | 0:9457949d5d00 | 192 | |
hajesusrodrigues | 0:9457949d5d00 | 193 | } |
hajesusrodrigues | 0:9457949d5d00 | 194 | } |
hajesusrodrigues | 0:9457949d5d00 | 195 | } |