AcidLizard11

Arduino Code

Mar 3rd, 2021
841
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LoRa 9x_TX
  2. // -*- mode: C++ -*-
  3. // Example sketch showing how to create a simple messaging client (transmitter)
  4. // with the RH_RF95 class. RH_RF95 class does not provide for addressing or
  5. // reliability, so you should only use RH_RF95 if you do not need the higher
  6. // level messaging abilities.
  7. // It is designed to work with the other example LoRa9x_RX
  8.  
  9. #include <SPI.h>
  10. #include <RH_RF95.h>
  11.  
  12. #define RFM95_RST 6
  13. #define RFM95_EN 4
  14. #define RFM95_CS 7
  15. #define RFM95_INT digitalPinToInterrupt(5)
  16.  
  17. // Change to 434.0 or other frequency, must match RX's freq!
  18. #define RF95_FREQ 915.0
  19.  
  20. // Singleton instance of the radio driver
  21. RH_RF95 rf95(RFM95_CS, RFM95_INT);
  22.  
  23. void setup()
  24. {
  25.   pinMode(RFM95_RST, OUTPUT);
  26.   digitalWrite(RFM95_RST, HIGH);
  27.  
  28.   while (!Serial);
  29.   Serial.begin(9600);
  30.   delay(100);
  31.  
  32.   Serial.println("Arduino LoRa TX Test!");
  33.  
  34.   // manual reset
  35.   digitalWrite(RFM95_RST, LOW);
  36.   delay(10);
  37.   digitalWrite(RFM95_RST, HIGH);
  38.   delay(10);
  39.  
  40.   while (!rf95.init()) {
  41.     Serial.println("LoRa radio init failed");
  42.     while (1);
  43.   }
  44.   Serial.println("LoRa radio init OK!");
  45.  
  46.   if (!rf95.setFrequency(RF95_FREQ)) {
  47.     Serial.println("setFrequency failed");
  48.     while (1);
  49.   }
  50.   Serial.print("Set Freq to: "); Serial.println(RF95_FREQ);
  51.  
  52.   // The default transmitter power is 13dBm, using PA_BOOST.
  53.   // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
  54.   // you can set transmitter powers from 5 to 23 dBm:
  55.   rf95.setTxPower(23, false);
  56. }
  57.  
  58. int16_t packetnum = 0;  // packet counter, we increment per xmission
  59.  
  60. void loop()
  61. {
  62.   Serial.println("Sending to rf95_server");
  63.   // Send a message to rf95_server
  64.  
  65.   char radiopacket[20] = "Hello World #      ";
  66.   itoa(packetnum++, radiopacket+13, 10);
  67.   Serial.print("Sending "); Serial.println(radiopacket);
  68.   radiopacket[19] = 0;
  69.  
  70.   Serial.println("Sending..."); delay(10);
  71.   rf95.send((uint8_t *)radiopacket, 20);
  72.  
  73.   Serial.println("Waiting for packet to complete..."); delay(10);
  74.   rf95.waitPacketSent();
  75.   // Now wait for a reply
  76.   uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];
  77.   uint8_t len = sizeof(buf);
  78.  
  79.   Serial.println("Waiting for reply..."); delay(10);
  80.   if (rf95.waitAvailableTimeout(1000))
  81.   {
  82.     // Should be a reply message for us now  
  83.     if (rf95.recv(buf, &len))
  84.    {
  85.       Serial.print("Got reply: ");
  86.       Serial.println((char*)buf);
  87.       Serial.print("RSSI: ");
  88.       Serial.println(rf95.lastRssi(), DEC);    
  89.     }
  90.     else
  91.     {
  92.       Serial.println("Receive failed");
  93.     }
  94.   }
  95.   else
  96.   {
  97.     Serial.println("No reply, is there a listener around?");
  98.   }
  99.   delay(1000);
  100. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×