From 8a4de491b80ee21176aee73d8aea011131b0fd9e Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Tue, 5 Nov 2013 22:02:01 -0500 Subject: [PATCH] Update Send/Receive examples --- Examples/Gateway/Gateway.ino | 36 +++++++++++++------- Examples/Node/Node.ino | 66 +++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 44 deletions(-) diff --git a/Examples/Gateway/Gateway.ino b/Examples/Gateway/Gateway.ino index 09396ae..4913c55 100644 --- a/Examples/Gateway/Gateway.ino +++ b/Examples/Gateway/Gateway.ino @@ -1,14 +1,24 @@ +// Sample RFM69 receiver/gateway sketch, with ACK and optional encryption +// Passes through any wireless received messages to the serial port & responds to ACKs +// It also looks for an onboard FLASH chip, if present +// Library and code by Felix Rusu - felix@lowpowerlab.com +// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/ + #include #include #include -#define NODEID 1 -#define NETWORKID 100 -#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ) -#define KEY "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less! -#define LED 9 -#define SERIAL_BAUD 115200 -#define ACK_TIME 30 // # of ms to wait for an ack +#define NODEID 1 //unique for each node on same network +#define NETWORKID 100 //the same on all nodes that talk to each other +//Match frequency to the hardware version of the radio on your Moteino (uncomment one): +#define FREQUENCY RF69_433MHZ +//#define FREQUENCY RF69_868MHZ +//#define FREQUENCY RF69_915MHZ +#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! +//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! +#define ACK_TIME 30 // max # of ms to wait for an ack +#define LED 9 // Moteinos have LEDs on D9 +#define SERIAL_BAUD 115200 RFM69 radio; SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip @@ -18,8 +28,10 @@ void setup() { Serial.begin(SERIAL_BAUD); delay(10); radio.initialize(FREQUENCY,NODEID,NETWORKID); - //radio.setHighPower(); //must uncomment for RFM69HW! - radio.encrypt(KEY); +#ifdef IS_RFM69HW + radio.setHighPower(); //uncomment only for RFM69HW! +#endif + radio.encrypt(ENCRYPTKEY); radio.promiscuous(promiscuousMode); char buff[50]; sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); @@ -39,7 +51,7 @@ void loop() { if (input == 'r') //d=dump all register values radio.readAllRegs(); if (input == 'E') //E=enable encryption - radio.encrypt(KEY); + radio.encrypt(ENCRYPTKEY); if (input == 'e') //e=disable encryption radio.encrypt(null); if (input == 'p') @@ -74,7 +86,7 @@ void loop() { word jedecid = flash.readDeviceId(); Serial.println(jedecid, HEX); } - if (input == 't') + if (input == 't') { byte temperature = radio.readTemperature(-1); // -1 = user cal factor, adjust for correct ambient byte fTemp = 1.8 * temperature + 32; // 9/5=1.8 @@ -129,4 +141,4 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); -} +} \ No newline at end of file diff --git a/Examples/Node/Node.ino b/Examples/Node/Node.ino index fae87b8..39d9903 100644 --- a/Examples/Node/Node.ino +++ b/Examples/Node/Node.ino @@ -1,18 +1,29 @@ +// Sample RFM69 sender/node sketch, with ACK and optional encryption +// Sends periodic messages of increasing length to gateway (id=1) +// It also looks for an onboard FLASH chip, if present +// Library and code by Felix Rusu - felix@lowpowerlab.com +// Get the RFM69 and SPIFlash library at: https://github.com/LowPowerLab/ + #include #include #include -#define NODEID 99 -#define NETWORKID 100 -#define GATEWAYID 1 -#define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ) -#define KEY "thisIsEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less! -#define LED 9 -#define SERIAL_BAUD 115200 -#define ACK_TIME 30 // # of ms to wait for an ack +#define NODEID 2 //unique for each node on same network +#define NETWORKID 100 //the same on all nodes that talk to each other +#define GATEWAYID 1 +//Match frequency to the hardware version of the radio on your Moteino (uncomment one): +#define FREQUENCY RF69_433MHZ +//#define FREQUENCY RF69_868MHZ +//#define FREQUENCY RF69_915MHZ +#define ENCRYPTKEY "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes! +//#define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! +#define ACK_TIME 30 // max # of ms to wait for an ack +#define LED 9 // Moteinos have LEDs on D9 +#define SERIAL_BAUD 115200 int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms) char payload[] = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; +char buff[20]; byte sendSize=0; boolean requestACK = false; SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip @@ -21,11 +32,14 @@ RFM69 radio; void setup() { Serial.begin(SERIAL_BAUD); radio.initialize(FREQUENCY,NODEID,NETWORKID); - //radio.setHighPower(); //must uncomment for RFM69HW! - radio.encrypt(KEY); +#ifdef IS_RFM69HW + radio.setHighPower(); //uncomment only for RFM69HW! +#endif + radio.encrypt(ENCRYPTKEY); char buff[50]; sprintf(buff, "\nTransmitting at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); Serial.println(buff); + if (flash.initialize()) Serial.println("SPI Flash Init OK!"); else @@ -99,6 +113,15 @@ void loop() { Serial.println(); } + //send FLASH id + if(sendSize==0) + { + sprintf(buff, "FLASH_MEM_ID:0x%X", flash.readDeviceId()); + byte buffLen=strlen(buff); + radio.sendWithRetry(GATEWAYID, buff, buffLen); + delay(TRANSMITPERIOD); + } + int currPeriod = millis()/TRANSMITPERIOD; if (currPeriod != lastPeriod) { @@ -113,17 +136,6 @@ void loop() { Serial.print(" ok!"); else Serial.print(" nothing..."); -// //manual ACK handling -// requestACK = ((sendSize % 3) == 0); //request ACK every 3rd xmission -// radio.send(GATEWAYID, payload, sendSize, requestACK); -// if (requestACK) -// { -// Serial.print(" - waiting for ACK..."); -// if (waitForAck(GATEWAYID)) Serial.print("ok!"); -// else Serial.print("nothing..."); -// } - - sendSize = (sendSize + 1) % 31; Serial.println(); Blink(LED,3); @@ -136,14 +148,4 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); -} - -//// wait a few milliseconds for proper ACK to me, return true if indeed received -//static bool waitForAck(byte theNodeID) { -// long now = millis(); -// while (millis() - now <= ACK_TIME) { -// if (radio.ACKReceived(theNodeID)) -// return true; -// } -// return false; -//} +} \ No newline at end of file