We are now going to take the same hardware & integrate it with Adafruit IO. We are going to use Adafruit IO to display the location of the project in real time on a map, and also display an alert in case the fence has been breached.

Here as well, the sketch is quite similar to the GPS test sketch, so I will only detail the most important parts that were added.

You will need the following libraries:

#include <Adafruit_SleepyDog.h>
#include <SoftwareSerial.h>
#include "Adafruit_FONA.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_FONA.h"

If needed, you will also have to enter the GPRS parameters for your SIM card:

#define FONA_APN             "internet"  
#define FONA_USERNAME        ""  
#define FONA_PASSWORD        ""  

This depends on your operator, so contact them directly if you are not sure about those parameters.

After that, you need to enter your Adafruit IO username & key:

#define AIO_SERVER           "io.adafruit.com"  
#define AIO_SERVERPORT       1883
#define AIO_USERNAME         "your-adafruit-io-name"  
#define AIO_KEY              "your-adafruit-io-key"

We also set a name for the Adafruit IO feed that will contain the location data:

#define LOCATION_FEED_NAME       "location"

We declare an instance of the MQTT library, that we will use to connect to Adafruit IO:

Adafruit_MQTT_FONA mqtt(&fona, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

After that, we declare different feeds that we will use to store data about our project: the location, the current charge of the battery, and the alerts:

const char LOCATION_FEED[] PROGMEM = AIO_USERNAME "/feeds/" LOCATION_FEED_NAME "/csv";
Adafruit_MQTT_Publish location_feed = Adafruit_MQTT_Publish(&mqtt, LOCATION_FEED);

const char BATTERY_FEED[] PROGMEM = AIO_USERNAME "/feeds/battery";
Adafruit_MQTT_Publish battery_feed = Adafruit_MQTT_Publish(&mqtt, BATTERY_FEED);

const char ALERTS_FEED[] PROGMEM = AIO_USERNAME "/feeds/alerts";
Adafruit_MQTT_Publish alerts_feed = Adafruit_MQTT_Publish(&mqtt, ALERTS_FEED);

In the setup() function of the sketch, we initialise the GPRS connection of the FONA:

fona.setGPRSNetworkSettings(F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD));
delay(2000);
Watchdog.reset();
Serial.println(F("Disabling GPRS"));
fona.enableGPRS(false);
delay(2000);
Watchdog.reset();
Serial.println(F("Enabling GPRS"));
if (!fona.enableGPRS(true)) {
  halt(F("Failed to turn GPRS on, resetting..."));
}
Serial.println(F("Connected to Cellular!"));

We also connect the project to Adafruit IO via MQTT:

int8_t ret = mqtt.connect();
if (ret != 0) {
  Serial.println(mqtt.connectErrorString(ret));
  halt(F("MQTT connection failed, resetting..."));
}
Serial.println(F("MQTT Connected!"));

Finally, as we as starting the sketch, we set the alerts feed to 0:

logAlert(0, alerts_feed);

In the loop() function, instead of setting an alarm, if the distance is breached we set the alerts feed to 1:

if (distance > maxDistance) {
  logAlert(1, alerts_feed);
}

We also get the current charge of the battery from the FONA breakout board:

uint16_t vbat;
fona.getBattPercent(&vbat);

Finally, we log the location & the battery charge on Adafruit IO:

logLocation(latitude, longitude, altitude, location_feed);
logBatteryPercent(vbat, battery_feed);
delay(5000);

You will find all the code inside the GitHub repository of the project:

It's now time to test the project! Make sure that you entered your Adafruit IO credentials into the sketch, as well as your GPRS APN settings.

Then, upload the code to the board, and log to Adafruit IO.

You should see that a new 'location' feed has been created, which contains the project GPS location:

That's good, but it would be much better with an actual map. Create a new dashboard, and then a map widget linked to the location feed:

You should immediately see the current location of the project inside the dashboard. Finally, add a gauge widget link to the battery, and a text widget linked to the alerts feed.

You should now have a complete dashboard to monitor your geofencing project:

This guide was first published on Nov 05, 2015. It was last updated on Nov 05, 2015.

This page (Integration with Adafruit IO) was last updated on Nov 03, 2015.

Text editor powered by tinymce.