All Collections
Connect your Devices
Connect an Electric Imp - imp002 Breakout Board to Ubidots over HTTP
Connect an Electric Imp - imp002 Breakout Board to Ubidots over HTTP

Learn to setup and communicate data from an Electric Imp - imp002 breakout board to Ubidots Cloud

Isabel Lopez avatar
Written by Isabel Lopez
Updated over a week ago

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 “Imp002 Breakout Board” 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 relayed to Ubidots to be made intelligent in your business' Applications.

The imp002 Breakout Board can be used for small projects, early prototyping, or simply evaluating the Electric Imp Platform. For designs for mass production please consider the imp003, imp004m and imp005 devices.

More about the imp002 Breakaout.

By following this guide you will be able to POST and GET data to/from Ubidots using the Electric Imp 002 module in just a couple of minutes!    

Requirements

Step-By-Step

1.  Electric Imp Setup
2. Sending (POST) Data to Ubidots
3. Receiving (GET) Data to Ubidots
4. Summary 

1. Electric Imp Setup

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

  • Activate & Connect your Imp Device

  • Create Product in impCentral

  • Create a Development Group in impCentral

  • Code management

IMPORTANT NOTE: If you already done with the points mentioned above, feel free to continue with the guide :)  

2. Sending (POST) Data to Ubidots

By following  the code provided below you will be able to post the analog readings taken from the Pins #5, #8 and #9

1. To begin, select the proper device group to use the send values sample code.  

2. Below you will find the respective codes for the Agent & Device section blocks. Copy and paste them in the respective category assigned in the 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-YTP65d9ngV6*******************")


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

#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 

data <- {};
data.temp <- 0;
data.humid <- 0;
data.pressure <- 0;

TempSensor <- hardware.pin9;
TempSensor.configure(ANALOG_IN);

HumSensor <- hardware.pin8;
HumSensor.configure(ANALOG_IN);

PressureSensor <- hardware.pin5;
PressureSensor.configure(ANALOG_IN);

function mainLoop() {
    data.temp = TempSensor.read();
    data.humid = HumSensor.read();
    data.pressure = PressureSensor.read();

    agent.send("saveValue", data);

    imp.wakeup(10.0, mainLoop);

}

mainLoop();


With both codes properly pasted into the impCentral, you should have something similar to this image below:

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

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

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

NOTE: The values taken from the analog pins of the imp module are not connected to any sensor, for this reason the values shown above are not related to a real-life example . 

 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.

3. Receiving (GET) Data to Ubidots

With the following sample code you will be able to get a value from Ubidots to start controlling any asset you desire.

1. To begin, select the proper device group to use the get value sample code.   

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

Once you pasted the Agent code, you need to assign your Ubidots token inside the client constructor; having as result something like this:

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

Also, you need to assign the following parameters where indicated in the sample codes below: 

  • Agent Code

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

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

local DEV_LABEL = "Assign_Device_Label";
local VAR_LABEL  =  "Assign_Variable_Label";

device.on("get", function(data){
    Ubidots.getLastValue(DEV_LABEL, VAR_LABEL, function(v){
        device.send("get", v);
    });
});
  • Device Code

function mainLoop() {
    agent.send("get", "Assign_Variable_Label");
    imp.wakeup(10.0, mainLoop);
}

mainLoop();

agent.on("get", function(data) {
    server.log(data);
});

With both codes properly pasted in the impCentral, you should have something similar to this image below: 

3. Check & Download the codes into the board following the same steps provided in the POST step above.
​ 
4. At this point, you should be able to receive the last value of the variable assigned in the sample code previously downloaded:

As you can in the above console, the values received from the variable in Ubidots sample code are "1" and "0"; these values are being assigned by using the Ubidots Control widget in the form of On and Off, respectively. 

Now is your time to take the values received from Ubidots in your imp board to start controlling whatever you need. 

4. Summary

With this simple tutorial you are able to POST & GET data to/from Ubidots with the ease of the impCentral and an imp002. If your wish to find more examples to handle timestamp values in your request checkout Ubidots documentation with the Electric Imp boards by clicking here

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  Happy Hacking! :) 

Other readers have also found useful...

Did this answer your question?