Electric Imp is an IoT Security Platform whose partnership with Ubidots cannot be overlooked. The Imp series offers perhaps the simplest way to enable any physical object to be connected to the Internet thanks to their “Imp” module (pictured above), a powerful piece of hardware that connects effortlessly to an available WiFi network with a card from-factor 802.11 node. Additionally, this little time-saver will unlock your board to sense its surroundings and send data to the impCentral platform where it can be collected and then be relayed to Ubidots to be made intelligent in your Business Applications.

In this guide you’ll learn how to read an analog input from your Electric Imp, send its value to Ubidots, and control the device directly from Ubidots.

Requirements

Step-By-Step

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

1. Hardware Setup

The best way to become familiar with the imp is to follow their Hardware setup guide, which helps you link your device to your Electric Imp account: Hardware setup guide. The most important thing to know is that the Electric Imp IDE is divided in three main windows: agent code, device code, and the log.

1. Connect one end of the photocell to “PIN9” of your Imp, and the other end to “3V3”.

2. Connect one end of the 1k resistor to “PIN9” of your Imp, and the other end to “GND”.

2. Firmware Setup

1. Open the Electric Imp IDE to create a new data flow.

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
#require "Ubidots.agent.lib.nut:1.0.0"

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

local VAR_LABEL  =  "humidity";

//Ubidots.setDeviceLabel("Electric-imp");

device.on("saveValue", function(value) {
    Ubidots.sendToVariable(VAR_LABEL, value);
    server.log("Sending data to Ubidots");
    server.log(value);
});

  • Device Code
sensor <- hardware.pin9;
sensor.configure(ANALOG_IN);

function mainLoop() {
    imp.wakeup(1.0, mainLoop);
    local value = hardware.pin9.read();
    agent.send("saveValue", value);
}

mainLoop();


2. Assign your Ubidots TOKEN where indicated in the code: Ubidots.client("YOUR_TOKEN") ;

3. Replace the variable called VAR_LABEL with a variable label of your choice.

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

User Interface Experience

1. Here’s how it would look like after pasting 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.

This should be displayed at the log window of the code:

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

Receiving (GET) 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
#require "Ubidots.agent.lib.nut:1.0.0"
Ubidots <- Ubidots.Client("YOUR_TOKEN");

local DEV_LABEL = "LABEL";
local VAR_LABEL  =  "humidity";
device.on("get", function(data){
    Ubidots.get(DEV_LABEL, VAR_LABEL, function(v){
        device.send("get", v);
    });
});
  • Device Code
function mainLoop() {
    agent.send("get","humidity");
    imp.wakeup(10.0, mainLoop);
}
mainLoop();
agent.on("get", function(data) {
server.log(data);
});

2. Assign your Ubidots TOKEN where indicated in the code: Ubidots.client("YOUR_TOKEN") ;

3. Replace the variables below based on your needs: 

  • DEV_LABEL  : Device Label of your hardware.
  • VAR_LABEL  : Variable Label of the variable within the Device displayed in Ubidots.

4. Place these codes on the "AGENT CODE" and "DEVICE CODE" platform and Click on "CHECK" the code to make sure code doesn't 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.

3. Summary

In just a few minutes you have created an integration between your Electric Imp imp001 to POST & GET data to/from Ubidots Platform respectively. Now is your turn to start building your IoT Business Intelligent solution with Electric Imp & Ubidots.

Other readers have found useful...

Did this answer your question?