Electric Imp Internet of Things Platform Tails: a guide to using the Env Tail
Electric Imp Tails are a great way to learn about creating exciting connected devices with the Electric Imp Platform. Each one clips straight onto an April breakout board and provides a set of sensors or other peripherals that are ready to use. You can build sophisticated connected devices with no electronics work — you may need to assemble the April but that’s it — and to focus instead on using Electric Imp’s impCentral™ and Electric Imp code libraries to get your device up and running very quickly.
The Env Tail provides a really easy way to find out how to read from a variety of environmental sensors using the connected Electric Imp Platform. 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. There’s also an LED that can be used for visual feedback.
Before you start working with a Tail, you’ll need to get your dev kit connected to the Internet. This is done using Electric Imp’s BlinkUp™ technology, for which you’ll need to download the Electric Imp app for either an Android or Apple smartphone or tablet. You’ll also need to sign up as an Electric Imp developer — it’s free — to program the imp in your dev kit with impCentral.
Now visit our Quick Start Guide, which will walk you through these preliminary steps and get you ready to start working with Tails.
The Env Tail’s three sensors units are the APDS9007 ambient light sensor, the LPS25H air pressure sensor and the Si7020 for reading temperature and humidity. All three units are accessed through Electric Imp code libraries that are ready for you to use.
The Tail’s sensors are wired directly to specific imp pins. So is the LED. Two of the sensors, the Si7020 and the LPS25H, connect across an I²C bus; their data (SDA) and clock (SCL) pins are wired up to two of the imp’s pins that support I²C operation. These are accessed through one of the imp API’s i2c objects, instantiated automatically as properties of the imp API’s hardware object, as are all the pin objects. Though these objects are established for you, you do need to configure them as required.
Sensor/Pin Function | imp pin Object | imp i2c Object |
---|---|---|
Pressure Interrupt (trigger to take a reading) | hardware.pin1 | |
LED | hardware.pin2 | |
Ambient Light Input | hardware.pin5 | |
Ambient Light Enable | hardware.pin7 | |
I²C SCL | hardware.i2c89 | |
I²C SDA | hardware.i2c89 |
To add the sensor libraries to your code, you need to place the following lines right at the top of the Device Code:
#require "APDS9007.class.nut:2.2.1"
#require "LPS25H.class.nut:2.0.1"
#require "Si702x.class.nut:1.0.0"
There’s a space between each #require and the strings that specify the name of the library we want to load and the version of it that we want to use. This statement has to come before your own program code, single-line comments aside.
Each library is instantiated using a constructor that takes the imp pin or i2c object to which the sensor is connected, plus further set-up information, such as a sensor’s I2C address. The pin and i2c objects need to be configured before they are passed to the new sensor objects. Let’s look at them in order. First, the APDS9007:
local lightOutputPin = hardware.pin5;
lightOutputPin.configure(ANALOG_IN);
local lightEnablePin = hardware.pin7;
lightEnablePin.configure(DIGITAL_OUT, 1);
local lightSensor = APDS9007(lightOutputPin, 47000, lightEnablePin);
The first four lines configure the two pin objects: one as an analog input, the second as a digital output with an initial state of 1
, or logic High. The fifth line creates the new sensor object with these properties — plus the value of the Tail’s sensor load resistor, 47,000Ω — and saves a reference to it in the variable lightSensor. The word local is a Squirrel keyword which tells the interpreter that the variable is local to the scope of the function in which it defined. Here that’s the main body of the program.
Here’s the code for the LPS25H:
hardware.i2c89.configure(CLOCK_SPEED_400_KHZ);
local pressureSensor = LPS25H(hardware.i2c89);
Here we configure the i2c object we’re using to the speed 400kHz and then pass it to the new sensor instance.
Having configured the I²C bus already, we don’t need to do it again for the Si7020:
local tempHumidSensor = Si702x(hardware.i2c89);
We’re now ready to begin reading data, which we do using methods provided by the libraries:
You can see the full Si7020 library API here.
enable(state) — When this method is passed true
, the sensor becomes active. You must enable the sensor before attempting to read data from it.
read(callback) — This method takes a function which will be called automatically when the sensor has taken a pressure reading. The function you provide needs a single parameter, through which the current pressure in hPa is passed. The sensor readings are taken asynchronously: your Squirrel code will continue to run, and the callback function will only be executed when the LPS25H returns the pressure value.
You can see the full LP25H library API here.
You can see the full APDS9007 library API here.
The Env Tail design has been released under the open source MIT licence.