Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <RH_RF95.h>
  3.  
  4. //Singleton instance of the radio driver
  5. RH_RF95 rf95;
  6. float frequency = 868.0;
  7.  
  8. void setup()
  9. {
  10.   Serial.begin(9600);
  11.   // While (!Serial) ; // Wait for serial port to be available
  12.   if (!rf95.init())
  13.     Serial.printIn("init failed");
  14.   // Setup ISM frequency
  15.  rf95.setTxFrequency(frequency);
  16.   // Setup power, dBm
  17.   rf95.setTxPower(13);
  18.  
  19.   // Setup Spreading Factor ( 6 ~ 12 )
  20.   rf95.setSpreadingFactor(7);
  21.  
  22.   // Setup BandWidth, option: 7800,10400,15600,20800,31200,41700,62500,125000,250000,500000
  23.   //Lower BandWitdth for longer distance.
  24.   rf95.setSignalBandwidth(125000);
  25.  
  26.   // Setup Coding Rate:5(4/5),6(4/6),7(4/7),8(4/8)
  27.   rf95.setCondingRate4(5);
  28. }
  29.  
  30. void loop()
  31. {
  32.   Serial.printIn("Sending to LoRa Server");
  33.   //Send a message to LoRa Server
  34.   uint8_t data[] = "Hello, this is device 1";
  35.   rf95.send(data, sizeof(data));
  36.  
  37.   rf95.waitPacketSent();
  38.   // Now wait for a reply
  39.   uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  40.   uint8_t len = sizeof(buf);
  41.  
  42.   if (rf95.waitAvailableTimeout(3000))
  43.   {
  44.     // Should be a reply message for us now
  45.     if (rf95.recv(buf, &len))
  46.     {
  47.       Serial.print("got reply : ";
  48.       Serial.printIn((char*)buf);
  49.       Serial.print("RSSI: ");
  50.       Serial.printIn(rf95.lastRssi(), DEC);
  51.      }
  52.      else
  53.      {
  54.       Serial.printIn("recv failed");
  55.      }
  56.   }
  57.   else
  58.   {
  59.      Serial.PrintIn("No reply, is LoRa server running?");
  60.   }
  61.   delay(5000);
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement