Skip to main content
All CollectionsIoT Projects Tutorials
Connect an Electric Imp Env Tails to Ubidots over HTTP
Connect an Electric Imp Env Tails to Ubidots over HTTP

Learn how to setup and connect the Electric Imp Tails to send data to Ubidots over HTTP.

Sergio M avatar
Written by Sergio M
Updated over 2 months ago

Electric Imp Tails are a great way to learn about creating 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 over HTTP.

Electric Imp Tails: The Env(ironment) Tail

The Env(ironment) 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 as a sample project to better deliver the sample code in the tutorial. 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 regarding the imp001 and imp002 and how these devices can be easily integrated with Ubidots.

Step-By-Step

  1. Electric Imp Setup

  2. Hardware Setup

  3. Sending (POST) Data to Ubidots

  4. Summary 

1. Electric Imp Setup

To start with the Electric Imp Env Tail, refer to this guide which will provide a detailed explanation of how to:

  • Activate & Connect your Imp device to impCentral

  • Create Product in impCentral

  • Create a Development Group in impCentral

  • Code management

2. Hardware Setup

1. Assemble the hardware.

3. Sending (POST) Data to Ubidots

By following  the sample code provided below you will be able to post readings provided from the sensors embed with the Env Sensor Tail.

1. To begin, select the proper device group to apply the below send (Agent Code & Device Code) values sample code.  

2. Below you will find the respective codes for the Agent & Device section blocks in the impCental. Copy and paste them in the respective category assigned within impCentral IDE.

Once you've pasted the Agent code, you need to assign your Ubidots token inside the client constructor. This will look a little something like: 

 Ubidots <- Ubidots.Client("BBFF-YP65d9ngV6*************") 

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

  • 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();

3. At this point, you need to verify if the codes included are in the right format. To verify the code, just press the "Check"  button. 

4. With the code verified, it's time to force the code into the imp module. To run and upload the code you have to the imp, press the button "Build and Force Restart"

Once the uploading process starts you will receive the logs of the uploading process and the payload which is being sent to Ubidots.

5. Go to the "Device" section of your Ubidots to see the values which are being reported by the imp module used: 

6. [OPTIONAL] If you desire to change the device name to a more friendly one reference this guide. Further, the device icon modification will also give you a quick identifier. To learn more reference this quick start icon guide click here.

4. Summary

The Electric Imp Env tails are a great and easy way to deploy a resourceful IoT Environmental Monitoring system  thanks to multiple sensors that it provides.

Electric Imp Tails are boards that enable users to understand the connection between the Electric Imp Platform (impCentral) and its modules to GET & POST data to and from the Ubidots IoT Application Development Platform. This guide has prepared you for interfacing with Tails, working with the impCentral, and visualizing your data in Ubidots. 

For additional resources and quick troubleshooting, do not forget to check out Ubidots Help Center for additional tips, tricks, and solutions to common problems experienced when building IoT Applications. 

Other readers have found useful...

Did this answer your question?