Connecting Phidgets With MSN Messenger

(back to: Back to Phidgets.Net)

What you will learn in this tutorial:

  • Using phidget components and skins in C#
  • Interacting with the MSN Messenger (by using the MSN Messenger API)
  • Display events from the MSN Messenger on the Phidget LC display and the Interface Kit (with LED)
  • Using RFID tags to control the online status


In this tutorial we will develop an application that uses several Phidget devices to control the Microsoft MSN messenger. The following functions will be implemented in the application:

  1. Show the current status of the user with a green and red LED
  2. Use Phidget RFID tags to control the user status
  3. Show changes of the status of other users in the Phidget Text LCD


Getting started:

Note: This tutorial explains all steps to create the application, but you can also download the complete VisualStudio MSNPhidget project here.


  1. If the Phidgets.NET library is not installed on the computer: Download the Phidget.NET installer from Grouplab Phidgets.NET download and install the package
  2. Add the phidget component to a Visual Studio 2003 tab; this is explained in detail on the Grouplab Phidgets.NET website
  3. Check if the Microsoft Messenger (recommended Version 7.0 or 7.5) AND the Windows Messenger (Version 4.7 or later) is installed, or download and install them from the Microsoft Download Website for Windows Messenger and the download website for Microsoft MSN Messenger 7.5
  4. Connect the Phidgets with your computer: The RFID reader, and the Text LC display (with integrated Interface Kit) with 2 LED (green and red)
  5. Start the MSN Messenger and login


Tutorial:

A) Starting with Phidgets

  • Start Visual Studio 2003 and create a new project (C# Windows Application, name: for example "MSNPhidgets")
  • From the 'Phidget' Toolbar Tab drag the following objects to the form: RFID, InterfaceKit and TextLCD, and also drag the Skins to the form: RFIDSkin, InterfaceKitSkin and TextLCDSkin
  • Change the phidget property of the skins: The 'RFID' property to 'rfid1', the 'InterfaceKit' property to 'interfacekit1' and the 'TextLCD' property ro 'textlcd1'
  • Run the application and test the components
  • If the RFID reader is not activated by default, add an event handler to the 'Attach' event and add the following code to the callback method:
private void rfid1_Attach(object sender, EventArgs e)
{
  this.rfid1.Outputs[3].State = true;
}


B) Connecting to the MSN Messenger: Display changes with Text LCD and LED

  • At first, we add a reference to the MSN Messenger API: Click on the Reference listing on the "Solution Explorer" window (usually on the right side of the screen), choose "Add reference...", click on the "COM" tab and search for the entry "Messenger API Type Library". Double-click on the entry and confirm the dialog window.
  • Add this to the beginning of the code
using GroupLab.Phidgets;
using MessengerAPI;
  • Add a private member of the type Messenger to the class:
private Messenger msn;
  • In the constructor of the form, create a new instance of the Messenger() and add the following event listener and the callback methods of the class (Remember that you can use the Tab key two times, to generate the callback method automatically)
msn = new Messenger();
msn.OnContactStatusChange +=
  new DMessengerEvents_OnContactStatusChangeEventHandler(msn_OnContactStatusChange);
msn.OnMyStatusChange +=
  new DMessengerEvents_OnMyStatusChangeEventHandler(msn_OnMyStatusChange);
  • Now we want to display the status of the users (if it changes) on the Text LC display. Add the following code to the first callback method we have created:
private void msn_OnContactStatusChange(object pMContact, MISTATUS mStatus)
{
  MessengerAPI.IMessengerContact contact = (IMessengerContact)pMContact;

  //
  // If Phidget TextLCD is attached, change the message of the display.
  //
  if(this.textLCD1.Attached)
  {
    this.textLCD1.Display = contact.FriendlyName + ": " + mStatus;
  }
}
  • And when the personal status changes, we want to visualize that with the LEDs: green for online and red for every other status:
private void msn_OnMyStatusChange(int hr, MISTATUS mMyStatus)
{
  //
  // If the status is ONLINE, then activate the green LED,
  // otherwise activate the red LED.
  //
  if(mMyStatus == MessengerAPI.MISTATUS.MISTATUS_ONLINE)
  {
    this.interfaceKit1.Outputs[0].State = true;
    this.interfaceKit1.Outputs[1].State = false;
  }
  else
  {
    this.interfaceKit1.Outputs[0].State = false;
    this.interfaceKit1.Outputs[1].State = true;
  }
}
  • Because we want to initialize the green and red LED when we start the application, we add a event listener to the 'Attach' event of the interfacekit1 (just click to the event in the properties and Visual Studio creates a callback method. Add the following code to the method:
private void interfaceKit1_Attach(object sender, EventArgs e)
{
  if(this.msn.MyStatus == MessengerAPI.MISTATUS.MISTATUS_ONLINE)
  {
    this.interfaceKit1.Outputs[0].State = true;
    this.interfaceKit1.Outputs[1].State = false;
  }
  else
  {
    this.interfaceKit1.Outputs[0].State = false;
    this.interfaceKit1.Outputs[1].State = true;
  }
}
  • We also initialize the Text LC display, so add also an event handler to this phidget and add the following code:
private void textLCD_Attach(object sender, EventArgs e)
{
  this.textLCD1.Backlight = true;
  this.textLCD1.CursorBlink = false;
  this.textLCD1.Display = "MSN Phidget";
}
  • We can now test this application to display the changes of the online states


C) Change the displayed messages

  • As you may have seen on the Text LC display, the messages about the status changes of the user display some ugly string messages; we change these messages by using a hashtable to translate these strings
  • Add this new hashtable member to the class:
private Hashtable onlineStates = new Hashtable();
  • Create a method initOnlineStatesHashtable to the class, and there add the following code to initialize our hashtable:
private void initOnlineStatesHashtable()
{
  this.onlineStates.Add(MISTATUS.MISTATUS_AWAY,  "Away");
  this.onlineStates.Add(MISTATUS.MISTATUS_BE_RIGHT_BACK"Be right back");
  this.onlineStates.Add(MISTATUS.MISTATUS_BUSY,  "Busy");
  this.onlineStates.Add(MISTATUS.MISTATUS_IDLE,  "Idle");
  this.onlineStates.Add(MISTATUS.MISTATUS_OFFLINE"Offline");
  this.onlineStates.Add(MISTATUS.MISTATUS_ON_THE_PHONE"On the phone");
  this.onlineStates.Add(MISTATUS.MISTATUS_ONLINE,  "Online");
  this.onlineStates.Add(MISTATUS.MISTATUS_OUT_TO_LUNCH"Out to lunch");
}
  • In the constructor of the form, call this method to initialize the hashtable on startup
  • We add now a lookup method, to translate the internal string messages to the displayed text
private string getStatusText(MISTATUS mMyStatus)
{
  if(this.onlineStates.ContainsKey(mMyStatus))
  {
    return (string)this.onlineStates[mMyStatus];
  }
  else
  {
    return "Unknown";
  }
}
  • Finally we change the display command in the method msn_OnContactStatusChange that we have created before;
if(this.textLCD1.Attached)
  {
    this.textLCD1.Display = contact.FriendlyName + ": "
      + this.getStatusText(mStatus);
  }
  • Now you can test the application with the changed display messages.


D) Using RFID tags to control MSN Messenger

  • Now we use the tags of the RFID reader to control our online status in the Microsoft Messenger.
  • Add an event listener to the TAG event of the rfid1 object, and add the following code to the callback method:
private void rfid_Tag(object sender, RFIDTagEventArgs e)
{
  if(e.Tag == "YourTag1")
  {
    if(!(this.msn.MyStatus == MessengerAPI.MISTATUS.MISTATUS_ONLINE))
    {
      this.msn.MyStatus = MessengerAPI.MISTATUS.MISTATUS_ONLINE;
    }
  }
  else if (e.Tag == "YourTag2")
  {
    if(!(this.msn.MyStatus == MessengerAPI.MISTATUS.MISTATUS_AWAY))
    {
      this.msn.MyStatus = MessengerAPI.MISTATUS.MISTATUS_AWAY;
    }
  }
}
  • Note: please replace the statement YourTag1 and YourTag2 with the tag ID numbers of the RFID tags you use.
  • In this method, we first check the RFID tag (with e.Tag) and then we check if we have to change the user state of the user (this prevents to much change events that we send to the Messenger, because a lot of similar RFID events are detected if you place a tag on the RFID reader)
  • As feedback that the RFID reader is activated, we add another method to the 'Attach' event listener for the RFID reader, and activate the digital LED output in the callback method:
private void rfid1_Attach(object sender, EventArgs e)
{
  this.rfid1.Outputs[3].State = true;
  this.rfid1.Outputs[1].State = true;
}
  • We also want to deactivate this LED when we end our application, so we create an event handler to the Closing event of the form and add the following code:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  this.rfid1.Outputs[3].State = false;
  this.rfid1.Outputs[1].State = false;
}
  • THAT'S ALL. You have finished your MSNPhidget tutorial, and successfully developed an application that uses Phidgets to interact with the MSN Messenger.


Downloads (General):


Downloads (this tutorial):


Links: