Test program for RFM69 library. Sends time,temperature and RSSI to "RFM69_listener"

Dependencies:   mbed RFM69

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Sender.cpp Source File

Sender.cpp

00001 // Code from TNode/TGateway @ Anarduino.com
00002 // NOTE: code sample was inspired from ideas gained from: https://github.com/aanon4/RFM69
00003 //
00004 // Sender - periodically send time(millis), radio temperature, RSSI to listener...
00005 // 2014 - anarduino.com
00006 //
00007 #include "RFM69.h"
00008 #include "mbed.h"
00009 
00010 #define GATEWAY_ID    1
00011 #define NODE_ID       41    // node ID
00012 #define NETWORKID     101    //the same on all nodes that talk to each other
00013 #define MSG_INTERVAL  100
00014 
00015 // Uncomment only one of the following three to match radio frequency
00016 //#define FREQUENCY     RF69_433MHZ    
00017 //#define FREQUENCY     RF69_868MHZ
00018 #define FREQUENCY     RF69_915MHZ
00019 
00020 #define IS_RFM69HW   //NOTE: uncomment this ONLY for RFM69HW or RFM69HCW
00021 #define ENCRYPT_KEY    "EncryptKey123456" 
00022 #define ACK_TIME       50                  
00023 #define LED            PC_13                   
00024 #define SERIAL_BAUD    115200
00025 #define VERSION  "1.0"
00026 
00027 #define MSGBUFSIZE 64   
00028 
00029 char msgBuf[MSGBUFSIZE];
00030 
00031 Serial pc(PA_2, PA_3);
00032 DigitalOut myled(PC_13);             //"Moteino Half Shield" has LED on D9
00033 //RFM69::RFM69(PinName  PinName mosi, PinName miso, PinName sclk,slaveSelectPin, PinName int)
00034 RFM69 radio(PB_15, PB_14, PB_13, PB_12, PA_8);
00035 
00036 bool promiscuousMode = false; // set 'true' to sniff all packets on the same network
00037 bool requestACK = false;
00038 Timer tmr;
00039 
00040 main() {
00041   memset(msgBuf,0,sizeof(msgBuf));
00042   int i=1;
00043   long l;
00044   tmr.start();
00045 
00046   pc.baud(SERIAL_BAUD);
00047   pc.printf("Sender %s startup at %d Mhz...\r\n",VERSION,(FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915));
00048   wait(2);
00049   radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
00050   radio.encrypt(0);
00051   radio.setPowerLevel(20);
00052   radio.promiscuous(promiscuousMode);
00053 #ifdef IS_RFM69HW
00054   radio.setHighPower(); //uncomment #define ONLY if radio is of type: RFM69HW or RFM69HCW 
00055 #endif
00056   msgBuf[0] = (uint8_t)NODE_ID;  // load NODEID
00057 
00058 while(1) {
00059   uint8_t tempC =  radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient
00060   uint8_t tempF = 1.8 * tempC + 32; // 9/5=1.8
00061  
00062   l = tmr.read_ms();  // load time
00063 
00064   sprintf((char*)msgBuf,"#%d, t=%Lu, temp=%dF, RSSI=%d ",i,l,tempF,radio.RSSI); 
00065    if(radio.sendWithRetry((uint8_t)GATEWAY_ID, msgBuf,strlen(msgBuf),true))
00066             pc.printf("Packet %d sent, Ack ok!\r\n",i++);
00067        else pc.printf("Packet %d sent, no Ack!\r\n",i++);
00068   wait_ms(MSG_INTERVAL);
00069   myled = !myled;
00070   }
00071 }