About http in NXT

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, 99jonathan, imaqine

About http in NXT

Postby PC Mesnil » Tue Sep 02, 2008 11:40 am

Hello,
I'm working on a project on a NXT model and I need to implement a http connection.
I saw some api about http in the leJos for RCX but not in the one design for NXT.
Do you know if it's possible to have a simple/tiny web serveur on a NXT connected to a PC via bluetooth.
Thanks a lot in advance.
PC Mesnil
New User
 
Posts: 2
Joined: Tue Sep 02, 2008 11:26 am

Postby bbagnall » Wed Sep 03, 2008 2:51 am

For a while we had the dream of programming WAP (or WAPB) which would enable the NXT to have access to the Internet via Bluetooth without the need for any special software running on your PC. We had to give this up unfortunately because the BC4 chip in the NXT is limited in the types of services it can support (SPP only).

We could still try to do something like this, but it would require us to make a proxy server that you would need to run on your PC in order to pass data through the Internet. If you know a thing or two about Java networking it wouldn't be a hugely difficult thing to do. You could use the leJOS RCX code for inspiration.
Last edited by bbagnall on Sat Sep 06, 2008 2:31 pm, edited 1 time in total.
Intelligence Unleashed: Creating LEGO NXT Robots with Java
Available September 2011
User avatar
bbagnall
Site Admin
 
Posts: 383
Joined: Fri Aug 04, 2006 4:03 pm

Postby lawrie » Thu Sep 04, 2008 7:20 pm

There is a Socket proxy that comes with leJOS NXT. You run nxjsocketproxy on the PC. On the NXT you need to wait for a Bluetooth connection (from the proxy) and then call NXTSocketProxy specifying the port you want to listen on and the BTConnection object. See the SocketTest sample, although that sample uses NXTSocket, not NXTServerSocket. Once you have establlished a server socket, you can wait for connections by calling accept which returns a NXTSocket object and you can then read and write data. On the PC connect your browser to http://localhost:port. Your socket on the NXT will then receive http request and can return http responses. You will need to parse http yourself.

I will see if I can write a new sample that does this.

I wrote the http server API on the RCX, but have not ported it to the NXT. I could implement the same API as on the RCX, if there is a demand for it.
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby lawrie » Fri Sep 05, 2008 6:47 pm

Here is the code for a simple web server that works with nxjsocketproxy. I have it working with the SVN version of leJOS, but I had to make some changes to NXJSocketProxy. It will need some changes to work with the 0.6 release. It assumes the command is GET and returns the name of the file requested, but you could easily change it to return the file contents or whatever HTML you want, such as sensor readings.

Code: Select all
import lejos.nxt.comm.*;
import lejos.nxt.socket.*;
import java.io.*;
import java.util.*;

/**
* Simple web server
*
*/
public class Webserver {

   private DataInputStream ins;
   private DataOutputStream outs;
   private BTConnection btc = null;
   private NXTServerSocket serverSock;
   private NXTSocket sock = null;

   public  Webserver() throws Exception{
      print("Waiting");
      btc = Bluetooth.waitForConnection();
      serverSock = new NXTServerSocket(8081,btc);
      print("Connected");
      
      while(true){
         print("Accepting");
         sock = serverSock.accept();
         print("Accepted");
         ins = sock.getDataInputStream();
         outs = sock.getDataOutputStream();
         String s = readLine(); // Print the HTTP command line
         print(s);
         
         StringTokenizer tokens = new StringTokenizer(s," ");
         
            String cmd = tokens.nextToken();
            print("Command = " + cmd);
            String file = tokens.nextToken();
            print("File = " + file);
                       
         while (true) { // Suck in the rest
            s = readLine();
            if (s.length() == 0 || s == null) break;
         }
         
         // Send reply
         writeLine("HTTP/1.1 000 OK");
         writeLine("");
         writeLine(file);
         closeStream();
      }
   }

   public void print(String s){
      System.out.println(s);
   }

   public void closeStream() throws IOException{
      ins.close();
      outs.close();
   }
   
   public String readLine() {
      String s = "";
      
      while (true) {
         try {
            int b = ins.readByte();   
            if (b == 13) continue;
            if (b == 10) return s;            
             s += (char) b;
         } catch (IOException ioe) {
            return null;
         }      
      }   
   }
   
   public void writeLine(String s) {
      try {
         for(int i=0;i<s.length();i++) {      
            outs.writeByte((byte) s.charAt(i));    
         }
         outs.writeByte(13);
         outs.writeByte(10);
         outs.flush();
      } catch (IOException ioe) {}
   }

   public static void main(String [] args)  throws Exception
   {
      new Webserver();
   }
}
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Postby kmhansen » Thu Oct 30, 2008 10:19 am

Nice. I am curious: what type of changes did you need to make to NXJProxySocket?

Best Regards
kmhansen
New User
 
Posts: 1
Joined: Thu Oct 30, 2008 10:18 am

Postby lawrie » Tue Nov 04, 2008 11:21 pm

SocketProxy did not work properly with more than one accept request so you could only connect to the NXT Web server once. I have fixed this and several other bugs for the 0.7 release.
lawrie
leJOS Team Member
 
Posts: 677
Joined: Mon Feb 05, 2007 1:27 pm

Re: About http in NXT

Postby ChefFD » Mon Dec 17, 2012 11:53 am

Hey guys,

i was looking for help to establish a webserver on the NXT Brick using Bluetooth and no extra wifi modul.

i used the above standing code for the brick but i cant get a connection to it.
After trying it with SocketTest and SocketServer (it worked fine) i uploaded the webserver on the brick and there was no connection possible.
I tried to connect by running first the nxjsocketproxy but didnt work also.
It always displays "cant connect to nxt"

Maybe everything dues to the updates of the lejos firmware since 2008.

I hope someone can help me with my problem,

many thanks in adcanve

regards
ChefFD
ChefFD
New User
 
Posts: 5
Joined: Mon Dec 17, 2012 11:32 am


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 2 guests

cron
more stuff