You are on page 1of 4

handson.dmt.fh-joanneum.

at

ARDUINO
Workpackage: Description: Arduino UNO GSM shield This workpackage deals with the Telefonica GSM shield. It will show you how to send a short message to another cell phone.

Additional: there are two more examples available, where you can receive short messages or send and receive mobile data. Difficulty (1-10): Overview: Requirements: moderate Hardware: - Arduino UNO - Telefonica GSM shield - Sim card - USB cable Software: - Arduino Studio

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

SEND A SHORT MESSAGE


The first task of this workpackage is to send a short message. Therefor it is important to correctly attach the gsm shield to the Arduino UNO. The side with the sim card slot must face opposite the USB jack on the Arduino UNO. A second source of problems can be the sim card. Make sure your sim card is working properly by inserting it to a cell phone and connect it to a carrier network. If it is working in a cell phone correctly, eventually it will work with the Arduino.

Instructions
Prepare the UNO and the GSM Shield
Attach the GSM shield on top of the Arduino. Mount the antenna on top of the GSM Shield. Insert the sim card and lock the leaver in the correct position.

Write the program


The first thing you have to do is to import the specific library. The Arduino needs some extra code to work properly with the GSM shield. The library we need is called GSM.
#include <GSM.h>

Depending on your sim card you probably will need a pin number. You can define a constant with your pin number, or you can use it inline later. If you dont have a pin number, just initialize it with an empty string.
#define PINNUMBER "7520"

The next thing is, to initialize the library instance.


GSM gsmAccess; // include a 'true' parameter for debug enabled GSM_SMS sms;

As in every Arduino program we have the two methods setup() and loop(). This time we use the setup method to initialize the sim card with the pin number and connect it to the carrier network. We also setup a serial communication for better monitoring and response. To initialize the GSM shield, we call the gsmAccess.begin method and handover the pin number. When the pin is accepted, the library tries to connect to the carrier network automatically. When the GSM shield is connected the GSM_READY flag becomes true and sets notConnected to false. This causes the outer loop to exit and setup() is executed successfully.

Carelse, Pauger, Pilz

Instructions

handson.dmt.fh-joanneum.at

void setup() { Serial.begin(9600); Serial.println("SMS Messages Sender"); boolean notConnected = true; // Start GSM shield while(notConnected) { if(gsmAccess.begin(PINNUMBER)==GSM_READY) notConnected = false; else { Serial.println("Not connected"); delay(1000); } } Serial.println("GSM initialized"); }

After executing the setup() method loop is performed as long as the Arduino stays powered on.
void loop() {

Like when you are writing a text message on a normal cell phone, you have to enter a valid telephone number of the recipient. Therefore, let the user enter a phone number via the serial console.
Serial.print("Enter a mobile number: (End the number with a #)"); char remoteNumber[20]; // telephone number to send sms readSerial(remoteNumber); Serial.println(remoteNumber);

After successfully entering the number, you can now let the user enter the message. Therefor we create a character Array with a maximum length of 200 chars. A normal SMS just delivers 160 chars, but lets add some space just in case
Serial.print("Now, enter SMS content: (End the message with a #)"); char txtMsg[200]; readSerial(txtMsg);

Now the message can be entered in the console. Therefor we create the method readSerial, but before we implement it, lets finish the loop(). After successfully completing readSerial the message should be sent. This can be done with the sms.beginSMS(recipientNumber) method. Dont forget to handover the recipients phone number. Send your payload with sms.print(text) and send it with sms.endSMS().
Serial.println("SENDING"); Serial.println(); Serial.println("Message:"); Serial.println(txtMsg); // send the message sms.beginSMS(remoteNumber); sms.print(txtMsg);
Carelse, Pauger, Pilz 3

Troubleshooting

handson.dmt.fh-joanneum.at

sms.endSMS(); Serial.println("\nCOMPLETE!\n"); }

Our loop function is complete now. So lets implement readSerial. In the official example there is an issue with the termination of the input, therefore we implement our one termination literal. The # causes the array to terminate with zero termination and we leave the function with the return command. Now we have successfully entered our payload.
int readSerial(char result[]) { int i = 0; while(1) { while (Serial.available() > 0) { char inChar = Serial.read(); if (inChar == '#') { result[i] = '\0'; Serial.flush(); return 0; } if(inChar!='\r') { result[i] = inChar; //Serial.print(inChar); i++; } } } }

Flash the program


To flash the program on the Arduino UNO just plug it in and flash it the usual way.

Troubleshooting
- In case that the GSM shield refuses to connect to the carrier network, insert the sim card into a cell phone. Try again to connect, if it fails, please check the validity of the sim card. - In case you are not able to enter digits or characters, make sure you are using the Arduino Studio Serial monitor. This worksheet was entirely tested and performed on an Arduino Uno with a Telefonica GSM shield. A T-Mobile Austria pre-paid sim card was used.

Useful Resource
The official Arduino example
http://arduino.cc/en/Tutorial/GSMExamplesSendSMS

Youtube video tutorial


http://www.youtube.com/watch?v=PMOZQgi7K14

Carelse, Pauger, Pilz