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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s