Last active
August 29, 2015 14:27
-
-
Save dwblair/67485e4ad9175239b1b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <RFM69.h> | |
| #include <SPI.h> | |
| #include <SPIFlash.h> | |
| #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 "sampleEncryptKey" //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 IS_RFM69HW | |
| 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 | |
| typedef struct { | |
| int nodeId; //store this nodeId | |
| unsigned long uptime; //uptime in ms | |
| int temp; //temperature maybe? | |
| } Payload; | |
| Payload theData; | |
| 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; | |
| void loop() { | |
| //process any serial input | |
| if (Serial.available() > 0) | |
| { | |
| char input = Serial.read(); | |
| if (input == 'r') //d=dump all register values | |
| radio.readAllRegs(); | |
| if (input == 'E') //E=enable encryption | |
| radio.encrypt(KEY); | |
| if (input == 'e') //e=disable encryption | |
| radio.encrypt(null); | |
| if (input == 'p') | |
| { | |
| promiscuousMode = !promiscuousMode; | |
| 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 == 'D') | |
| { | |
| Serial.print("Deleting Flash chip content... "); | |
| 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()) | |
| { | |
| //Serial.print('[');Serial.print(radio.SENDERID, DEC);Serial.print("] "); | |
| //Serial.print(" [RX_RSSI:");Serial.print(radio.readRSSI());Serial.print("]"); | |
| if (promiscuousMode) | |
| { | |
| //Serial.print("to [");Serial.print(radio.TARGETID, DEC);Serial.print("] "); | |
| } | |
| if (radio.DATALEN != sizeof(Payload)) | |
| Serial.print("Invalid payload received, not matching Payload struct!"); | |
| else | |
| { | |
| theData = *(Payload*)radio.DATA; //assume radio.DATA actually contains our struct and not something else | |
| //Serial.print(" nodeId="); | |
| //Serial.print(theData.nodeId); | |
| //Serial.print(" lightLevel="); | |
| //Serial.print(theData.uptime); | |
| //float tempC = (float(theData.temp)/1024.*3.3-.5)/.01; //conversion for LM50, assuming 3.3V ARef | |
| //Serial.print(" temp="); | |
| //Serial.print(tempC); | |
| Serial.print(millis()); | |
| Serial.print(","); | |
| Serial.print(theData.uptime); | |
| Serial.print(","); | |
| Serial.println(theData.temp); | |
| } | |
| if (radio.ACKRequested()) | |
| { | |
| byte theNodeID = radio.SENDERID; | |
| radio.sendACK(); | |
| //Serial.print(" - ACK sent."); | |
| // 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==0) | |
| { | |
| // Serial.print(" Pinging node "); | |
| // Serial.print(theNodeID); | |
| // Serial.print(" - ACK..."); | |
| delay(3); //need this when sending right after reception .. ? | |
| // if (radio.sendWithRetry(theNodeID, "ACK TEST", 8, 0)) // 0 = only 1 attempt, no retries | |
| // Serial.print("ok!"); | |
| // else Serial.print("nothing"); | |
| } | |
| } | |
| //Serial.println(); | |
| Blink(LED,3); | |
| } | |
| } | |
| void Blink(byte PIN, int DELAY_MS) | |
| { | |
| pinMode(PIN, OUTPUT); | |
| digitalWrite(PIN,HIGH); | |
| delay(DELAY_MS); | |
| digitalWrite(PIN,LOW); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment