Coding with SmartElex gsm shield for arduino

#include <SoftwareSerial.h>

SoftwareSerial gsmSerial(3, 2); // RX, TX pins for the GSM module

const int inputPin1 = 4;
const int inputPin2 = 5;
const int inputPin3 = 6;
const int outputPinRed = 9;
const int outputPinYellow = 10;
const int outputPinBlue = 11;
const int checkPin = 8;

String receivedMessage = "";
String phoneNumbers[] = {
  "+1234567890",   // Replace with the first phone number
  "+9876543210"    // Replace with the second phone number
};
int numPhoneNumbers = sizeof(phoneNumbers) / sizeof(phoneNumbers[0]);

void setup() {
  pinMode(inputPin1, INPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
  pinMode(outputPinRed, OUTPUT);
  pinMode(outputPinYellow, OUTPUT);
  pinMode(outputPinBlue, OUTPUT);
  pinMode(checkPin, INPUT); 

  digitalWrite(outputPinRed, LOW);
  digitalWrite(outputPinYellow, LOW);
  digitalWrite(outputPinBlue, LOW);

  Serial.begin(9600);
  gsmSerial.begin(9600);

  sendSMS("System initialized.");
}

void loop() {
  while (gsmSerial.available()) {
    char receivedChar = gsmSerial.read();
    receivedMessage += receivedChar;
    delay(10);

    if (receivedChar == '\n') {
      processMessage(receivedMessage);
      receivedMessage = "";
    }
  }
}

void processMessage(String message) {
  message.trim(); // Remove leading/trailing whitespaces

  if (message == "red") {
    digitalWrite(outputPinYellow, LOW);
    digitalWrite(outputPinBlue, LOW);
    delay(1000);
    digitalWrite(outputPinRed, HIGH);
    sendSMS("Relay turned on: RED");
    checkPinStatus();
  } else if (message == "yellow") {
    digitalWrite(outputPinRed, LOW);
    digitalWrite(outputPinBlue, LOW);
    delay(1000);
    digitalWrite(outputPinYellow, HIGH);
    sendSMS("Relay turned on: YELLOW");
    checkPinStatus();
  } else if (message == "blue") {
    digitalWrite(outputPinRed, LOW);
    digitalWrite(outputPinYellow, LOW);
    delay(1000);
    digitalWrite(outputPinBlue, HIGH);
    sendSMS("Relay turned on: BLUE");
    checkPinStatus();
  } else if (message == "status") {
    String status = "Showing status of input side: ";
    if (digitalRead(inputPin1) == HIGH) {
      status += "Red phase is high ";
    } else {
      status += "Red phase is low ";
    }
    if (digitalRead(inputPin2) == HIGH) {
      status += "Yellow phase is high ";
    } else {
      status += "Yellow phase is low ";
    }
    if (digitalRead(inputPin3) == HIGH) {
      status += "Blue phase is high ";
    } else {
      status += "Blue phase is low ";
    }
    sendSMS(status);
  } else {
    sendSMS("Invalid command.");
  }
}

void sendSMS(String message) {
  gsmSerial.println("AT+CMGF=1"); // Set SMS mode to text
  delay(100);

  for (int i = 0; i < numPhoneNumbers; i++) {
    gsmSerial.print("AT+CMGS=\"");
    gsmSerial.print(phoneNumbers[i]);
    gsmSerial.println("\"");
    delay(100);

    gsmSerial.println(message);
    delay(100);

    gsmSerial.println((char)26); // End message
    delay(1000);
  }
}

void checkPinStatus() {
  // Check if digital pin 8 is high
  if (digitalRead(checkPin) == HIGH) {
    sendSMS("Digital pin 8 is high.");
  } else {
    sendSMS("Digital pin 8 is not high.");
  }
}

When the system is turned on, "System initialized" message is received via sms. But other sms functions are not working

Can anybody help me out?

What about using the MKRGSM lib?

Are you referring to this one? This also didn't work. I'll try again and let you know. Thanks

When I am using the example code of sendSMS that is also not working with the library that I mentioned

This one should work. I adopted a bit, without testing, it's from the examples.
Check also the net coverage, you can't compare a mkr1400 with a modern smartphone.

You can do with

GSMScanner scannerNetworks;
scannerNetworks.getSignalStrength();
/*
 SMS sender

 This sketch, for the MKR GSM 1400 board,sends an SMS message
 you enter in the serial monitor. Connect your Arduino with the
 GSM shield and SIM card, open the serial monitor, and wait for
 the "READY" message to appear in the monitor. Next, type a
 message to send and press "return". Make sure the serial
 monitor is set to send a newline when you press return.

 Circuit:
 * MKR GSM 1400 board
 * Antenna
 * SIM card that can send SMS

 created 25 Feb 2012
 by Tom Igoe
*/

// Include the GSM library
#include <MKRGSM.h>

#include "arduino_secrets.h" 
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = Here goes your pin or remove pin from SIM;

// initialize the library instance, with true you see all the AT console in the serial monitor
GSM gsmAccess(true);
GSM_SMS sms;
GSMScanner scannerNetworks;

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("SMS Messages Sender");

  // connection state
  bool connected = false;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while (!connected) {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
      connected = true;
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  Serial.println("GSM initialized");
}

void loop() {
  Serial.print("Signal strenght: ");
  Serial.println(scannerNetworks.getSignalStrength());
  Serial.print("Enter a mobile number: ");
  char remoteNum[20];  // telephone number to send sms
  readSerial(remoteNum);
  Serial.println(remoteNum);

  // sms text
  Serial.print("Now, enter SMS content: ");
  char txtMsg[200];
  readSerial(txtMsg);
  Serial.println("SENDING");
  Serial.println();
  Serial.println("Message:");
  Serial.println(txtMsg);

  // send the message
  sms.beginSMS(remoteNum);
  sms.print(txtMsg);
  sms.endSMS();
  Serial.println("\nCOMPLETE!\n");
}

/*
  Read input serial
 */
int readSerial(char result[]) {
  int i = 0;
  while (1) {
    while (Serial.available() > 0) {
      char inChar = Serial.read();
      if (inChar == '\n') {
        result[i] = '\0';
        Serial.flush();
        return 0;
      }
      if (inChar != '\r') {
        result[i] = inChar;
        i++;
      }
    }
  }
}

I succeeded in sending an sms to my phone by using a different code. But I'm getting stuck while receiving the sms from my phone. The GSM is not responding to any sms sent from the phone. Can it be done without using the MKR GSM library?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.