Adafruit FONA MiniGSM is an all-in-one cellular module that lets you add voice, text, SMS and data to your projects. In this tutorial, Ubidots will demonstrate how to send (POST) a simple value to the Ubidots API and how to receive (GET) the last value of one variable of your Ubidots account, using an Adafruit FONA device.
Requirements
An active SIM card with a data plan
Adafruit_FONA library (Optional)
Step-by-Step
1. Hardware Setup
2. Setting up the Arduino IDE
3. Sending (POST) Data to Ubidots
4. Receiving (GET) Data from Ubidots
5. Summary
1. Hardware Setup
Following the table below, assemble the wired connections between the Arduino UNO and the FONA module.
2. Setting up the Arduino IDE
1. Check if your device can connect to the internet. To this, download Adafruit_FONA library and run FONAtest example.
To run the sample code, just follow the points below:
Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon press it to verify your code.
Upload the code into your Arduino UNO + FONA module. To do this, choose the "right-arrow" icon beside the "check mark" icon.
To verify the connectivity of the device, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
IMPORTANT NOTE: Before running the FONAtest example, make sure you have an active data plan. You will also need your mobile operator’s APN settings (APN, USERNAME, PASSWORD). You should be able to easily find these settings in Google or on your operator’s website.
2. Download the Ubidots FONA library and install it. For a detailed explanation of how to install libraries using the Arduino IDE, refer to this guide.
3. Sending (POST) Data to Ubidots
With the following sample code you will be able to post the ANALOG readings taken from Arduino UNO analog port.
1. To post your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your unique Ubidots TOKEN and the APN (Access Point Name) of your cellular provider with username and password.
#include <UbidotsFONA.h>
#include <SoftwareSerial.h>
#define APN "xxxxxxxxxx" // The APN of your operator
#define USER "xxxxxxxxxx" // if your apn doesnt have username just leave it ""
#define PASS "xxxxxxxxxx" // if your apn doesnt have password just leave it ""
#define TOKEN "Your_token_here" // Replace it with your Ubidots token
#define VARIABLE_LABEL_1 "temperature" // Replace it with your Ubidots variable label
#define VARIABLE_LABEL_2 "humidity" // Replace it with your Ubidots variable label
#define VARIABLE_LABEL_3 "pressure" // Replace it with your Ubidots variable label
Ubidots client(TOKEN);
void setup() {
Serial.begin(115200);
delay(2000);
client.setDebug(true); // comment this line to set DEBUG off
//client.setDeviceName("new_device_name"); // uncomment this line to assign a different device name
//client.setDeviceLabel("new_device_label"); // uncomment this line to assign a different device label
while(!client.setApn(APN, USER, PASS));
}
void loop() {
float value1 = analogRead(A0);
float value2 = analogRead(A1);
float value3 = analogRead(A2);
client.add(VARIABLE_LABEL_1, value1);
client.add(VARIABLE_LABEL_2, value2);
client.add(VARIABLE_LABEL_3, value3);
if(client.sendAll()){
Serial.println("values sent properly");
}
}
2. Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon press it to verify your code.
3. Upload the code into your Arduino UNO + FONA module. To do this, choose the "right-arrow" icon beside the "check mark" icon.
4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
NOTE: 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 the code (115200).
Troubleshooting & FAQs: If the message received in the serial monitor is “PDP DEACT - error” Your service provider may be blocking some ports. You should call your service provider or try with another SIM card with an active data plan.
5. Confirm your data in Ubidots. Now you should see the posted data in your Ubidots account, located the device called "fona".
4. Receiving (GET) Data from Ubidots
With the following sample code you will be able to get a value from Ubidots to start controlling any asset you desire.
1. To start getting values from Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, be sure to assign the following parameters:
APN (Access Point Name) of your cellular provider with username and password.
Device Label of the device which contain the variable desired to be obtained
Variable Label of the variable desired to be obtained
#include <UbidotsFONA.h>
#include <SoftwareSerial.h>
#define APN "xxxxxxxxxx" // The APN of your operator
#define USER "xxxxxxxxxx" // if your apn doesnt have username just leave it ""
#define PASS "xxxxxxxxxx" // if your apn doesnt have password just leave it ""
#define TOKEN "Your_token_here" // Replace it with your Ubidots token
#define DEVICE_LABEL "Your_device_label" // Replace it with your Ubidots Device Label
#define VARIABLE_LABEL "Your_variable_label" // Replace it with your Ubidots Variable Label
Ubidots client(TOKEN);
void setup() {
Serial.begin(115200);
delay(2000);
client.setDebug(true); // comment this line to set DEBUG off
while(!client.setApn(APN, USER, PASS));
}
void loop() {
float value = client.getValue(DEVICE_LABEL, VARIABLE_LABEL);
Serial.print("The last value is: ");
Serial.println(value);
}
2. Verify & Upload the code into the board following the same steps provided in the POST step above.
3. To verify the connectivity of the device and the data which is being received, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs.
NOTE: 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 the code (115200).
Troubleshooting & FAQs: If the message received in the serial monitor is “PDP DEACT - error” Your service provider may be blocking some ports. You should call your service provider or try with another SIM card with a data plan.
4. At this point, you should be able to see the last value received in Ubidots of the variable specified in the firmware.
5. Summary
With this simple tutorial we are able to POST & GET data to/from Ubidots with the ease of the Arduino IDE and a Arduino UNO + FONA Module. If your wish to find more examples to handle context or timestamp values in your request checkout Ubidots documentation with the FONA by clicking here.
Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution! Happy Hacking! :)
Other readers have also found useful...