For global IoT deployments requiring a SIM card in multiple countries, Soracom's got you covered. Soracom's SIM card can transmit data to and from your device over cellular networks, either with 2G, 3G, 4G LTE, or Cat-M1. Additionally, the SIM has a global reach and provides M2M devices with IoT connectivity in more than 300 countries.
In this article, you'll learn how to use a Soracom SIM along with an Arduino MKR GSM 1400 to send and retrieve data from Ubidots. You may purchase a Starter IoT Kit directly from Soracom, for more information on getting started with this Kit, please view this guide.
Requirements
Arduino MKR GSM 1400 and antenna or any other GSM Module
1. Register IoT SIM to a Soracom account
If you purchased an IoT Starter Kit from Soracom then you can skip this step, as your SIM will have been shipped pre-registered.
If you got your IoT Card separately, please follow these steps to register your IoT SIM to your Soracom Account.
2. Hardware Setup
Attach the antenna to the Arduino MKR GSM 1400.
Although it's possible to use the PC's USB port as a power supply, it isn't recommended as the GSM module can draw up to 2A peaks, which can exceed the USB port's current limit. For that reason, we recommend using an external power supply such as a LiPo battery or a power source through Vin with a higher current limit.
3. Arduino IDE Setup
Once the Arduino IDE has been downloaded, there are two main things to install before using an Arduino MKR GSM 1400: the board definition and its library. Please follow these steps in Soracom's guide for setting up the Arduino IDE.
4. IMEI Registration
Some countries require the GSM module to be registered according to their regulations in order to avoid having it blocked. Please search if your country requires this process and how it should be done.
5. Send Data to Ubidots
To send data to Ubidots, simply copy the following code to your IDE, assign the Token and Device label, then upload it to the board:
#include <Arduino.h>
#include <MKRGSM.h>
#define SERIAL_SPEED 9600
#define PINNUMBER ""
#define GPRS_APN "soracom.io"
#define GPRS_LOGIN "sora"
#define GPRS_PASSWORD "sora"
#define UBIDOTS_TOKEN "<YOUR_UBIDOTS_TOKEN>"
#define DEVICE_LABEL "<DEVICE_LABEL>"
#define UBIDOTS_SERVER "industrial.api.ubidots.com"
#define UBIDOTS_PORT 80
GSMModem modem;
GSM gsmAccess(false); // set true to see and debug modem AT commands on the Serial port
GPRS gprs;
GSMClient client;
boolean notConnected = true;
void setup()
{
Serial.begin(SERIAL_SPEED);
SerialGSM.begin(115200);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
for (int loopCount = 0; loopCount <= 60; loopCount++)
while (!Serial) // Wait for a serial connection, timeout after 60 loops (close to 1 min)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(1000);
}
Serial.println();
Serial.println(F("Starting Soracom Ubidots test.. "));
while (!SerialGSM)
delay(1000);
Serial.println(F("Starting Modem, GSM, GPRS and Ubidots Cloud connection"));
while (notConnected)
{
digitalWrite(LED_BUILTIN, LOW);
if (gsmAccess.begin(PINNUMBER, true, true) == GSM_READY)
{
//delay(1000);
Serial.print(F("\t[1/3] GSM_READY "));
Serial.print(F(" Modem IMEI : "));
Serial.print(modem.getIMEI());
Serial.print(F(" SIM ICCID : "));
Serial.println(modem.getICCID());
if (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD, true) == GPRS_READY)
{
//delay(1000);
Serial.println(F("\t[2/3] GPRS_READY "));
if (client.connect(UBIDOTS_SERVER, UBIDOTS_PORT) == true)
{
//delay(1000);
Serial.println(F("\t[3/3] Ubidots connection ready "));
notConnected = false;
}
else
{
Serial.println("Connection failed");
client.stop();
}
}
else
{
gprs.detachGPRS();
}
}
else
{
gsmAccess.shutdown();
}
Serial.print(F("."));
delay(1000);
}
}
void loop()
{
if (client.connected())
{
float value1 = random(0, 9) * 10;
float value2 = random(0, 9) * 100;
float value3 = random(0, 9) * 1000;
String payload = "{\"var1\":"+String(value1)+",\"var2\":"+String(value2)+",\"var3\":"+String(value3)+"}";
int payloadSize = payload.length();
Serial.println(F("\t Sending message to Ubidots"));
digitalWrite(LED_BUILTIN, HIGH);
Serial.print(F("Ubidots connection is UP, sending '"));
Serial.print(payload);
client.print(F("POST /api/v1.6/devices/"));
client.print(DEVICE_LABEL);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(UBIDOTS_SERVER);
client.println(F("User-Agent: Soracom/1.0"));
client.println(F("Content-type: application/json"));
client.print(F("X-Auth-Token: "));
client.println(F(UBIDOTS_TOKEN));
client.print(F("Content-Length: "));
client.println(payloadSize);
client.println();
client.println(payload);
while(!client.available());
if (client.ready() == 0)
{
Serial.print(F("."));
delay(500);
}
while (client.available())
{
char c = client.read();
Serial.print(c);
}
digitalWrite(LED_BUILTIN, LOW);
}
else
{
Serial.println("Not connected, entering else loop");
client.connect(UBIDOTS_SERVER, UBIDOTS_PORT);
}
delay(5000);
}
NOTE: This code is based on the code found here.
In this case, we used the Device label "soracom-test"
Once the code has been uploaded, you may begin to see the data flow into your Ubidots account.
6. Get Variable Data from Ubidots
To retrieve a variable's last Dot from Ubidots, simply copy the following code to your IDE, assign the Token, Device label, and Variable label, then upload it to the board:
#include <Arduino.h>
#include <MKRGSM.h>
#define SERIAL_SPEED 9600
#define PINNUMBER ""
#define GPRS_APN "soracom.io"
#define GPRS_LOGIN "sora"
#define GPRS_PASSWORD "sora"
#define UBIDOTS_TOKEN "<YOUR_UBIDOTS_TOKEN>"
#define DEVICE_LABEL "<DEVICE_LABEl>"
#define VARIABLE_LABEL "<VARIABLE_LABEL>"
#define UBIDOTS_SERVER "industrial.api.ubidots.com"
#define UBIDOTS_PORT 80
GSMModem modem;
GSM gsmAccess(false); // set true to see and debug modem AT commands on the Serial port
GPRS gprs;
GSMClient client;
boolean notConnected = true;
void setup()
{
Serial.begin(SERIAL_SPEED);
SerialGSM.begin(115200);
delay(1000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(250);
for (int loopCount = 0; loopCount <= 60; loopCount++)
while (!Serial) // Wait for a serial connection, timeout after 60 loops (close to 1 min)
{
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(1000);
}
Serial.println();
Serial.println(F("Starting Soracom Ubidots test.. "));
while (!SerialGSM)
delay(1000);
Serial.println(F("Starting Modem, GSM, GPRS and Ubidots Cloud connection"));
while (notConnected)
{
digitalWrite(LED_BUILTIN, LOW);
if (gsmAccess.begin(PINNUMBER, true, true) == GSM_READY)
{
//delay(1000);
Serial.print(F("\t[1/3] GSM_READY "));
Serial.print(F(" Modem IMEI : "));
Serial.print(modem.getIMEI());
Serial.print(F(" SIM ICCID : "));
Serial.println(modem.getICCID());
if (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD, true) == GPRS_READY)
{
//delay(1000);
Serial.println(F("\t[2/3] GPRS_READY "));
if (client.connect(UBIDOTS_SERVER, UBIDOTS_PORT) == true)
{
//delay(1000);
Serial.println(F("\t[3/3] Ubidots connection ready "));
notConnected = false;
}
else
{
Serial.println("Connection failed");
client.stop();
}
}
else
{
gprs.detachGPRS();
}
}
else
{
gsmAccess.shutdown();
}
Serial.print(F("."));
delay(1000);
}
}
void loop()
{
if (client.connected())
{
Serial.println(F("\t Retrieving last Dot from Ubidots"));
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(F("Ubidots connection is UP, getting last Dot '"));
client.print(F("GET /api/v1.6/devices/"));
client.print(DEVICE_LABEL);
client.print("/");
client.print(VARIABLE_LABEL);
client.print("/values/?page_size=1");
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.println(UBIDOTS_SERVER);
client.println(F("User-Agent: Soracom/1.0"));
//client.println(F("Connection: close"));
client.println(F("Content-type: application/json"));
client.print(F("X-Auth-Token: "));
client.println(F(UBIDOTS_TOKEN));
client.println();
while(!client.available());
if (client.ready() == 0)
{
Serial.print(F("."));
delay(500);
}
while (client.available())
{
char c = client.read();
Serial.print(c);
}
digitalWrite(LED_BUILTIN, LOW);
}
else
{
Serial.println("Not connected, entering else loop");
client.connect(UBIDOTS_SERVER, UBIDOTS_PORT);
}
delay(5000);
}
NOTE: This code is based on the code found here.
In this case, we used the Device label "soracom-test"
and Variable label "var1"
Once the code is running you can view the retrieved value in the Serial monitor of the Arduino IDE:
Summary
In this article, we've gone through the process of using a Soracom IoT SIM to send and retrieve data to Ubidots. Now it's time for you to develop and deploy your IoT application using Soracom's IoT SIM all around the globe.