Projects‎ > ‎Personal Projects‎ > ‎

Arduino Temperature Tweeter


So my wife basically put me to a small challenge.  You see, her office temperature appears to fluctuate a lot.  The people that she works with thinks that she is making it up.  But I've been in her office and I believe her.  So she told me one day that she wanted something that would tweet out the temperature in her office.  I've been meaning to play with an arduino for a while now.  I have of course heard much about them, and many of my students either have them or have played with them.  So I thought this project might be a good project to see what this Arduino can do.  I ordered an Arduino starter kit from Sparkfun along with the Ethernet shield.  As usual, I got my items from Sparkfun fairly quick with no problems (gotta love Sparkfun :).  I then started to play around with the arduino processing software and pound out some code.  The results are the sketchbook attached and the very simple schematic.  In addition to the temperature sensor, there is also a light sensor.  I thought it would be neat to see if the lights were on or off.  Again, this was kind of a silly project, so I just added stuff to add.  I had the sensor in the starter kit, so why not.  

Here are a few pictures on the project

The Arduino + Ethernet Shield + the breadboard that comes with the starter kit


A close up of the light sensor (the thing with the squiggly line) and temp sensor (center of the picture)

Project Notes

Twitter will reject tweets that are identical to avoid duplicates, so two tweets in a row can't be the same.  If the temp is the same, it is technically possible for this to happen.  Two things I did to help with this.  First, you will notice that the temperature is out to 2 decimal places.  It's not that I needed temperature readings that accurate, it was to help avoid this.  The second thing was a random number generator.  The number in () is just a random number to again help avoid Twitter from throwing out the second tweet if the temperature is exactly the same.

I was testing this at home, which is behind a firewall of course.  The Twitter.h includes a way to read back the status of the tweet.  If it's a good send, Twitter should respond with HTTP 200 for OK.  I however was getting 0, which is not even a valid HTTP response code.  However my tweets were clearly getting through and showing up in my account.  Some more digging in this showed that what was probably happening was that the Twitter.h was not getting any response back.  This is probably caused by being behind a firewall.  My wife works at a university, so more than likely this will not happen there and will either confirm or deny my theory.

Update:  So, this did not go away despite having a static IP and no firewall.  I am pretty sure there is a glitch in the twitter.h library but I haven't looked into the library yet to what it might be.  We did see a response 500 (internal server error) once so it is working somewhat, it just doesn't seem to be responding with 200 like it should and the status comes from the twitter.h function, so it's either there or a problem with Twitter.

The latest version of the Twitter.h no longer supports the username:password method to login to your twitter account.  Instead, it uses OAuth and a token that you will need to generate.  Fortunatly the author created a site that you can use to allow the Twitter library to connect to your account and to create that token.  There is a place in the code where you need to insert this token in order for it to work properly.  You can visit http://arduino-tweet.appspot.com/

Quite a bit of code was code taken from various sources on the web.  Mostly from the Twitter.h example and the PString examples.  I'm sure there are other sources as I did some searching while troubleshooting.

The arduino is a great tool for those getting started in microcontrollers.  For those that already have programmed microcontrollers (like myself)...it can be frustrating.  The GUI that most use to program the Arduino is very limiting, but it gets the job done which is a plus.  Still, there are reasons why tools like MPLAB have certain functions, it does make troubleshooting easier...if you know how to use them.  

All in all this was a fun little project.  So I thank my wife for the challenge.  Now she can show everyone that she is not crazy and that her office changes temperatures quite a bit.  Plus she won't have to wonder if she left the lights on :)

Update:  Does it work?  Yes, yes it does.

Update1:  I found a small bug where I didn't have the random number generator in the code posted.  Make sure you have code version 1.03 (revision 2 in the attachments area)
Update2:  Found another minor bug when it was tweeting that the lights were off, I forgot a "(".  Fixed, recent version should be 1.04 (revision 3)

Update3 (1/6/2011):  I have uploaded code version 1.10 (version 4).  This adds a webserver that just outputs the temp and all analog pins.  This could be handy for diagnostics or if you don't want to wait for the next tweet to check your temp.  Also, this will compile with version 0018 of the Adruino software, but not with the newer ones.  Something to do with the Ethernet libraries and wanting to use the SPI library as well.  I'm looking into it.  I will probably fix that issue, and then maybe I'll zip up the source and all libraries I'm using and upload.  After that, I think I'm done for a while.  Adding the webserver was sort of the last thing I wanted the project to do.  Until I get another challenge or come up with something else to do with it :)

Update 4:  So I found two nasty bugs.  One, I didn't use a long int when I should have been, and two I put the counter variable inside the loop (yeah, doesn't work too well to count down something if you are always setting it back to the initial value).  So, please use version 1.11 (version 5).  Also, the web server now also spits out the counter value.  This is just a debug tool to make sure that it is counting down.

Update 5:  Ok, so last thing after some more testing.  Adding the webserver routine of course added some additional delays due to the clock cycles going to service that routine in the main loop.  That I knew, but I figured it wouldn't be much, but it or something is nearly doubling the time it takes for it to countdown (or I'm doing something else wrong).  It looks like you need to cut your rate that is defined in half to get about the time you need.  There is a delay in the main loop, but removing that makes it go really fast :)  So there are some other things at play here.  For now, just change your RATE define in half and it should be pretty close.  Once I have time again I'll try to see more on what is going on in those routines.
Č
ċ
ď
Temp_monitor_proj.pde
(7k)
Matthew Nelson,
Jan 6, 2011 7:39 PM
ą
ď
Matthew Nelson,
Jul 12, 2010 8:25 PM