Advertisement
Guest User

Untitled

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