Electric Imp: The agent and the device

One of the really interesting things about the Electric Imp is that you are able to program both the device, and something called an "agent", which is a secure, programmable web service that you use to interface with your Electric Imp device. If you are coming from a platform like Arduino, you may be wondering why you can't talk directly to the Electric Imp itself. That question is beyond the scope of this tutorial, but I have found that the agent is a very handy service to have. Here are a few great features of agents:
  • Communicate using HTTPS
  • Additional memory and processing power for code
  • If you device goes offline, your agent will continue to execute code and can inform you.
As an example of the last point, if you use an Electric Imp in a security system, and a thief disables your connection or the device, your agent can still alert you that there is a problem.

The device can operate without any programming in the agent, so the first thing we need to do is program the device to read data from our thermocouple amplifier via SPI.

Log into your Electric Imp account and open the Web IDE. Find the Electric Imp that you will be using by its hardware address in the list of your Devices on the left hand side. Mouse over it, and click the gear symbol to create new Device Settings. Click the down arrow beside associated model, and then type a new model name, such as "MAX31855".
Note: While the web IDE is still in Beta, you may find that the screenshots and commands listed here are slightly different from the version that you are using.
The Electric Imp code for this tutorial is available at the GitHub page listed below. Typically, you store code for the device in a file called "device.nut", and code for the agent in a file called "agent.nut" The Electric Imp programming language, "ImpOS" is a slightly modified version of the Squirrel programming language.

https://github.com/joel-wehr/Tutorial_Electric_Imp_MAX31855

Credit for this code goes to a number of members of the Electric Imp forum community as well as members of the Electric Imp team. Big thanks to beardedinventor, bodinegl, rivers, and mjkuwp94. Check out the Electric Imp GitHub page for example and reference code.
Copy the code from device.nut into the Device panel in the web IDE.
Click "Build and Run"

If all is well, you should see the Imp download the new code, and start logging data every 10 seconds like this:

2013-09-04 21:33:28 UTC+4: [Status] Downloading new code
2013-09-04 21:33:28 UTC+4: [Status] Device configured to be "MAX31855"
2013-09-04 21:33:28 UTC+4: [Device] 24.5°C
2013-09-04 21:33:28 UTC+4: [Device] 76.1°F

Nice! The Electric Imp is reading our thermocouple data over SPI!

If you haven't read through LadyAda's tutorial on thermocouples, amplifiers and the Seebeck effect, I highly recommend that you do. Working with thermocouples will make much more sense.

http://learn.adafruit.com/thermocouple/overview

Troubleshooting: If you are getting unexpected data from your thermocouple, first make sure that the leads are fully secured in the screw terminals. Hold the end of the thermocouple in your hand, and check to see if the temperature reading rises. If it doesn't, or goes lower, swap the red and yellow thermocouple leads and try again.
If you "Check" your code, it will attempt to compile, and you will see an X in a red box by the lines of code that throw an error. If you "Build and Run" your code and errors occur, check the log for the line number that threw the error.

This guide was first published on Sep 11, 2013. It was last updated on Sep 11, 2013.

This page (Get the Device code) was last updated on Oct 22, 2021.

Text editor powered by tinymce.