Update PiGateway example

This commit is contained in:
LowPowerLab 2016-08-16 09:30:31 -04:00
parent 8e2fc296ff
commit 742a750cc4
2 changed files with 103 additions and 81 deletions

View File

@ -1,34 +1,48 @@
// ********************************************************************************************************** // **********************************************************************************************************
// GarageMote garage door controller base receiver sketch that works with Moteinos equipped with HopeRF RFM69W/RFM69HW // Moteino gateway/base sketch that works with Moteinos equipped with HopeRF RFM69 transceivers (W/CW/HW/HCW)
// Can be adapted to use Moteinos using RFM12B // This is a basic gateway sketch that receives packets from end node Moteinos, formats them as ASCII strings
// This is the sketch for the base, not the controller itself, and meant as another example on how to use a // with the end node [ID] and passes them to Pi/host computer via serial port
// Moteino as a gateway/base/receiver // (ex: "messageFromNode" from node 123 gets passed to serial as "[123] messageFromNode")
// 2014-07-14 (C) felix@lowpowerlab.com, http://www.LowPowerLab.com // 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 // License: GPL 3.0, please see the License.txt file in library for details
// You are free to use/extend this code/library but please abide with the CCSA license: // https://www.gnu.org/licenses/gpl-3.0.en.html
// http://creativecommons.org/licenses/by-sa/4.0/ // **********************************************************************************************************
// ********************************************************************************** #include <RFM69.h> //get it here: https://github.com/lowpowerlab/rfm69
#include <RFM69_ATC.h> //get it here: https://github.com/lowpowerlab/RFM69
#include <RFM69.h> //get it here: http://github.com/lowpowerlab/rfm69 #include <SPIFlash.h> //get it here: https://github.com/lowpowerlab/spiflash
#include <SPIFlash.h> //get it here: http://github.com/lowpowerlab/spiflash
#include <WirelessHEX69.h> //get it here: https://github.com/LowPowerLab/WirelessProgramming #include <WirelessHEX69.h> //get it here: https://github.com/LowPowerLab/WirelessProgramming
#include <SPI.h> //comes with Arduino IDE (www.arduino.cc) #include <SPI.h> //already included in ArduinoIDE (www.arduino.cc)
//***************************************************************************************************************************** //*****************************************************************************************************************************
// ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION! // ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION!
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define NODEID 1 #define NODEID 1 //the ID of this node
#define NETWORKID 200 #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 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 IS_RFM69HW //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define LED 9
#define FLASH_CS 8 #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
#define SERIAL_BAUD 19200 #define ATC_RSSI -75 //target RSSI for RFM69_ATC (recommended > -80)
#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 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 #ifdef SERIAL_EN
#define DEBUG(input) {Serial.print(input); delay(1);} #define DEBUG(input) {Serial.print(input); delay(1);}
@ -38,8 +52,13 @@
#define DEBUGln(input); #define DEBUGln(input);
#endif #endif
RFM69 radio; #ifdef ENABLE_ATC
SPIFlash flash(FLASH_CS, 0xEF30); //EF40 for 16mbit windbond chip RFM69_ATC radio;
#else
RFM69 radio;
#endif
SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond FlashMEM chip
void setup() { void setup() {
Serial.begin(SERIAL_BAUD); Serial.begin(SERIAL_BAUD);
@ -49,15 +68,23 @@ void setup() {
radio.setHighPower(); //uncomment only for RFM69HW! radio.setHighPower(); //uncomment only for RFM69HW!
#endif #endif
radio.encrypt(ENCRYPTKEY); radio.encrypt(ENCRYPTKEY);
#ifdef ENABLE_ATC
radio.enableAutoPower(ATC_RSSI);
#endif
char buff[50]; 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); DEBUGln(buff);
if (flash.initialize()) if (flash.initialize())
{ {
DEBUGln("SPI Flash Init OK!"); DEBUGln("SPI Flash Init OK!");
} }
else else
DEBUGln("SPI Flash Init FAIL! (is chip present?)"); {
DEBUGln("SPI FlashMEM not found (is chip onboard?)");
}
} }
byte ackCount=0; byte ackCount=0;
@ -65,6 +92,7 @@ byte inputLen=0;
char input[64]; char input[64];
byte buff[61]; byte buff[61];
String inputstr; String inputstr;
void loop() { void loop() {
inputLen = readSerialLine(input); inputLen = readSerialLine(input);
inputstr = String(input); inputstr = String(input);
@ -80,10 +108,14 @@ void loop() {
byte targetId = inputstr.toInt(); //extract ID if any byte targetId = inputstr.toInt(); //extract ID if any
byte colonIndex = inputstr.indexOf(":"); //find position of first colon 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) if (targetId > 0 && targetId != NODEID && targetId != RF69_BROADCAST_ADDR && colonIndex>0 && colonIndex<4 && inputstr.length()>0)
{ {
inputstr.getBytes(buff, 61); inputstr.getBytes(buff, 61);
//DEBUGln((char*)buff); //DEBUGln((char*)buff);
//DEBUGln(targetId); //DEBUGln(targetId);
@ -128,19 +160,3 @@ void Blink(byte PIN, int DELAY_MS)
delay(DELAY_MS); delay(DELAY_MS);
digitalWrite(PIN,LOW); 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;
//}

View File

@ -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 // Moteino gateway/base sketch that works with Moteinos equipped with HopeRF RFM69 transceivers (W/CW/HW/HCW)
// You are free to use/extend this code/library but please abide with the CCSA license: // This sketch uses a 2x20 newhaven RGB backlight LCD to display incoming messages from end nodes
// http://creativecommons.org/licenses/by-sa/4.0/ // 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 <RFM69.h> //get it here: http://github.com/lowpowerlab/rfm69 #include <RFM69.h> //get it here: http://github.com/lowpowerlab/rfm69
#include <SPIFlash.h> //get it here: http://github.com/lowpowerlab/spiflash #include <SPIFlash.h> //get it here: http://github.com/lowpowerlab/spiflash
@ -20,18 +24,29 @@
//***************************************************************************************************************************** //*****************************************************************************************************************************
// ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION! // ADJUST THE SETTINGS BELOW DEPENDING ON YOUR HARDWARE/SITUATION!
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define NODEID 1 #define NODEID 1 //the ID of this node
#define NETWORKID 200 #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 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 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 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 #ifdef SERIAL_EN
#define DEBUG(input) {Serial.print(input); delay(1);} #define DEBUG(input) {Serial.print(input); delay(1);}
@ -42,7 +57,7 @@
#endif #endif
RFM69 radio; RFM69 radio;
SPIFlash flash(FLASH_CS, 0xEF30); //EF40 for 16mbit windbond chip SPIFlash flash(FLASH_SS, 0xEF30); //EF40 for 16mbit windbond chip
//initialize LCD //initialize LCD
ST7036 lcd = ST7036(2, 20, 0x78, BACKLIGHTPIN); //row count, column count, I2C addr, pin for backlight PWM 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); Serial.begin(SERIAL_BAUD);
delay(10); delay(10);
radio.initialize(FREQUENCY,NODEID,NETWORKID); radio.initialize(FREQUENCY,NODEID,NETWORKID);
#ifdef IS_RFM69HW #ifdef IS_RFM69HW
radio.setHighPower(); //uncomment only for RFM69HW! radio.setHighPower(); //uncomment only for RFM69HW!
#endif #endif
radio.encrypt(ENCRYPTKEY); radio.encrypt(ENCRYPTKEY);
char buff[50]; 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); DEBUGln(buff);
if (flash.initialize()) if (flash.initialize())
{ {
DEBUGln("SPI Flash Init OK!"); DEBUGln("SPI Flash Init OK!");
} }
else else
DEBUGln("SPI Flash Init FAIL! (is chip present?)"); {
DEBUGln("SPI FlashMEM not found (is chip onboard?)");
}
lcd.init(); lcd.init();
lcd.setContrast(10); lcd.setContrast(10);
@ -84,6 +105,7 @@ char LO[20];
char BAT[20]; char BAT[20];
char temp[25]; char temp[25];
String inputstr; String inputstr;
void loop() { void loop() {
inputLen = readSerialLine(input, 10, 64, 10); //readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=10); inputLen = readSerialLine(input, 10, 64, 10); //readSerialLine(char* input, char endOfLineChar=10, byte maxLength=64, uint16_t timeout=10);
inputstr = String(input); inputstr = String(input);
@ -167,19 +189,3 @@ void Blink(byte PIN, int DELAY_MS)
delay(DELAY_MS); delay(DELAY_MS);
digitalWrite(PIN,LOW); 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;
//}