Brick in the Cloud

The Lego Machine Cloud Project

NXC Programming

Leave a comment

NXC (Not eXactly C) is an alternative way to program Lego Mindstorms. While the drag and drop interface is actually pretty good, I wanted the flexibility of a programming language. Having determined that HumaRobotics sensor has a NXC library I set about installing the necessary custom firmware and compiler on my Mac. It turns out the community tooling and IDE’s are much more advanced for Windows, however some support for Mac users does exist.

next_tools

Having installed the replacement firmware and thankfully avoided bricking my brick! The new firmware actually still permits the standard Lego software programs plus NXC programs to be downloaded. I set about trying a few NXC programs, they compile and downloaded to the robot in a few seconds. Here is my first program that reads the value of the colour sensor!

task main()
{
    SetSensorColorFull(S3);
    while(true)
    {
        ColorSensorReadType csr;
        csr.Port = S3;
        SysColorSensorRead(csr);
        if (csr.Result == NO_ERR) {
            NumOut(0, LCD_LINE1, csr.ColorValue);
        }
        Wait(1000);
    }
}

I was also able to get the sample WifiBlock NXC code working. It demonstrates the robot making some HTTP GET to some simple Web Services hosted on the HumaRobotics website. Displaying the results on its LCD display.

#define WB_PORT IN_4

byte humaroboticsip[]={88,190,16,18};

void getDateTime() {
  string date=quickGET(WB_PORT,humaroboticsip,"/wb/date.php");
  string time=quickGET(WB_PORT,humaroboticsip,"/wb/time.php");
  if (date!="" && time!="") {
    TextOut(0,LCD_LINE1,"Date: "+date+"      ");
    TextOut(0,LCD_LINE2,"Time: "+time+"      ");
  }
  else TextOut(0,LCD_LINE1,"GET ERROR");
}

void getCityFromZIP(string country,string zip) {
  string res=quickGET(WB_PORT,humaroboticsip,"/wb/cp.php?c="+country+"&z="+zip);
  if (res!="") {
    TextOut(0,LCD_LINE1,zip+", "+country+"         ");
    TextOut(0,LCD_LINE2,res+"          ");
  }
  else TextOut(0,LCD_LINE1,"GET ERROR");
}

task main()
{
  byte ip[]={192,168,0,222};
  byte mask[]={255,255,255,0};
  byte gw[]={192,168,0,1};
  if (quickConnect(WB_PORT,"myssid",WB_SECURITY_WPA2,"wifipass",ip,mask,gw)) {
    while (true) {
      getDateTime();
      getCityFromZIP("US","02101"); // Boston
    }
  }
  else {
    TextOut(0,LCD_LINE1,"CONNECT ERROR");
  }
  Wait(60000);
}

I’m quite pleased with this weekends work, in between fixing a family laptop with a corrupt master boot record, its been a tech heavy weekend! Fun!

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 )

Facebook photo

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

Connecting to %s