All Collections
Connect your Devices
Connect an Electric Imp imp006 to Ubidots
Connect an Electric Imp imp006 to Ubidots

Send data from an imp006 breakout board to your Ubidots account

Agustin Pelaez avatar
Written by Agustin Pelaez
Updated over a week ago

In this guide we'll send temperature and humidity data from Twilio/Electric Imp's imp006 breakout board to Ubidots.

Requirements

Step-By-Step

1. Activate the imp006 Breakout Kit

Electric Imp provides a comprehensive guide to setup your imp006 for the first time:



NOTE: Even though the imp006 features cellular connectivity, we recommend doing the first tests using WiFi. You can always choose to add cellular connectivity later.

2. Device code: Read temperature and humidity sensors

Electric Imp has a built-in library to read on-board temperature and humidity sensors through I2C. This guide provides the sample device code to read such sensors and send the readings to the impCloud, where they can be managed using the agent code:



NOTE: You can read the full guide to understand how to manage such readings, though here we'll stick to just providing the device code. Make sure you have followed these steps though:

  1. Activate your device with BlinkUp.

  2. Log into impCentral.

  3. Copy and Paste the Device Code into the Device Code pane in the impCentral code editor.

DEVICE CODE

// Reading a Sensor Device Code
// ---------------------------------------------------

// SENSOR LIBRARY
// ---------------------------------------------------
// Libraries must be included before all other code

// Temperature Humidity sensor Library
#require "HTS221.device.lib.nut:2.0.1"


// SETUP
// ---------------------------------------------------
// The HTS221 library uses the sensor's i2c interface
// To initialize the library we need to configure the
// i2c and pass in the 12c address for our hardware.

// The i2c address for the Explorer Kits and the
// Battery Powered Sensor Node are all 0xBE.
const I2C_ADDR = 0xBE;

// Find the i2c for your hardware from the list below.
// Paste the hardware.i2c for your hardware into the
// i2c variable on line 32.

// imp006 Breakout Board Kit i2c = hardware.i2cLM
// impExplorer Dev Kit 001 i2c = hardware.i2c89
// impExplorer Dev Kit 004m i2c = hardware.i2cNM
// impAccelerator Battery Powered Sensor Node i2c = hardware.i2cAB
// impC001 Cellular Breakout Board Kit i2c = hardware.i2cKL

// Configure i2c
// Paste your i2c hardware in the variable below
local i2c = hardware.i2cLM;
i2c.configure(CLOCK_SPEED_400_KHZ);

// Initialize the temperature/humidity sensor
local tempHumid = HTS221(i2c, I2C_ADDR);

// Before we can take a reading we need to configure
// the sensor. Note: These steps vary for different
// sensors. This sensor we just need to set the mode.

// We are going to set up the sensor to take a single
// reading when we call the read method.
tempHumid.setMode(HTS221_MODE.ONE_SHOT);


// APPLICATION FUNCTION(S)
// ---------------------------------------------------
// The sensor is now configured to taking readings.
// Lets set up a loop to take readings and send the
// result to the agent.

function loop() {
// Take a reading
local result = tempHumid.read();

// Check the result
if ("error" in result) {
// We had an issue taking the reading, lets log it
server.error(result.error);
} else {
// Let's log the reading
server.log(format("Current Humidity: %0.2f %s, Current Temperature: %0.2f °C", result.humidity, "%", result.temperature));
// Send the reading to the agent
agent.send("reading", result);
}

// Schedule next reading in 10sec
// Change the first parameter to imp.wakeup to
// adjust the loop time
imp.wakeup(10, loop);
}

// RUNTIME
// ---------------------------------------------------
server.log("Device running...");

// Start the readings loop
loop();

3. Agent code: Send data to Ubidots

Thanks to our native ElectricImp library, sending data to Ubidots can be done in just a few lines.

First, make sure you grab an Ubidots token from your account, in "My Profile" --> "API Credentials" section. In this case, I've created one specifically for this project:

Adding Ubidots token as Environmental Variable

Although you could use this token directly in the agent code, it makes more sense to set it as an environmental variable, so it can be easily managed from a single source. To do so, head to your impCentral account, then go to the Device Group created in the previous step, then click "Settings":

In the "Environmental Variables" section, add a valid JSON with your Ubidots token:

{
"token": "xxxx-xxxxxxxxxxxxxxxxxxxxx"
}

Lastly, go to the Code section and paste the following agent code.

AGENT CODE:

// Reading a Sensor Agent Code
// ---------------------------------------------------

// CLOUD SERVICE LIBRARY
// ---------------------------------------------------
// Libraries must be included before all other code
#require "Ubidots.agent.lib.nut:1.0.0"


// RUNTIME
// ---------------------------------------------------
server.log("Agent running...");

// We recommend setting your Ubidots token as an environmental variable.
// See https://developer.electricimp.com/tools/impcentral/environmentvariables
Ubidots <- Ubidots.Client(__VARS.token);

// Open listener for "reading" messages from the device

device.on("reading", function(reading) {

// Log readings coming from the device, to ease debugging
server.log(http.jsonencode(reading));

// The Ubidots library will setup the JSON payload for you. Just add one each reading per row, then use the sendToDevice function:

data <- {};
data.temperature <- reading.temperature;
data.humidity <- reading.humidity;

Ubidots.sendToDevice(data);

})

NOTE: When you instantiate the Ubidots client object, the device label is automatically set to the ID of your device. This is useful when deploying many devices because new devices will automatically appear in your Ubidots account as they are added to your imp's fleet .

4. Summary

imp006 and Ubidots

In this guide we've not only learnt how to setup an imp device and program it, but also how to use Ubidots' ElectricImp library to send sensor readings from device to cloud.

Check out these other guides to start scaling your fleet.

Did this answer your question?