Advertisement
joaopedros2

Untitled

Sep 18th, 2023
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.17 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include "LoRaWan_APP.h"
  3.  
  4. // ================================================================================================
  5. // LoRa Configs
  6. // ================================================================================================
  7.  
  8. #define RF_FREQUENCY                433000000  // Hz
  9. #define TX_OUTPUT_POWER             17         // dBm
  10. #define LORA_BANDWIDTH              0          // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
  11. #define LORA_SPREADING_FACTOR       9          // [SF7..SF12]
  12. #define LORA_CODINGRATE             4          // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  13. #define LORA_PREAMBLE_LENGTH        8          // Same for Tx and Rx
  14. #define LORA_SYMBOL_TIMEOUT         0          // Symbols
  15. #define LORA_FIX_LENGTH_PAYLOAD_ON  false
  16. #define LORA_IQ_INVERSION_ON        false
  17. #define RX_TIMEOUT_VALUE            1000
  18. #define BUFFER_SIZE                 30         // Define the payload size here
  19.  
  20. char txpacket[BUFFER_SIZE];
  21. char rxpacket[BUFFER_SIZE];
  22.  
  23. bool lora_idle=true;
  24.  
  25. static RadioEvents_t RadioEvents;
  26. void OnTxDone( void );
  27. void OnTxTimeout( void );
  28.  
  29. // ================================================================================================
  30. // Define Pins
  31. // ================================================================================================
  32.  
  33. #define LED_PIN                     35         // Define the LED pin
  34.  
  35. #define VBAT_PIN                    1          // Define the battery voltage pin
  36. #define VBAT_READ_CNTRL_PIN         37         // Control pin for battery voltage reading
  37.  
  38. #define MOISTURE_PIN                7          // Define the moisture sensor pin
  39. #define MOISTURE_PIN_POWER          6          // Define the moisture sensor pin power
  40.  
  41. // ================================================================================================
  42. // Settings
  43. // ================================================================================================
  44.  
  45. //For Sector Number
  46. const int SECTOR_NUMBER = 1;                   // Define the sector number
  47.  
  48. //For Deep Sleep Mode
  49. const uint32_t DEEP_SLEEP_TIME = 10;           // Define the time duration in minutes
  50.  
  51. // ================================================================================================
  52.  
  53. int batteryAnalogValue;
  54. int moistureAnalogValue;
  55.  
  56. bool executedOnce = false;
  57.  
  58. // ================================================================================================
  59. // Function to Read Battery Analog Value
  60. // ================================================================================================
  61.  
  62. int batteryFunction() {
  63.  
  64.   delay(2000);
  65.  
  66.   int totalBattery = 0;
  67.  
  68.   for (int i = 0; i < 10; i++) {
  69.     totalBattery += analogRead(VBAT_PIN);
  70.     delay(100);
  71.   }
  72.  
  73.   int averageBattery = totalBattery / 10;
  74.  
  75.   return averageBattery;
  76. }
  77.  
  78. // ================================================================================================
  79. // Function to Read Moisture Analog Value
  80. // ================================================================================================
  81.  
  82. int moistureFunction() {
  83.  
  84.   delay(2000);
  85.  
  86.   int totalMoisture = 0;
  87.  
  88.   for (int i = 0; i < 10; i++) {
  89.     totalMoisture += analogRead(MOISTURE_PIN);
  90.     delay(100);
  91.   }
  92.  
  93.   int averageMoisture = totalMoisture / 10;
  94.  
  95.   return averageMoisture;
  96. }
  97.  
  98. // ================================================================================================
  99. // Function to Enter in Deep Sleep Mode
  100. // ================================================================================================
  101.  
  102. void enterDeepSleep() {
  103.  
  104.   // Put LoRa radio to sleep
  105.   Radio.Sleep();
  106.  
  107.   // End SPI communication
  108.   SPI.end();
  109.  
  110.   // Set pins to analog mode
  111.   pinMode(RADIO_DIO_1, ANALOG);
  112.   pinMode(RADIO_NSS, ANALOG);
  113.   pinMode(RADIO_RESET, ANALOG);
  114.   pinMode(RADIO_BUSY, ANALOG);
  115.   pinMode(LORA_CLK, ANALOG);
  116.   pinMode(LORA_MISO, ANALOG);
  117.   pinMode(LORA_MOSI, ANALOG);
  118.  
  119.   esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
  120.   Serial.printf("\nEntering in deep sleep mode for %u ", DEEP_SLEEP_TIME);
  121.   if (DEEP_SLEEP_TIME == 1) {
  122.     Serial.println("minute...\n");
  123.   } else {
  124.     Serial.println("minutes...\n");
  125.   }
  126.   esp_deep_sleep_start();
  127. }
  128.  
  129. // ================================================================================================
  130.  
  131. void setup() {
  132.   Serial.begin(115200);
  133.  
  134.   // Set the LED pin as an output and turn it off
  135.   pinMode(LED_PIN, OUTPUT);
  136.   digitalWrite(LED_PIN, LOW);
  137.  
  138.   // Set the Vext pin as an output and turn it off
  139.   pinMode(Vext, OUTPUT);
  140.   digitalWrite(Vext, HIGH);
  141.  
  142.   // Configure the VBAT_READ_CNTRL_PIN as an output and turn it off
  143.   pinMode(VBAT_READ_CNTRL_PIN, OUTPUT);
  144.   digitalWrite(VBAT_READ_CNTRL_PIN, LOW);
  145.  
  146.   Serial.println("\nGetting Analog Values from the sensors...");
  147.   delay(1000);
  148.  
  149.   batteryAnalogValue = batteryFunction();
  150.   Serial.print("Battery: ");
  151.   Serial.println(batteryAnalogValue);
  152.  
  153.   // Configure the VBAT_PIN as an output and turn it off
  154.   pinMode(VBAT_PIN, OUTPUT);
  155.   digitalWrite(VBAT_PIN, LOW);
  156.   delay(100);
  157.  
  158.   // Set the MOISTURE_PIN_POWER pin as an output and turn it on
  159.   pinMode(MOISTURE_PIN_POWER, OUTPUT);
  160.   digitalWrite(MOISTURE_PIN_POWER, HIGH);
  161.   delay(1000);
  162.  
  163.   moistureAnalogValue = moistureFunction();
  164.   Serial.print("Moisture: ");
  165.   Serial.println(moistureAnalogValue);
  166.  
  167.   // Set the MOISTURE_PIN pin as an output and turn it off
  168.   pinMode(MOISTURE_PIN, OUTPUT);
  169.   digitalWrite(MOISTURE_PIN, LOW);
  170.  
  171.   // Set the MOISTURE_PIN_POWER pin as an output and turn it off
  172.   pinMode(MOISTURE_PIN_POWER, OUTPUT);
  173.   digitalWrite(MOISTURE_PIN_POWER, LOW);
  174.  
  175.   Mcu.begin();
  176.   Serial.print("\nInitializing LoRa Radio...");
  177.   delay(1000);
  178.  
  179.   RadioEvents.TxDone = OnTxDone;
  180.   RadioEvents.TxTimeout = OnTxTimeout;
  181.  
  182.   Radio.Init( &RadioEvents );
  183.   Radio.SetChannel( RF_FREQUENCY );
  184.   Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
  185.                                  LORA_SPREADING_FACTOR, LORA_CODINGRATE,
  186.                                  LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
  187.                                  true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
  188. }
  189.  
  190. // ================================================================================================
  191.  
  192. void loop() {
  193.  
  194.   if (!executedOnce) {
  195.    
  196.     if(lora_idle == true) {
  197.      
  198.       sprintf(txpacket, "%d:%d:%d", SECTOR_NUMBER, batteryAnalogValue, moistureAnalogValue);
  199.  
  200.       Serial.printf("\r\nSending packet \"%s\" , length %d\r\n",txpacket, strlen(txpacket));
  201.  
  202.       // Send the package out
  203.       Radio.Send( (uint8_t *)txpacket, strlen(txpacket) );
  204.  
  205.       lora_idle = false;
  206.     }
  207.     executedOnce = true;
  208.   }
  209.   Radio.IrqProcess();
  210. }
  211.  
  212. // ================================================================================================
  213.  
  214. void OnTxDone( void ) {
  215.   Serial.println("TX done......");
  216.   lora_idle = true;
  217.  
  218.   delay(100);
  219.   enterDeepSleep();
  220. }
  221.  
  222. // ================================================================================================
  223.  
  224. void OnTxTimeout( void ) {
  225.   Radio.Sleep();
  226.   Serial.println("TX Timeout......");
  227.   lora_idle = true;
  228.  
  229.   delay(100);
  230.   enterDeepSleep();
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement