Go Down

Topic: Help with Relay Control via SMS (Read 335 times) previous topic - next topic

luego

Hi guys! I am trying to control a single channel relay via SMS. The problem I have is that I cannot send/receive messages or even dial the module. When I try to dial the number on the module, it doesn't even ring on my phone (cannot be reached). I tried googling the solution but I still can't make it work. There's also this thing with the serial monitor where it only repeatedly displays: 

+CPIN: READY
III
RDY
+CFUN

I am using arduino UNO, a sim900A gsm module, and a single channel relay. I have also tried the other baud rates but the one I am using is the only one that works. Here is my code:

Code: [Select]
#include <SoftwareSerial.h> // Library for using serial communication
SoftwareSerial SIM900(7, 8); // Pins 7, 8 are used as used as software serial pins

String incomingData;   // for storing incoming serial data
String message = "";   // A String for storing the message
int relay_pin = 2;    // Initialized a pin for relay module

void setup()
{
  Serial.begin(9600); // baudrate for serial monitor
  SIM900.begin(115200); // baudrate for GSM shield

  pinMode(relay_pin, OUTPUT);   // Setting erlay pin as output pin
  digitalWrite(relay_pin, HIGH);  // Making relay pin initailly low

  // set SMS mode to text mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
 
  // set gsm module to tp show the output on serial out
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  delay(100);
}

void loop()
{
  //Function for receiving sms
  receive_message();

  // if received command is to turn on relay
  if(incomingData.indexOf("Led_on")>=0)
  {
    digitalWrite(relay_pin, LOW);
    message = "Led is turned ON";
    // Send a sms back to confirm that the relay is turned on
    send_message(message);
  }
 
  // if received command is to turn off relay
  if(incomingData.indexOf("Led_off")>=0)
  {
    digitalWrite(relay_pin, HIGH);
    message = "Led is turned OFF";
    // Send a sms back to confirm that the relay is turned off
    send_message(message);
  }       
}

void receive_message()
{
  if (SIM900.available() > 0)
  {
    incomingData = SIM900.readString(); // Get the data from the serial port.
    Serial.print(incomingData);
    delay(10);
  }
}

void send_message(String message)
{
  SIM900.println("AT+CMGF=1");    //Set the GSM Module in Text Mode
  delay(100); 
  SIM900.println("AT+CMGS=\"+63xxxxxxxxxx\""); // Replace it with your mobile number
  delay(100);
  SIM900.println(message);   // The SMS text you want to send
  delay(100);
  SIM900.println((char)26);  // ASCII code of CTRL+Z
  delay(100);
  SIM900.println();
  delay(1000); 
}

I have already replaced the x's with my number and the wirings are also correct if you'd ask.

Can anyone help me with this problem? I have already tried simpler codes that only focus on sending and receiving messages with the module but still, no luck. Thank you in advance!

ronaldgadget

I have what you want to do working with a Arduino Mega and a SIM7000E modem. It took hours to get everything working reliably - and ended up not using the modem libraries and process everything myself.

your modem response

Code: [Select]
+CPIN: READY
III
RDY


is suspicious. I recommend to get the modem AT command manual and carefully check what is expected at reset. Note when using software-serial, often characters in communication are lost - leading to things not working at all.

I get the following with the SIM7000E at power on reset:
Code: [Select]
+CFUN: 1   
+CPIN: SIM PIN

and then I need to enter the PIN code.

Good luck, let me know.

Ronald

Ronald Gadget - discovered Arduino in Sept 2014 - built 4 systems with it. Professionally building uServers - see www.zurich.ibm.com/microserver

rivelino911

#2
Feb 01, 2021, 09:04 pm Last Edit: Feb 01, 2021, 09:27 pm by rivelino911
Hi ,
Try that
Doesn't matter in software is SIM800. Commands are the same.
Install FONA library , from   Sketch -> Include library -> Manage library. Type FONA and install Adafruit FONA library.
Change in software Pin number according your set-up.

Also check this : http://acoptex.com/wp/basics-project-105/

Code: [Select]
/*
  Tutorial: Control servo movement with gesture using ESP32 and Arduino
  
  Board:
  - TTGO T-Call ESP32 with SIM800L GPRS Module
    https://my.cytron.io/p-ttgo-t-call-esp32-with-sim800l-gprs-module
  Output:
  - 1CH Active H/L 5V OptoCoupler Relay Module
    https://my.cytron.io/c-electronic-components/c-relays/p-1ch-active-h-l-5v-optocoupler-relay-module
  Connections   TTGO | Servo
                  5V - DC+
                 GND - DC-
                  IN - 14
  External libraries:
  - Adafruit Fona Library by Adafruit Version 1.3.5
*/

#include "Adafruit_FONA.h"

#define SIM800L_RX     7
#define SIM800L_TX     8
#define SIM800L_PWRKEY 4
#define SIM800L_RST    5
#define SIM800L_POWER  23

char replybuffer[255];

HardwareSerial *sim800lSerial = &Serial1;
Adafruit_FONA sim800l = Adafruit_FONA(SIM800L_PWRKEY);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);

#define LED_BLUE  14
#define RELAY 13

String smsString = "";
//char imei2[16]  = {866782042363462} ;

void setup()
{
  pinMode(LED_BLUE, OUTPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(SIM800L_POWER, OUTPUT);

  digitalWrite(LED_BLUE, HIGH);
  digitalWrite(SIM800L_POWER, HIGH);

  Serial.begin(115200);
  Serial.println(F("ESP32 with GSM SIM800L"));
  Serial.println(F("Initializing....(May take more than 10 seconds)"));
  
  delay(10000);

  // Make it slow so its easy to read!
  sim800lSerial->begin(4800, SERIAL_8N1, SIM800L_TX, SIM800L_RX);
  if (!sim800l.begin(*sim800lSerial)) {
    Serial.println(F("Couldn't find GSM SIM800L"));
    while (1);
  }
  Serial.println(F("GSM SIM800L is OK"));
  sim800l.unlockSIM("3375");
 
  char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
  uint8_t imeiLen = sim800l.getIMEI(imei);
  if (imeiLen > 0) {
    Serial.print("GSM IMEI: "); Serial.println(imei);
  char* imei2[16] = {imei};
   // if (imei!= imei2){
      Serial.println("GSM IMEI nu este bun!! ");
      Serial.println(imei2[16]);
   // }
    
      
  }

  // Set up the FONA to send a +CMTI notification
  // when an SMS is received
  sim800lSerial->print("AT+CNMI=2,1\r\n");
 
  Serial.println("GSM SIM800L Ready");
}

long prevMillis = 0;
int interval = 1000;
char sim800lNotificationBuffer[64];          //for notifications from the FONA
char smsBuffer[250];
boolean ledState = false;

void loop()
{
  if (millis() - prevMillis > interval) {
    ledState = !ledState;
    digitalWrite(LED_BLUE, ledState);

    prevMillis = millis();
  }
  
  char* bufPtr = sim800lNotificationBuffer;    //handy buffer pointer

  if (sim800l.available()) {
    int slot = 0; // this will be the slot number of the SMS
    int charCount = 0;

    // Read the notification into fonaInBuffer
    do {
      *bufPtr = sim800l.read();
      Serial.write(*bufPtr);
      delay(1);
    } while ((*bufPtr++ != '\n') && (sim800l.available()) && (++charCount < (sizeof(sim800lNotificationBuffer)-1)));
    
    //Add a terminal NULL to the notification string
    *bufPtr = 0;

    //Scan the notification string for an SMS received notification.
    //  If it's an SMS message, we'll get the slot number in 'slot'
    if (1 == sscanf(sim800lNotificationBuffer, "+CMTI: \"SM\",%d", &slot)) {
      Serial.print("slot: "); Serial.println(slot);
      
      char callerIDbuffer[32];  //we'll store the SMS sender number in here
      
      // Retrieve SMS sender address/phone number.
      if (!sim800l.getSMSSender(slot, callerIDbuffer, 31)) {
        Serial.println("Didn't find SMS message in slot!");
      }
      Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);

      // Retrieve SMS value.
      uint16_t smslen;
      // Pass in buffer and max len!
      if (sim800l.readSMS(slot, smsBuffer, 250, &smslen)) {
        smsString = String(smsBuffer);
        Serial.print(F("FROM: "));
        Serial.println(callerIDbuffer);
        Serial.print("MESAGE SMS : ");
        Serial.println(smsString);
      }

      if (smsString.equalsIgnoreCase("RELAY ON")) {
        Serial.println("Relay is activated.");
        digitalWrite(RELAY, HIGH);
        delay(100);
        // Send SMS for status
        if (!sim800l.sendSMS(callerIDbuffer, "Relay is activated.")) {
          Serial.println(F("Failed"));
         } else {
          Serial.println(F("Sent!"));
        }
      }
      else if (smsString.equalsIgnoreCase("RELAY OFF")) {
        Serial.println("Relay is deactivated.");
        digitalWrite(RELAY, LOW);
        delay(100);
        // Send SMS for status
        if (!sim800l.sendSMS(callerIDbuffer, "Relay is deactivated.")) {
          Serial.println(F("Failed"));
        } else {
          Serial.println(F("Sent!"));
        }
      }

      while (1) {
        if (sim800l.deleteSMS(slot)) {
          Serial.println(F("OK!"));
          Serial.print(F("Quality signal  "));
           sim800l.println("AT+CSQ");
          break;
        }
        else {
          Serial.print(F("Couldn't delete SMS in slot ")); Serial.println(slot);
          sim800l.print(F("AT+CMGD=?\r\n"));
          Serial.print(F("Quality signal "));
           sim800lSerial->print("AT+CSQ?\r\n");
        }
      }
    }
  }
}

 

Go Up