Alternative for those who don't like the web IDE

I made this bash script so that I can use any IDE for developing imp code. When you run the script, it will build the code and run it on the device/model of your choosing. I have it setup to run automatically everytime I save the device file. WARNING: this will of course overwrite the model of your choosing in the ide, so make sure you have the code for the model in the web ide saved somewhere (you probably already do)

Pre-reqs:
*Firefox
*node.js, npm (apt-get install node, and jsesc (npm install -g jsesc)
*curl (apt-get install curl)

The only reason it requires jsesc is to escape quotes and new lines. You can use sed or another tool of your choosing, this was just easiest for me. The same concept can be adapted to work for windows.

Steps:

  1. Login to ide.electricimp.com in firefox (you may close it after logging in, just dont log out)
  2. Set the environment variable FFX_PROFILE to the directory location of your firefox profile (mine is ~/.mozilla/firefox/mwad0hks.default)
  3. Run this script like so ./impide {model} {device} {agent code file} {device code file}
    a) If you refresh the ide page, you will see the code from your local files there!
    b) {model} and {device} are integer ids that can be seen in the URL when you have a model/device selected in the ide.
  4. (optional) Run a when-changed script to run this script everytime a file is saved. Here’s one I use: https://github.com/joh/when-changed

Here’s the script:
`
#!/bin/bash

Warning: hack

Get’s around the imp ide. This script will send out the post request that builds

device and agent code and sends it through the electric imp service to be built

and run.

You must first open firefox and login to the ide there. The session cookie is stolen

from firefox! Make sure to have the env var FFX_PROFILE set to the profile dir.

eval ffxProfile=$FFX_PROFILE
model=$1
device=$2
agentCode=cat $3 | jsesc --double-quotes
impCode=cat $4 | jsesc --double-quotes

impToken=sqlite3 $ffxProfile/cookies.sqlite "SELECT value FROM moz_cookies WHERE baseDomain='electricimp.com' and name='imp.token';"

curl “https://ide.electricimp.com/ide/v3/models/$model/code” \
-H “Cookie: imp.token=$impToken” \
-H ‘Origin: https://ide.electricimp.com’ \
-H ‘Accept-Encoding: gzip,deflate,sdch’ \
-H ‘Accept-Language: en-US,en;q=0.8’ \
-H ‘User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36’ \
-H ‘Content-Type: application/json’ \
-H ‘Accept: application/json, text/javascript, /; q=0.01’ \
-H “Referer: https://ide.electricimp.com/ide/models/$model/devices/$device” \
-H ‘X-Requested-With: XMLHttpRequest’ \
-H ‘Connection: keep-alive’ \
–data-binary “{“device_id”:”$device",“imp_code”:"$impCode",“agent_code”:"$agentCode"}" --compressed
`

I should point out that the REST API used by this script is undocumented, unsupported and subject to change without warning.

Until then, fill your boots.

We are looking into ways to provide a supported API for this kind of thing. No timescale yet, though.

I took the liberty of turning mikob’s script into a slightly more elaborate code uploader (as I’m too lazy to type all that stuff on a commandline).

Here’s my version, if anyone’s interested:

Nice job wejn! Always nice to have it in a VCS. I agree that so many arguments can be tedious on the command line. I just use bash’s reverse command search (ctrl-r) and edit parameters as necessary.

The call to update the code has been changed from ide.electricimp.com to api.electricimp.com in recent times. Here is the updated bash script with the one line updated:

`
#!/bin/bash

Warning: hack

Get’s around the imp ide. This script will send out the post request that builds

device and agent code and sends it through the electric imp service to be built

and run.

You must first open firefox and login to the ide there. The session cookie is stolen

from firefox! Make sure to have the env var FFX_PROFILE set to the profile dir.

eval ffxProfile=$FFX_PROFILE
model=$1
device=$2
agentCode=cat $3 | jsesc --double-quotes
impCode=cat $4 | jsesc --double-quotes

impToken=sqlite3 $ffxProfile/cookies.sqlite "SELECT value FROM moz_cookies WHERE baseDomain='electricimp.com' and name='imp.token';"

curl “https://api.electricimp.com/ide/v3/models/$model/code” \
-H “Cookie: imp.token=$impToken” \
-H ‘Origin: https://ide.electricimp.com’ \
-H ‘Accept-Encoding: gzip,deflate,sdch’ \
-H ‘Accept-Language: en-US,en;q=0.8’ \
-H ‘User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.132 Safari/537.36’ \
-H ‘Content-Type: application/json’ \
-H ‘Accept: application/json, text/javascript, /; q=0.01’ \
-H “Referer: https://ide.electricimp.com/ide/models/$model/devices/$device” \
-H ‘X-Requested-With: XMLHttpRequest’ \
-H ‘Connection: keep-alive’ \
–data-binary “{“device_id”:”$device",“imp_code”:"$impCode",“agent_code”:"$agentCode"}" --compressed
`

This still works. But for any future persons stumbling in here, there is now an official electric imp build api that should be used instead.