Wifi Week: Crank Up The Speed!

Wifi baud rates are so fast, and here is how to get them.The Dexter Industries Lego Wifi Sensor can communicate faster than any other sensor made for Lego Mindstorms NXT.  Not only can it circle the globe with information, but it can do it at almost 1 Megabyte per second.

We can speed up and slow down the speed of the Wifi sensor by changing the baud rate.  Today’s post is short.  And really fast.  Just like the Wifi sensor.

Also,  from time to time, when trying out new programs, we accidentally lock up the sensor (or forget the baud rate we set it at).  As with all hardware, sometimes it just locks up.  When cycling the power just won’t reset the sensor, try running this program.

We also demonstrate how to save a profile in the Wifi sensor.  This will not only save the baud rate, but  any setting we have modified on the wifi sensor.  For a list of settings, see our PDF here in the downloads section.

The first thing we do is try to detect the baud rate the Wifi sensor is currently set at.  To do this, we will cycle through possible baud rates with the NXT and wait until we hear a coherent response from the Wifi sensor.

Possible baud rates for the wifi sensor are 9600, 19200, 38400, 57600, 115200, 230400, 460800, and a blazing fast 921,600.

We will test the response by sending “+++” to the Wifi Sensor and listen for the response.  If the response is “ERR”, “OK”, “0″, or “2″ we have the correct baud rate.  If it’s anything else, the message is garbled and we’re communicating with the sensor at the wrong baud rate.

After finding the correct baud rate, we will reset the baud rate to the new baud rate.  We can do this by sending the command

atb=<baud-rate>

Where baud rate is any of the acceptable baud rates: 9600, 19200, 38400, 57600, 115200, 230400, 460800, and a blazing fast 921600.

Finally, we will save the profile, and then make the profile the default profile on startup. To save the profile  to the Lego Wifi Sensor we use the command:

AT&Wn

Where “n” is any number you like.  We save it as profile 1 by sending AT&W1 over the High Speed Line.

To set a profile as the default profile on startup, we can use the command:

AT&Yn

Again, where “n” is the profile we want to save as the default.  Thus if we sent the command “AT&Y1″, we save profile 1 as the default profile.  On restarting the Wifi sensor it would automatically load the settings we saved.

Example code of everything we just went over above, pasted below.  Please note that it is in RobotC.

DIWIFI_Set_Baud.c

// Wifi Set Baud Rate
//
// From time to time, we either forget the Baud rate our wifi sensor
// is set at.  The sensor will seem jammed, but in reality, you're trying
// to communicate at the wrong baud rate between the NXT and the sensor.
//
// This program solves that problem.  It will scan over possible baud rates
// and connect at the proper baud rate.  Then it will reset the baud rate
// to 9600 (a standar baud rate; you can modify the code as you please).
//
// A big thanks to Xander Soldaat at Botbench.com who originally developed
// the basis of this code.  See more of Xanders work at botbench.com
//
// For more information on the Wifi Sensor for Lego Mindstorms NXT, please
// go to http://www.dexterindustries.com/wifi.html
//


#include "drivers/common.h"
#include "DIWIFI-Set_Baud.h"

string strBaud = "9600";    // Here is where we'll set the baud rates.
                            // Baud Speed can be any of the following speeds:
                            // 9600, 19200, 38400, 57600, 115200, 230400,460800, 921600

////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                        Main Task
////////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
  eraseDisplay();
  bNxtLCDStatusDisplay = true; // Enable top status line display

  // factorydefaults();        // Calling this will set the sensor back to factory defaults.

  scanBaudRate();           // This function scans the baud rate and sets up the server.  You can also call the "setupHighSpeedLink" function
                            // if you want a specific baud rate.
  PlaySound(soundBeepBeep); // Make some noise when we've found the baud rate.
  echo_all_input_off();     // Housekeeping: turn the echo off.
  software_flow_control();  // Housekeeping: set flow control.
  set_verbose(1);           // Housekeeping: we want the response to be verbose.
  set_baud_sensor(strBaud);        // Now set the baud rate for the wifi sensor.  You can adjust this to any supported baud you like.
  clear_read_buffer();      // Clear out anything waiting in the Port 4 buffer.
  SaveProfile();            // Save the parameters to profile.
  DefaultProfile();         // Set the new baud rate as the default profile.
  PlayTone(500, 100);         // Make some noise when we're connected.
}

DIWIFI_Set_Baud.h


ubyte BytesRead[8];
const ubyte newline[] = {0x0D};
typedef ubyte buff_t[128];
buff_t buffer;

long baudrates[] = {921600, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600};

// This function sends the array out Port 4.

void debugnxtWriteRawHS(const ubyte &pData, const short nLength)
{
  string tmpString;
  ubyte buff[30];
  memset(buff[0], 0, 30);
  memcpy(buff[0], pData, nLength);
  StringFromChars(tmpString, buff);
  writeDebugStream("%s", tmpString);
  nxtWriteRawHS(pData, nLength);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//      Clear Read Buffer
//      Run this to clear out the reading buffer.
//      Simply sends a carriage return, then clears the buffer out.
////////////////////////////////////////////////////////////////////////////////////////////////////////

void clear_read_buffer()
{
    ubyte nData[] = {13};
    debugnxtWriteRawHS(nData[0], 1);   // Send the carriage return
    wait10Msec(100);
    while(BytesRead[0] < 0){
      nxtReadRawHS(BytesRead[0], 1);    // Read the response.  Probably an error.
    }
    wait10Msec(100);
}

/////////////////////////////////////////////////////////
//Scan Baud Rate - Scans the baud rate, sets up the sensor.
/////////////////////////////////////////////////////////
long scanBaudRate() {
  ubyte tmpbuff[8];
  string tmpString;
  ubyte attention[] = {'+','+','+',13};
  for (int i = 0; i < 8; i++) {
    memset(tmpbuff, 0, sizeof(tmpbuff));
    nxtDisableHSPort();
	  wait1Msec(10);
	  nxtEnableHSPort();
	  nxtSetHSBaudRate(baudrates[i]);
	  writeDebugStream("Testing: ");
	  writeDebugStream("%i", baudrates[i]);
	  nxtHS_Mode = hsRawMode;
	  clear_read_buffer();
	  wait1Msec(1000);
	  nxtWriteRawHS(attention, sizeof(attention));
	  nxtReadRawHS(tmpbuff, 7);  // make sure last ubyte is always NULL
    StringFromChars(tmpString, tmpbuff);

    if ((StringFind(tmpString, "ERR") > -1) ||
        (StringFind(tmpString, "OK") > -1) ||
        (StringFind(tmpString, "0") > -1) ||
        (StringFind(tmpString, "2") > -1)) {
      clear_read_buffer();
      return baudrates[i];
    }
	}
	clear_read_buffer();
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
// Receive Bytes
// Reads whatever is in the buffer and prints to debug
////////////////////////////////////////////////////////////////////////////////////////////////////////

void Receive(bool wait=false)
{
  if (wait)
    while (nxtGetAvailHSBytes() == 0) wait1Msec(5);

  while (nxtGetAvailHSBytes() > 0) {
    nxtReadRawHS(BytesRead[0], 1);
    writeDebugStream("%c", BytesRead[0]);
    wait1Msec(2);
  }
}

int appendToBuff(buff_t &buf, const long index, const ubyte &pData, const long nLength)
{
  if (index == 0) memset(buf, 0, sizeof(buf));

  memcpy(buf[index], pData, nLength);
  return index + nLength;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//      Echo All Input Off - Turns off the echo effect on the wifi.
//      Sending the serial command "ate0" which turns off the echo effect.
//      Sends one single byte at a time, pauses.
//      Drains receiver with a read each time.
////////////////////////////////////////////////////////////////////////////////////////////////////////

void echo_all_input_off()
{
  writeDebugStreamLine("echo_all_input_off");
  ubyte nData[] = {'a','t','e','0',13};

  for(int i = 0; i < 5; i++){
    debugnxtWriteRawHS(nData[i], 1);               // Send the command, byte by byte.
    nxtReadRawHS(BytesRead[0], 8);         // Clear out the echo.
    wait10Msec(10);
  }

  Receive(true);
  eraseDisplay();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//      Software Flow Control On
//      Send string "at&k1" and carriage return.
//      Shouldn't need the wait or read now that we've got the echo off.
////////////////////////////////////////////////////////////////////////////////////////////////////////
void software_flow_control()
{
  writeDebugStreamLine("sfwr_flw_cntrl");
    ubyte nData[] = {'a','t','&','k','1',13};
    debugnxtWriteRawHS(nData[0], 6);            // Send the command, byte by byte.
    Receive(true);
}

//////////////////////////////////////////////////////////////////
// Configure the verbose mode of the wifi sensor.
// n = 0, verbose disabled.
// n = 1, verbose enable.
//////////////////////////////////////////////////////////////////
void set_verbose(int n)
{
  if(n>1) n = 1;                                  // Shouldn't be larger than 1 so set it back.
  n = n+48;
  writeDebugStreamLine("set_verbose");
  ubyte nData[] = {'a','t','v',n,13};
  debugnxtWriteRawHS(nData[0], sizeof(nData));            // Send the command, byte by byte.
  Receive(true);
}


//////////////////////////////////////////////////////////////////
// RESET TO FACTORY
//////////////////////////////////////////////////////////////////
void factorydefaults()
{
  int index = 0;
  ubyte linebuff[20];

  writeDebugStreamLine("factory defaults");
  string resetfactory = "AT&F";
  //index = appendToBuff(buffer, index, cmdBaud, sizeof(cmdBaud));
  //index = appendToBuff(buffer, index, baudspeed, sizeof(baudspeed));
  memcpy(linebuff, resetfactory, strlen(resetfactory));
  index = appendToBuff(buffer, index, linebuff, sizeof(linebuff));
  index = appendToBuff(buffer, index, newline, sizeof(newline));

  debugnxtWriteRawHS(buffer, index);            // Send the command, byte by byte.
  Receive(true);
}


//////////////////////////////////////////////////////////////////
// Set baud to a new rate
//////////////////////////////////////////////////////////////////
void set_baud_sensor(string strBaud)
{
  int index = 0;
  ubyte linebuff[20];

  writeDebugStreamLine("set_baud");
  ubyte cmdBaud[] = {'a','t','b','='};

  index = appendToBuff(buffer, index, cmdBaud, sizeof(cmdBaud));
  //index = appendToBuff(buffer, index, baudspeed, sizeof(baudspeed));
  memcpy(linebuff, strBaud, strlen(strBaud));
  index = appendToBuff(buffer, index, linebuff, sizeof(linebuff));
  index = appendToBuff(buffer, index, newline, sizeof(newline));

  debugnxtWriteRawHS(buffer, index);            // Send the command, byte by byte.
  Receive(true);
}

//
// NOW CHANGE THE BAUD READING TO WHATEVER YOU JUST SET IT TO!
//
/*
void set_baud_nxt(const ubyte &baudspeed)
{

  int intbaudspeed = 0;
  for(int x = 0; x < sizeof(baudspeed); x++){
    intbaudspeed = (intbaudspeed*10)+(baudspeed[x]-48);
  }
  writeDebugStreamLine("%i", intbaudspeed);
	nxtSetHSBaudRate(intbaudspeed);   // Set the baud of the NXT to 9600.

  wait10Msec(10);
  nxtDisableHSPort();
	wait1Msec(10);
	nxtEnableHSPort();
  nxtSetHSBaudRate(9600);
	// nxtSetHSBaudRate(115200);
	nxtHS_Mode = hsRawMode;
	clear_read_buffer();
  //Receive(true);
}
*/

//////////////////////////////////////////////////////////////////
// Save profile
//////////////////////////////////////////////////////////////////
void SaveProfile()
{
  int index = 0;
  writeDebugStreamLine("Save Profile");
  ubyte cmdSave[] = {'A','T','&','W','1'};

  index = appendToBuff(buffer, index, cmdSave, sizeof(cmdSave));
  index = appendToBuff(buffer, index, newline, sizeof(newline));

  debugnxtWriteRawHS(buffer, index);            // Send the command, byte by byte.
  Receive(true);
}

//////////////////////////////////////////////////////////////////
// Default the profile
//////////////////////////////////////////////////////////////////
void DefaultProfile()
{
  int index = 0;
  writeDebugStreamLine("Make Profile");
  writeDebugStreamLine("Default Profile");
    ubyte cmdSave[] = {'A','T','&','Y','1'};
    //ubyte cmdSave[] = {'a','t','&','V'};

  index = appendToBuff(buffer, index, cmdSave, sizeof(cmdSave));
  index = appendToBuff(buffer, index, newline, sizeof(newline));

  debugnxtWriteRawHS(buffer, index);            // Send the command, byte by byte.
  Receive(true);
  Receive(true);
  Receive(true);

}

Did you like this? Share it:

Related Posts:

Leave a Reply

  

  

  


7 × seven =