Category Archives: software

Wireless switch using arduino and a mac

Ability to control lights and fans wirelessly has long moved from being an expensive proposition to having commonplace solutions neatly packaged and available for consumers. Even then, coming up with your own custom solution is a lot of fun for hobbyists and tinkerers.

This is an example of one such implementation using Arduino, solid state relay, bluetooth module and a MAC. One of the challenges of using a MAC (OSX) is that direct access to hardware resources (like serial ports etc) is sometimes difficult (especially using Cocoa and Objective C).

RobotGrrl has provided a couple of easy to use libraries (frameworks for MAC lingo) to address the above issue and has provided some great tutorials at Apps for arduino. The source code is also freely available at github (search Matatino/Wijourno). The Matatino framework provides a simple library for connecting to the serial port, while Wijourno utilizes WIFI and Apple’s Bonjour Service (I think!).

For this experiment, I wanted to go wireless! Have a simple application that I can use from my MAC to control a power switch! Since I has previously invested in a Bluetooth module, I decided to use the Matatino framework to connect to my arduino over the air. For the power switch, I used a solid state relay kit , but a simple SPDT Relay available from Radio Shacks works as well. The connections from the Arduino to the Relay kit are straightforward:
Vcc -> Vcc
Gnd -> Gnd
Cntrl -> Pin 13

The Arduino setup is shown below:

A simple sketch is uploaded to the arduino, that listens on the serial port. When it sees a 1, it sets PIN 13 to high, thereby powering or switching on the relay, and on receiving a 0, it switches it off.

int ledPin = 13;

void setup()
{
  Serial.begin(115200);
  pinMode(ledPin,OUTPUT);
}

void loop()
{
  if(Serial.available() > 0)
  {
     char cmd = Serial.read();
     Serial.print(cmd);
     if(cmd =='1')
     {
       digitalWrite(ledPin,HIGH);
     }
     else if(cmd =='0')
     {
        digitalWrite(ledPin,LOW);
     }
  }
  delay(20);
}

The Matatino framework provides an arduino delegate that can be safely used to open a connection. For my simple user interface design shown below, only a couple of methods need to be implemented.

The first method is called when the user presses the Connect button. This method tries and connects to the arduino if its not connected, else disconnects it.

- (IBAction)connectToBT:(NSButton *)sender
{
    if(![arduino isConnected])
    {
        NSString *myDevice = @"/dev/cu.RN42-9A7D-SPP";
        // Connect to your device with 9600 baud
        if([arduino connect:myDevice withBaud:B115200])
        {
            sender.title = @"Connected";
            connectionButtonState = true;
        }
    }
    else
    {
        [arduino disconnect];
        sender.title = @"Connect";
        connectionButtonState = false;
    }
}

The second function is called when the Switch button is pressed and the function just toggles between sending 1 or 0 to the Arduino.


- (IBAction)switchOnOff:(NSButton *)sender
{
    if(!powerButtonState)
    {
        if([arduino isConnected])
        {
            [arduino send:@"1"];
            powerButtonState = true;
        }
    }
    else
    {
        if([arduino isConnected])
        {
            [arduino send:@"0"];
            powerButtonState = false;
        }
    }
}

The entire source code including the Xcode Project is available at Github.

The next steps for this project is to create an iOS app that allows you to do the same. Unfortunately, as Apple does not support the SPP Bluetooth profile on iOS devices, it would take the integration of Wijourno and Matatino to create a working prototype using the above hardware.

Online courses galore!

I’ve been busy recently! Rather, I’ve been kept extremely busy with the sudden extraordinary explosion of online courses (all for free!). I know there is tons of information out there on the web, but still having quality professors teach you stuff is quite amazing.

For example, I am currently taking a course on Udacity on Programming a Robotic Car taught by Prof Sebastian Thrun.  The experience has been quite amazing so far. I had taken a similar course during my Graduate School based out of his Probabilistic Robotics book and really enjoyed the class then, but to have him teach it, is quite fantastic.

Previously, most of the course ware offered online were in the form of lecture notes, some videos (MIT OCW site, and some other universities) and limited access to the HWs and tests. However, now all of a sudden, there are multiple high level online courses that are being offered and I am enrolling in all that I can find ( I currently have a heavier load then during some of the semesters I was in school). These courses are all free, with HWs, tests (that have deadlines and grades and almost all material required) and with a completion certificate on passing the course. Of course, you could just attend the course for the knowledge too without worrying about any grades.

While most of the courses are software related (CS101, Programming, Machine Learning, Computer Vision etc),  it seems other topics are also popping up.  MIT has just launched an introductory course on Electrical Design running as a part of their MITx initiative. CalTech is stepping it up further offering live lectures directly from the university.

Though nothing beats hacking around to learn new stuff, some formal classes only open up new things to play with! Here are some of the sites offering free online courses:

Udacity : CS101, Programming Languages, Program a Robotic Car etc

Coursera : offering courses from multiple universities- Machine Learning, Computer Vision, Software As a Service etc

MITx : Circuits and Electronics

iTunes University

My Eagle Guide

Eagle is one of the more popular PCB design softwares out there! One of the major plusses for the software is that it provides different license versions for the users (starting from the Free version to hobbyist version and all the way to a professional version). For me, personally the free version is more than enough!

I use Eagle to document some of my schematics and try and design custom arduino shields for my projects. I have compiled the following list of links (tutorials) from the web that I refer to all the time.

Firstly I recommend the set of video lessons by Jason from RPC electronics. He has these tutorials recorded and available in Youtube. The links to the individual lessons are below:

Lesson 1:  Creating a new project (1:28)
Lesson 2: Eagle Schematics (Description of basic tools) (6:20)
Lesson 3: Basic Design using Eagle schematics I (6:16)
Lesson 4: Basic Design using Eagle schematics II (6:31)
Lesson 5: Basic Design using Eagle schematics III (4:49)
Lesson 6: Creating the PCB Board from schematics I (8:43)
Lesson 7: Creating the PCB Board from schematics II (7:22)
Lesson 8: Creating the PCB Board from schematics III (Routing) (8:41)
Lesson 9: Auto routing and Ground planing (9:20)
Lesson 10: Silk screen generation (6:17)
(Script files and cam files)
Lesson 11: Gerber file generation (8:44)
Lesson 12: Q & A (17:38)

I also heavily use the library made available by spark fun. Apart from the library, Sparkfun also has some Eagle tutorials:
a) Getting started with Eagle I
b) Getting Started with Eagle II

For creating a custom part in eagle, I use the following sparkfun tutorial as well as the instructable:
a) Instructable
b) Sparkfun tutorial

Hope this is helpful and saves some google searches 😉

scorecards (mysql) on the web!

I love cricket! For those of you unfamiliar with it, its a sport kind of like baseball. I play this local league with 3 teams generally after work and we take it pretty seriously! We like to keep scorecards of the games, store them someplace analyze them incessantly and use them to brag! Up till now these were in paper (impossible to share, maintain and aggregate).

With mobile phones theses days, its become easy to store data electronically. It was quite easy to find an Iphone App (CricMitra) that could be used for scoring and exporting the scorecards in HTML. Initially having scorecards available online was a huge thrill, but it was quickly passed over by the need to aggregate data and see statistics for the season.

This is typically done using databases and that’s where MySql comes in. Storing data in the database allows one to easily mine the data and get various statistics. Without going in details about the schema of my database, the real difficulty comes in using them comes from a) insertion of data (in our case HTML scorecards) and b) hosting this database so that it is easily accessible by webpages online.

There were typically 3 options for a novice in web like me and I tried them all. I list each below:

1) The original solution was to setup a linux server, install LAMP on the server and use this server as the website. The basic idea being to parse the HTML scorecards and store the information in the MySQL database. This database is then accessed using PHP to display contents online. I do believe that this was the best option, and I had everything setup but for the issue of a static IP. Websites, typically have static IPs (you can get around it using dynamic dns with daemon running), but my ISP blocks common ports like 80. So while I had a great website for my home network, it was useless outside!

2) To use a web hosting service. Most Web hosting services provide a LAMP setup available for users. This is really useful in setting up forms on your websites for easy storage in to databases. Here, I ran in to basic issue that the databases on the servers (MySQL) are not remotely accessible. What this means is that the databases do not accept remote connections. So I cannot connect to the database using an SQL client (I recommend Sequel Pro for Mac)  externally. Typically this is a good security design, but for my case where I had code written that parses the exported scorecard (html) and then exports data to a database, access to My SQL remotely was a must.

3) This is when I ran across db4free.net. They offer a free service of hosting MySQL databases online that can be remotely accessed. This is a test server and they do not recommend hosting production level details on the server. But for a small website, where the traffic is fairly limited, this is an ideal solution.

Currently, my database is set up in db4free.net and I have free web hosting using 000webhost. There are simple PHP pages that query the database for information and we have our own league statistics. Pretty cool and easy (provided you know a bit of coding, a bit of databases and a bit of the web).

Check it out at mplrocks.comze.com. I know that the website still needs some design, but the data is pretty cool.

The options I listed out are again in the order I feel are the best. My third option is not ideal and I would like to move to option 2, where I can use the MySQL database provided my 000webhost to store data, but that would require me to write some PHP code that could parse HTML into the database (something I am still working on 🙂 ).