Mobilogix BAT (Basic Asset Terminal) is a robust GPS tracker which transmits data over cellular and LPWAN connectivity such as CAT-M1 and NBIoT for a better battery life management. Another key feature of the BAT are the environmental sensors embedded such as Accelerometer, gyro, temperature, pressure, humidity, and ambient light allowing to the device sense different variables in around the asset where the sensor is deployed.
Following this guide, you’ll be able to manage the data obtained by the Mobilogix BAT in Ubidots IoT Development Platform to start monitoring the data, take actions based on the data obtained, and the opportunity to generate recurrent reports.
Requirements
Ubidots account with UbiFunction add-on enabled.
IMPORTANT NOTE: This guide assumes that the Mobilogix device is already configured and transmitting data to Mobilogix main services and it provides a detailed process for the API communication between Mobilogix, and Ubidots.
Step-by-Step
UbiFunction setup
Coding an UbiFunction
Verify data reception
Debugging and Logs
Summary
1. UbiFunction setup
To create a serverless function to parse data from Mobilogix to Ubidots you need to have an active Ubidots subscription.
1. To create the UbiFunction, go to the "Data" tab and select "Function" and create a new function.
2. Create a new function (click the "+" icon)
3. In the following window, give the new Function a descriptive name that resembles the parse function to be executed. In this case we called it "BAT1". In the HTTPS Endpoint URL, select "POST" as the method to trigger the function and set the Time based trigger to 5 minutes, then click on “Make it live” button:
NOTE: if your account does not have a ‘Time based trigger’ as the image above, please contact Ubidots support team at support@ubidots.com to enable this feature in your account. This trigger feature will run the function automatically every X minutes to retrieve the latest data from Mobilogix’s servers through the API.
2. Coding an UbiFunction
The sample code below has 4 functions:
A main function that manages the other 3 functions which handle authentication with Mobilogix to get the Auth Token, the get of sensor data such as GPS and environmental sensors, and the POST request to Ubidots, updating any device or variable information in the process.
A auth_login function authenticates with Mobilogix’s API using the account credentials and retrieves the Auth Token.
A mobilogix_request function that uses the above Mobilogix’s Auth Token to retrieve a list of the available data within the account.
A ubidots_request function, which uses the specified TOKEN, device_label and a JSON payload as an input to make an HTTP POST request to Ubidots' API.
Using the above logic, we obtain the ability to retrieve GPS, temperature, humidity, pressure and battery status data from a Mobilogix account with registered BAT device, parse the retrieved data, and then post said data to Ubidots.
1. Place the code in the UbiFunction created. Then, assign your Ubidots Token, the Mobilogix account credentials, and the BAT serial number where indicated.
// Assign Ubidots' Token
var ubidots_token = "assign_your_ubidots_token_here";
// Import the 'request-promise' library so we can make HTTP request from the function
var request = require('request-promise');
// Main function - runs every time the function is executed.
async function main() {
// Define const variables
var username = "assign_mobilelogix_username"; // Assign username's account where the device is registered
var password = "assign_mobilelogix_password"; // Assign password's account where the device is registered
var serial_number = "assign_BAT_serial_numer"; // Assign device serial number
// Access Mobilogix to get the Authentication Token
var token_response = await auth_login(username, password);
var token = JSON.parse(token_response);
// Save the Token returned
var mobi_token = token["auth_token"];
// Make request to Mobilogix to get GPS, and enviromental sensors data
var gps = await mobilogix_request("LocateDevice", serial_number, mobi_token);
var sensors = await mobilogix_request("GetDeviceDetails", serial_number, mobi_token);
// Verify GPS status to set 1 as fixed value for "location" variable
var isTripActive = 0;
if( gps['isTripActive'] == true ){
isTripActive = 1;
}
// Build Ubidots payload with battery, temperature, humidity, pressure and location data
var payload = {
"battery-status": sensors['deviceDetails']['value'][0]['batteryStatus'],
"temperature": sensors['deviceDetails']['value'][0]['temperatureValue'],
"humidity": sensors['deviceDetails']['value'][0]['humidityValue'],
"pressure": sensors['deviceDetails']['value'][0]['pressureValue'],
"location": {
"value": isTripActive,
"context": {
"lat": gps['latitude'],
"lng": gps['longitude']
}
}
};
// Send the payload to Ubidots
var response = await ubidots_request(ubidots_token, serial_number, payload);
// Pass Ubidots' API response as the function's reponse
return response;
}
// Builds an HTTP POST request to Mobilogix to login the account, and get auth token
async function auth_login(username, password) {
var options = {
url: 'https://fusion.mobilogix.com:6443/api/Auth/login',
json: true,
headers: {
'Accept': 'application/json',
'x-api-version':'1.0'
},
body: {
'username': username,
'password': password
}
};
return await request.post(options);
}
// Builds an HTTP GET request to Mobilogix
// Endpoints - GPS: "LocateDevice", Enviromental Sensors: "GetDeviceDetails"
async function mobilogix_request(endpoint, serial_number, mobi_token) {
var options = {
url: ${"https://fusion.mobilogix.com:9443/api/DeviceIOTSettings/"}${endpoint}${"/"}${serial_number},
json: true,
headers: {
'Accept': 'application/json',
'x-api-version':'1.0',
'Authorization': 'Bearer ' + mobi_token
}
};
return await request.get(options);
}
// This function builds an HTTP POST request to Ubidots
async function ubidots_request(token, label, body) {
var options = {
method: 'POST',
url: ${"https://industrial.api.ubidots.com/api/v1.6/devices/"}${label},
body: body,
json: true,
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token
}
};
return await request.post(options);
}
3. Verify data reception
After your function executes the first time, the data from the Mobilogix BAT device will POST to Ubidots and it will be displayed similar to the image below:
Once you go through the Device Overview, this is how it looks the variable window:
4. Debugging and Logs
Also, a great debugging resource is the ability to view the logs of your function. To do so, refer to the control bar and select the "logs" icon as shown below to view the history of your Function's executions:
In the Function's logs, you can confirm if data was successfully posted to Ubidots with the 201 response code:
5. Summary
In just a couple of minutes, you successfully integrated your Mobilogix BAT device to stream live data to Ubidots where the data can go to work in your Ubidots powered App.
Connect with Mobilogix at info@mobilogix.com, for any technical question about Mobilogix devices.
Now it's time to assemble dashboards and add users who can see and engage with your Apps' data.
Other developers also found helpful: