B4R Tutorial GSM / GPRS - Control the Arduino with SMS messages

SS-2016-05-11_13.27.00.jpg


The GSM shield allows the Arduino to connect to cellular networks. This opens the door for many interesting outdoor solutions.

There are all kinds of shields available. Make sure that the shield you are using is configured to work with pins other than 0 and 1 (which are used for programming the Arduino). We will use SoftwareSerial with AsyncStreams to communicate with the GSM module.

The GSM shield is controlled with AT commands. There are many online resources about the supported commands. Here is a one: http://www.codeproject.com/Articles/85636/Introduction-to-AT-commands-and-its-uses

The GSM code module, included in the attached example, implements the most common commands.

- Sending and receiving SMS messages.
- Making phone calls (the speaker part is not implemented).
- Incoming phone calls.
- Checking the registration status.

Only one task can be done at a time. So there is a "busy" flag that is set and cleared to make sure that commands do not mix.

In this specific example the led is controlled with SMS messages. The button sends a "click!!!" message:
B4X:
Sub btn_StateChanged (State As Boolean)
   If State = False Then
     GSM.SendSMS(phoneNumber, "Click!!!")
   End If
End Sub

Sub MessageArrived(msg() As Byte)
   If msg = "On" Then
     led.DigitalWrite(True)
   Else if msg = "Off" Then
     led.DigitalWrite(False)
   Else
     Log("error: ", msg)
   End If
End Sub

Useful information about these shields:
http://www.seeedstudio.com/wiki/GPRS_Shield_V1.0
http://www.seeedstudio.com/wiki/GPRS_Shield_V2.0
 

Attachments

  • GSM_Example.zip
    2.3 KB · Views: 1,587
Last edited:

roberto64

Active Member
Licensed User
Longtime User
Hi Erel,
How can I convert the GSM module so that I can carry it to B4J, I'm experimenting with an example with B4J to connect with my smartphone and send and receive messages directly from B4J.
Greetings
 

falbertini

Member
Licensed User
Longtime User
Hello Erel, could you post wire schema of GPRS Shield to Arduino for the project as in your initial image ? Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

Shay

Well-Known Member
Licensed User
Longtime User
I got the A6 module,
I have this code:
B4X:
Sub MessageArrived(msg() As Byte)
    Log ("Got SMS", msg)
End Sub

when I am getting SMS, I see only gibrish in the logs
I tried changing both Serial baudrate, and GSM serial baud rate
still cannot see any text.
beside appstart, all is gibrish from A6
 

Shay

Well-Known Member
Licensed User
Longtime User
update: I switch between rx and tx pin, now I can see text (weird since it is connected ok),
but I am getting this:
****************
+CIEV: "MESSAGE",1
+CMT: ,29
07917952939899F9040C91795224229BFD06DDDF723619

and I am sending to the a6: "Hello World"
 

Shay

Well-Known Member
Licensed User
Longtime User
Not related to my board, this is general question,
if for example I am getting SMS, how do I manipulate the text, since all is in byte()
meaning putting into var (do I need to use bc.copy?)
 
Hi everybody, I recently purchased a GSM/GPRS shield identical to the one used in the example posted by Erel and uploaded the GSM_Example code. Unfortunately it doesn't work. More specifically, I noticed that in GSM Module, the "busy" variable remains setted to TRUE after the Sub SendCommand is executed, therefore I cannot send any SMS.

I also forced the execution of the sub by commenting the line

B4X:
If busy Then Return False

but, anyway, it doesn't send any SMS.

I tried uploading the source code via the .ino sketch attached and it works properly.

#include <SoftwareSerial.h>

SoftwareSerial SIM900(7, 8);

void setup() {
Serial.begin(115200);
SIM900.begin(19200);

Serial.println("AppStart");
delay(20000);
sendSMS();
}

void loop() {

}

void sendSMS() {

SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+xxxxxxxxx\"");
delay(100);
SIM900.println("Hello, how doin'?!!");
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}

I also tried to substitute the original code in the Sub EnableSMSEvents with the "AT+CMGF=1" command, and to add the slashes (\) to the phone number, but I failed.
Has anyone an idea of what I've done wrong?

Thanks in advance

Maurizio
 
Top