Advertisement
joaopedros2

Untitled

Nov 2nd, 2023
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.51 KB | Software | 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.  
  10. #define TX_OUTPUT_POWER             17         // dBm
  11.  
  12. #define LORA_BANDWIDTH              0          // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
  13.  
  14. #define LORA_SPREADING_FACTOR       9          // [SF7..SF12]
  15. #define LORA_CODINGRATE             4          // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
  16.  
  17. #define LORA_PREAMBLE_LENGTH        8          // Same for Tx and Rx
  18. #define LORA_SYMBOL_TIMEOUT         0          // Symbols
  19. #define LORA_FIX_LENGTH_PAYLOAD_ON  false
  20. #define LORA_IQ_INVERSION_ON        false
  21.  
  22. #define RX_TIMEOUT_VALUE            1000
  23. #define BUFFER_SIZE                 30         // Define the payload size here
  24.  
  25. char txpacket[BUFFER_SIZE];
  26. char rxpacket[BUFFER_SIZE];
  27.  
  28. bool lora_idle=true;
  29.  
  30. static RadioEvents_t RadioEvents;
  31. void OnTxDone( void );
  32. void OnTxTimeout( void );
  33.  
  34. // ----------------------------------------------------------------------------
  35. // Define Pins
  36. // ----------------------------------------------------------------------------
  37.  
  38. #define LED_PIN                     35        // Define the LED pin
  39.  
  40. #define VBAT_PIN                    1          // Define the battery voltage pin
  41. #define VBAT_READ_CNTRL_PIN         37         // Control pin for battery voltage reading
  42.  
  43. #define MOISTURE_PIN                7          // Define the moisture sensor pin
  44. #define MOISTURE_PIN_POWER          6          // Define the moisture sensor pin power
  45.  
  46. // ----------------------------------------------------------------------------
  47. // Settings
  48. // ----------------------------------------------------------------------------
  49.  
  50. //For Sector Number
  51. const int SECTOR_NUMBER = 2;                   // Define the sector number
  52.  
  53. //For Deep Sleep Mode
  54. const uint32_t DEEP_SLEEP_TIME = 5;           // Define the time duration in minutes
  55.  
  56. // ----------------------------------------------------------------------------
  57. // Moving Average
  58. // ----------------------------------------------------------------------------
  59.  
  60. #define WINDOW_SIZE 1000
  61.  
  62. int INDEX = 0;
  63. int SUM = 0;
  64. int READINGS[WINDOW_SIZE];
  65. int AVERAGED = 0;
  66.  
  67. // ----------------------------------------------------------------------------
  68. // Global Variables
  69. // ----------------------------------------------------------------------------
  70.  
  71. int batteryValue = 0;
  72.  
  73. // ----------------------------------------------------------------------------
  74. // Function to read sensor values and update moving average
  75. // ----------------------------------------------------------------------------
  76.  
  77. void initializeMovingAverage(int& index, int& sum, int* readings, int& averaged, int window_size, int sensor_pin) {
  78.   int value = analogRead(sensor_pin);
  79.   for (int i = 0; i < window_size; i++) {
  80.     readings[i] = value;
  81.     sum += value;
  82.   }
  83.   averaged = sum / window_size;
  84. }
  85.  
  86. void updateSensorValues(int& index, int& sum, int* readings, int& averaged, int window_size, int sensor_pin) {
  87.   sum = sum - readings[index];
  88.   int value = analogRead(sensor_pin);
  89.   readings[index] = value;
  90.   sum = sum + value;
  91.   index = (index + 1) % window_size;
  92.   averaged = sum / window_size;
  93. }
  94.  
  95. // ----------------------------------------------------------------------------
  96. // Function to read BATTERY sensor values
  97. // ----------------------------------------------------------------------------
  98.  
  99. int readBatterySensor() {
  100.   unsigned long startTime = millis();
  101.   while (millis() - startTime < 5000) {
  102.     updateSensorValues(INDEX, SUM, READINGS, AVERAGED, WINDOW_SIZE, VBAT_PIN);
  103.   }
  104.   return AVERAGED;
  105. }
  106.  
  107. // ----------------------------------------------------------------------------
  108. // Function to read MOISTURE sensor values
  109. // ----------------------------------------------------------------------------
  110.  
  111. int readMoistureSensor() {
  112.   unsigned long startTime = millis();
  113.   while (millis() - startTime < 5000) {
  114.     updateSensorValues(INDEX, SUM, READINGS, AVERAGED, WINDOW_SIZE, MOISTURE_PIN);
  115.   }
  116.   return AVERAGED;
  117. }
  118.  
  119. // ----------------------------------------------------------------------------
  120. // Function to Enter in Deep Sleep Mode
  121. // ----------------------------------------------------------------------------
  122.  
  123. void deepSleep() {
  124.  
  125.   // Set the MOISTURE_PIN_POWER pin off
  126.   digitalWrite(MOISTURE_PIN_POWER, LOW);
  127.   delay(100);
  128.  
  129.   // Put LoRa radio to sleep
  130.   Radio.Sleep();
  131.   delay(100);
  132.  
  133.   // End SPI communication
  134.   SPI.end();
  135.   delay(100);
  136.  
  137.   // Set pins as an analog input to save power
  138.   pinMode(RADIO_DIO_1, ANALOG);
  139.   pinMode(RADIO_NSS, ANALOG);
  140.   pinMode(RADIO_RESET, ANALOG);
  141.   pinMode(RADIO_BUSY, ANALOG);
  142.   pinMode(LORA_CLK, ANALOG);
  143.   pinMode(LORA_MISO, ANALOG);
  144.   pinMode(LORA_MOSI, ANALOG);
  145.   delay(100);
  146.  
  147.   // Configure power domains to optimize power consumption
  148.   //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,   ESP_PD_OPTION_OFF); // Turn off RTC peripherals
  149.   //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF); // Turn off RTC slow memory
  150.   //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); // Turn off RTC fast memory
  151.   //esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,         ESP_PD_OPTION_OFF); // Turn off the crystal oscillator
  152.  
  153.   // Enable timer wake-up for deep sleep
  154. //  esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
  155.  
  156.   // Enable timer wake-up for deep sleep if battery level is sufficient
  157.   if (batteryValue >= 812) {
  158.     esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
  159.   }
  160.  
  161.   Serial.printf("\nEntering deep sleep mode for %u %s...\n", DEEP_SLEEP_TIME, (DEEP_SLEEP_TIME == 1) ? "minute" : "minutes");
  162.   delay(100);
  163.  
  164.   // Enter deep sleep mode
  165.   esp_deep_sleep_start();
  166. }
  167.  
  168. // ----------------------------------------------------------------------------
  169. // Function to blink LED
  170. // ----------------------------------------------------------------------------
  171.  
  172. void blinkLED() {
  173.   digitalWrite(LED_PIN, HIGH); // LED on
  174.   delay(200);
  175.   digitalWrite(LED_PIN, LOW); // LED off
  176.   delay(10);
  177. }
  178.  
  179. // ----------------------------------------------------------------------------
  180.  
  181. void setup() {
  182.  
  183.   Serial.begin(115200);
  184.  
  185.   // Set the LED pin as an output
  186.   pinMode(LED_PIN, OUTPUT);
  187.  
  188.   // Set the VBAT_READ_CNTRL_PIN pin to turn it off
  189.   pinMode(VBAT_READ_CNTRL_PIN, OUTPUT);
  190.   digitalWrite(VBAT_READ_CNTRL_PIN, LOW);
  191.  
  192.   // Set the MOISTURE_PIN_POWER pin to turn it on
  193.   pinMode(MOISTURE_PIN_POWER, OUTPUT);
  194.   digitalWrite(MOISTURE_PIN_POWER, HIGH);
  195.  
  196.   delay(2000);
  197.  
  198.   Mcu.begin();
  199.   Serial.println("\nInitializing LoRa Radio...");
  200.  
  201.   RadioEvents.TxDone = OnTxDone;
  202.   RadioEvents.TxTimeout = OnTxTimeout;
  203.  
  204.   Radio.Init( &RadioEvents );
  205.   Radio.SetChannel( RF_FREQUENCY );
  206.   Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
  207.                                  LORA_SPREADING_FACTOR, LORA_CODINGRATE,
  208.                                  LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
  209.                                  true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
  210.  
  211.   if(lora_idle == true) {
  212.    
  213.     delay(1000);
  214.  
  215.     Serial.println("\nGetting Analog Values from the sensors...");
  216.  
  217.     int batteryValue = readBatterySensor();
  218.     int moistureValue = readMoistureSensor();
  219.  
  220. //    int batteryValue = analogRead(VBAT_PIN);
  221. //    int moistureValue = analogRead(MOISTURE_PIN);
  222.  
  223.     Serial.print("Battery: ");
  224.     Serial.println(batteryValue);
  225.     Serial.print("Moisture: ");
  226.     Serial.println(moistureValue);
  227.    
  228.     // Start a package
  229.     sprintf(txpacket, "%d:%d:%d", SECTOR_NUMBER, batteryValue, moistureValue);
  230.    
  231.     Serial.printf("\r\nSending Packet: \"%s\", length %d\r\n",txpacket, strlen(txpacket));
  232.    
  233.     // Send the package out
  234.     Radio.Send((uint8_t *)txpacket, strlen(txpacket));
  235.     lora_idle = false;
  236.   }
  237. }
  238.  
  239. // ----------------------------------------------------------------------------
  240.  
  241. void loop() {
  242.  
  243.   Radio.IrqProcess();
  244. }
  245.  
  246. // ----------------------------------------------------------------------------
  247.  
  248. void OnTxDone( void ) {
  249.  
  250.   Serial.println("TX done......");
  251.   lora_idle = true;
  252.  
  253.   // Blink the LED when sending the package out
  254.   blinkLED();
  255.  
  256.   delay(100);
  257.   deepSleep();
  258. }
  259.  
  260. // ----------------------------------------------------------------------------
  261.  
  262. void OnTxTimeout( void ){
  263.  
  264.   Serial.println("TX Timeout......");
  265.   lora_idle = true;
  266.  
  267.   delay(100);
  268.   deepSleep();
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement