Advertisement
pleasedontcode

**LoRa Communication** rev_01

Jan 9th, 2025
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **LoRa Communication**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-01-09 15:43:10
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Implement a robust LoRa communication system using */
  21.     /* SX127x module with defined SPI and digital pins */
  22.     /* for data transmission and reception. Ensure proper */
  23.     /* initialization and handling of reset and data */
  24.     /* output states for reliable performance. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <SPI.h>
  31. #include <LoRa.h>   //https://github.com/sandeepmistry/arduino-LoRa
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs(void);
  37.  
  38. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  39. const uint8_t dio0 = 2; // DIO0 pin for LoRa module
  40.  
  41. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  42. const uint8_t rst = 14; // Reset pin for LoRa module
  43.  
  44. /***** DEFINITION OF SPI PINS *****/
  45. const uint8_t lora_SX127x_SPI_PIN_MOSI_D23 = 23;
  46. const uint8_t lora_SX127x_SPI_PIN_MISO_D19 = 19;
  47. const uint8_t lora_SX127x_SPI_PIN_SCLK_D18 = 18;
  48. const uint8_t lora_SX127x_SPI_PIN_CS_D5 = 5; // Chip select pin for LoRa module
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. bool lora_SX127x_NRESET_PIN_D4_rawData = 0; // Raw data for reset pin
  52.  
  53. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  54. float lora_SX127x_NRESET_PIN_D4_phyData = 0.0; // Physical data for reset pin
  55.  
  56. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  57.  
  58. int counter = 0; // Counter for sending packets
  59.  
  60. void setup(void)
  61. {
  62.     // Initialize serial communication
  63.     Serial.begin(115200);
  64.     while (!Serial);
  65.     Serial.println("LoRa Sender");
  66.  
  67.     // Set pin modes
  68.     pinMode(rst, OUTPUT);
  69.     pinMode(lora_SX127x_SPI_PIN_CS_D5, OUTPUT);
  70.     pinMode(dio0, INPUT);
  71.    
  72.     // Start the SPI library
  73.     SPI.begin();
  74.  
  75.     // Initialize LoRa with defined pins
  76.     LoRa.setPins(lora_SX127x_SPI_PIN_CS_D5, rst, dio0);
  77.  
  78.     // Begin LoRa communication
  79.     while (!LoRa.begin(433E6)) // 433E6 - Asia, 866E6 - Europe, 915E6 - North America
  80.     {
  81.         Serial.println(".");
  82.         delay(500);
  83.     }
  84.     LoRa.setSyncWord(0xA5); // Set sync word for communication
  85.     Serial.println("LoRa Initializing OK!");
  86. }
  87.  
  88. void loop(void)
  89. {
  90.     // Send a packet
  91.     Serial.print("Sending packet: ");
  92.     Serial.println(counter);
  93.  
  94.     LoRa.beginPacket();   // Start LoRa packet
  95.     LoRa.print("hello ");
  96.     LoRa.print(counter);
  97.     LoRa.endPacket(); // End the packet
  98.  
  99.     counter++; // Increment counter
  100.  
  101.     updateOutputs(); // Refresh output data
  102.  
  103.     delay(10); // Delay for stability
  104. }
  105.  
  106. void updateOutputs()
  107. {
  108.     // Update the reset pin state
  109.     digitalWrite(rst, lora_SX127x_NRESET_PIN_D4_rawData);
  110. }
  111.  
  112. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement