d
Overview

The NXT network server makes it possible to communicate with the NXT over a network using the LEGO Bluetooth protocol. The idea is simply to have a server between a number of network clients and the NXT. This makes it possible to forward messages from TCP/IP to Bluetooth and vice versa. This provides a very easy way to read sensor value, start a program, upload a file and even control the motors over the Internet. The C++ communication library makes it easy to send commands to the NXT network server. To get started please follow the tutorial below. If you don't want to get your fingers dirty simply download the Network and BT remote and get started right away.

With the server you can:

  • Select the maximum number of clients that can connect to the server (max 100)
  • Set a password
  • Set for a timeout for client inactivity.
  • Select whether the server should stop or reconnect if communication is lost
  • Log events and store these in a file
  • Preview the log
  • Select whether IP addresses should be rejected or accepted
  • Throw off clients when the server is running

Download

Download NXT network server

If you use this software in one of your projects, find a bug or have any comments or questions please send me an email.

Sending commands to the server with the C++ Communication library

All functions in the C++ Bluetooth library can be used with the Bluetooth TCP/IP server. To start writing programs that can send commands to the NXT over a network connection just follow the three steps below. To make things simple this example will run the server and client on the same computer:

  • Download the latest version of the library and make sure that you can compile and run a Bluetooth sample program.
  • Download and start the NXT Network server with password, client timeout and access control disabled - make the sure that the server uses port 1000 and that your firewall is forwarding traffic to the server. Normally a dialog box will pop up and ask whether or not you want to unblock traffic to/from the server.
  • Place the sample code from sample 20 in your main file and replace the Bluetooth connection with a network connection as shown below.
server.cpp
#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"
using namespace std;

//set up the NXT
Connection *connection = new Nxt_network();
Sensor *sensor1 = new Touch(IN_1, connection);
int main()
{
  Server_settings settings;
  try{
    cout << "Try to connect to the NXT" << endl;
    connection->connect(1000, "127.0.0.1", settings);//connect to localhost
    cout << "Connected" << endl;
    cout << "Client timeout" << settings.timeout <<endl;
    if(settings.mode == CLOSE_DOWN){
      cout << "Server will close down if connection is lost" << endl;
    }
    else{
      cout << "Server will reconnect if connection is lost" << endl;
    }
    cout << "Read the touch sensor - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
      cout << sensor1->print() << endl;
    }
    connection->disconnect();
  }
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    cout << e.who() << endl;
    connection->disconnect();
  }
  return 0;
}

That's it - with the program you should be able to read the touch sensor value over a network connection. To connect from a different computer simply change the IP address. To use other features of the library check out the other library samples or view a complete list of available commands and sensor opportunities in the library documentation.

FAQ

Q: I am able to connect to the server when I run the client and server on the same computer. But when I what to connect over the internet I get an error - why?
A: If your are running the server behind a router or a firewall - please make sure that traffic to the server is forwarded

Q: Is it possible to use the server with other Bluetooth libraries such as Icommand or write my own software that sends commands to the server over TCP/IP?
A: Yes as long as you are able to send the Bluetooth commands over a network connection the server will forward the bytes to the NXT. To get accepted by the server you need to send the a 10 byte password immediately after you have established a TCP/IP connection with the server. If password is disabled on the server the client should still send the default password to the server in order to get accepted. The default password is LEGOCLIENT so in this case the password string would be:

touch.cpp Byte[0] = ‘L’
Byte[1] = ‘E’
Byte[2] = ‘G’
Byte[3] = ‘O’
Byte[4] = ‘C’
Byte[5] = ‘L’
Byte[6] = ‘I’
Byte[7] = ‘E’
Byte[8] = ‘N’
Byte[9] = ‘T’
Byte[10] = ‘\’

If the password is enabled on the server the password string send to the server should of course match the password set on the server. For more information please check out the network source code in for the C++ library documentation.

Q: Sometimes I seem to get a slow reply from the NXT -why?
A: Normally this shouldn't be an issue but since only one client at a time can communicate with the NXT you might have to wait for other clients to finish. Deleting the flash memory on the NXT involves a four seconds sleep - so use it with care to avoid blocking other users.

Q: The status byte in the reply from the NXT contains a value that is not listed in the official LEGO Bluetooth protocol - why?
A: This is because the server can fail - errors from the server are sent to the client in the status byte. A list of error codes from the server can be found in the error list .

If you have any questions that it not listed here please send me an email.