Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Arduino.h"
- #include "LoRaWan_APP.h"
- // ----------------------------------------------------------------------------
- // LoRa Configs
- // ----------------------------------------------------------------------------
- #define RF_FREQUENCY 433000000 // Hz
- #define TX_OUTPUT_POWER 17 // dBm
- #define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
- #define LORA_SPREADING_FACTOR 9 // [SF7..SF12]
- #define LORA_CODINGRATE 4 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
- #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
- #define LORA_SYMBOL_TIMEOUT 0 // Symbols
- #define LORA_FIX_LENGTH_PAYLOAD_ON false
- #define LORA_IQ_INVERSION_ON false
- #define RX_TIMEOUT_VALUE 1000
- #define BUFFER_SIZE 30 // Define the payload size here
- char txpacket[BUFFER_SIZE];
- char rxpacket[BUFFER_SIZE];
- bool lora_idle=true;
- static RadioEvents_t RadioEvents;
- void OnTxDone( void );
- void OnTxTimeout( void );
- // ----------------------------------------------------------------------------
- // Define Pins
- // ----------------------------------------------------------------------------
- #define LED_PIN 35 // Define the LED pin
- #define VBAT_PIN 1 // Define the battery voltage pin
- #define VBAT_READ_CNTRL_PIN 37 // Control pin for battery voltage reading
- #define MOISTURE_PIN 7 // Define the moisture sensor pin
- #define MOISTURE_PIN_POWER 6 // Define the moisture sensor pin power
- // ----------------------------------------------------------------------------
- // Settings
- // ----------------------------------------------------------------------------
- //For Sector Number
- const int SECTOR_NUMBER = 2; // Define the sector number
- //For Deep Sleep Mode
- const uint32_t DEEP_SLEEP_TIME = 5; // Define the time duration in minutes
- // ----------------------------------------------------------------------------
- // Moving Average
- // ----------------------------------------------------------------------------
- #define WINDOW_SIZE 1000
- int INDEX = 0;
- int SUM = 0;
- int READINGS[WINDOW_SIZE];
- int AVERAGED = 0;
- // ----------------------------------------------------------------------------
- // Global Variables
- // ----------------------------------------------------------------------------
- int batteryValue = 0;
- // ----------------------------------------------------------------------------
- // Function to read sensor values and update moving average
- // ----------------------------------------------------------------------------
- void initializeMovingAverage(int& index, int& sum, int* readings, int& averaged, int window_size, int sensor_pin) {
- int value = analogRead(sensor_pin);
- for (int i = 0; i < window_size; i++) {
- readings[i] = value;
- sum += value;
- }
- averaged = sum / window_size;
- }
- void updateSensorValues(int& index, int& sum, int* readings, int& averaged, int window_size, int sensor_pin) {
- sum = sum - readings[index];
- int value = analogRead(sensor_pin);
- readings[index] = value;
- sum = sum + value;
- index = (index + 1) % window_size;
- averaged = sum / window_size;
- }
- // ----------------------------------------------------------------------------
- // Function to read BATTERY sensor values
- // ----------------------------------------------------------------------------
- int readBatterySensor() {
- unsigned long startTime = millis();
- while (millis() - startTime < 5000) {
- updateSensorValues(INDEX, SUM, READINGS, AVERAGED, WINDOW_SIZE, VBAT_PIN);
- }
- return AVERAGED;
- }
- // ----------------------------------------------------------------------------
- // Function to read MOISTURE sensor values
- // ----------------------------------------------------------------------------
- int readMoistureSensor() {
- unsigned long startTime = millis();
- while (millis() - startTime < 5000) {
- updateSensorValues(INDEX, SUM, READINGS, AVERAGED, WINDOW_SIZE, MOISTURE_PIN);
- }
- return AVERAGED;
- }
- // ----------------------------------------------------------------------------
- // Function to Enter in Deep Sleep Mode
- // ----------------------------------------------------------------------------
- void deepSleep() {
- // Set the MOISTURE_PIN_POWER pin off
- digitalWrite(MOISTURE_PIN_POWER, LOW);
- delay(100);
- // Put LoRa radio to sleep
- Radio.Sleep();
- delay(100);
- // End SPI communication
- SPI.end();
- delay(100);
- // Set pins as an analog input to save power
- pinMode(RADIO_DIO_1, ANALOG);
- pinMode(RADIO_NSS, ANALOG);
- pinMode(RADIO_RESET, ANALOG);
- pinMode(RADIO_BUSY, ANALOG);
- pinMode(LORA_CLK, ANALOG);
- pinMode(LORA_MISO, ANALOG);
- pinMode(LORA_MOSI, ANALOG);
- delay(100);
- // Configure power domains to optimize power consumption
- //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); // Turn off RTC peripherals
- //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF); // Turn off RTC slow memory
- //esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); // Turn off RTC fast memory
- //esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_OFF); // Turn off the crystal oscillator
- // Enable timer wake-up for deep sleep
- // esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
- // Enable timer wake-up for deep sleep if battery level is sufficient
- if (batteryValue >= 812) {
- esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
- }
- Serial.printf("\nEntering deep sleep mode for %u %s...\n", DEEP_SLEEP_TIME, (DEEP_SLEEP_TIME == 1) ? "minute" : "minutes");
- delay(100);
- // Enter deep sleep mode
- esp_deep_sleep_start();
- }
- // ----------------------------------------------------------------------------
- // Function to blink LED
- // ----------------------------------------------------------------------------
- void blinkLED() {
- digitalWrite(LED_PIN, HIGH); // LED on
- delay(200);
- digitalWrite(LED_PIN, LOW); // LED off
- delay(10);
- }
- // ----------------------------------------------------------------------------
- void setup() {
- Serial.begin(115200);
- // Set the LED pin as an output
- pinMode(LED_PIN, OUTPUT);
- // Set the VBAT_READ_CNTRL_PIN pin to turn it off
- pinMode(VBAT_READ_CNTRL_PIN, OUTPUT);
- digitalWrite(VBAT_READ_CNTRL_PIN, LOW);
- // Set the MOISTURE_PIN_POWER pin to turn it on
- pinMode(MOISTURE_PIN_POWER, OUTPUT);
- digitalWrite(MOISTURE_PIN_POWER, HIGH);
- delay(2000);
- Mcu.begin();
- Serial.println("\nInitializing LoRa Radio...");
- RadioEvents.TxDone = OnTxDone;
- RadioEvents.TxTimeout = OnTxTimeout;
- Radio.Init( &RadioEvents );
- Radio.SetChannel( RF_FREQUENCY );
- Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
- LORA_SPREADING_FACTOR, LORA_CODINGRATE,
- LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
- true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
- if(lora_idle == true) {
- delay(1000);
- Serial.println("\nGetting Analog Values from the sensors...");
- int batteryValue = readBatterySensor();
- int moistureValue = readMoistureSensor();
- // int batteryValue = analogRead(VBAT_PIN);
- // int moistureValue = analogRead(MOISTURE_PIN);
- Serial.print("Battery: ");
- Serial.println(batteryValue);
- Serial.print("Moisture: ");
- Serial.println(moistureValue);
- // Start a package
- sprintf(txpacket, "%d:%d:%d", SECTOR_NUMBER, batteryValue, moistureValue);
- Serial.printf("\r\nSending Packet: \"%s\", length %d\r\n",txpacket, strlen(txpacket));
- // Send the package out
- Radio.Send((uint8_t *)txpacket, strlen(txpacket));
- lora_idle = false;
- }
- }
- // ----------------------------------------------------------------------------
- void loop() {
- Radio.IrqProcess();
- }
- // ----------------------------------------------------------------------------
- void OnTxDone( void ) {
- Serial.println("TX done......");
- lora_idle = true;
- // Blink the LED when sending the package out
- blinkLED();
- delay(100);
- deepSleep();
- }
- // ----------------------------------------------------------------------------
- void OnTxTimeout( void ){
- Serial.println("TX Timeout......");
- lora_idle = true;
- delay(100);
- deepSleep();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement