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

Electric Imp Tails: The Env(ironment) Tail

The Env 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.

It’s important that you become familiar with the Env Sensor tail, which helps you with all information about it.

You can use The Env Sensor Tail with your Electric imp001 or imp002.

Requirements

Setup

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

1.- Assemble the hardware.

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

3.- Program the project. Open the Electric Imp IDE in a web browser configure your device and setting up your Ubidots account. Please note that you should replace your TOKEN with the one from your Ubidots account.

4.- Run the code. The code you pasted into the IDE is ready to run, so click on the ‘Build and Run’ button.

5.- Now check you ubidots account. Your Weather Station is ready!

Send one value to Ubidots

Copy and paste these Agent and Device blocks of code:

Agent Code

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

Ubidots <- Ubidots.Client("9DriKmYujOJZ2x637376xbFtaSLvq0");

//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();
Did this answer your question?