Electric Imp Tails are a great way to learn about creating exciting connected devices with the Electric Imp Platform and Ubidots. In this guide you will learn how to use the Electric Imp Tails and send data to Ubidots.

Electric Imp Tails: The Env(ironment) Tail

The Env(ironment) Tail provides a really easy way to find out how to read from a variety of environmental sensors using the Electric Imp Platform and Ubidots . Each Tail contains three sensors able to measure four qualities of your surroundings: the ambient light level, the air pressure, the temperature and the humidity. We’ll put all of these sensors to use in a Weather Station project. There’s also an LED we can use for a little visual feedback.

You can use The Env Sensor Tail with your Electric Imp001 or Imp002.

Requirements

 Before setting up, we recommend you to check out our documentation of imp001 and imp002.

Step-By-Step

1. Hardware Setup
2. Firmware Setup
    - Send (POST) a value to Ubidots
    - Receiving (GET) Values from Ubidots  
3. Summary 

1. Hardware Setup

1. Assemble the hardware.

2.Connect Your Imp to the Internet. If you don’t know how, just click here -> Quick Start Guide

3.Program the project. Open the Electric Imp IDE in a web browser configure your device and setting up your Ubidots account. 

2. Firmware Setup

1. In the Electric Imp IDE, you should see an empty workspace with this description "THERE ARE NO PRODUCTS IN YOUR ACCOUNT" This means your has no project created:

2. Click on “CREATE PRODUCT” and assign your desired Application Group Name to house your devices. Click on "Create".

3. After the product is created, you should be directed unto this page as shown below.
Note: This page is where the Agent code block and Device code block are located.

Send(POST) a value to Ubidots

1. Copy and paste the followings Agent and Device blocks of code located below into the IDE windows of the imp platform.

Note: Agent Code = Agent Block, Device Code = Device Block

  • Agent Code
// Agent Code
#require "Ubidots.agent.lib.nut:1.0.0"

Ubidots <- Ubidots.Client("YOUR_TOKEN");

//Ubidots.setDeviceLabel("electric-imp"); // to set the device name

device.on("saveValue", function(data){

    Ubidots.sendToDevice(data);
    server.log("Sending data to Ubidots");
    server.log(http.jsonencode(data));
});

  • Device Code
// Device Code
#require "Si702x.class.nut:1.0.0"
#require "APDS9007.class.nut:1.0.0"
#require "LPS25H.class.nut:1.0.0"

// Establish a global variable to hold environmental data

data <- {};
data.temp <- 0;
data.humid <- 0;
data.pressure <- 0;
data.day <- true;
data.lux <- 0;

// Instance the Si702x and save a reference in tempHumidSensor
hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
local tempHumidSensor = Si702x(hardware.i2c89);

// Instance the LPS25H and save a reference in pressureSensor
local pressureSensor = LPS25H(hardware.i2c89);
pressureSensor.enable(true);

// Instance the APDS9007 and save a reference in lightSensor
local lightOutputPin = hardware.pin5;
lightOutputPin.configure(ANALOG_IN);

local lightEnablePin = hardware.pin7;
lightEnablePin.configure(DIGITAL_OUT, 1);

local lightSensor = APDS9007(lightOutputPin, 47000, lightEnablePin);

// Configure the LED (on pin 2) as digital out with 0 start state
local led = hardware.pin2;
led.configure(DIGITAL_OUT, 0);

// This function will be called regularly to take the temperature
// and log it to the device’s agent

function getReadings() {

    // Flash the LED
    flashLed();

    // Get the light level
    local lux = lightSensor.read();

    // Day or night?
    if (lux > 300) {
        data.day = true;
    } else {
        data.day = false;
    }

    // Get the pressure. This is an asynchronous call, so we need to
    // pass a function that will be called only when the sensor
    // has a value for us.
    pressureSensor.read(function(pressure) {
        data.pressure = pressure;

        // Now get the temperature and humidity. Again, this is an
        // asynchronous call: we need to a pass a function to be
        // called when the data has been returned. This time
        // the callback function also has to bundle the data
        // and send it to the agent. Then it puts the device into
        // deep sleep until it's next time for a reading.

        tempHumidSensor.read(function(reading) {
            data.temp = reading.temperature;
            data.humid = reading.humidity;

            // Send the data to the agent
            agent.send("saveValue", data);

            imp.onidle(function() { server.sleepfor(5); } );
        });
    });
}

function flashLed() {

    led.write(1); // Turn the LED on (write a HIGH value)
    imp.sleep(0.5); // Pause for half a second
    led.write(0); // Turn the LED off
}

getReadings();

2. Assign your Ubidots TOKEN with the one from your Ubidots account at Ubidots.client("YOUR_TOKEN") ;

NOTE: The Ubidots library takes the Device ID and creates a new device in your Ubidots account. If it's desired, you can change this default configuration using the method setDeviceLabel("New-Device-Name")  as is shown in the code below.

User Interface Experience

1. Here’s how it would look like after putting your code:

2. Click "ASSIGN DEVICES" (found at the bottom of the page) and if you followed guide from electric imp correctly, you will find the ID of your device awaiting you in the modal screen as shown below:

3. Check the box for the device you wish to assign and click Assign when complete. 

4.  Next, click on "CHECK" to make sure your agent and device codes do not have any errors.

5. Then, "BUILD AND FORCE RESTART" to assemble code and start sending to the Ubidots Platform. These commands can be found in the top left corner, above the agent code.

NOTICE: As seen below is the Imptails kit tab. With this tab you can easily restart, conditionally restart, assign and unassign devices.

6. Now check your Ubidots account and your Weather Station is ready!

Receiving (GET) Last Values from Ubidots

1.  Copy and paste the followings Agent and Device blocks of code located below into the imp IDE shown after your new product has been created. See Firmware Setup Step 1 for additional details to setting up your first device in the imp IDE.

Note: Agent Code = Agent Block, Device Code = Device Block

  • Agent Code
// MIT License  
//  
// Copyright 2017 Ubidots  
// Copyright 2017 Electric  
//  
// SPDX-License-Identifier: MIT  
//  
// Permission is hereby granted, free of charge, to any person obtaining a copy  
// of this software and associated documentation files (the "Software"), to deal  
// in the Software without restriction, including without limitation the rights  
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
// copies of the Software, and to permit persons to whom the Software is  
// furnished to do so, subject to the following conditions:  
//  
// The above copyright notice and this permission notice shall be  
// included in all copies or substantial portions of the Software.  
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO  
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES  
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  
// OTHER DEALINGS IN THE SOFTWARE.

'#require "Ubidots.agent.lib.nut:1.0.0"    

Ubidots <- Ubidots.Client("YOUR_TOKEN");  
local DEV_LABEL = "ElectricImp";  
local VAR_LABEL  =  "temp";  
 
device.on("get", function(data){      Ubidots.getLastValue(DEV_LABEL, VAR_LABEL, function(v){      
  device.send("get", v);  
  });
}); '
  • Device Code
// MIT License  
//  
// Copyright 2017 Ubidots  
// Copyright 2017 Electric  
//  
// SPDX-License-Identifier: MIT  
//  
// Permission is hereby granted, free of charge, to any person obtaining a copy  
// of this software and associated documentation files (the "Software"), to deal  
// in the Software without restriction, including without limitation the rights  
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
// copies of the Software, and to permit persons to whom the Software is  
// furnished to do so, subject to the following conditions:  
//  
// The above copyright notice and this permission notice shall be  
// included in all copies or substantial portions of the Software.  
//  
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,  
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF  
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO  
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES  
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  
// OTHER DEALINGS IN THE SOFTWARE.  
 
'function mainLoop() {    
  agent.send("get", "temp");    
  imp.wakeup(10.0, mainLoop);
}   mainLoop();    
 
   agent.on("get", function(data) {        
      server.log(data);
});'

2. Assign your Ubidots TOKEN with the one from your Ubidots account at Ubidots.client("YOUR_TOKEN") ;

3. Replace the variables below based on your needs:
 Dev_LABEL : Device Label desired to be obtained.
 VAR_LABEL : Variable Label desired to be obtained.

4. Next, click on "CHECK" to make sure your agent and device codes do not have any errors.

5. Then, "BUILD AND FORCE RESTART" to assemble code and start sending to the Ubidots Platform. These commands can be found in the top left corner, above the agent code.

Here’s how it would look like after putting your code and building:

3. Summary

Electric Imp Tails are boards that enable users to understand the connection devices with the Electric Imp Platform and its module to GET & POST data to and from the Ubidots data platform respectively. This guide has prepared you for the basis to interfacing with Tails, Electric Imp and Ubidots. I trust that addressed your inquiry

Other readers have found useful...





Did this answer your question?