Build Instructions for TempBug: Internet-Connected Thermometer Step 1: Gather the parts Take a look at the bill of materials for this project to see all the parts youʼll need and where you can get them. Itʼs just an Imp and Imp breakout board, a battery, a temperature sensor, and a couple of passive components. I put mine together for less than $50.
You'll need some tools and supplies, as well: 1. Soldering iron 2. Solder 3. A bit of foam tape or other thick, double-sided tape for securing the battery to the breakout board 4. Angle cutters / wire cutters 5. Wire strippers 6. A smart phone for using BlinkUp to put the Imp on your WiFi network 7. A computer to program the Imp All right! Let's get started! Step 2: Wire it up Take a look at this breadboard diagram to see how weʼll be wiring up the TempBug.
First, connect the battery clip to the breakout board. 1. If your battery clip has a connector on the end, cut it off. Strip about 1/8" off the end of each wire and twist the strands to keep them together. If you don't want long leads hanging off your TempBug, you'll want to cut the wire to about 2" long or a little less. 2. Secure your Electric Imp breakout board to your work surface and heat up your soldering iron.
3. One pad at a time, use the soldering iron to heat the "P+" and "P-" battery pads on the breakout board. For each pad, when it gets hot, flow a good-sized bead of solder onto the pad. Take care not to short the pads together. 4. Place the red lead from the battery clip against the bead of solder on the "P+" pad and heat the pad and wire together with the soldering iron. When the solder flows, hold the parts together and remove the iron. Let the solder cool, then let go. 5. Repeat previous step with black lead and "P-" pad. Refer to photos to see how this should be arranged. Note that you should follow the label on the board, not right vs. left, as your breakout board may have the pads on opposite sides. Connect the capacitor
When the Imp turns on its WiFi transmitter, it can draw a lot of current (up to 250 ma, during initial calibration) from your power supply. To keep this sudden current draw from "drooping" the power supply and browning out your TempBug, we've got a BIG CAPACITOR. We'll connect the capacitor in parallel with the battery, the battery will charge the capacitor to the same voltage as the battery, and if the Imp suddenly demands more current than the battery can dish out, the capacitor will pitch in to help out. 1. The leads on the capacitor go through the breakout board at "VIN" and "GND" which are next to each other along the bottom edge of the board. Note that the negative side of the capacitor is marked with a big stripe with a "-" sign in it, and should go to ground 2. One pad at a time, use the soldering iron to heat the wire and the pad, then flow some solder onto the joint and remove the iron. 3. When you're all secure, trim the excess from the capacitor leads with your angle cutters or wire cutters. Connect the resistor 1. The 100kΩ resistor can go either way around and is connected between pin9 and 3V3 on the Electric Imp breakout board. 2. Bend the wire leads to fit and push them through the holes marked "3V3" and "Pin9". 3. Just as you did with the capacitor leads, solder ONLY THE 3V3 SIDE to the breakout board. The thermistor needs to share the Pin9 pad with the resistor, so don't solder that side yet!
Connect the thermistor 1. Just like the resistor, the thermistor is a passive component and can go either way around. If you bought the same one as me, the wire ends come pre-stripped. Push one end through the Pin9 hole and the other through the hole marked "Pin8." 2. Now, solder the resistor and thermistor leads together through the Pin9 pad.
3. Lastly, solder the last thermistor lead into the Pin8 pad. All right, all wired up! Let's take a look at what you just built. Step 3: How's this thing work? The operating principle behind the TempBug is really simple. All we're doing here is making a resistive divider using a resistor and a thermistor, and measuring the voltage in the middle using an analog-to-digital converter in the Electric Imp. Wait, what's a thermistor? A thermistor is a temperature-dependent resistor. It has a specific resistance at room temperature, and its resistance varies in a known proportion with the temperature of the component. In our case, we've selected a NTC, or negative-temperature-coefficient thermistor; the resistance of the part will decrease as temperature increases. Got it, but what's the deal with the resistor? Together, the resistor and thermistor form a resistive divider; the two resistors, wired up in series (end-to-end) have an effective resistance equal to the sum of their individual resistances. This determines the amount of current that flows through them: V=I*R (Ohm's law), V=3.3V (from the Imp breakout board's 3.3V on-board power supply), and R = the 100kΩ + the thermistor's resistance at the current temperature. Now we know how much current is flowing through the circuit.
Going back to Ohm's Law, we know that the voltage across the thermistor = I*R, where I is the current through the divider (they're in series, so both components see the same current), and R is the current resistance of the thermistor. Therefore, if we can measure the voltage at Pin9, we can solve for the resistance of the thermistor. Given the thermistor's resistance, we can determine the temperature of the part. Hey, wait, the other side of the thermistor isn't connected to ground! Right you are. It's connected to Pin8. This is a sneaky trick we'll use to save the battery. Current flowing through the resistive divider drains the battery, so we'll only let the current flow when we want to measure the temperature. We do this by programming pin8 to be a digital output. When we set Pin8 high, the voltage drop across the divider is 0V, so no current flows. When we want to take a measurement, we drive Pin8 low and sink current from the divider through the Imp. How do we actually do the measuring? To measure the voltage across the thermistor, we set up Pin9 as an analog-to-digital converter, which allows us to take an accurate measurement of the analog voltage on the pin using the Imp. Check out the software in a few steps to see how that's done. Step 4: Power up! Ok, enough math. Let's get to the actual temperature-measuring part. Connect the battery To secure the battery to the breakout board, a little square of foam tape will do the trick. Cut a small piece off with a pair of scissors and stick the sticky side to the back of the breakout board. If you went ahead and got a spring clip to hold the battery (which makes battery changes easy), youʼll want to stick that to the square of foam tape. Otherwise, you can just stick the battery straight to the board; itʼs going to last a long time. Peel back the backing film, and stick the battery clip or battery on, making sure to keep it nice and square this stuff is sticky! Once you've got it stuck in place, go ahead and connect the battery clip. Check the jumper Make sure the jumper on your breakout board is set to connect the middle pin and the "BAT" pin. Check the photos if you're not sure. If your Imp doesn't power up when you click it into the socket, double-check the jumper.
Plug in the Imp Insert your Imp into the socket on the breakout board, and it should start to blink red or orange (unless you've connected it before on this network, in which case it will just connect again). Congrats! You're powered up and ready to connect your Imp with BlinkUp and program it. BlinkUp If you haven't already registered as an Electric Imp developer, you'll need to do that now. Head to ide.electricimp.com to sign up (it's free). You'll also need to download the Electric Imp app on your smart phone, which is also free just search for "Electric Imp." Log into the app with the same account credentials you used to register at ide.electricimp.com. Once you're logged in, add your wireless network to the list of networks in the Electric Imp app. If you're on ios, it will prompt you to allow it to use the network you're already on, and you'll just need to add the password yourself. To add a new network manually, select "other network" and type in your SSID and password. Check the "save this network" button! Once you've added the network, you're ready to configure the device with BlinkUp. Make sure the Imp is powered up. If it's powered up but stopped blinking, reset the power by "push-pushing" the card (push once to disconnect, count to 3, push again to reconnect) so you can be sure it's listening for the BlinkUp signal. Hold the screen of your phone against the top edge of the Imp and press "send BlinkUp" in the Electric Imp App. The screen on your phone will begin to blink quickly for about 10 to 20 seconds, then stop. The Imp should blink green once ("got it!"), then start to blink red/orange ("found the network, connecting to the Electric Imp server"). After a second or so, the Imp should be able to phone home to the Electric Imp server, and will start to blink green. You're connected. Excellent. Now it's time to program the Imp! Head to the next step. Step 5: Setting your Imp Firmware Point your browser to ide.electricimp.com again and log in. This takes you back to the IDE window. If you've connected this Imp breakout board before (you pro), it will pop back up in the IDE under the model it was last a part of. If this is a brand-new breakout board, it will appear in the IDE's left-hand nav panel under "New Devices", at the top of the panel.
You'll see a little number in this panel showing the number of new devices there waiting to be configured probably just one! Click the small triangle in "New Devices" to see the list of new devices. Again, if you've never configured this device before, it won't have a name, so it will default to a long, random-looking string of characters (this device's Device ID). Mouse over the name of the device and click the gear icon to open up the settings for the device. This is where you can name the device and assign it a model. Go ahead and name it whatever you like, and create a new model by typing a name for your new model in the model drop-down. When you hit "save changes", you'll be taken to the code window for this new model, and you'll see your new device is now listed under that model in the lefthand nav. A model has two parts: an agent and a device. The device firmware is simply the code that runs on the Imp itself. The agent is that device's partner a tiny server that runs in the Electric Imp Cloud. The device and agent can send information back and forth, and the agent can talk to the rest of the Internet and has its own URL. You get to write code for both of them.
... Or you can use the code that's already written for you :) Head to GitHub, and notice that there are two files here one for the agent and one for the device. Accordingly, there are two windows for you in the IDE one will say "agent" and the other "device." Copy the correct code into each window. If you hit "Build and Run" now, you'll see the device read the temperature and then go to sleep, but the agent will have an error when it tries to log the data to Xively because you haven't added your Xively account yet. Let's do that next. If you did do this, and your device has gone to sleep for 15 minutes, you can disconnect and reconnect power (by simply pulling and re-inserting the jumper) to get the Imp to wake up again. Let's connect your new thermometer to Xively to log the data. Then we can come back and take a look at what makes the firmware work. Step 6: Connecting to Xively To connect your thermometer to Xively, you'll first need a Xively account. Head to https://xively.com and register if you haven't already (again, it's free). When you register, you'll be taken to a page that enthusiastically encourages you to create a new device. Go ahead and click the big plus button. Creating a new device is quite simple: name it, describe it, click the "Private Device" radio button (unless you want a public device!), and click the "add device" button. This will take you back to the dashboard for your new device, where there's some important info waiting for you.
Inside the dashboard for your newly created device, you'll find your API key and a "feed ID" for the feed you've just created. You need to add these parameters to the agent in order to get things working. Copy each from the channel dashboard and use them to fill in the appropriate lines at the top of your agent firmware. The agent is set up to create a temperature channel for you inside this feed, so you don't even need to do that. You can go ahead and hit "Build and Run" in the agent now, and
you're off to the races. After a few updates, you'll have a nice graph of the history of your datastream! Let's head back to the code editor and take a look at how the Imp is working this magic. Step 7: Digging into the Code Imp firmware is written in an object-oriented language called Squirrel, which looks and feels a lot like JavaScript. Squirrel runs in a virtual machine on top of the impos inside the Imp, so runtime errors won't knock your device offline, and you have access to a lot of nice OS-based features, like buffered serial input and easy one-line APIs to set GPIOs or communicate with peripheral devices. To take a look at all the things you can do in your Imp firmware, check out the Electric Imp API reference and Electric Imp getting started page. To see which pins can do what, check out the Imp pin mux. Our code here is pretty simple. Here's our basic operating procedure: Set some constants Define a class for our temperature sensor Instantiate our class to create a temperature sensor object Read the temperature sensor Send the data point to the agent Schedule ourselves to wake up in a bit and do it all again The constants at the top of the code look a little ominous, but they're simple. These are the parameters for your thermistor you can find them on the datasheet for the thermistor you're using. If you're using the thermistor I linked to earlier, you can even keep the parameters already set here. The next functional block is a class definition this is basically a set of instructions for how to do various important things with a thermistor, like read the temperature. This comes straight from GitHub it's a nice, modular piece of code so that it can easily be reused. All thatʼs left is to write our actual application logic! Runtime actually starts at the bottom of the file. First, we configure the two pins we're using. Remember from when we built the device one of the pins is an enable pin, which will allow current to flow through the
thermistor when it is set low. The other pin is our analog input pin, which we'll use to read the voltage across the thermistor. // Configure Pins // pin 8 is driven high to turn off temp monitor (saves power) or low t o read therm_en_l <- hardware.pin8; therm_en_l.configure(digital_out); therm_en_l.write(1); // pin 9 is the middle of the voltage divider formed by the NTC - read the analog voltage to determine temperature temp_sns <- hardware.pin9; Next, we call on our class definition from earlier to create a thermistor object. This object takes in the constants we set earlier, and has methods that allow us to read the temperature in Celsius or Fahrenheit. // instantiate our thermistor class mythermistor <- thermistor(temp_sns, b_therm, t0_therm, r_therm, 10, fa lse); Finally, weʼre down to where the magic happens we enable the sensor, take a reading, send it to the agent, and disable the sensor again to save battery. // enable the temperature sensor therm_en_l.write(0); imp.sleep(0.001); local id = hardware.getdeviceid(); local datapoint = { } "id" : id, "temp" : format("%.2f",mythermistor.read_f()) agent.send("data",datapoint); therm_en_l.write(1); And lastly, schedule a wakeup in fifteen minutes. We'll go to deep sleep until then, saving the battery: //Sleep for 15 minutes and 1 second, minus the time past the 0:15 //so we wake up near each 15 minute mark (prevents drifting on slow DHC P) imp.wakeup(3, function() {
}); server.sleepfor(1 + 15*60 - (time() % (15*60))); // full firmware is reloaded and run from the top on each wake cycle That's the device firmware let's take a look at the agent. You've already seen part of the agent we set the Xively API Key and Feed ID at the top of the agent firmware earlier. Below that, we have more class definitions three of them. Just like the class definition that gave instructions for working with a thermistor, these class definitions give instructions for sending and receiving data from Xively over HTTP. Below our Xively class definition is a really interesting bit of code this is the hook that the agent uses to receive new temperature data from the device. This is done by registering an agent "callback" with agent.on. This says, "when the device sends you an event called ʻtemp,ʼ call the following function with the data marked with that tag." Looking back at line 131 of the device firmware, we see that we sent data marked "temp" with a function called "agent.send". This is where that data went! device.on("data", function(datapoint) { }); posttoxively(datapoint.temp, datapoint.id); Lastly, we see the agent do two things when it first starts running. The first: it sends a log message letting us know that it started and is ready to get data. Second, it instantiates two of the Xively objects we defined earlier, letting us use the methods of those classes to send data to Xively as it comes in. The agent doesn't restart or sleep; it starts the first time the device boots with this model, and continues to run forever, handling each new data point when it comes in by calling the "device.on" callback. It's the device's brain in the Cloud! That's the firmware! Enjoy your new Internet-connected thermometer.