I'm trying to use SMS messages to turn a relay on/off but before doing that i need to have a password system before allowing the user to turn the relay on/off. Basically, you have to send the password to the GSM via SMS first, then have an option select of ON or OFF, which you also send to the GSM. I can already do the password and ON/OFF of the relay separately, but I'm having trouble trying to combine them together. Here is the code:

String _buffer;
String number = "+639562284415";
String textMessage;
String relayState = "HIGH";

int relay = 24;
int _timeout;

void setup() {
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);

  Serial.print("SIM900 ready...");

void loop() {
  if (Serial1.available() > 0) {
    textMessage = Serial1.readString();
  if (passFlag == 0) {
    if (textMessage.indexOf("1231") >= 0) {
      if (textMessage.indexOf("ON") >= 0) {
        // Turn on relay and save current state
        digitalWrite(relay, LOW);
        relayState = "on";
        Serial.println("Relay set to ON");
        textMessage = "";

      if (textMessage.indexOf("OFF") >= 0) {
        // Turn off relay and save current state
        digitalWrite(relay, HIGH);
        relayState = "off";
        Serial.println("Relay set to OFF");
        textMessage = "";

void commandList() {
  Serial.println ("Sending Message");
  Serial1.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  Serial.println ("Set SMS Number");
  Serial1.println("AT+CMGS=\"" + number + "\"\r"); //Mobile phone number to send message
  String SMS = "Enter your command!";
  Serial1.println((char)26);// ASCII code of CTRL+Z
  _buffer = _readSerial();

String _readSerial() {
  _timeout = 0;
  while  (!Serial1.available() && _timeout < 12000  )
  if (Serial1.available()) {
    return Serial1.readString();

Basically what happens with this code is that it succeeds with the password entry, but it somehow skips over the ON/OFF option select. Since I used a flag to increment the password entry (so it doesn't endlessly send me the "Enter your command" message), the loop completely stop after loop. I want to use switch case but I don't really know how to use it in this case. I don't really know how to best approach this problem, so if anyone can give some advice I would really appreciate it! BTW I'm using a SIM800L and Arduino MEGA for this

  • If you first receive a password and then receive a command separately, in my opinion, the password is useless, because the command can be send from another sender and would be aceepted even if the password comes from the first sender. So I would send the command and the password in one message. e.g #<password>#ON or so. – Peter Paul Kiefer Feb 25 '20 at 15:32
  • The problem with the code above is that you only look after the pass word or one of the commands if passFlag is 0 (btw the variable is not declared in the code above. So you check for the password that succeeds the give me a command SMS is send and then you test for the command before the command SMS was received. In a later loop run the command SMS will be received but the if statements that test for the command will never be executed, because passFlag>0. You could try to move the if (textMessage.indexOf("???") >= 0) out of the if (passFlag == 0) { block. – Peter Paul Kiefer Feb 25 '20 at 15:44
  • i decided to go with making the password go together with the command, because what you said makes sense to me (password is useless if because command can come from another sender after entering the password). thanks! – YMSmash Feb 25 '20 at 16:02
  • That has been a wise decision IMHO. Your welcome. Cheers. – Peter Paul Kiefer Feb 25 '20 at 16:27

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.