Arduino Hacks

Arduino is an open-source hardware platform based on a simple controller board and a development environment that implements the Processing/Wiring language. Arduino can be used to develop various interactive objects or can be connected to software on your computer. More information and software can be found at arduino.cc. We will be using a Arduino clone, aptly named as Freeduino for our work.

Freeduino

Displaying the visitors on your WordPress blog

In this hack we will see how to display the total numbers of visitors online on a LCD display using the Arduino. Before we start playing with the hardware part, we need to somehow get the visitor count from the blog. The first thing we need to do is define a proxy that will return the results from our WordPress site. For this we will use the code developed in one of my previous posts.

Arduino prototype shield

Breadboards are wonderful for testing new circuit ideas, but once a design is done it is better to create a permanent circuit on a PCB.

The Protoshield is a Arduino compatible board that connects with the Arduino USB board and gives the user a small soldering area on which one can design new curcuits, two general LEDs and a single tact switch which works as a Arduino reset button. A blank shield is shown below.

arduino prototype shield

The above shield after the LCD module has been soldered on.
arduino prototype lcd module

The prototype shield backside. The tangle of wires could have been soldered beneath the LCD, thus hiding them, but it would make it difficult to change the wiring if the need arises.

arduino prototype board backside

LCD pin connections

The following table shows the pin details of the LCD.

LCD Pin NumberSymbolFunction
1VssDisplay power ground
2VddDisplay power +5V
3VoContrast Adjust. Altered by adjusting the voltage to this pin, grounding it sets it to maximum contrast.
4RSRegister select
5R/W
6EEnable strobe
7DB0Data Bus 0
8DB1Data Bus 1
9DB2Data Bus 2
10DB3Data Bus 3
11DB4Data Bus 4
12DB5Data Bus 5
13DB6Data Bus 6
14DB7Data Bus 7
15A

LED backlight power +5V
16KLED backlight power ground

Connecting the LCD to the Arduino

You need to connect the correct data pins of the LCD to the Arduino. The pin details are given below.

Arduino PinLCD PinLCD Pin Name
24RS
35RW
46Enable
53Contrast (grounded with a variable resistor)
911Data 4
1012Data 5
1113Data 6
1214Data 7

Arduino code

The following is the Arduino code to monitor the serial port and print the message passed by the Processing applet.

/* Arduino code */

/* include the LCD library code */
#include 

char buffer[100];

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 9, 10, 11, 12);

void setup()
{
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  // initialize the serial communications:
  Serial.begin(9600);
}

void loop()
{
  int count = 0;
  
  // when characters arrive over the serial port...
  if (Serial.available()) 
  {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    lcd.setCursor(0, 1);
  
    // read all the available characters
    while (Serial.available() > 0) 
    {
      // display each character to the LCD
      lcd.print(Serial.read(), BYTE);
      }
  }
}

Processing code

Below is the Processing code that accesses my blog and gets the total users online as a JSON response.

/* Processing code */

import org.json.*;
import processing.serial.*;

int       result;
boolean   error = false;
String    total;

Serial    myPort;
String baseURL = "http://www.codediesel.com/remote/proxy.php";

void setup() 
{
  size(4, 4);
  myPort = new Serial(this, Serial.list()[1], 9600);
}

void draw() 
{
  /* Get the total users by connecting to the site */
  getSiteData();
  
  if(error == false)
  {
    myPort.write("Total Users : " + total);
  }
  else
  {
    myPort.write("Error connecting...");
    error = false;
  }
  
  /* Wait for 5 seconds before re-checking */
  delay(5000);
}


void getSiteData() 
{
  try 
  {
    JSONObject siteData = new JSONObject(join(loadStrings(baseURL), ""));
    total = str(siteData.getInt("total"));
  }
  catch (JSONException e) 
  {
    error = true;
    println ("There was an error parsing the JSONObject.");
  };
}

The final result

The following image shows how the final result looks like after connecting everything. The display now shows the total visitors currently online on my blog. Casual users will balk with the idea of going all this way to just display a user count on a LCD. But the essential element lies not with the end result but with the spirit of tinkering and experimentation; connecting things up and seeing it work.

arduino final

The next step

A much better way to connect to a website will be to use the Arduino Ethernet shield. This will allow me bypass the PC completely and will directly connect the Arduino to the Internet.

Arduino Ethernet Shield

2 thoughts to “Arduino Hacks”

Leave a Reply

Your email address will not be published. Required fields are marked *