sandeepmistry / arduino-LoRa

An Arduino library for sending and receiving data using LoRa radios.
MIT License
1.65k stars 630 forks source link

Problem with INT0 and INT1 #547

Closed hoseinaghajari closed 2 years ago

hoseinaghajari commented 2 years ago

Hi, to long story short, I want to use an interrupt pin of Arduino UNO for another purpose(I need an interrupt pin for RTC module, INT0 or INT1 to wake my Arduino after sleep for some minutes). And in the mean time I have a LoRa (sx1278 RA-02) module for sending and receive a packet in wake_up time. My problem is when I set irqPin = 3; or irqPin = 2; (same as default), LoRa works correctly(send and receive) but it trig the Interrupt pin which I've used for RTC just a few second after sleeping. I guess the problem is about the role of int_0 and int_1 in receiving process.

here is my code:

#include "LowPower.h"
#include <DS3232RTC.h>      
#include <SPI.h>     
#include <LoRa.h>  

const int wakeUpPin = 2;
const int time_interval=1;// Sets the wakeup intervall in minutes
const int csPin = 10;          // LoRa radio chip select
const int resetPin = 9;       // LoRa radio reset
const int irqPin = 2; 

int temp = 25;

void wakeUp()
{
     detachInterrupt(0); 
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("LoRa Duplex with callback (Node)");
  LoRa.setPins(csPin, resetPin, irqPin);// set CS, reset, IRQ pin

  if (!LoRa.begin(433E6)) {            
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                     
  }
  pinMode(LED_BUILTIN,OUTPUT);
  pinMode(wakeUpPin, INPUT_PULLUP); 
  digitalWrite(LED_BUILTIN,HIGH);
  setTime(0, 0, 0, 19, 1, 2022);   //set the system time to 23h31m30s on 13Feb2009
  RTC.set(now());  
    RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
    RTC.alarm(ALARM_1);
    RTC.alarmInterrupt(ALARM_1, false);
    RTC.squareWave(SQWAVE_NONE);
     time_t t; //create a temporary time variable so we can set the time and read the time from the RTC
    t=RTC.get();//Gets the current time of the RTC
    RTC.setAlarm(ALM1_MATCH_MINUTES , 0, minute(t)+time_interval, 0, 0);// Setting alarm 1 to go off 5 minutes from now
    // clear the alarm flag
    RTC.alarm(ALARM_1);
    // configure the INT/SQW pin for "interrupt" operation (disable square wave output)
    RTC.squareWave(SQWAVE_NONE);
    // enable interrupt output for Alarm 1
    RTC.alarmInterrupt(ALARM_1, true);
  LoRa.onReceive(onReceive);
  LoRa.receive();
  Serial.println("LoRa init succeeded.");
}

void loop() {
 delay(5000);
 Going_To_Sleep();
}

void Going_To_Sleep(){
    attachInterrupt(0, wakeUp, LOW);
    digitalWrite(LED_BUILTIN,LOW);          
    time_t t;                                             
    t=RTC.get();                                             
    Serial.println("Sleep  Time: "+String(hour(t))+":"+String(minute(t))+":"+String(second(t)));
    delay(1000);                                       
    LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF); 
    Serial.println("just woke up!");
    digitalWrite(LED_BUILTIN,HIGH);
    setTime(0, 0, 0, 19, 1, 2022);   //set the system time to 23h31m30s on 13Feb2009
    RTC.set(now());  
    t=RTC.get();
    Serial.println("WakeUp Time: "+String(hour(t))+":"+String(minute(t))+":"+String(second(t)));
    RTC.setAlarm(ALM1_MATCH_MINUTES , 0, minute(t)+time_interval, 0, 0);
    RTC.alarm(ALARM_1);
    pinMode(wakeUpPin, OUTPUT); 
    LoRa.setPins(csPin, resetPin, 2);
    String message = "Node2"+String(temp);  
    sendMessage(message);
    temp=random(22,25);
    Serial.println("Sending " + message);
    LoRa.receive(); 
  }

void sendMessage(String outgoing) {
  LoRa.beginPacket();       
  LoRa.print(outgoing);               
  LoRa.endPacket(true);
  LoRa.receive();              
}

void onReceive(int packetSize) {
  if (packetSize == 0) return;       
  String incoming = "";          
  while (LoRa.available()) {        
    incoming += (char)LoRa.read();  
  }
   String l = incoming.substring(0,7);
    if(l=="answer2"){
       digitalWrite(4,HIGH);
          delay(500);
           digitalWrite(4,LOW);
  Serial.print("   Message: " + incoming);
  Serial.print("   RSSI: " + String(LoRa.packetRssi()));
  Serial.print("   Snr: " + String(LoRa.packetSnr()));
  Serial.println();
    }
  LoRa.receive(); 
}