Code: Select all
// rf69 demo tx rx.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple addressed, reliable messaging client
// with the RH_RF69 class. RH_RF69 class does not provide for addressing or
// reliability, so you should only use RH_RF69 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf69_server.
// Demonstrates the use of AES encryption, setting the frequency and modem
// configuration
#include <SPI.h>
#include <RH_RF69.h>
#include <RHReliableDatagram.h>
/************ Radio Setup ***************/
// Radio Frequency
#define RF69_FREQ 433.0
// Where to send packets
#define DEST_ADDRESS 10
// change addresses for each client board, any number
#define MY_ADDRESS 15
#define RFM69_CS 8
#define RFM69_INT 3
#define RFM69_RST 4
#define LED 13
// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram rf69_manager(rf69, MY_ADDRESS);
int16_t packetnum = 0; // packet counter, we increment per xmission
void setup()
{
Serial.begin(115200);
while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer
Serial1.begin(9600);
pinMode(LED, OUTPUT);
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, LOW);
//Serial.println("Feather Addressed RFM69 TX Test!");
//Serial.println();
// manual reset
digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);
if (!rf69_manager.init()) {
Serial.println("RFM69 radio init failed");
while (1);
}
Serial.println("RFM69 radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
// No encryption
// Set encryption key[] has to be same as server
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);
if (!rf69.setFrequency(RF69_FREQ)) {
Serial.println("setFrequency failed");
}
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
// ishighpowermodule flag set like this:
rf69.setTxPower(14, true); // range from 14-20 for power, 2nd arg must be true for 69HCW
//Serial.print("RFM69 radio @"); Serial.print((int)RF69_FREQ); Serial.println(" MHz");
}
// Dont put this on the stack:
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
uint8_t data[] = " OK";
void loop() {
static uint8_t inByte1 = B00000000;
static uint8_t inByte2 = B00000000;
static uint8_t inByte3 = B00000000;
static uint8_t inByte4 = B00000000;
static uint8_t chkSum = B00000000;
static uint8_t valueCheck = B00000000;
static int loopIteration = 0;
static int multiplier = 5;
static int bytesStored = 0;
Serial.print("inByte1 = ");
Serial.println(inByte1);
Serial.print("inByte2 = ");
Serial.println(inByte2);
Serial.print("inByte3 = ");
Serial.println(inByte3);
Serial.print("inByte4 = ");
Serial.println(inByte4);
Serial.print("chkSum = ");
Serial.println(chkSum);
Serial.print("loop iteration = ");
Serial.println(loopIteration);
Serial.print("multiplier = ");
Serial.println(multiplier);
Serial.print("The number of bytes stored = ");
Serial.println(bytesStored);
uint8_t len = sizeof(buf);
buf[len] = 0;
for (int i=0; i < len; i++){
Serial.print("Buffer"); Serial.print(i); Serial.print(" = "); Serial.println(buf[i]);
}
while(1){
if(Serial1.available()>4){
inByte1 = Serial1.read();
inByte2 = Serial1.read();
inByte3 = Serial1.read();
inByte4 = Serial1.read();
chkSum = Serial1.read();
loopIteration++;
bytesStored = loopIteration * multiplier;
Serial.print("The number of bytes Stored is: ");
Serial.println(bytesStored);
if (bytesStored == 125){
Serial.flush();
}
for (int i=0; i < 60; i++){
Serial.print("Buffer"); Serial.print(i); Serial.print(" = "); Serial.println(buf[i]);
}
}
uint8_t radiopacket[5] = {inByte1, inByte2, inByte3, inByte4, chkSum};
valueCheck = (inByte1) + (inByte2) + (inByte3) + (inByte4) % 256;
Serial.print("chkSum = ");
Serial.println(chkSum);
Serial.print("valueCheck = ");
Serial.println(valueCheck);
if(valueCheck == chkSum){
Serial.println("Hallelujah!");
}
radiopacket[0] = inByte1;
radiopacket[1] = inByte2;
radiopacket[2] = inByte3;
radiopacket[3] = inByte4;
radiopacket[4] = chkSum;
//Blink(LED, 40, 3); //blink LED 3 times, 40ms between blinks
// Send a message to the DESTINATION!
if (rf69_manager.sendtoWait(radiopacket, 5, DEST_ADDRESS)) {
// Now wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (rf69_manager.recvfromAckTimeout(buf, &len, 2000, &from)) {
buf[len] = 0; // zero out remaining string
Serial.println("Problem Here");
}
}
// Serial.print("Got reply from #"); Serial.print(from);
// Serial.print(" [RSSI :");
// Serial.print(rf69.lastRssi());
// Serial.print("] : ");
// Serial.println((char*)buf);
// }
// else {
// Serial.println("No reply, is anyone listening?");
// }
// }
// else {
// Serial.println("Sending failed (no ack)");
// }
}// end while loop
}
void Blink(byte PIN, byte DELAY_MS, byte loops) {
for (byte i=0; i<loops; i++) {
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
delay(DELAY_MS);
}
}