refactor Pi gateway examples

This commit is contained in:
Felix Rusu 2020-04-17 12:33:46 -04:00
parent 2b7fdbd8f6
commit bce10f3e39
3 changed files with 44 additions and 53 deletions

View File

@ -26,13 +26,13 @@
#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 FREQUENCY_EXACT 916000000 //uncomment and set to a specific frequency in Hz, if commented the center frequency is used //#define FREQUENCY_EXACT 916000000 //uncomment and set to a specific frequency in Hz, if commented the center frequency is used
#define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less! #define ENCRYPTKEY "sampleEncryptKey" //has to be same 16 characters/bytes on all nodes, not more not less!
#define IS_RFM69HCW //uncomment only for RFM69HW/HCW #define IS_RFM69HW_HCW //required for RFM69HW/HCW, comment out for RFM69W/CW!
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL //more here: http://lowpowerlab.com/blog/2015/11/11/rfm69_atc-automatic-transmission-control/ #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL //more here: http://lowpowerlab.com/blog/2015/11/11/rfm69_atc-automatic-transmission-control/
#define ENABLE_WIRELESS_PROGRAMMING //comment out this line to disable Wireless Programming of this gateway node #define ENABLE_WIRELESS_PROGRAMMING //comment out this line to disable Wireless Programming of this gateway node
#define ENABLE_LCD //comment this out if you don't have or don't want to use the LCD #define ENABLE_LCD //comment this out if you don't have or don't want to use the LCD
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define SERIAL_BAUD 115200 //use 115200 with PiGateway v9.1 or later, 19200 with v9.0 or prior #define SERIAL_BAUD 19200
//#define DEBUG_EN //comment out if you don't want any serial verbose output (keep out in real use) #define DEBUG_EN //comment out if you don't want any serial verbose output (keep out in real use)
#define BTN_LED_RED 9 #define BTN_LED_RED 9
#define BTN_LED_GRN 6 // This will indicate when Pi has power #define BTN_LED_GRN 6 // This will indicate when Pi has power
@ -566,10 +566,6 @@ void handle2Buttons()
if (backlightLevel==BACKLIGHTLEVELS) backlightLevel=0; if (backlightLevel==BACKLIGHTLEVELS) backlightLevel=0;
else backlightLevel++; else backlightLevel++;
LCD_BACKLIGHT(backlightLevel); LCD_BACKLIGHT(backlightLevel);
Pbuff="";
Pbuff << "LCDlight:" << (100*backlightLevel/BACKLIGHTLEVELS) << "/100";
saveToHistory(buff, 0);
refreshLCD();
#endif #endif
} }
@ -645,25 +641,21 @@ void setup() {
#ifdef ENCRYPTKEY #ifdef ENCRYPTKEY
radio.encrypt(ENCRYPTKEY); radio.encrypt(ENCRYPTKEY);
#endif #endif
#ifdef IS_RFM69HCW #ifdef IS_RFM69HW_HCW
radio.setHighPower(); radio.setHighPower();
#endif #endif
Pbuff=""; Pbuff="";
Pbuff << "Listening @ " << radio.getFrequency() << "Hz"; Pbuff << "Listening @ " << radio.getFrequency() << "Hz";
DEBUGln(buff); DEBUGln(buff);
if (flash.initialize()) DEBUGln("SPI Flash Init OK!"); if (flash.initialize()) DEBUGln("SPI_Flash_Init_OK");
else DEBUGln(F("SPI Flash MEM FAIL!")); else DEBUGln(F("SPI_Flash_Init_FAIL"));
#ifdef FREQUENCY_EXACT #ifdef FREQUENCY_EXACT
radio.setFrequency(FREQUENCY_EXACT); //set frequency to some custom frequency radio.setFrequency(FREQUENCY_EXACT); //set frequency to some custom frequency
#endif #endif
#ifdef ENABLE_ATC
DEBUGln(F("RFM69_ATC Enabled (Auto Transmission Control)"));
#endif
readBattery(); readBattery();
DEBUG(F("Free RAM bytes: "));DEBUG(freeRAM()); DEBUG(F("DEBUG:freeRAM():"));DEBUGln(freeRAM());
#ifdef ENABLE_LCD #ifdef ENABLE_LCD
pinMode(PIN_LCD_LIGHT, OUTPUT); //LCD backlight, LOW = backlight ON pinMode(PIN_LCD_LIGHT, OUTPUT); //LCD backlight, LOW = backlight ON
@ -861,7 +853,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
else aux=aux->next; else aux=aux->next;
} }
DEBUG("VOIDED commands: ");DEBUGln(removed); DEBUG("DEBUG:VOIDED_commands:");DEBUGln(removed);
size_of_queue = size_of_queue - removed; size_of_queue = size_of_queue - removed;
return; return;
} }
@ -881,12 +873,12 @@ void processCommand(char data[], boolean allowDuplicate=false) {
aux = queue; aux = queue;
while(aux != NULL) while(aux != NULL)
{ {
DEBUGln("While"); //DEBUGln("While");
if (aux->nodeId==targetId) if (aux->nodeId==targetId)
{ {
if (strcmp(aux->data, dataPart)==0) if (strcmp(aux->data, dataPart)==0)
{ {
DEBUGln(F("processCommand() skip (duplicate)")); //DEBUGln(F("processCommand() skip (duplicate)"));
return; return;
} }
} }
@ -897,15 +889,15 @@ void processCommand(char data[], boolean allowDuplicate=false) {
//all checks OK, attempt to add to queue //all checks OK, attempt to add to queue
if (insert(targetId, dataPart)) if (insert(targetId, dataPart))
{ {
DEBUG(F("-> inserted: ")); //DEBUG(F("-> inserted: "));
DEBUG(targetId); //DEBUG(targetId);
DEBUG("_"); //DEBUG("_");
DEBUGln(dataPart); //DEBUGln(dataPart);
size_of_queue++; size_of_queue++;
} }
else else
{ {
DEBUGln(F("INSERT FAIL - MEM FULL!")); DEBUGln(F("DEBUG:INSERT_FAIL:MEM_FULL"));
Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl; Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl;
} }
} }
@ -940,9 +932,8 @@ void handleSerialData() {
case '\n': case '\n':
if (input_pos==0) break; // ignore empty lines if (input_pos==0) break; // ignore empty lines
input_line[input_pos] = 0; // null terminate the string input_line[input_pos] = 0; // null terminate the string
DEBUG("Processing: ["); DEBUG("DEBUG:handleSerialData:");
DEBUG(input_line); DEBUGln(input_line);
DEBUGln("]");
processCommand(input_line); // fill up queue processCommand(input_line); // fill up queue
input_pos = 0; // reset buffer for next time input_pos = 0; // reset buffer for next time
break; break;
@ -955,7 +946,7 @@ void handleSerialData() {
} else { } else {
// if theres no EOL coming before MAX_BUFF_CHARS is exceeded we'll just terminate and send it, last char is then lost // if theres no EOL coming before MAX_BUFF_CHARS is exceeded we'll just terminate and send it, last char is then lost
input_line[input_pos] = 0; // null terminate the string input_line[input_pos] = 0; // null terminate the string
DEBUG("Attempting to add (default): "); DEBUG("DEBUG:MAX_BUFF_CHARS is exceeded - attempting to add (default): ");
DEBUGln(input_line); DEBUGln(input_line);
processCommand(input_line); //add to queue processCommand(input_line); //add to queue
input_pos = 0; //reset buffer for next line input_pos = 0; //reset buffer for next line

View File

@ -23,14 +23,14 @@
#define NETWORKID 200 //the network ID of all nodes this node listens/talks to #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" //identical 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_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW! #define IS_RFM69HW_HCW //required for RFM69HW/HCW, comment out for RFM69W/CW!
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
#define ATC_RSSI -90 //target RSSI for RFM69_ATC (recommended > -80) #define ATC_RSSI -90 //target RSSI for RFM69_ATC (recommended > -80)
//***************************************************************************************************************************** //*****************************************************************************************************************************
// Serial baud rate must match your Pi/host computer serial port baud rate! // Serial baud rate must match your Pi/host computer serial port baud rate!
#define DEBUG_EN //comment out if you don't want any serial verbose output #define DEBUG_EN //comment out if you don't want any serial verbose output
#define SERIAL_BAUD 115200 //use 115200 with PiGateway v9.1 or later, 19200 with v9.0 or prior #define SERIAL_BAUD 19200
//***************************************************************************************************************************** //*****************************************************************************************************************************
#ifdef DEBUG_EN #ifdef DEBUG_EN
#define DEBUG(input) {Serial.print(input);} #define DEBUG(input) {Serial.print(input);}
@ -268,7 +268,7 @@ void processCommand(char data[], boolean allowDuplicate=false) {
} }
else aux=aux->next; else aux=aux->next;
} }
DEBUG("VOIDED commands: ");DEBUGln(removed); DEBUG("DEBUG:VOIDED commands: ");DEBUGln(removed);
size_of_queue = size_of_queue - removed; size_of_queue = size_of_queue - removed;
return; return;
} }
@ -288,12 +288,12 @@ void processCommand(char data[], boolean allowDuplicate=false) {
aux = queue; aux = queue;
while(aux != NULL) while(aux != NULL)
{ {
DEBUGln("While"); //DEBUGln("While");
if (aux->nodeId==targetId) if (aux->nodeId==targetId)
{ {
if (strcmp(aux->data, dataPart)==0) if (strcmp(aux->data, dataPart)==0)
{ {
DEBUGln(F("processCommand() skip (duplicate)")); DEBUGln(F("DEBUG:processCommand() skip (duplicate)"));
return; return;
} }
} }
@ -304,15 +304,15 @@ void processCommand(char data[], boolean allowDuplicate=false) {
//all checks OK, attempt to add to queue //all checks OK, attempt to add to queue
if (insert(targetId, dataPart)) if (insert(targetId, dataPart))
{ {
DEBUG(F("-> inserted: ")); //DEBUG(F("-> inserted: "));
DEBUG(targetId); //DEBUG(targetId);
DEBUG("_"); //DEBUG("_");
DEBUGln(dataPart); //DEBUGln(dataPart);
size_of_queue++; size_of_queue++;
} }
else else
{ {
DEBUGln(F("INSERT FAIL - MEM FULL!")); DEBUGln(F("DEBUG:INSERT FAIL - MEM FULL!"));
Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl; Serial << F("[") << targetId << F("] ") << dataPart << F(":MEMFULL") << endl;
} }
} }
@ -347,9 +347,8 @@ void handleSerialData() {
case '\n': case '\n':
if (input_pos==0) break; // ignore empty lines if (input_pos==0) break; // ignore empty lines
input_line[input_pos] = 0; // null terminate the string input_line[input_pos] = 0; // null terminate the string
DEBUG("Processing: ["); DEBUG("DEBUG:handleSerialData:");
DEBUG(input_line); DEBUGln(input_line);
DEBUGln("]");
processCommand(input_line); // fill up queue processCommand(input_line); // fill up queue
input_pos = 0; // reset buffer for next time input_pos = 0; // reset buffer for next time
break; break;
@ -362,7 +361,7 @@ void handleSerialData() {
} else { } else {
// if theres no EOL coming before MAX_BUFF_CHARS is exceeded we'll just terminate and send it, last char is then lost // if theres no EOL coming before MAX_BUFF_CHARS is exceeded we'll just terminate and send it, last char is then lost
input_line[input_pos] = 0; // null terminate the string input_line[input_pos] = 0; // null terminate the string
DEBUG("Attempting to add (default): "); DEBUG("DEBUG:MAX_BUFF_CHARS is exceeded - attempting to add (default): ");
DEBUGln(input_line); DEBUGln(input_line);
processCommand(input_line); //add to queue processCommand(input_line); //add to queue
input_pos = 0; //reset buffer for next line input_pos = 0; //reset buffer for next line

View File

@ -20,7 +20,7 @@
#define NETWORKID 200 //the network ID of all nodes this node listens/talks to #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" //identical 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_HCW //uncomment only for RFM69HW/HCW! Leave out if you have RFM69W/CW! #define IS_RFM69HW_HCW //required for RFM69HW/HCW, comment out for RFM69W/CW!
#define ACK_TIME 30 // # of ms to wait for an ack packet #define ACK_TIME 30 // # of ms to wait for an ack packet
//***************************************************************************************************************************** //*****************************************************************************************************************************
#define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL #define ENABLE_ATC //comment out this line to disable AUTO TRANSMISSION CONTROL
@ -28,7 +28,7 @@
//***************************************************************************************************************************** //*****************************************************************************************************************************
// Serial baud rate must match your Pi/host computer serial port baud rate! // Serial baud rate must match your Pi/host computer serial port baud rate!
#define DEBUG_EN //comment out if you don't want any serial verbose output #define DEBUG_EN //comment out if you don't want any serial verbose output
#define SERIAL_BAUD 115200 //use 115200 with PiGateway v9.1 or later, 19200 with v9.0 or prior #define SERIAL_BAUD 19200
//***************************************************************************************************************************** //*****************************************************************************************************************************
#ifdef DEBUG_EN #ifdef DEBUG_EN
#define DEBUG(input) {Serial.print(input);} #define DEBUG(input) {Serial.print(input);}
@ -62,16 +62,16 @@ void setup() {
#endif #endif
char buff[50]; char buff[50];
sprintf(buff, "\nTransmitting at %d Mhz...", radio.getFrequency()/1000000); sprintf(buff, "\nDEBUG:Transmitting at %d Mhz...", radio.getFrequency()/1000000);
DEBUGln(buff); DEBUGln(buff);
if (flash.initialize()) if (flash.initialize())
{ {
DEBUGln("SPI Flash Init OK!"); DEBUGln("DEBUG:SPI Flash Init OK!");
} }
else else
{ {
DEBUGln("SPI FlashMEM not found (is chip onboard?)"); DEBUGln("DEBUG:SPI FlashMEM not found (is chip onboard?)");
} }
} }
@ -111,22 +111,23 @@ void loop() {
{ {
LED_HIGH; LED_HIGH;
int rssi = radio.RSSI; int rssi = radio.RSSI;
DEBUG('[');DEBUG(radio.SENDERID);DEBUG("] "); Serial.print('[');Serial.print(radio.SENDERID);Serial.print("] ");
if (radio.DATALEN > 0) if (radio.DATALEN > 0)
{ {
for (byte i = 0; i < radio.DATALEN; i++) for (byte i = 0; i < radio.DATALEN; i++)
DEBUG((char)radio.DATA[i]); Serial.print((char)radio.DATA[i]);
DEBUG(" [RSSI:");DEBUG(rssi);DEBUG("]"); Serial.print(" [RSSI:");Serial.print(rssi);Serial.print(']');
} }
Serial.println();
CheckForWirelessHEX(radio, flash, false); //non verbose DEBUG CheckForWirelessHEX(radio, flash, false); //non verbose DEBUG
if (radio.ACKRequested()) if (radio.ACKRequested())
{ {
radio.sendACK(); radio.sendACK();
DEBUG("[ACK-sent]"); DEBUGln("DEBUG:ACK-sent");
} }
DEBUGln();
LED_LOW; LED_LOW;
} }
} }