Advertisement
Guest User

RFM69_ATTiny_Sloeber

a guest
Apr 13th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Arduino.h"
  2. #include <RFM69.h>
  3. #include <SPI.h>
  4. #include <SPIFlash.h>
  5.  
  6.  
  7. // Addresses for this node. CHANGE THESE FOR EACH NODE!
  8.  
  9. #define NETWORKID     0   // Must be the same for all nodes
  10. #define MYNODEID      1   // My node ID
  11. #define TONODEID      2   // Destination node ID
  12.  
  13. // RFM69 frequency, uncomment the frequency of your module:
  14.  
  15. #define FREQUENCY   RF69_433MHZ
  16. //#define FREQUENCY     RF69_915MHZ
  17.  
  18. // AES encryption (or not):
  19.  
  20. #define ENCRYPT       true // Set to "true" to use encryption
  21. #define ENCRYPTKEY    "TOPSECRETPASSWRD" // Use the same 16-byte key on
  22. all nodes
  23.  
  24. // Use ACKnowledge when sending messages (or not):
  25.  
  26. #define USEACK        true // Request ACKs or not
  27.  
  28. // Packet sent/received indicator LED (optional):
  29.  
  30. #define LED           8 // LED positive pin
  31. //#define GND           8 // LED ground pin
  32.  
  33. // Create a library object for our RFM69HCW module:
  34.  
  35. RFM69 radio;
  36.  
  37. void setup()
  38. {
  39.   // Open a serial port so we can send keystrokes to the module:
  40.  
  41.   Serial.begin(9600);
  42.   Serial.print(F("Node "));
  43.   Serial.print(MYNODEID,DEC);
  44.   Serial.println(F(" ready"));
  45.  
  46.   // Set up the indicator LED (optional):
  47.  
  48.   pinMode(LED,OUTPUT);
  49.   digitalWrite(LED,HIGH);
  50.   //pinMode(GND,OUTPUT);
  51.   //digitalWrite(GND,LOW);
  52.  
  53.   // Initialize the RFM69HCW:
  54.   // radio.setCS(10);  //uncomment this if using Pro Micro
  55.   radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
  56.   radio.setHighPower(); // Always use this for RFM69HCW
  57.  
  58.   // Turn on encryption if desired:
  59.  
  60.   if (ENCRYPT)
  61.     radio.encrypt(ENCRYPTKEY);
  62. }
  63.  
  64. void loop()
  65. {
  66.   // Set up a "buffer" for characters that we'll send:
  67.  
  68.   static char sendbuffer[62];
  69.   static int sendlength = 0;
  70.  
  71.   // SENDING
  72.  
  73.   // In this section, we'll gather serial characters and
  74.   // send them to the other node if we (1) get a carriage return,
  75.   // or (2) the buffer is full (61 characters).
  76.  
  77.   // If there is any serial input, add it to the buffer:
  78.  
  79.   if (Serial.available() > 0)
  80.   {
  81.     char input = Serial.read();
  82.  
  83.     if (input != '\r') // not a carriage return
  84.     {
  85.       sendbuffer[sendlength] = input;
  86.       sendlength++;
  87.     }
  88.  
  89.     // If the input is a carriage return, or the buffer is full:
  90.  
  91.     if ((input == '\r') || (sendlength == 61)) // CR or buffer full
  92.     {
  93.       // Send the packet!
  94.  
  95.  
  96.       Serial.print(F("sending to node "));
  97.       Serial.print(TONODEID, DEC);
  98.       Serial.print(F(", message ["));
  99.       for (byte i = 0; i < sendlength; i++)
  100.         Serial.print(sendbuffer[i]);
  101.       Serial.println(F("]"));
  102.  
  103.       // There are two ways to send packets. If you want
  104.       // acknowledgements, use sendWithRetry():
  105.  
  106.       if (USEACK)
  107.       {
  108.         if (radio.sendWithRetry(TONODEID, sendbuffer, sendlength))
  109.           Serial.println(F("ACK received!"));
  110.         else
  111.           Serial.println(F("no ACK received"));
  112.       }
  113.  
  114.       // If you don't need acknowledgements, just use send():
  115.  
  116.       else // don't use ACK
  117.       {
  118.         radio.send(TONODEID, sendbuffer, sendlength);
  119.       }
  120.  
  121.       sendlength = 0; // reset the packet
  122.       Blink(LED,10);
  123.     }
  124.   }
  125.  
  126.   // RECEIVING
  127.  
  128.   // In this section, we'll check with the RFM69HCW to see
  129.   // if it has received any packets:
  130.  
  131.   if (radio.receiveDone()) // Got one!
  132.   {
  133.     // Print out the information:
  134.  
  135.     Serial.print(F("received from node "));
  136.     Serial.print(radio.SENDERID, DEC);
  137.     Serial.print(F(", message ["));
  138.  
  139.     // The actual message is contained in the DATA array,
  140.     // and is DATALEN bytes in size:
  141.  
  142.     for (byte i = 0; i < radio.DATALEN; i++)
  143.       Serial.print((char)radio.DATA[i]);
  144.  
  145.     // RSSI is the "Receive Signal Strength Indicator",
  146.     // smaller numbers mean higher power.
  147.  
  148.     Serial.print(F("], RSSI "));
  149.     Serial.println(radio.RSSI);
  150.  
  151.     // Send an ACK if requested.
  152.     // (You don't need this code if you're not using ACKs.)
  153.  
  154.     if (radio.ACKRequested())
  155.     {
  156.       radio.sendACK();
  157.       Serial.println(F("ACK sent"));
  158.     }
  159.     Blink(LED,10);
  160.   }
  161. }
  162.  
  163. void Blink(byte PIN, int DELAY_MS)
  164. // Blink an LED for a given number of ms
  165. {
  166.   digitalWrite(PIN,HIGH);
  167.   delay(DELAY_MS);
  168.   digitalWrite(PIN,LOW);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement