Control Two Relays with an SMS

Drive the relays of the MKR Relay Shield connected to a MKR GSM 1400 with an SMS.

Components and Supplies

About This Project

This project shows how to control a MKR Relay Shield using a MKR GSM 1400 and the Short Message Service (SMS); this solution works with a plain SIM with no data plan and it is suitable for applications where the GSM network is with poor coverage.

What You Need

The project uses a MKR GSM 1400, the antenna, a battery pack, a mobile phone, one SIM card, two LEDs, two 220 ohm resistors, a breadboard, cables, and a MKR Relay Shield.

  • The MKR GSM 1400 executes the sketch and manages the incoming and outgoing SMSs;
  • Antenna and battery pack are respectively used tor allow the connection to the cellular network with a good signal and to power the device when other power supplies are not available;
  • The MKR Relay Proto Shield is a board that includes two relays and is made for MKR format boards. It is used to switch loads and voltages usually not manageable with solid state solutions (MOSFET). In this project it just switches LEDs;
  • The mobile phone is required to send and receive the text messages;
  • The SIM card is required to access the GSM network and allow network operation;
  • LEDs, resistors, breadboard and cables, are used in this project to show the opening and closing of the relay contacts.
Arduino MKR GSM 1400, MKR Relay Proto Shield, LiPo battery and Antenna
Arduino MKR GSM 1400, MKR Relay Proto Shield, LiPo battery and Antenna

Hardware Setup

The antenna, the SIM and the battery pack should be connected to their own connector on head and tail of the MKR GSM 1400.

Connect the antenna and the LiPo battery to the MKR GSM 1400 board
Connect the antenna and the LiPo battery to the MKR GSM 1400 board

The circuit on the breadboard is simple: bring 5V and GND to the rails on the breadboard (5V to the red rail and GND to the black one). Put on the breadboard each LED so that the short leg (-) goes to the black rail with a wire jumper. Each long leg (+) goes to the 220 ohm resistor that is connected with a wire jumper to one of the NO contact on the relay connector. The common of each relay goes to the red rail of the breadboard (5V).

When the relay is excited, it closes the NO contact bringing 5V to the LED that will light up.
When the relay is excited, it closes the NO contact bringing 5V to the LED that will light up.

How It Works

This project uses the SMS capabilities of the GSM network to receive some text that is parsed and used to toggle the two relays. For security reasons, the sketch checks the number of the sender and this information must be stored in the arduino_secrets.h file. We don't know the status of the relays and therefore the sketch uses a "toggle" approach, where each SMS received with 1 or 2 as text toggles the status of the corresponding relays. To let you know the result of the operation, an SMS is sent back with the message "Relay (number), state: 0 or 1". Looking at the history of the messages you should be able to keep track of the relays status.

The Sketch

The first code section is used to includes the libraries required by the application; MKRGSM include all the GSM connection functionalities, these are available through the object GSMClient, GPRS and GSM, the header GSM_SMS import the APIs through which the sketch is able manage the SMS sended or received by the MKRGSM:

1#include <MKRGSM.h>
2GSM gsmAccess;
3GSM_SMS sms;

After the include section, we provide the two lines of code that refer to your secret pin number and incoming number. With this syntax the web editor generates a new Tab called Secrets where you input the two constants:

1char PINNUMBER [] = SECRET_PINNUMBER;
2String sender = SECRET_YOUR_NUMBER;

The setup section initializes all the objects used by the sketch; the GSM connection sequence that allow to register to the network occupies most of this section.

1void setup()
2{
3// initialize serial communications and wait for port to open:
4Serial.begin(9600);
5while (!Serial) {
6; // wait for serial port to connect. Needed for native USB port only
7}
8Serial.println("SMS Messages Receiver");
9// connection state
10bool connected = false;
11// Start GSM connection
12while (!connected) {
13if (gsmAccess.begin(PINNUMBER) == GSM_READY) {
14connected = true;
15} else {
16Serial.println("Not connected");
17delay(1000);
18}
19}
20Serial.println("GSM initialized");
21Serial.println("Waiting for messages");
22}

Last code section is the loop function where the basic control logic is executed. In our caseit simply checks if an SMS is available, if true, gets the SMS from the SIM and checks the text. If it is from the right sender it decodes the first character and switches the relay specified by the user. To confirm the operation it sends an SMS with the status of the Relay switched. It is important to delete the received SMS from the SIM card to avoid that it fills up and stops receiving SMSs. The sketch uses the ASCII code of the numbers 1 and 2 in the

swtch ... case
structure.

1void loop()
2{
3 int c;
4 String texmsg = "";
5 // If there are any SMSs available()
6 if (sms.available()) {
7Serial.println("Message received from:");
8// Get remote number
9sms.remoteNumber(senderNumber, 20);
10Serial.println(senderNumber);
11if (String(senderNumber) == sender) {
12// An example of message disposal
13// Any messages starting with # should be discarded
14if (sms.peek() == '#') {
15Serial.println("Discarded SMS");
16sms.flush();
17}
18c = sms.read();
19switch (c) {
20case 49:
21digitalWrite(1, !digitalRead(1));
22texmsg = "Relay 1, state: " + String(digitalRead(1));
23sms.beginSMS(senderNumber);
24sms.print(texmsg);
25sms.endSMS();
26break;
27case 50:
28digitalWrite(2, !digitalRead(2));
29texmsg = "Relay 2, state: " + String(digitalRead(2));
30sms.beginSMS(senderNumber);
31sms.print(texmsg);
32sms.endSMS();
33break;
34default:
35break;
36}
37Serial.println("\nEND OF MESSAGE");
38// Delete message from modem memory
39sms.flush();
40Serial.println("MESSAGE DELETED");
41} else {
42sms.flush();
43Serial.println("MESSAGE DELETED");
44}
45 }
46 delay(1000);
47}

Complete Sketch

How to Use It

Double check that your phone number is properly stored in the Secrets Tab, then upload the sketch and open the Serial Monitor to verify that the module properly attaches to the network.

From a simple mobile phone, send an SMS with just "1" or "2" to the mobile number associated with the SIM inserted in the MKR GSM 1400 board. You should see in a few seconds the LED turning on or off depending on its state before the SMS was sent. Together with the change of state the sketch sends back a message containing the current state of the toggled LED.

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.