Adafruit FONA MiniGSM is an all-in-one cellular phone module that lets you add voice, text, SMS and data to your project. Here you will learn how to send a simple value to the Ubidots API and how to get the last value of one variable of your Ubidots account, using an Adafruit FONA device and Ubidots library.

Requirements items

Before running the examples, make sure you have an active data plan. You will also need your mobile operator’s APN settings (APN, USER, PASSWORD). You should be able to easily find these settings in Google or in your operator’s website.

Before running our examples please, downolad Adafruit_FONA library and run FONAtest example

Pin Connections FONA -> Arduino UNO

  • Vio –> 5V (or 3V, if your Arduino has 3V logic)
  • GND –> GND
  • FONA_RX –> Pin 2
  • FONA_TX –> Pin 3
  • FONA_RST –> Pin 4
  • FONA_KEY –> GND

Run the examples!

  1. Download the UbidotsFONA library here
  2. Go to the Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library
  3. Select the UbidotsFONA .ZIP file and then “Accept” or “Choose
  4. Do the same with the Adafruit_FONA library.
  5. Close the Arduino IDE and open it again. Before continuing, make sure your device can connect to the Internet! You can run Adafruit’s example “FONAtest.ino” contained in Adafruit’s FONA library to make sure your FONA is working properly and is able to connect to your mobile network.
  6. Now go to File -> Examples -> UbidotsFONA library and select one example.
  7. Put your Ubidots TOKEN.
  8. Put your mobile operator’s APN settings (APN, USER, PASSWORD). You should be able to easily find your operator’s APN settings on Google or in the operator’s website.
  9. Upload the code, open the Serial monitor to check the results. If no response is seen, try unplugging your Arduino and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code.

Once you see some activity, go to your Ubidots account to check your data!

To manually install the libraries, copy the Ubidots_FONA folder contained in the ZIP file into your arduinosketchfolder/libraries/ folder. You may need to create the libraries subfolder if its your first library, then restart the IDE.

If you’re not sure how to add a library to the Arduino IDE please click here to see the steps as explained by Arduino.

To use this library, put your Ubidots token and variable label where indicated, as well as the APN settings. After uploading each example to your Arduino, open the Serial monitor to check the results. If no response is seen, try unplugging your Arduino and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code.

Included Examples

Send a value with latitude and longitude

To send a value with latitude and longitude use the example UbidotsSendValuesWithContext or just copy the following code:

#include <UbidotsFONA.h>
#include <SoftwareSerial.h>

#define APN  ""  // The APN of your operator
#define USER ""  // if your apn doesnt have username just leave it ""
#define PASS ""  // if your apn doesnt have password just leave it ""
#define TOKEN "Your_Token_Here"  // Replace it with your Ubidots token

Ubidots client(TOKEN);

void setup() {
  Serial.begin(115200);
  delay(2000);
  client.setApn(APN, USER, PASS);
}
void loop() {
    float value1 = analogRead(A0);
    float value2 = analogRead(A1);
    char context[25];
    char context_2[45];
    sprintf(context, "lat=%s$lng=%s", char(1.1232), char(4323.123));
    // To send latitude and longitude to Ubidots and see it in a map widget
    // The format of the context is like this, you must send it like this example
    sprintf(context_2, "Color=RED$Position=Down$Quantity=3");
    // You can send multiple context in one variable, to send it you must add a "$" symbol between every context
    client.add("Temperature", value1, context);  // Change for your variable name
    client.add("Humedity", value2,  context_2);
    client.sendAll();
    delay(5000);
}

If the results are “PDP DEACT - error” Your service provider may be blocking ports. You should call your service provider or try with another SIM card with a data plan.

Ubidots has a widget to show the position in a map. To show the position this widget need to receive the latitude and longitude. The format to save latitude is “lat” and the format to save longitude is “lng”.These extra information is sended into the “context” of the code example.

Get a value from a Ubidots variable

To get a value you could use the example inside the library or copy the following code:

#include <UbidotsFONA.h>
#include <SoftwareSerial.h>


#define APN  ""  // The APN of your operator
#define USER ""  // if your apn doesnt have username just leave it ""
#define PASS ""  // if your apn doesnt have password just leave it ""
#define TOKEN "Your_token_here"  // Replace it with your Ubidots token
#define ID "Your_id_here" // Replace it with your Ubidots' variable ID


Ubidots client(TOKEN);  
 
void setup() {
  Serial.begin(115200);
  delay(2000);
  while(!client.setApn(APN, USER, PASS));
}

void loop() {
  float value = client.getValueWithDatasource("Data_Source_Label", "Variable_Label");
  delay(1000);
  Serial.println(value);
}
Did this answer your question?