World’s Most Complicated Hit Counter

Hit counters are found on very site on the internet – though they barely go by that name anymore. Modern hit counters are invisible, fast and do more than keep track of the number of times a web page was loaded. Suites like Google Analytics record which geographical region a visitor is from, what operating system they use, and how much time they spend on the website.

The hit counter I built may not do any of those things, but it does keep track of hits, and in the most complicated way possible.

PHP scripts? No Thanks, I have Lego.

A mechanical counter is used to store the number of times a webpage is loaded. Every time a person loads this page, the mechanical counter is incremented by one. To retrieve the hit count, a webcam takes a picture of the counter. That picture is then displayed to the person viewing the site by loading the image as part of the webpage. Here it is in action:

The picture above was taken just now. Right as this webpage was loading. If you refresh this page, you will cause the counter to count up by one. The new image loaded here will show this new number. The counter sites on a windowsill, so you’ll also get a different background scenery depending on the time of day. Here is what the web hit counter set up looks like:

Setup of the most complicated hit counter

The mechanical counter


How It’s Made

There are four main components in play here. There is of course the vintage counter which displays the current web hits. Behind the scenes there is the web cam that takes the pictures on request. There is an Arduino microcontroller that controls the mechanical counter. And lastly there is a custom server program that receives requests over the internet and controls the Arduino and the webcam.

When the server gets a request for the hit count image it does a few things to create it. First it sends a message to the Arduino over the serial port, commanding it to increment the counter. It waits for the arduino to sends a response back, to indicate that the counter has incremented to the next number. This is a relatively slow process because it takes the counter about 1/5 of a second to roll over to the next digit. Once the response is received, the server commands the USB webcam to take a picture. It saves this picture as webcam.jpg and then servers it up to the person on the other side of the internet.

I wrote the server using a windows scripting language called AutoIt. The server is mostly a compilation of different code  that I found on the AutoIt forums and put together. If you would like to experiment with the server yourself there is a download link at the bottom of this post. Here is a snippet of code from the server, where a lot of the hit counter related action takes place:

Code snippet from the server

The Arduino sketch is fairly simple. It only needs to do three things: Wait for the serial message to arrive, pulse a pin high-low-high and send back a serial message. You can download the sketch, also at the bottom of this post.

When I found the mechanical counter it was mounted to a heavy piece of metal, with about 29 more identical counters mounted around it in a neat rectangle array. This thing smelled and looked old. The solder on the backside was so aged that it was covered in thin film that you don’t normally see on solder. Many of the counters were broken. I don’t know what voltage these were designed to operate on, but I’ve tried them with 12 volts and that worked really well. Ultimately I ended up powering it with 5 volts directly from the Arduino I/O pin because it was easy to wire up and it looks neat. It works, but the counter clicks to the next number noticeably weaker. This is part of the reason why it takes so long to advance to the next number. I’m sure if I used an external 12 volt supply for the counter the server would be able to handle more than five requests per second.


 Source Code

If you would like to try running the server yourself or just poke around the code, feel free to download the source files.

Download the server source files

Download the Arduino sketch

When you run the server it is going to try to find the Arduino connected to your computer. It does this by sending some data to each comm port on your computer and listening to see on which port it gets back a proper response. If you don’t have an Arduino or just want to run the server standalone then you should comment out this part of the code – as it’s programmed to not start up the server if it can’t find the Arduino . Likewise, if you do want to run the server with the Arduino attached, make sure the Arduino has the corresponding sketch loaded on it so it can respond to the server when the server tries to find it.

One other quirk about the server. When it comes to stability, it is as reliable as the weather. If you came to this page and the counter image is not loading for you, then I apologize. Some dude and his friend probably DDOSed it and brought it down by pressing F5 at a very moderate rate. I haven’t yet investigated why the server is so finicky. So far I’ve been pretty excited just watching it work.

One Comment on “World’s Most Complicated Hit Counter”

  1. john smith says:

    what happens when you hit 1000 views?!?!?! (end of the world)

Leave a Reply

Your email address will not be published.