Monday, October 7, 2013

Arduino Fun Starter

 

It has been over two years since I have got my first Arduino Uno and just this January acquired my first Raspberry Pi. The excitement returned and got another one since I broke the first one’s SSD slot.  Now I have full time two Raspberry Pi’s and two Arduino’s boards kicking in data and doing stuff.

I was presented by an Indoor/Outdoor digital thermometer as a gift. Lucky me! Then on a gloomy weekend which we had a few extra ones this season here in MN, I started looking around to create a basic temperature logging circuitry using Arduino Uno. I ran into this site. http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html

All I needed was DS18B20 digital temperature sensor, a 4.7 kOhm resistor. The circuitry was pretty basic which is exactly what I wanted. Here it is:

CircuitDiagram2

As you can see, the wiring can’t be simpler than this. Here is my set up:

TempCollectionTakeOne

The close up circuitry for the record is this:

CircuitDiagram1

Then I modified the built-in sketch file for the Ethernet shield to accommodate the temperature sensor. As it turns out, you need two Arduino libraries shown below:

TempServerSketchNext you will write a bit of code to publish your reading to the web site the Ethernet shield is serving.

Here it goes – first the setup():

setup

Next the loop() in two images:

looploop2

Apologize these are simple images, but this is really cookie-cutter Ethernet shield code modified. Please type it up and see how it works instead of cloning a piece of code from github or codeplex and contacting me a few years later because it won’t work in 2017. Who knows? Maybe it will, maybe it won’t. It works in 2013.

This code will render an html page on your local network (IP address : 192.168.1.177 in this case) as follows:

websiterender1

Now it is time to persist this data. How to do this? How about nodejs and postreSQL? My relatively new friends… Here is the nodejs code which scrapes this data and moves it into a postresql database running locally.

nodejs1

Yet another image, but come on. It is only a few lines of code. Should not be an issue. I promise next blog will have “github” pull downs & forks.  Basically what the above code is doing is calling in two libraries, “request” and “cheerio” (hope they are available in 2017, but this is open source, you never know).  First “request'” library kicks in. It hooks up the uri address, loads up the body, returns the text of the h2 element. How exciting is that? Yes, very!!

But I want to run this for every 5 minutes… How do I do that? Well, I have a full time running Windows machine, how about I create a “Scheduled Task” for that and be done. Yes, Virginia. It is simple and it works. Look at all other scheduled tasks in your system if you don’t believe me.

ScheduledTasks

Now you read your data value, perhaps you want to persist it somewhere. AWS is a bit too pricey for my taste, for now I will stay local. How about a table inside a postgreSQL database. Here it is:

Postgresql1

So that verifies that our data is being persistent and we are collecting it. Yeah! That is how I roll, or not. Who cares if no one would see your data?

How about a simple Rails app running on Raspberry Pi? Anyone? Yeah, okay me. After two hours of installing Rails on Raspberry Pi, found here, you will have it running in no time* (it took me over an hour to get it working)

Then you can write an app like this:

Homeye1

Note that I am using a 3rd party library called jquery.flot.js. Then index.html.erb is this:

indexhtmlerb

Now that you have a web page to render your temperature data for a given date. If lucky, you can see the actual rendering here. How cool is that?

FinalRendering

And it runs on Raspberry Pi. How cool is that? Well, the answer is always “it depends”. But what I have done and showed is using Arduino, NodeJs, PostgreSQL, Raspberry Pi, and Rails to collect and present temperature data.

Since then I have acquired this excellent book and completed the project within. Awesome book, by the way. It introduced me to Fritzing and other components and tools. Easy to follow, highly recommend it.

DistributedData

Well, now that this project is wrapped and published, I am looking at other ideas and suggestions with Arduino. I am also attending arduino.mn once a month as schedule allows to exchange other ideas and collaborate with others. Bluetooth, zigbee, wifi are all interesting and I have to explore more.

Please let me know what you think of this and love to hear your feedback.

Happy Hacking!

2 comments:

  1. Hi Baskin, thanks for posting the info about your project. It has been few months I wanted to comment, but just now got the time.

    I followed your steps to understand why/how you use each tool. It all made sense until you brought in the Raspberry Pi. I am not sure why you decided to add the RPI to be the server, since you already have a PC running the scrapper and DB. Could you explain the decision? Was it just to get to play with the Pi?

    Again, thanks for posting the project. I'm really impressed the service is still running. It looks like your office is at a comfortable 70F right now :)

    (One suggested improvement is to change the x axis to time...)

    Igor

    ReplyDelete
  2. Yes, exactly. I wanted to see if I could install Rails server on the Raspberry PI. It is slow running. I have a few future ideas with the PI in order to control simple devices. This was phase #1 for that.

    At this time, I have no other hardware hooked up on the PI, but ready for expansion.

    Thanks for checking it and your feedback. Much appreciated.

    Baskin

    ReplyDelete