From 742a750cc44611bac78d03943f8df718a2a137e6 Mon Sep 17 00:00:00 2001 From: LowPowerLab Date: Tue, 16 Aug 2016 09:30:31 -0400 Subject: [PATCH] Update PiGateway example --- Examples/PiGateway/PiGateway.ino | 104 +++++++++++++---------- Examples/PiGateway/PiGateway_withLCD.ino | 80 +++++++++-------- 2 files changed, 103 insertions(+), 81 deletions(-) diff --git a/Examples/PiGateway/PiGateway.ino b/Examples/PiGateway/PiGateway.ino index e681d44..e078d16 100644 --- a/Examples/PiGateway/PiGateway.ino +++ b/Examples/PiGateway/PiGateway.ino @@ -1,34 +1,48 @@ // ********************************************************************************************************** -// GarageMote garage door controller base receiver sketch that works with Moteinos equipped with HopeRF RFM69W/RFM69HW -// Can be adapted to use Moteinos using RFM12B -// This is the sketch for the base, not the controller itself, and meant as another example on how to use a -// Moteino as a gateway/base/receiver -// 2014-07-14 (C) felix@lowpowerlab.com, http://www.LowPowerLab.com +// Moteino gateway/base sketch that works with Moteinos equipped with HopeRF RFM69 transceivers (W/CW/HW/HCW) +// This is a basic gateway sketch that receives packets from end node Moteinos, formats them as ASCII strings +// with the end node [ID] and passes them to Pi/host computer via serial port +// (ex: "messageFromNode" from node 123 gets passed to serial as "[123] messageFromNode") +// It also listens to serial messages that should be sent to listening end nodes +// (ex: "123:messageToNode" sends "messageToNode" to node 123) +// Make sure to adjust the settings to match your transceiver settings (frequency, HW etc). +// 2016 (C) Felix Rusu, http://www.LowPowerLab.com // ********************************************************************************************************** -// Creative Commons Attrib Share-Alike License -// You are free to use/extend this code/library but please abide with the CCSA license: -// http://creativecommons.org/licenses/by-sa/4.0/ -// ********************************************************************************** - -#include //get it here: http://github.com/lowpowerlab/rfm69 -#include //get it here: http://github.com/lowpowerlab/spiflash +// License: GPL 3.0, please see the License.txt file in library for details +// https://www.gnu.org/licenses/gpl-3.0.en.html +// ********************************************************************************************************** +#include //get it here: https://github.com/lowpowerlab/rfm69 +#include //get it here: https://github.com/lowpowerlab/RFM69 +#include //get it here: https://github.com/lowpowerlab/spiflash #include //get it here: https://github.com/LowPowerLab/WirelessProgramming -#include //comes with Arduino IDE (www.arduino.cc) +#include //already included in ArduinoIDE (www.arduino.cc) //***************************************************************************************************************************** // ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION! //***************************************************************************************************************************** -#define NODEID 1 -#define NETWORKID 200 +#define NODEID 1 //the ID of this node +#define NETWORKID 200 //the network ID of all nodes this node listens/talks to #define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ) -#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less! +#define ENCRYPTKEY "sampleEncryptKey" //identical 16 characters/bytes on all nodes, not more not less! #define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! -#define LED 9 -#define FLASH_CS 8 -#define SERIAL_BAUD 19200 -#define SERIAL_EN //comment out if you don't want any serial verbose output -#define ACK_TIME 30 // # of ms to wait for an ack + +#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL +#define ATC_RSSI -75 //target RSSI for RFM69_ATC (recommended > -80) + +#define ACK_TIME 30 // # of ms to wait for an ack packet //***************************************************************************************************************************** +// Serial baud rate must match your Pi/host computer serial port baud rate! +#define SERIAL_EN //comment out if you don't want any serial verbose output +#define SERIAL_BAUD 19200 +//***************************************************************************************************************************** + +#ifdef __AVR_ATmega1284P__ + #define LED 15 // Moteino MEGAs have LEDs on D15 + #define FLASH_SS 23 // and FLASH SS on D23 +#else + #define LED 9 // Moteinos have LEDs on D9 + #define FLASH_SS 8 // and FLASH SS on D8 +#endif #ifdef SERIAL_EN #define DEBUG(input) {Serial.print(input); delay(1);} @@ -38,8 +52,13 @@ #define DEBUGln(input); #endif -RFM69 radio; -SPIFlash flash(FLASH_CS, 0xEF30); //EF40 for 16mbit windbond chip +#ifdef ENABLE_ATC + RFM69_ATC radio; +#else + RFM69 radio; +#endif + +SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond FlashMEM chip void setup() { Serial.begin(SERIAL_BAUD); @@ -49,15 +68,23 @@ void setup() { radio.setHighPower(); //uncomment only for RFM69HW! #endif radio.encrypt(ENCRYPTKEY); + +#ifdef ENABLE_ATC + radio.enableAutoPower(ATC_RSSI); +#endif + char buff[50]; - sprintf(buff, "\nListening at %d Mhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); + sprintf(buff, "\nTransmitting at %d Mhz...", radio.getFrequency()/1000000); DEBUGln(buff); + if (flash.initialize()) { DEBUGln("SPI Flash Init OK!"); } else - DEBUGln("SPI Flash Init FAIL! (is chip present?)"); + { + DEBUGln("SPI FlashMEM not found (is chip onboard?)"); + } } byte ackCount=0; @@ -65,6 +92,7 @@ byte inputLen=0; char input[64]; byte buff[61]; String inputstr; + void loop() { inputLen = readSerialLine(input); inputstr = String(input); @@ -80,10 +108,14 @@ void loop() { byte targetId = inputstr.toInt(); //extract ID if any byte colonIndex = inputstr.indexOf(":"); //find position of first colon - if (targetId > 0) inputstr = inputstr.substring(colonIndex+1); //trim "ID:" if any + + if (targetId > 0) + { + inputstr = inputstr.substring(colonIndex+1); //trim "ID:" if any + } + if (targetId > 0 && targetId != NODEID && targetId != RF69_BROADCAST_ADDR && colonIndex>0 && colonIndex<4 && inputstr.length()>0) { - inputstr.getBytes(buff, 61); //DEBUGln((char*)buff); //DEBUGln(targetId); @@ -127,20 +159,4 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); -} - -//readSerialLine already defined in WirelessHEX69 -// reads a line feed (\n) terminated line from the serial stream -// returns # of bytes read, up to 255 -// timeout in ms, will timeout and return after so long -//byte readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=10); -//byte readSerialLine(char* input, char endOfLineChar, byte maxLength, uint16_t timeout) -//{ -// byte inputLen = 0; -// Serial.setTimeout(timeout); -// inputLen = Serial.readBytesUntil(endOfLineChar, input, maxLength); -// input[inputLen]=0;//null-terminate it -// Serial.setTimeout(0); -// //Serial.println(); -// return inputLen; -//} +} \ No newline at end of file diff --git a/Examples/PiGateway/PiGateway_withLCD.ino b/Examples/PiGateway/PiGateway_withLCD.ino index 4d3759e..004283f 100644 --- a/Examples/PiGateway/PiGateway_withLCD.ino +++ b/Examples/PiGateway/PiGateway_withLCD.ino @@ -1,13 +1,17 @@ -// GarageMote garage door controller base receiver sketch that works with Moteinos equipped with HopeRF RFM69W/RFM69HW -// Can be adapted to use Moteinos using RFM12B -// This is the sketch for the base, not the controller itself, and meant as another example on how to use a -// Moteino as a gateway/base/receiver -// 2014-07-14 (C) felix@lowpowerlab.com, http://www.LowPowerLab.com // ********************************************************************************************************** -// Creative Commons Attrib Share-Alike License -// You are free to use/extend this code/library but please abide with the CCSA license: -// http://creativecommons.org/licenses/by-sa/4.0/ -// ********************************************************************************** +// Moteino gateway/base sketch that works with Moteinos equipped with HopeRF RFM69 transceivers (W/CW/HW/HCW) +// This sketch uses a 2x20 newhaven RGB backlight LCD to display incoming messages from end nodes +// This is a basic gateway sketch that receives packets from end node Moteinos, formats them as ASCII strings +// with the end node [ID] and passes them to Pi/host computer via serial port +// (ex: "messageFromNode" from node 123 gets passed to serial as "[123] messageFromNode") +// It also listens to serial messages that should be sent to listening end nodes +// (ex: "123:messageToNode" sends "messageToNode" to node 123) +// Make sure to adjust the settings to match your transceiver settings (frequency, HW etc). +// 2014 (C) Felix Rusu, http://www.LowPowerLab.com +// ********************************************************************************************************** +// License: GPL 3.0, please see the License.txt file in library for details +// https://www.gnu.org/licenses/gpl-3.0.en.html +// ********************************************************************************************************** #include //get it here: http://github.com/lowpowerlab/rfm69 #include //get it here: http://github.com/lowpowerlab/spiflash @@ -20,18 +24,29 @@ //***************************************************************************************************************************** // ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION! //***************************************************************************************************************************** -#define NODEID 1 -#define NETWORKID 200 +#define NODEID 1 //the ID of this node +#define NETWORKID 200 //the network ID of all nodes this node listens/talks to #define FREQUENCY RF69_915MHZ //Match this with the version of your Moteino! (others: RF69_433MHZ, RF69_868MHZ) -#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less! +#define ENCRYPTKEY "sampleEncryptKey" //identical 16 characters/bytes on all nodes, not more not less! #define IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W! -#define LED 9 -#define FLASH_CS 8 -#define SERIAL_BAUD 19200 -#define SERIAL_EN //comment out if you don't want any serial verbose output + #define ACK_TIME 30 // # of ms to wait for an ack -#define BACKLIGHTPIN 5 //3=R,5=G,6=B + //***************************************************************************************************************************** +// Serial baud rate must match your Pi/host computer serial port baud rate! +#define SERIAL_EN //comment out if you don't want any serial verbose output +#define SERIAL_BAUD 19200 +//***************************************************************************************************************************** + +#define BACKLIGHTPIN 5 //3=R,5=G,6=B + +#ifdef __AVR_ATmega1284P__ + #define LED 15 // Moteino MEGAs have LEDs on D15 + #define FLASH_SS 23 // and FLASH SS on D23 +#else + #define LED 9 // Moteinos have LEDs on D9 + #define FLASH_SS 8 // and FLASH SS on D8 +#endif #ifdef SERIAL_EN #define DEBUG(input) {Serial.print(input); delay(1);} @@ -42,7 +57,7 @@ #endif RFM69 radio; -SPIFlash flash(FLASH_CS, 0xEF30); //EF40 for 16mbit windbond chip +SPIFlash flash(FLASH_SS, 0xEF30); //EF40 for 16mbit windbond chip //initialize LCD ST7036 lcd = ST7036(2, 20, 0x78, BACKLIGHTPIN); //row count, column count, I2C addr, pin for backlight PWM @@ -53,19 +68,25 @@ void setup() { Serial.begin(SERIAL_BAUD); delay(10); radio.initialize(FREQUENCY,NODEID,NETWORKID); + #ifdef IS_RFM69HW radio.setHighPower(); //uncomment only for RFM69HW! #endif radio.encrypt(ENCRYPTKEY); + char buff[50]; - sprintf(buff, "\nListening @ %dmhz...", FREQUENCY==RF69_433MHZ ? 433 : FREQUENCY==RF69_868MHZ ? 868 : 915); + sprintf(buff, "\nTransmitting at %d Mhz...", radio.getFrequency()/1000000); + DEBUGln(buff); + if (flash.initialize()) { DEBUGln("SPI Flash Init OK!"); } else - DEBUGln("SPI Flash Init FAIL! (is chip present?)"); + { + DEBUGln("SPI FlashMEM not found (is chip onboard?)"); + } lcd.init(); lcd.setContrast(10); @@ -84,6 +105,7 @@ char LO[20]; char BAT[20]; char temp[25]; String inputstr; + void loop() { inputLen = readSerialLine(input, 10, 64, 10); //readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=10); inputstr = String(input); @@ -166,20 +188,4 @@ void Blink(byte PIN, int DELAY_MS) digitalWrite(PIN,HIGH); delay(DELAY_MS); digitalWrite(PIN,LOW); -} - -//readSerialLine already defined in WirelessHEX69 -// reads a line feed (\n) terminated line from the serial stream -// returns # of bytes read, up to 255 -// timeout in ms, will timeout and return after so long -//byte readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=10); -//byte readSerialLine(char* input, char endOfLineChar, byte maxLength, uint16_t timeout) -//{ -// byte inputLen = 0; -// Serial.setTimeout(timeout); -// inputLen = Serial.readBytesUntil(endOfLineChar, input, maxLength); -// input[inputLen]=0;//null-terminate it -// Serial.setTimeout(0); -// //Serial.println(); -// return inputLen; -//} \ No newline at end of file +} \ No newline at end of file