Monday, June 23, 2008

Sending Information from wiimote through sockets II

In our last session, I made an introduction, of the current project I have just developed, and explained how to obtain basic information about the wiimote.
I wanted the server to pass this information, to clients that requested it.
The basic work flow of my program is as follows:

One Client makes a petition of the information to the Server,The Server asks the wiimote for this information, the wiimote gives back the desired information. The Server then sends this info to the Client. The Server, then takes another petition, a petition which can come from the same Client, or from another completely different one. This cycle is repeated till the end of time, or till America's Next Top Model is on, and we need to start doing important stuff.

My project is therefore divided into 2 parts, the server part and the client part.
The Server part, is divided into:
  • Creating the Server, have it listening on a certain address and Port for petitions.
  • Having the Server always accept incoming connections to it.
  • Once the server has a connection, the Server must attend this Client. It will create a Thread, for each of the Clients that want a connection with him.
  • The "Thread Function" will receive a petition of what it's client wants. One Thread will receive from it's client a petition to obtain the Wii's Acceleration, another Thread might receive the petition to give the Pitch angle that the Wii has etc. Each Thread will receive a petition of a desired info from it's Client.
    The Thread, in order to get this info, will call a function that depending on the String of text it receives, will send information to the client about the wiimote. This function, that sends to the Client it's desired information, receives therefore a *char,with the kind of information it wants, and the accept Socket,so this function can once and for all, write back to client, and send him the desired info.

Now, because Thread Functions can only be created with this
DWORD WINAPI ThreadProc(LPVOID lpParameter)  syntax, we need to do a whole lot of 
hassel of packing arguments in a temporary wrapper object andt hen unpacking
them inside some wrapper function. To understand how I managed it, I recommend you guys read this article,
it gives a nice explanation of the problem, and solves it in a very neat manner.



The Client on the otherhand:
  • Tries to connect to the desired Server, if the desired Server is not available, the program finishes.
  • Once it is connected to the Server, it sends to it, a command, if the command doesn't exist, the server sends back to the Client an error message, if the command does exists, the Server interprets, what kind of info the Client is looking for, according to the command it sent.

As for the part of Server, Client connections, I based my program on this tutorial. And it also helped me to know
that the accept function, only returns after it has received a connection!!!!!

I will post later a link to where my code can be downloaded. It is not that complicated, but it took me a while, to
understand how things were working, and to plan my program.


Sending Information from wiimote through sockets

Lately I have been playing around with the Wiimote, and have started to do some simple programs with it, using some of the code that http://wiiyourself.gl.tter.org/ had to offer.

I have just made a program that uses sockets,and connects a server with multiple clients.The clients make certain petitions to the server, in order to obtain information they desire. The information that I made available to them, was the current acceleration that the wiimote had, the pitch angle and the rolling angle. ( for now this is the only information I am interested in sending to the clients.)

Because I used part of the code, that wiiyourself had to offer, and they coded everything with the Windows api, I had to development everything exclusively for Windows. Therefore when I coded the sockets, I used winsock2.


The first thing that I wanted to do was to get the project that WiiYourself had available up and running. The exe. from their project can be downloaded from their webpage, and can be very easily tried out. Their exe. first checks to see if there is a wiimote connected. (If there is a wiimote that is connected to the computer's Bluethooth, or to an external bluethooth that is connected to the computer) If not, it will keep searching until a wiimote is finally connected.
Once it finds a connection to a wiimote, it will display on the screen a lot of information related to the wiimote, such as which buttons are being pressed, what the orientation from the remote is, the x, y and z acceleration, the pitch angle etc.

I wanted to use, this code that that the staff in WiiYourself had already coded, so the first thing that I needed to do, was download their version 1, of the project. (Available in the webpage I mentioned)

In order to compile this project it is necessary to have certain headers. The MS Driver Development Kit (DDK) is required to build (for the HID API). It's a free download, and can be obtained from:
http://www.microsoft.com/whdc/devtools/ddk/default.mspx

It takes a while to download, and then it also takes a while to install, but it does work!

In the project, it is then necessary to add its 'inc/wxp' dir to the include paths, and 'lib/wxp/i386' to the library paths.

With this, one should be able to compile the project.
You can now try it out!

I have to confess, that in this part, I suffered many problems!

In my home computer, I was able to run this program without any troubles,but when I tried to install it in a computer in my school, I got the blue screen of deaTH!! I tried other computers, and all of them, once the program had found the wiimote, got the blue screen of death!!!
At first I was dazzled, I didn't know why the heck I was getting that screen, I thought for a moment that it might have to do with the ddk that I installed, maybe it wasn't a good match with the service pack that these computers had....yeah I had no ideA, what the hell???

Then I realized, that in my home computer I had another version of bluesoleil. (I am currently using Bluesoleil to connect the wiimote to the computer's bluethooth in a more effective and quick manner. http://www.bluesoleil.com)

In my school, I downloaded the free Bluesoleil version, and I think that might have been the reason, I was getting the bluescreen of death. The free download version, limits the amount of information, that can be sent and recived. I am assuming that the wiimote, tried to send in more data then what was currenlty available. I believe this is the reason, because once I installed the Bluesoleil version that I had in my house, everything worked out fine. If anyone has suffered forom this before, and has a better explanation to why this was happening please let me know!


Angry