Loading user information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading user information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

LEGO, IR and Netduino?

Today's Hardware Friday post takes three of our favorite hardware things and show us how we can make them work together. Really, what's more more fun then a series that helps us build hardware and write code that let's Netduino, the .Net Micro Framework talk to LEGOs!

Using netduino and .NET Microframework to pilot any Lego Power Function thru Infrared (part 1)

I’m part of FREELUG, the French Enthusiast Lego User Group. And in this group, there are lots of discussions on Lego of course. In one of the thread someone ask the question if it was possible to pilot an Lego train using the new Power Function with a PC. The need is during expo, it makes it easier to run trains, stop them in a programmatic way.

I did a quick answer on the list saying that it can be quite easy if the protocol was not too complex and the IR technology used was close to RC5 from Philips. A small oscillator behind a serial or parallel port would do the trick. Philo, one of the FREELUG member answer me with a link to the protocol. And also tell me it should not be as easy as I was thinking. And he was more than right! No real way to make this work with a simple serial or parallel port on a PC. The protocol is more complex and need quite a bit of work to implement. I’ll come later on the first explanation on how to do it.

So I decided to see if it was possible to implement this on netduino using .NET Microframework. this board has lots of IO, analogic and digital, do implement busses like I2C but also SPI. As any project, I started my project with my friend Bing. And start searching for similar projects. And I found Mario Vernari who I’ve mentioned in my previous post who was doing something similar. And we’ve exchange couple of emails to find a good way to implement an Infrared emitter using .NET Microframework. We will create the wave form in a buffer and then send it thru the MOSI port of an SPI port linked to the infrared led. So I will use the ideas and implementation Mario explain in his blog to pilot the Lego Power Function.



Using netduino and .NET Microframework to pilot any Lego Power Function thru Infrared (part 2)

In a previous post, I’ve started to describe the Lego Power Function protocol and how I’ve implemented it on a netduino board using .NET Microframework. My work is based on ideas and implementation Mario explain in his blog. We’ve exchange couple of emails to make both our projects work

To continue where I stopped, The message_pause function is like this:


Using netduino and .NET Microframework to pilot any Lego Power Function thru Infrared (part 3)

In the previous post, I’ve explain how to create a class and the hardware that will be able to pilot any Lego Power Function. In this article, I will explain how to create a web server (like IIS or Apache but much much much more simpler) and create a simple way to call the class.

I’ve already explained how to create such a web server into one of my previous article. So Il will use the same idea. I will only explain the implementation of the specific part to call the Combo Mode function as I already take it in the past articles.

The function is part of the LegoInfrared class and looks like

public bool ComboMode(LegoSpeed blue_speed, LegoSpeed red_speed, LegoChannel channel)

LegoSpeed and LegoChannel are both enums.

The simple view of the HTTPServer Class is the following:


I just create the form as it should be, very simply. Filling a string and outputting the string to the response object. Be careful as in netduino, the maximum size of any object, so including strings is 1024. The strResp object can’t contain more than 1024 caracters, so it is necessary to empty it. The way I do it is thru the OutPutStream function which output the string value into the response stream. I’ve already explained it in a past article.

So here is it for this part. I’ve explained how to be able to create a set of Web API, very simple to control this infrared emitter. This principle can be apply to anything! I have couple of ideas for the next step. I can implement a system with sensors (can be ILS for examples) and signals (green/red lights) and switch management. But I’m not there yet! Any feedback and ideas welcome.

private static void ProcessCombo(HttpListenerContext context)
    HttpListenerRequest request = context.Request;
    HttpListenerResponse response = context.Response;
    string strParam = request.RawUrl;
    string strResp = "";
    if (DecryptCombo(strParam))
        strResp = strOK;
        strResp = strProblem;
    OutPutStream(response, strResp);

This function is very simple and just do a branch depending on the result of the DecryptCombo function. If it is successful, then it will return OK, if not, it will return Problem.

private static bool DecryptCombo(string StrDecrypt)
    // decode params
    Param[] Params = decryptParam(StrDecrypt);
    int mChannel = -1;
    int mComboBlue = -1;
    int mComboRed = -1;
    bool isvalid = true;
    if (Params != null)
        for (int i = 0; i < Params.Length; i++)
            //on cherche le paramètre strMonth
            int j = Params[i].Name.ToLower().IndexOf(paramChannel);
            if (j == 0)
                mChannel = Convert.ToInt32(Params[i].Value);
                if (!((mChannel >= (int)LegoInfrared.LegoChannel.CH1) && (mChannel <= (int)LegoInfrared.LegoChannel.CH4)))
                    isvalid = false;
            j = Params[i].Name.ToLower().IndexOf(paramComboBlue);
            if (j == 0)
                mComboBlue = Convert.ToInt32(Params[i].Value);
                if (!((mComboBlue == (int)LegoInfrared.LegoSpeed.BLUE_BRK) || (mComboBlue == (int)LegoInfrared.LegoSpeed.BLUE_FLT) ||
                    (mComboBlue == (int)LegoInfrared.LegoSpeed.BLUE_FWD) || (mComboBlue == (int)LegoInfrared.LegoSpeed.BLUE_REV)))
                    isvalid = false;

            j = Params[i].Name.ToLower().IndexOf(paramComboRed);
            if (j == 0)
                mComboRed = Convert.ToInt32(Params[i].Value);
                if (!((mComboRed >= (int)LegoInfrared.LegoSpeed.RED_FLT) && (mComboRed <= (int)LegoInfrared.LegoSpeed.RED_BRK)))
                    isvalid = false;

    // check if all params are correct

    if ((isvalid) && (mComboRed != -1) && (mChannel != -1) && (mComboBlue != -1))
        if (!Microsoft.SPOT.Hardware.SystemInfo.IsEmulator)
                isvalid = myLego.ComboMode((LegoInfrared.LegoSpeed)mComboBlue, (LegoInfrared.LegoSpeed)mComboRed, (LegoInfrared.LegoChannel)mChannel);
            Debug.Print("Sent Combo blue " + mComboBlue.ToString() + " red " + mComboRed.ToString() + " channel " + mChannel.ToString());
        isvalid = false;
    return isvalid;


How can this be used in a project? You only need to look here...

Lighting my Lego city using .NET Microframework


Lego and Netduino? Yes, you can...


Follow the discussion

  • Oops, something didn't work.

    Getting subscription
    Subscribe to this conversation

Remove this comment

Remove this thread


Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.