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 = 1; // Define the sector number
- //For Deep Sleep Mode
- const uint32_t DEEP_SLEEP_TIME = 10; // Define the time duration in minutes
- // ================================================================================================
- int batteryAnalogValue;
- int moistureAnalogValue;
- bool executedOnce = false;
- // ================================================================================================
- // Function to Read Battery Analog Value
- // ================================================================================================
- int batteryFunction() {
- delay(2000);
- int totalBattery = 0;
- for (int i = 0; i < 10; i++) {
- totalBattery += analogRead(VBAT_PIN);
- delay(100);
- }
- int averageBattery = totalBattery / 10;
- return averageBattery;
- }
- // ================================================================================================
- // Function to Read Moisture Analog Value
- // ================================================================================================
- int moistureFunction() {
- delay(2000);
- int totalMoisture = 0;
- for (int i = 0; i < 10; i++) {
- totalMoisture += analogRead(MOISTURE_PIN);
- delay(100);
- }
- int averageMoisture = totalMoisture / 10;
- return averageMoisture;
- }
- // ================================================================================================
- // Function to Enter in Deep Sleep Mode
- // ================================================================================================
- void enterDeepSleep() {
- // Put LoRa radio to sleep
- Radio.Sleep();
- // End SPI communication
- SPI.end();
- // Set pins to analog mode
- 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);
- esp_sleep_enable_timer_wakeup(DEEP_SLEEP_TIME * 60 * 1000000);
- Serial.printf("\nEntering in deep sleep mode for %u ", DEEP_SLEEP_TIME);
- if (DEEP_SLEEP_TIME == 1) {
- Serial.println("minute...\n");
- } else {
- Serial.println("minutes...\n");
- }
- esp_deep_sleep_start();
- }
- // ================================================================================================
- void setup() {
- Serial.begin(115200);
- // Set the LED pin as an output and turn it off
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW);
- // Set the Vext pin as an output and turn it off
- pinMode(Vext, OUTPUT);
- digitalWrite(Vext, HIGH);
- // Configure the VBAT_READ_CNTRL_PIN as an output and turn it off
- pinMode(VBAT_READ_CNTRL_PIN, OUTPUT);
- digitalWrite(VBAT_READ_CNTRL_PIN, LOW);
- Serial.println("\nGetting Analog Values from the sensors...");
- delay(1000);
- batteryAnalogValue = batteryFunction();
- Serial.print("Battery: ");
- Serial.println(batteryAnalogValue);
- // Configure the VBAT_PIN as an output and turn it off
- pinMode(VBAT_PIN, OUTPUT);
- digitalWrite(VBAT_PIN, LOW);
- delay(100);
- // Set the MOISTURE_PIN_POWER pin as an output and turn it on
- pinMode(MOISTURE_PIN_POWER, OUTPUT);
- digitalWrite(MOISTURE_PIN_POWER, HIGH);
- delay(1000);
- moistureAnalogValue = moistureFunction();
- Serial.print("Moisture: ");
- Serial.println(moistureAnalogValue);
- // Set the MOISTURE_PIN pin as an output and turn it off
- pinMode(MOISTURE_PIN, OUTPUT);
- digitalWrite(MOISTURE_PIN, LOW);
- // Set the MOISTURE_PIN_POWER pin as an output and turn it off
- pinMode(MOISTURE_PIN_POWER, OUTPUT);
- digitalWrite(MOISTURE_PIN_POWER, LOW);
- Mcu.begin();
- Serial.print("\nInitializing LoRa Radio...");
- delay(1000);
- 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 );
- }
- // ================================================================================================
- void loop() {
- if (!executedOnce) {
- if(lora_idle == true) {
- sprintf(txpacket, "%d:%d:%d", SECTOR_NUMBER, batteryAnalogValue, moistureAnalogValue);
- 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;
- }
- executedOnce = true;
- }
- Radio.IrqProcess();
- }
- // ================================================================================================
- void OnTxDone( void ) {
- Serial.println("TX done......");
- lora_idle = true;
- delay(100);
- enterDeepSleep();
- }
- // ================================================================================================
- void OnTxTimeout( void ) {
- Radio.Sleep();
- Serial.println("TX Timeout......");
- lora_idle = true;
- delay(100);
- enterDeepSleep();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement