Connect an Arduino GSM Shield with Cellular Networks
Published: 2024-01-12 10:12:34

**The principles described in this blog from our archive are still relevant today but the specific examples this article uses target GPRS, which is not available in all Countries. Please bear this in mind if you are following the steps in this tutorial**

In the past, we have showcased the PubNub Arduino-controlled smart home at CES and other conferences, and a frequently asked question was if PubNub worked over GSM/GPRS networks. Could I connect my Arduino GSM/GPRS shield to the Internet to [stream data bidirectionally](https://www.pubnub.com/solutions/data-streaming/)? We knew it would work, but had never actually implemented it ourselves.

Until now…

In this blog post, we’ll walk through a simple demonstration of PubNub with GSM/GPRS. In other words, we’re going to easily connect our Arduino to a mobile cell network. This is incredibly exciting and opens the doors for mobile [IoT applications](https://www.pubnub.com/solutions/iot-device-control/), like connected car or drones, or apps without access to a WiFi signal, like weather stations or sprinkler systems.

So let’s get started!

[![GRPS SLD_04](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpg/748bdd937c0bc4de3db714a44c5d7c84/GRPS-SLD_04.jpg "GRPS SLD_04")](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpg/748bdd937c0bc4de3db714a44c5d7c84/GRPS-SLD_04.jpg)

### Arduino GSM/GPRS Parts List

Here’s what I used:

-   Arduino UNO Rev3
-   Seeed Studio GPRS Shield V2.0
-   SIM Card (standalone)
-   C size batteries (x5)
-   C size battery holder (x5)
-   Smartphone that accepts a physical SIM
-   Laptop

I bought the GPRS Shield and AT&T SIM card from Amazon; they have a section for universal SIMS. It may be hard to find the standalone SIM card in other retail stores.

### Setting Up the SIM Card

First, you’ll need to unlock your SIM card and attach a data plan to it. This is why a smartphone is in the parts list.

Put the SIM card in your phone and read the instructions included within the package. In the case of the AT&T SIM I purchased, I dialed a series of numbers on my phone to activate the card, and then configured a plan online. Make sure your mobile plan supports data transfer, not just calls and texts.

Once you’ve activated the SIM card, try connecting to a website on your mobile phone. If it succeeds, you have data and it’ll work within your Arduino.

### Arduino GPRS Setup

Take your Arduino GPRS Shield, and plug the SIM card in. Seeed Studio has [a great tutorial about how to do this](https://wiki.seeedstudio.com/GPRS_Shield_V2.0/). Follow that tutorial but stop and return here before uploading code in “Test Setup”.

[![Arduino GPRS GSM](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hph/9cb2b3cc0d462bd0ccce25f4696c360c/Arduino-GPRS-GSM.png "Arduino GPRS GSM")](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hph/9cb2b3cc0d462bd0ccce25f4696c360c/Arduino-GPRS-GSM.png)

### Arduino Libraries

You’ll need a whole heap of libraries to get this thing setup on Arduino, but trust me, it makes it a whole lot easier. I tried writing them myself!

You can find the [specific libraries for the Seeed Studio GPRS Shield V2 here](https://github.com/Seeed-Studio/GPRS_Shield_Suli). Pay attention to the docs, you need 3 libraries to get this baby rolling:

-   [GPRS\_Shield\_Suli](https://github.com/Seeed-Studio/GPRS_Shield_Suli) – GPRS Module Library,
-   SoftwareSerial – we use software serial to communicate with GPRS module
-   [Suli\_Arduino](https://github.com/Seeed-Studio/Suli) – the Seeed Unified Library interface

Import all three of these libraries into your Arduino setup.

### Software Serial

Now for the fun part. Your shield and Arduino environment are all setup. So let’s play with cell networks.

First, we need a way to talk to the SIM900 chip on the Arduino. We could use a Hardware serial and talk directly to it, but that’s hard, so let’s just use Arduino’s software serial.

Open up the 'SIM\_900\_Serial\_Debug' example from your Arduino examples folder.

[![
Connect Arduino GSM/GPRS](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpi/0b0470d16b584924954410e4fe312d6e/687474703a2f2f692e696d6775722e636f6d2f3658483531466f2e706e67.png "
Connect Arduino GSM/GPRS")](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpi/0b0470d16b584924954410e4fe312d6e/687474703a2f2f692e696d6775722e636f6d2f3658483531466f2e706e67.png)

This example is simple, it just proxies the serial data from Arduino’s serial port into SIM900’s. This let’s us use the built in Arduino Serial debug tool to interact with our SIM900.

```js
void loop(){
  if(gprs.available()){
    Serial.write(gprs.read());
  }
  if(Serial.available()){     
    gprs.write(Serial.read()); 
  }
}
```

### Call Yourself

Let’s try calling ourselves with the Arduino GSM shield to test everything out. Load the Software Serial example onto the Arduino, and open up the Serial Debugger. Make sure you power on the SIM card on the Arduino GPRS shield using the button on the side.

_Power up the SIM900 by pressing the power button in for about 2 seconds. The red LED will be on. The green one beside it will blink. If the shield joins the network successfully, the green LED will blink every 3 seconds._

Make sure you put your old SMS card back into your phone after testing the Arduino SIM!

Now, type the following into your Arduino Serial Window:

```js
AT
```

Serial output should show something like:

```js
RDY
+CFUN: 1
+CPIN: READY
+PACSP: 0
Call Ready
```

If you can’t see the messages in the serial monitor, you should click the “send new” option that will add carriage return at the end of AT command and then send AT command “AT+IPR=19200” to set the baud rate of the SIM900.

Now try calling yourself. Enter the following command, replacing '1\*\*\*8675309' with your own number.

```js
ATD1***8675309;
```

If it succeeds, a message 'ATH OK' will show up as the picture below. Otherwise, 'No CARRIER' will show up instead. The reason might be nonexistent phone number or incorrect command format.

### Connect to the Internet

We’ve successfully configured our Arduino project to work over GSM. Now let’s get it connected to the Internet.

_Make sure that the baud rate of SIM900 is 9600! You can use the AT Command(AT+IPR=9600) to set it through SerialDebug_

Load up the the GPRS\_TCPConnection example from GPRS\_Shield\_Suli. This example makes a simple request to 'mbed.org/media/uploads/mbed\_official/hello.txt HTTP/1.0'  
  

However, it probably won’t run when you load it on your Arduino. This is because it’s not configured to work with your specific cellular network.

In my case I’m using AT&T, so I needed to change the following lines to configure my gprs connection:

```js
GPRS gprs(PIN_TX, PIN_RX, BAUDRATE,"cmnet");
```

This line isn’t sufficient in most cases. We’ll also need to supply an APN, username, and password. These parameters are omitted from the example but you can simply supply them on your own. You can find the relevant code [here](https://github.com/Seeed-Studio/GPRS_Shield_Suli/blob/master/GPRS_Shield_Arduino.h).

```js
GPRS(int tx, int rx, uint32_t baudRate = 9600, const char* apn = NULL, const char* userName = NULL, const char *passWord = NULL);
```

To connect to AT&T, use the following config:

```js
GPRS gprs(PIN_TX, PIN_RX, BAUDRATE, "wap.cingular", "WAP@CINGULARGPRS.COM", "CINGULAR1");
```

Now run the code. You should be able to obtain an IP and issue the request. However, the app will probably crash (SIM turns off) when the request has been issued. This might be confusing, but there’s an easy fix.

### Batteries

The Seeed Studio and a variety of other shields advertise to be “plug and play” with Arduino but this is not always the case. This shield in particular doesn’t have enough power from the single 5v USB supply to successfully complete TCP requests.

This is easy enough to fix though, all we need to do is give it more power! Online forums suggest adding 5 C cell batteries to power the GSM chip, which is what I have done.

[![Connect Arduino GSM/GPRS](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpk/229a1ccaccf1df92b657be3c095b068f/687474703a2f2f692e696d6775722e636f6d2f45486a7942684b2e6a7067-1024x681.jpg "Connect Arduino GSM/GPRS")](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpj/523b7d13b8c3b81ff0c49733d501b432/687474703a2f2f692e696d6775722e636f6d2f45486a7942684b2e6a7067.jpg)

The batteries should be wired in series, with the positive end wired into the VIN input and negative wired into the GND.

After you’ve added batteries, try the example again. It should connect successfully.

Great! Now we’re ready to signal with PubNub.

### Implementing PubNub to Signal

Now that we’re fully-powered, let’s get to the fun part. Load the following example onto your board, changing the 'GPRS gprs()' configuration as mentioned previously.

```js
#include <GPRS_Shield_Arduino.h>
  #include <SoftwareSerial.h>
  #include <Wire.h>
  #include <Suli.h>
  #define PIN_TX    7
  #define PIN_RX    8
  //make sure that the baud rate of SIM900 is 9600!
  //you can use the AT Command(AT+IPR=9600) to set it through SerialDebug
  #define BAUDRATE  9600
  GPRS gprs(PIN_TX, PIN_RX, BAUDRATE, "wap.cingular", "WAP@CINGULARGPRS.COM", "CINGULAR1");
  void publishData(String data) {
    Serial.println("attempting to publish");
    while (false == gprs.join()) {
      Serial.println("gprs join network error");
      delay(2000);
    }
    String str = "GET /publish/demo/demo/0/pubnub_gprs/0/\"" + data + "\" HTTP/1.0\r\n\r\n";
    // Length (with one extra character for the null terminator)
    int str_len = str.length() + 1; 
    // Prepare the character array (the buffer) 
    char http_cmd[str_len];
    // Copy it over 
    str.toCharArray(http_cmd, str_len);
    char buffer[512];
    if (false == gprs.connect(TCP, "pubsub.pubnub.com", 80)) {
      Serial.println("connect error");
    } else {
      Serial.println("publish success");
      Serial.println(data);
    }
    gprs.send(http_cmd, sizeof(http_cmd) - 1);
    gprs.close();
  }
  int counter = 0;
  void setup() {
    Serial.println("starting");
    Serial.begin(9600);
    // use DHCP
    gprs.init();
    // attempt DHCP
    while(true){
      publishData(String(counter));
      counter++;
      delay(1000);
    }
    gprs.disconnect();
  }
  void loop() {
  }
```

[](https://www.pubnub.com/docs/general/basics/initialize-pubnub)

[It may be confusing at first, but let’s break it down. The url is formatted according to the](https://www.pubnub.com/docs/general/basics/initialize-pubnub) [PubNub REST Push API](https://pubnub.com/docs/rest-api).

```js
http://pubsub.pubnub.com
/publish
/pub-key
/sub-key
/signature
/channel
/callback
/message
```

The domain 'pubsub.pubnub.com' is defined later in the function. The 'publish' key refers to is the pubnub command (you can also subscribe). The 'pub-key' and 'sub-key' come from your own [PubNub account](https://www.pubnub.com/docs/general/basics/set-up-your-account), but you can use the 'demo' keys for now. Don’t worry about signature.

The 'channel' is really important. This is like an IRC channel for your PubNub devices to talk to each other.

### Show me the data

Since we don’t have any other device listening for messages from the Arduino, let’s use the [PubNub console](https://www.pubnub.com/docs/console/) to observe our channel activity.

If your demo is running you should see a count like 1, 2, 3, 4 appear in the “message” pane.

[![Connect Arduino GSM/GPRS](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpm/f98dab6173290ad076b29dc1c0b1ad48/687474703a2f2f692e696d6775722e636f6d2f77474c463865652e706e67-1024x468.png "Connect Arduino GSM/GPRS")](https://images.ctfassets.net/3prze68gbwl1/asset-17suaysk1qa1hpl/938e26e4848e88a0f3e2aaeb114f7589/687474703a2f2f692e696d6775722e636f6d2f77474c463865652e706e67.png)

We’re able to view the data coming out of the Arduino because we plugged the same publish and subscribe key into the PubNub console as we did into the Arduino. Since we’re using the 'demo' keys the channel is shared publicly, but if you create your own account your data will be privatized.

How can PubNub help you?
========================

This article was originally published on [PubNub.com](https://www.pubnub.com/blog/connect-arduino-gsm-shield-with-cellular-networks/)

Our platform helps developers build, deliver, and manage real-time interactivity for web apps, mobile apps, and IoT devices.

The foundation of our platform is the industry's largest and most scalable real-time edge messaging network. With over 15 points-of-presence worldwide supporting 800 million monthly active users, and 99.999% reliability, you'll never have to worry about outages, concurrency limits, or any latency issues caused by traffic spikes.

Experience PubNub
-----------------

Check out [Live Tour](https://www.pubnub.com/tour/introduction/) to understand the essential concepts behind every PubNub-powered app in less than 5 minutes

Get Setup
---------

Sign up for a [PubNub account](https://admin.pubnub.com/signup/) for immediate access to PubNub keys for free

Get Started
-----------

The [PubNub docs](https://www.pubnub.com/docs) will get you up and running, regardless of your use case or [SDK](https://www.pubnub.com/docs)