Brick in the Cloud

The Lego Machine Cloud Project


7 Comments

Its alive! Lego Robot and Salesforce Chatter Demo

After a few late nights / mornings I finally got my second robot chatting with me via Salesforce Chatter! I chose to switch designs as it was easier to mount the Wifi sensor and move this robot around than the Alpha Rex. You cannot see it in the video but I was controlling it via the Chatter app on my iPhone, I took some screenshots after the video.

photo photo[1]

The NXC code I developed for this communicates to Salesforce via a custom Heroku RESTful Service. Which provides two services /command and /chat that wrap the Chatter API in something easier to code against in NXC. I’ll share the Java code for these  services in a later blog. In the meantime enjoy!

task main()
{
   SetSensorLight(S3, true);
   if (quickConnect(WB_PORT,"ssid",WB_SECURITY_WPA2,"pass"))
   {
    string lastCommand;
    while (true)
    {
     string command = quickGETHostname(WB_PORT,"brickinthecloud.herokuapp.com","/services/command");
     if (command!="") {
       TextOut(0,LCD_LINE1, "Cmd:" + command + "      " );
       if(strcmp(command, lastCommand)!=0)
       {
          string message = "Afirmative!";
          if(strcmp(command, "Status?")==0)
          {
             float volts = BatteryLevel() / 1000.0;
             message = FormatNum("Battery:%5.3f", volts);
          }
          else if(strcmp(command, "Take aim!")==0)
          {
             RotateMotor(OUT_B, 180, 180);
             RotateMotor(OUT_C, 180, 180);
          }
          else if(strcmp(command, "Fire!")==0)
          {
             RotateMotor(OUT_A, 360, 360);
          }
          else if(strcmp(command, "Light on!")==0)
          {
             SetSensorColorRed(S3);
          }
          else if(strcmp(command, "Light off!")==0)
          {
             SetSensorColorNone(S3);
          }
          string response = quickGETHostname(WB_PORT,"brickinthecloud.herokuapp.com","/services/chat?message=" + message + "&comment=true");
          TextOut(0,LCD_LINE1, "Response:" + response + "      " );
          strcpy(lastCommand, command);
       }
     }
     else {
       TextOut(0,LCD_LINE1,"GET ERROR");
     }
    }
   }
}
Advertisements


Leave a comment

NXC Programming

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!