Wednesday, April 23, 2014

Handel's Blog

Minimize

Coffee Track : Power control

Posted by: Michael Stephens on 1/27/2010

To control our coffee pot we had many hurdles to overcome. We needed to be able to switch a pot on and off from Silverlight.

PC Interface

For the PC interface we chose to use Arduino over X10 because it would be more fun to integrate with. Arduino is a microcontroller platform that is designed to be programmed via a computer with no additional programmers required. To actually control the coffee pot we picked up a $10 wireless Christmas light controller that can be found at any home improvement or retail store. Ours came from Wal-Mart.  We went with a wireless controller to completely isolate ourselves from the 120v coffee pot. We added some relays between the Arduino and the remote control and voila we have an X10 like wireless light controller.

 

 

Wireless Light Controller

Wireless light controller purchased at Walmart.

 

schematic

Schematic of System

 

Control Bread Board

Breadboard of system

Finally the Arduino “sketch” aka code receives a ‘1’ to turn on the “on” relay for 1 second or a ‘0’ to turn the “off” relay on for 1 second.  The commands are sent over a standard serial port (Arduino has an onboard USB->Serial converter).

const int ledPin1 = 13; // the pin that the LED is attached to
const int ledPin2 = 12; // the pin that the LED is attached to

int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}

void loop() {
  
  
  // see if there's incoming serial data:
  if (Serial.available() > 0) 
  {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    
   Serial.println((char)incomingByte);
   if (incomingByte == '0') {
      digitalWrite(ledPin1, HIGH);
      delay(1000);
      digitalWrite(ledPin1, LOW);
      Serial.println("Pot is off");
    } 
    else if (incomingByte == '1') {
      digitalWrite(ledPin2, HIGH);
      delay(1000);
      digitalWrite(ledPin2, LOW);
      Serial.println("Pot is on!");
    }
  }
}


Silverlight Integration

Believe it or not classic web applications are not designed to talk to hardware.  Most workarounds require hosting a local web service so that your web application can talk to hardware on your machine. I refer to this as a “Bridge”.  With Silverlight 4 however the possibilities become endless because Silverlight 4 allows us to talk to COM libraries.  Not knowing of any standard Microsoft COM libraries to communicate with a serial port we downloaded ActiveXperts ActiveComport library (free for 30 days). We could have built a .net library and converted it to COM but this was a suitable direction for a RiteTrack challenge system. 

After reading some very handy VBScript examples we were able to command the Arduino in SILVERLIGHT!  Below is some code snippets of how we make it happen.

 
private void Init_Click(object sender, RoutedEventArgs e)
{

    com = ComAutomationFactory.CreateObject("ActiveXperts.ComPort");
    dynamic count = com.GetDeviceCount();
    StringBuilder sb = new StringBuilder();

    List<dynamic> devices = new List<dynamic>();
    for (int i = 1; i <= 9; i++)
    {
        devices.Add("COM" + i);
    }

    for (int i = 0; i < count; i++)
    {
        devices.Add(com.GetDevice(i));

    }

    COMS.ItemsSource = devices;
    COMS.IsEnabled = true;
    Connect.IsEnabled = true;

}

private void Connect_Click(object sender, RoutedEventArgs e)
{
    if (COMS.SelectedItem == null) { MessageBox.Show("Please pick a port"); return; }
    com.Device = COMS.SelectedItem.ToString();
    com.Open();
    MessageBox.Show(com.GetErrorDescription(com.LastError));
    string buffer = "";
    System.Threading.Thread t = new Thread(new ThreadStart(delegate()
    {

        while (1 == 1)
        {
            com.Sleep(200);
            buffer = com.ReadString();
            if (buffer == "") { continue; }
            Output.Dispatcher.BeginInvoke(delegate()
            {
                Output.Text += "\r\n" + buffer;
            });
        }
    }));
    t.Start();
    Send.IsEnabled = true;
    SendTxt.IsEnabled = true;
    Output.IsEnabled = true;
    connected = true;
    InitPot();
  
}
void SendMessage()
{
    foreach (char c in SendTxt.Text)
    {
        if (c == '1') { ison = true; }
        else if (c == '0') { ison = false; }
        com.WriteByte((byte)c);
    }
    SendTxt.Text = "";
}

 

 

Note: Please be aware that this will only run as an “out of browser” application for security purposes.

RiteTrack 4 Integration

Now that we have it worked out how Silverlight and our coffee pot controller can talk we need to get it integrated into RiteTrack 4.  To do this we decided that the commands would be stored in a table dtCoffeeCommand.  We did this so that commands could come from any source (twitter, RiteTrack, etc) and so that commands could be scheduled.  We added our table in Design Studio Silverlight and Traxed our application.

image

Screenshot of configured table in Design Studio Silverlight

To get our data we simply made a request every 30 seconds to recover the information.

 

void timer_Tick(object sender, EventArgs e)
{
    if (connected)
    {
        dtCoffeeCommandProxy proxy = new dtCoffeeCommandProxy();
        proxy.dtCoffeeCommandLoadCompleted += new dtCoffeeCommandLoadCompletedHandler(p_dtCoffeeCommandLoadCompleted);
        proxy.LoadAsync("where processed=0 and CommandDate , new Dictionary<string, object>());
    }

}
void p_dtCoffeeCommandLoadCompleted(List<dtCoffeeCommand> dtCoffeeCommandLCs)
{
    foreach (var cmd in dtCoffeeCommandLCs)
    {
        this.SendTxt.Text = cmd.CommandTypeID.ToString();
        SendMessage();
        cmd.Processed = true;
        cmd.UpdateAsync();
    }
}


 

Conclusion

We were able to control our coffee pot from within Silverlight 4 by linking a COM library to an Arduino to control some custom hardware.  This was only possible with Silverlight 4.  We are hoping that we can have direct .Net access in the future instead of talking to old COM libraries. RiteTrack 4’s data layer and visual designer made it very easy to get up and running in a short amount of time. If you are looking for how we did this outside of RiteTrack check out my Christmas Light Controller which was built after this project using the same hardware and direct twitter access.

Create a trackback from your own site.



  
  

Archive

Maximize
  

Featured Posts

Maximize
  

Minimize
  
Copyright 2012 by Handel Information Technologies