From 404a5add1d02758b4ad37619ae77b5ca6d45c8b9 Mon Sep 17 00:00:00 2001 From: Felix Rusu Date: Tue, 30 Jul 2013 16:50:37 -0400 Subject: [PATCH] Sketch updates, minor fixes --- Examples/Gateway/Gateway.ino | 59 ++++++++++++++++++++++++++++-------- Examples/Node/Node.ino | 55 ++++++++++++++++++++++++++------- RFM69.cpp | 1 - RFM69.h | 2 +- 4 files changed, 91 insertions(+), 26 deletions(-) diff --git a/Examples/Gateway/Gateway.ino b/Examples/Gateway/Gateway.ino index ab35a21..98d1d11 100644 --- a/Examples/Gateway/Gateway.ino +++ b/Examples/Gateway/Gateway.ino @@ -1,25 +1,33 @@ #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_915MHZ) -#define KEY "thisIsEncryptKey" +#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 10 // # of ms to wait for an ack +#define ACK_TIME 30 // # of ms to wait for an ack RFM69 radio; +SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip bool promiscuousMode = false; //set to 'true' to sniff all packets on the same network void setup() { Serial.begin(SERIAL_BAUD); + delay(10); radio.initialize(FREQUENCY,NODEID,NETWORKID); + //radio.setHighPower(); //uncomment only for RFM69HW! radio.encrypt(KEY); radio.promiscuous(promiscuousMode); char buff[50]; sprintf(buff, "\nListening 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 + Serial.println("SPI Flash Init FAIL! (is chip present?)"); } byte ackCount=0; @@ -28,7 +36,7 @@ void loop() { if (Serial.available() > 0) { char input = Serial.read(); - if (input == 'd') //d=dump all register values + if (input == 'r') //d=dump all register values radio.readAllRegs(); if (input == 'E') //E=enable encryption radio.encrypt(KEY); @@ -40,6 +48,32 @@ void loop() { radio.promiscuous(promiscuousMode); Serial.print("Promiscuous mode ");Serial.println(promiscuousMode ? "on" : "off"); } + + if (input == 'd') //d=dump flash area + { + Serial.println("Flash content:"); + int counter = 0; + + while(counter<=256){ + Serial.print(flash.readByte(counter++), HEX); + Serial.print('.'); + } + while(flash.busy()); + Serial.println(); + } + if (input == 'e') + { + Serial.print("Erasing Flash chip ... "); + flash.chipErase(); + while(flash.busy()); + Serial.println("DONE"); + } + if (input == 'i') + { + Serial.print("DeviceID: "); + word jedecid = flash.readDeviceId(); + Serial.println(jedecid, HEX); + } } if (radio.receiveDone()) @@ -62,18 +96,17 @@ void loop() { // When a node requests an ACK, respond to the ACK // and also send a packet requesting an ACK (every 3rd one only) // This way both TX/RX NODE functions are tested on 1 end at the GATEWAY - /* - if (ackCount++%3) + if (ackCount++%3==0) { - Serial.print(" Sending packet to node "); + Serial.print(" Pinging node "); Serial.print(theNodeID); delay(5); radio.send(theNodeID, "ACK TEST", 8, true); - Serial.print(" - waiting for ACK..."); + Serial.print(" - ACK..."); if (waitForAck(theNodeID)) Serial.print("ok!"); - else Serial.print("nothing..."); + else Serial.print("nothing"); } - */ + } Serial.println(); Blink(LED,3); @@ -90,10 +123,10 @@ static bool waitForAck(byte theNodeID) { return false; } -void Blink(byte PIN, int delay_ms) +void Blink(byte PIN, int DELAY_MS) { pinMode(PIN, OUTPUT); digitalWrite(PIN,HIGH); - delay(delay_ms); + 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 b72352e..3a9edd6 100644 --- a/Examples/Node/Node.ino +++ b/Examples/Node/Node.ino @@ -1,29 +1,35 @@ #include #include +#include -#define NODEID 25 +#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_915MHZ) -#define KEY "thisIsEncryptKey" +#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 10 // # of ms to wait for an ack +#define ACK_TIME 30 // # of ms to wait for an ack -int TRANSMITPERIOD = 500; //transmit a packet to gateway so often (in ms) +int TRANSMITPERIOD = 300; //transmit a packet to gateway so often (in ms) char payload[] = "123 ABCDEFGHIJKLMNOPQRSTUVWXYZ"; byte sendSize=0; boolean requestACK = false; +SPIFlash flash(8, 0xEF30); //EF40 for 16mbit windbond chip RFM69 radio; void setup() { Serial.begin(SERIAL_BAUD); radio.initialize(FREQUENCY,NODEID,NETWORKID); - //radio.setHighPower(); //only for RFM69HW! + //radio.setHighPower(); //uncomment only for RFM69HW! radio.encrypt(KEY); 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 + Serial.println("SPI Flash Init FAIL! (is chip present?)"); } long lastPeriod = -1; @@ -41,12 +47,38 @@ void loop() { Serial.println("ms\n"); } - if (input == 'd') //d=dump register values + if (input == 'r') //d=dump register values radio.readAllRegs(); if (input == 'E') //E=enable encryption radio.encrypt(KEY); if (input == 'e') //e=disable encryption radio.encrypt(null); + + if (input == 'd') //d=dump flash area + { + Serial.println("Flash content:"); + int counter = 0; + + while(counter<=256){ + Serial.print(flash.readByte(counter++), HEX); + Serial.print('.'); + } + while(flash.busy()); + Serial.println(); + } + if (input == 'e') + { + Serial.print("Erasing Flash chip ... "); + flash.chipErase(); + while(flash.busy()); + Serial.println("DONE"); + } + if (input == 'i') + { + Serial.print("DeviceID: "); + word jedecid = flash.readDeviceId(); + Serial.println(jedecid, HEX); + } } //check for any received packets @@ -60,7 +92,8 @@ void loop() { if (radio.ACK_REQUESTED) { radio.sendACK(); - Serial.print(" - ACK sent."); + Serial.print(" - ACK sent"); + delay(10); } Blink(LED,5); Serial.println(); @@ -97,11 +130,11 @@ void loop() { } } -void Blink(byte PIN, int delay_ms) +void Blink(byte PIN, int DELAY_MS) { pinMode(PIN, OUTPUT); digitalWrite(PIN,HIGH); - delay(delay_ms); + delay(DELAY_MS); digitalWrite(PIN,LOW); } @@ -113,4 +146,4 @@ void Blink(byte PIN, int delay_ms) // return true; // } // return false; -//} +//} \ No newline at end of file diff --git a/RFM69.cpp b/RFM69.cpp index e74f888..b344ebc 100644 --- a/RFM69.cpp +++ b/RFM69.cpp @@ -162,7 +162,6 @@ void RFM69::send(byte toAddress, const void* buffer, byte bufferSize, bool reque // replies usually take only 5-8ms at 50kbps@915Mhz bool RFM69::sendWithRetry(byte toAddress, const void* buffer, byte bufferSize, byte retries, byte retryWaitTime) { long sentTime; - byte howLong=0; for (byte i=0; i<=retries; i++) { send(toAddress, buffer, bufferSize, true); diff --git a/RFM69.h b/RFM69.h index 20b6bce..7821590 100644 --- a/RFM69.h +++ b/RFM69.h @@ -49,7 +49,7 @@ class RFM69 { _isRFM69HW = isRFM69HW; } - bool initialize(byte ID, byte freqBand, byte networkID=1); + bool initialize(byte freqBand, byte ID, byte networkID=1); void setAddress(byte addr); bool canSend(); void send(byte toAddress, const void* buffer, byte bufferSize, bool requestACK=false);