![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test program for RFM69 library. Sends time,temperature and RSSI to "RFM69_listener"
Sender.cpp@3:450e37e0ffd6, 2015-02-26 (annotated)
- Committer:
- br549
- Date:
- Thu Feb 26 14:49:35 2015 +0000
- Revision:
- 3:450e37e0ffd6
- Parent:
- 2:2143c1d1f460
Add Compile check for various Mbeds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
br549 | 0:43464f6d23ee | 1 | #include "mbed.h" |
br549 | 0:43464f6d23ee | 2 | |
br549 | 0:43464f6d23ee | 3 | // Code from TNode/TGateway @ Anarduino.com |
br549 | 0:43464f6d23ee | 4 | // NOTE: code sample was inspired from ideas gained from: https://github.com/aanon4/RFM69 |
br549 | 0:43464f6d23ee | 5 | // |
br549 | 0:43464f6d23ee | 6 | // Sender - periodically send time(millis), radio temperature, RSSI to listener... |
br549 | 0:43464f6d23ee | 7 | // 2014 - anarduino.com |
br549 | 0:43464f6d23ee | 8 | // |
br549 | 0:43464f6d23ee | 9 | #include <RFM69.h> |
br549 | 0:43464f6d23ee | 10 | |
br549 | 0:43464f6d23ee | 11 | #define GATEWAY_ID 1 |
br549 | 2:2143c1d1f460 | 12 | #define NODE_ID 9 // node ID |
br549 | 0:43464f6d23ee | 13 | #define NETWORKID 101 //the same on all nodes that talk to each other |
br549 | 0:43464f6d23ee | 14 | #define MSG_INTERVAL 100 |
br549 | 0:43464f6d23ee | 15 | |
br549 | 0:43464f6d23ee | 16 | // Uncomment only one of the following three to match radio frequency |
br549 | 0:43464f6d23ee | 17 | //#define FREQUENCY RF69_433MHZ |
br549 | 0:43464f6d23ee | 18 | //#define FREQUENCY RF69_868MHZ |
br549 | 0:43464f6d23ee | 19 | #define FREQUENCY RF69_915MHZ |
br549 | 0:43464f6d23ee | 20 | |
br549 | 2:2143c1d1f460 | 21 | #define IS_RFM69HW //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW |
br549 | 0:43464f6d23ee | 22 | #define ENCRYPT_KEY "EncryptKey123456" // use same 16byte encryption key for all devices on net |
br549 | 0:43464f6d23ee | 23 | #define ACK_TIME 50 // max msec for ACK wait |
br549 | 0:43464f6d23ee | 24 | #define LED 9 // Anardino miniWireless has LEDs on D9 |
br549 | 0:43464f6d23ee | 25 | #define SERIAL_BAUD 115200 |
br549 | 0:43464f6d23ee | 26 | #define VERSION "1.0" |
br549 | 0:43464f6d23ee | 27 | |
br549 | 0:43464f6d23ee | 28 | #define MSGBUFSIZE 64 // message buffersize, but for this demo we only use: |
br549 | 0:43464f6d23ee | 29 | // 1-byte NODEID + 4-bytes for time + 1-byte for temp in C + 2-bytes for vcc(mV) |
br549 | 0:43464f6d23ee | 30 | char msgBuf[MSGBUFSIZE]; |
br549 | 0:43464f6d23ee | 31 | |
br549 | 0:43464f6d23ee | 32 | #ifdef TARGET_NUCLEO_F401RE |
br549 | 0:43464f6d23ee | 33 | Serial pc(USBTX, USBRX); |
br549 | 3:450e37e0ffd6 | 34 | DigitalOut myled(D9); //"Moteino Half Shield" has LED on D9 |
br549 | 0:43464f6d23ee | 35 | //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int) |
br549 | 0:43464f6d23ee | 36 | RFM69 radio(D11,D12,D13,D10,D8); |
br549 | 0:43464f6d23ee | 37 | #elif defined(TARGET_NUCLEO_L152RE) |
br549 | 0:43464f6d23ee | 38 | Serial pc(USBTX, USBRX); |
br549 | 3:450e37e0ffd6 | 39 | DigitalOut myled(D9); //"Moteino Half Shield" has LED on D9 |
br549 | 0:43464f6d23ee | 40 | //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int) |
br549 | 0:43464f6d23ee | 41 | RFM69 radio(D11,D12,D13,D10,D8); |
br549 | 0:43464f6d23ee | 42 | #elif defined(TARGET_LPC1114) |
br549 | 0:43464f6d23ee | 43 | Serial pc(USBTX, USBRX); |
br549 | 3:450e37e0ffd6 | 44 | DigitalOut myled(LED1); |
br549 | 0:43464f6d23ee | 45 | //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int) |
br549 | 0:43464f6d23ee | 46 | RFM69 radio(dp2,dp1,dp6,dp4,dp9); |
br549 | 0:43464f6d23ee | 47 | #elif defined(TARGET_LPC1768) |
br549 | 0:43464f6d23ee | 48 | Serial pc(USBTX, USBRX); |
br549 | 3:450e37e0ffd6 | 49 | DigitalOut myled(D9); //"Moteino Half Shield" has LED on D9 |
br549 | 0:43464f6d23ee | 50 | //RFM69::RFM69(PinName PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int) |
br549 | 2:2143c1d1f460 | 51 | RFM69 radio(D11,D12,D13,D10,D8); // Seeedstudio Arch Pro |
br549 | 2:2143c1d1f460 | 52 | //RFM69 radio(p5, p6, p7, p10, p9,p8) // Mbed 1768 ? |
br549 | 0:43464f6d23ee | 53 | #endif |
br549 | 0:43464f6d23ee | 54 | |
br549 | 0:43464f6d23ee | 55 | bool promiscuousMode = false; // set 'true' to sniff all packets on the same network |
br549 | 0:43464f6d23ee | 56 | bool requestACK=false; |
br549 | 0:43464f6d23ee | 57 | Timer tmr; |
br549 | 0:43464f6d23ee | 58 | |
br549 | 0:43464f6d23ee | 59 | main() { |
br549 | 0:43464f6d23ee | 60 | memset(msgBuf,0,sizeof(msgBuf)); |
br549 | 0:43464f6d23ee | 61 | int i=1; |
br549 | 0:43464f6d23ee | 62 | long l; |
br549 | 0:43464f6d23ee | 63 | tmr.start(); |
br549 | 0:43464f6d23ee | 64 | |
br549 | 0:43464f6d23ee | 65 | pc.baud(SERIAL_BAUD); |
br549 | 0:43464f6d23ee | 66 | pc.printf("Sender %s startup at %d Mhz...\r\n",VERSION,(FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915)); |
br549 | 0:43464f6d23ee | 67 | wait(2); |
br549 | 0:43464f6d23ee | 68 | radio.initialize(FREQUENCY, NODE_ID, NETWORKID); |
br549 | 0:43464f6d23ee | 69 | radio.encrypt(0); |
br549 | 2:2143c1d1f460 | 70 | radio.setPowerLevel(20); |
br549 | 0:43464f6d23ee | 71 | radio.promiscuous(promiscuousMode); |
br549 | 0:43464f6d23ee | 72 | #ifdef IS_RFM69HW |
br549 | 0:43464f6d23ee | 73 | radio.setHighPower(); //uncomment #define ONLY if radio is of type: RFM69HW or RFM69HCW |
br549 | 0:43464f6d23ee | 74 | #endif |
br549 | 0:43464f6d23ee | 75 | msgBuf[0] = (uint8_t)NODE_ID; // load NODEID |
br549 | 0:43464f6d23ee | 76 | |
br549 | 0:43464f6d23ee | 77 | while(1) { |
br549 | 0:43464f6d23ee | 78 | uint8_t tempC = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient |
br549 | 0:43464f6d23ee | 79 | uint8_t tempF = 1.8 * tempC + 32; // 9/5=1.8 |
br549 | 0:43464f6d23ee | 80 | |
br549 | 0:43464f6d23ee | 81 | l = tmr.read_ms(); // load time |
br549 | 0:43464f6d23ee | 82 | |
br549 | 0:43464f6d23ee | 83 | sprintf((char*)msgBuf,"#%d, t=%Lu, temp=%dF, RSSI=%d ",i,l,tempF,radio.RSSI); |
br549 | 0:43464f6d23ee | 84 | if(radio.sendWithRetry((uint8_t)GATEWAY_ID, msgBuf,strlen(msgBuf),true)) |
br549 | 0:43464f6d23ee | 85 | pc.printf("Packet %d sent, Ack ok!\r\n",i++); |
br549 | 0:43464f6d23ee | 86 | else pc.printf("Packet %d sent, no Ack!\r\n",i++); |
br549 | 0:43464f6d23ee | 87 | wait_ms(MSG_INTERVAL); |
br549 | 0:43464f6d23ee | 88 | myled = !myled; |
br549 | 0:43464f6d23ee | 89 | } |
br549 | 0:43464f6d23ee | 90 | } |