Tech Guides‎ > ‎

From Electric Imp To Open.Sen.Se and back

About the Electric Imp

  • http://electricimp.com/
  • Wifi-enabled Microcontroller
  • All coding is done online through the imp cloud. (No cables or drivers to install!)
  • Code language is Squirrel - Fairly easy to grasp, if you have been doing some php, java, Javascript, C# etc.  '

About Open.Sen.Se

  • http://open.sen.se/
  • A really nicely designed engine for the internet of things
  • Nice graphs to display data from your device (Is it warm or cold? )
  • Nice buttons etc. to control things (Turn on the heat)
  • Triggers on data can execute various events. (Post to twitter, send data etc.)
  • Data is sent and received using JSON
  • Still in beta - You have to request access to get in

Squirrel and JSON is a good match

It is very easy to work with JSON in Squirrel - You can declare the Table-type in Squirrel using JSON: 
local data = {"feed_id": feedid, "value": value};

And two methods in the Electric Imp makes it easy to encode and decode for http:

From Electric Imp to Open.Sen.Se

Sending sensor data from your Electric Imp to Open.Sen.Se

Assuming you have an account at Open.Sen.Se, do this: 

Find your API-Key http://open.sen.se/api/

Add a device
  • Go to Channels and Add/Register a device
    • "Arduino & other physical or virtual devices"
  • Select Device is : Other  
  • Sending data (from your Imp) using
    • HTTP Posting
  • Receiving data (from Open.Sen.Se )
    • HTTP Push to : xxxx <Your Agent-url - https://agent.electricimp.com/*********  >
Add a Feed
  • Name the feed
  • Select "input" 
  • Set the other parameters of your feed
  • Finish and note the Feed Id. 

Agent Code snippet

const OPENSENSE_URL = "http://api.sen.se/events/";
const OPENSENSE_KEY = "********************";

function sendToOpenSenSe(feedid, value){
    local url = OPENSENSE_URL;
    local headers = { "sense_key" : OPENSENSE_KEY, "Content-Type": "application/json" };
    local data = {"feed_id": feedid, "value": value};
    local body = http.jsonencode(data);
    
    // debug. Write to the log to make sure the json is pretty
    server.log(body);
  
    // send data to open sen se
    http.post(url, headers, body).sendsync();
}

// If the device sends the code "LightLevel", execute this function 
// At device: agent.send("LightLevel",current_light);
device.on("LightLevel", function(light) {
    
    // Send the value to the OpenSenSe feed with feedid 50425 (Insert your feed id) 
    // Whenever the device decides to call this, we just pass it on to Open.Sen.Se - The device controls the update frequency
    sendToOpenSenSe(50425,light);

});

Device Code snippet

function readSensors()
 {
    // Get the light level from the build in light sensor in the Imp
    local current_light = hardware.lightlevel();
    agent.send("LightLevel",current_light);
    

    // Wake up  again (if for some reason the imp fell asleep) in 10 seconds and execute this function again
    // (Not really a deep sleep, Serial Stuff still runs)
    imp.wakeup(10.0, readSensors);
 }
 
 // Call the function first time
readSensors();


Build and Run

  • Build and run your Electric Imp code, go to Open.Sen.Se, and see the data come flowing in your Sense-river. 
  • Add nice graphs to show your lovely data

From Open.Sen.Se to Electric Imp

Put a button on your Sense-board, that controls an LED on the Electric Imp

Select your device and create a new feed. 
  • Channels -> My Devices -> <device> -> View recap instructions -> Edit Feeds -> Add a feed
  • Select "Output" for this feed
  • Choose "boolean values"
  • Finish and Note the feed id
Add the App "Universal manual logger" to you senseboard, using the "Virtual switch to turn things On/Off"

When you click 0 or 1 at the button, Open.Sen.Se sends a key-value pair to the post-address you defined for your device earlier. 
(Your Agent-url)
All things are sent to your device, so you will have to filter it in your code by the Feed_Id.  

Hardware

Add an LED to you Electric Imp breakout board like this: 

Agent Code 

Note: It looks like Open.Sen.Se is sending the JSON as a query string. Luckily http.urldecode can take care of that. 

const LIGHTSWITCH_FEED_ID = 49108;

function requestHandler(request, response) {
  try {
    // parse the url form encoded data into a table
    local data = http.urldecode(request.body);

    // Check if we have a feed_id and a value in the data
    if("feed_id" in data && "value" in data){
        server.log("feed_id: " + data.feed_id);
        server.log("value:" + data.value);
        // Check if the feed_id is correct
        if(data.feed_id = LIGHTSWITCH_FEED_ID){
            local ledState = data.value.tointeger();
            // send "led" message to device, and send ledState as the data
            device.send("led", ledState);        
        }
    }
    
    // send a response back saying everything was OK.
    response.send(200, "OK");
    } catch (ex) {
        response.send(500, "Internal Server Error: " + ex);
        //response.send(200, "Not OK " + ex);
    }
}
 
// register the HTTP handler
http.onrequest(requestHandler);

Device Code

// create a global variabled called led, 
// and assign pin5 to it
led <- hardware.pin5;
 
// configure led to be a digital output
led.configure(DIGITAL_OUT);
 
// function to turn LED on or off
function setLed(ledState) {
  server.log("Set LED: " + ledState);
  led.write(ledState);
}
 
// register a handler for "led" messages from the agent
agent.on("led", setLed);

Build and Run

Build your imp code, and you should be able to control the LED from your Senseboard app. :-) 
Comments