Remote Control with SIM800 & Arduino UNO:

This sample project uses the SIM800L module with Arduino UNO.
The software serial port uses D3(RX) and D4(TX) pins of the UNO  to communicate with SIM800L.

Use the  Arduino serial monitor to see the activity.
Once the SIM800L has powered up, the NET LED flashes every second. Once the SIM has connected to a mobile network, the flashing rate changes to once every 3 seconds. The program waits until the SIM has connected to a network. Then it waits for the SMS command.



Components List:

1.  SIM800L GPRS module
2.  Arduino UNO or similar
3.  7805 Voltage Regulator IC
4.   BC337 / BC547  or similar NPN Transistor
5.   12V relay
6.   2 x Resistors 10K/.25W , 2K2/.25W
7.   3 x Capacitors 100UF/25V, 1000UF/16V, 100nF
7. 12V DC Adapter
8. Light Bulb or Heater, Mains plug.

Arduino Code:

//GPRS remote controll based on SIM800 unit
//It moniors the text messages comming in and respond accordingly
//Needs a working Mini SIM card.
//AMP technology @ 2019

#include <SoftwareSerial.h>
#include <stdio.h>

//Define Software serial pins.
SoftwareSerial Sim800Serial(4, 3); // SIM800 TX  and RX (TX-PD4,RX-PD3 on Arduino UNO)
//Define the Relay Control pin
int RelyContPin = 13;

String Mystring;
String RxString  = "";
char   RxChar    = ' ';
int Counter;
char inData[200];
char inChar = "";
byte index = 0;
//---------------------------------------------
void setup() {
  Sim800Serial.begin(9600);         //Software Serial 
  Serial.begin(9600);               //Hardware Serial
  pinMode(RelyContPin, OUTPUT);     // sets the digital pin as output
  digitalWrite(RelyContPin, LOW);   //Switch off the relay
//-----------------------------------------------  
//Wait for Mobile network connection:
//LED blinks once every second if not connected or no SIM
//Once connected, LED blinks once every 3 seconds 

  while (! Connected()){    
    Serial.println("Waiting for Mobile Network Connection");
    delay(1000);
  }
  Serial.println("Connected to Mobile network!");
   
//Setup Sim800 for text mode & SMS mode  
  Sim800Serial.println("AT+CMGF=1"); // Configuring TEXT mode
  updateSerial();
  Sim800Serial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
  updateSerial();
}
//================================================
void loop()
{
  if (Sim800Serial.available()){  //Any text message?
    ProcessTextCommand();         //process and respond to commands
  delay(500);
  } 
}

//============================================
void updateSerial()
{
  delay(500);
  Mystring = "";
  while (Sim800Serial.available()) 
  {
    Serial.write(Sim800Serial.read());//Forward what Serial received to Hardware Serial Port
  }
  while(Sim800Serial.available()) 
  {
    Serial.write(Sim800Serial.read());//Forward what Software Serial received to Hardware Serial Port
  } 
}
//============================================
void ProcessTextCommand()
{
// scan for data from SIM800 software serial port
  RxString = "";
  Counter = 0;
  //Read first 200 charactors from Som800 serial port
  while(Sim800Serial.available()){
    delay(1);  // short delay to give time for new data to be placed in buffer
  // get new character
    RxChar = char(Sim800Serial.read());
  //add first 200 character to string
    if (Counter < 200) {
      RxString.concat(RxChar);
      Counter = Counter + 1;
    }
  }
  RxString.trim();
  Serial.println(RxString);
  String PhoneNo = RxString.substring(7, 19);
  Serial.print("PhoneNo = ");
  Serial.println(PhoneNo);
  String DateAndTime = RxString.substring(25, 45);
  Serial.print("Date & Time = ");
  Serial.println(DateAndTime);
  String Message = RxString.substring(47);
  Serial.print(F("Message = "));
  Message.trim();
  Serial.println(Message);
  
  if (Message == "Power ON"){
      digitalWrite(RelyContPin, HIGH);
      Serial.println(F("Light ON")); 
  }
  if (Message == "Power OFF"){
      digitalWrite(RelyContPin, LOW); 
      Serial.println("Power OFF");
  }
}
//=====================================================

int Connected(){
  Sim800Serial.println("AT+CGATT?");
  delay(300);
  index = 0;
  while (Sim800Serial.available()){   
      inChar = Sim800Serial.read(); // Read a character
      inData[index] = inChar; // Store it
      index++; // Increment where to write next
      //inData[index] = '\0'; // Null terminate the string
    }
  if (inData[20] == 49){  //check 20th char, should be 1 if mobile network connected
    return 1;
  }
  else return 0;  
}


0Comments

For Comment you need to Login