0

Good day, I would like to ask some help for my program code regarding the problem of nRF24L01 and sim800L. I have the code below

#include <Sleep_n0m1.h>
#include <Time.h>
#include <TinyGPS++.h>           // Include TinyGPS++ library
#include <SoftwareSerial.h>      // Include software serial library
#include <Sim800l.h>
#define SIM800_Send 2
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


RF24 radio(7, 8);
TinyGPSPlus gps;
#define S_RX    3                // Define software serial RX pin
#define S_TX    4             // Define software serial TX pin
SoftwareSerial mySerial(10,11);
SoftwareSerial SoftSerial(S_RX, S_TX);    // Configure SoftSerial library

char incomingByte; 
String inputString;
long previousMillis = 0;
long interval = 1000;
int green= 9;
int red= 13;
int buzzer=12;
int i = 0;
int x = 0;
const byte rxAddr[6] = "00001";
String content = "";
char text;


void setup() {
  Serial.begin(9600);
  SoftSerial.begin(9600);
  while (!Serial);
  Serial.begin(9600);
  pinMode(A0,OUTPUT); //tap indicator
  pinMode(A2,INPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(buzzer, OUTPUT);
  digitalWrite(buzzer,HIGH);
  pinMode(SIM800_Send,INPUT); 
  radio.begin();
  mySerial.begin(9600); 
     while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\"");
      delay(500);
  }


void loop() {
  nrf();
  //tracking();
  battery();
}

void tracking(){
  if(i == 0){
   digitalWrite(buzzer,HIGH);
   mySerial.begin(9600);    
    while(mySerial.available()){   //if there was a text received
      delay(100);
      // Serial Buffer
      while(mySerial.available()){
        incomingByte = mySerial.read(); // the GSM reads the character
        inputString += incomingByte;    //if the incoming string was a new line
        }
        delay(10);      
        Serial.println(inputString);
        inputString.toUpperCase(); // Uppercase the Received Message
         if (inputString.indexOf("OFF") > -1){        
          delay(100);
          mySerial.end();
          delay(1000);
          SoftSerial.begin(9600);
          delay(1000);
          i = 1;
          x = 1;
          delay(1000);
          }          
        delay(50);
        //Delete Messages & Save Memory
        if (inputString.indexOf("OK") == -1){
        mySerial.println("AT+CMGDA=\"DEL ALL\"");
        delay(1000);}
        inputString = "";
  }
  }
  if(i == 1){
// the buzzer would make a noise if the device receives an off message  
    if(x==1)
   {
    digitalWrite(buzzer,LOW);
    //delay(120000);
    digitalWrite(buzzer,HIGH);
    x=0;
    } 
// finding the coordinates
    while(SoftSerial.available()>0) {    //if the GPS module was available
    if(gps.encode(SoftSerial.read())) {  //if the GPS has coordinates
      if(gps.location.isValid()) {   // checking if it is valid
      // printing at the Serial monitor the latitude and the longitude 
       Serial.print("Latitude   = ");
       Serial.println(gps.location.lat(), 6);
       Serial.print("Longitude  = ");
       Serial.println(gps.location.lng(), 6);
       digitalWrite(buzzer, LOW);
        // sending the latitude and the longitude to the server
       delay(1000);
        mySerial.write("AT+CMGF=1\r\n");   // Set the module into text mode
        mySerial.write("AT+CMGS=\"+639324497090\"\r\n");  // send the text
        mySerial.print(gps.location.lat(), 6);
        mySerial.print(" ");
        mySerial.print(gps.location.lng(), 6);
        mySerial.write((char)26);   // write all the characters*/
      }
    }
  }
  delay(100);
  }

}

void nrf(){
  digitalWrite(8, LOW);
  radio.openReadingPipe(0, rxAddr);
  radio.startListening();
  if(radio.available()){   
    //radio.powerUp();
    radio.read(&text, sizeof(text));
    //Serial.println(text);
    if(text == '2'){
      content = "F7DB9173";
      Serial.println(content);\
      for(x=0; x<=1; x++)
      {
       digitalWrite(green,LOW);
        digitalWrite(red,LOW);
      digitalWrite(A0,HIGH);
      delay(3000);
      }
      digitalWrite(A0,LOW);
  }
  else if(text == '1'){
      content = "E5C1A179";
      Serial.println(content);
      digitalWrite(A0,LOW);
      delay(100);
    }
  }

}



void battery(){
    int sensorValue = analogRead(A2);
    float voltage = sensorValue * (5.0 / 1023.0);
    voltage = (voltage/2.5)*5;
    //Serial.println(voltage);
    unsigned long currentMillis = millis();
            if (currentMillis - previousMillis > interval) 
            {
              previousMillis = currentMillis;
                if (voltage <= 2.5)
                {
                  Serial.println("Low Battery");
                  digitalWrite(red,HIGH);
                  digitalWrite(green,LOW);                
                }
                 else{             
                  digitalWrite(green,HIGH);
                  digitalWrite(red,LOW);
                  //text="HIGH BATTERY";              
                }
            }
    delay(100);

}

When the radio.begin() initializes, the code for sim800l in the setup (while(!mySerial.available()){
        mySerial.println("AT");
        delay(1000); 
        Serial.println("Connecting...");
        }
      Serial.println("Connected!");  
      mySerial.println("AT+CMGF=1");  //Set SMS to Text Mode 
      delay(1000);  
      mySerial.println("AT+CNMI=1,2,0,0,0");  //Procedure to handle newly arrived messages(command name in text: new message indications to TE) 
      delay(1000);
      mySerial.println("AT+CMGL=\"REC UNREAD\"");
      delay(500);) 

Keeps on responding as CONNECTING but when I remove the radio.begin(), it works perfectly fine. Is there a way that you could help me? What I need is the nRF24L01 and sim800L are just to wait for the incoming data. What I did was, when coding separately for nRF and sim800L, they work fine but when they are merged together, it no longer work. Hoping you could help me with my year-end project. I am using only one Arduino Nano for both of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.