Simple WebSocket client on EV3 brick

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

Moderators: roger, gloomyandy, skoehler

Simple WebSocket client on EV3 brick

Postby sander » Wed Nov 06, 2013 1:28 pm

Hi,

Allow me to elaborate on what the app should do and then on the steps I took to get there. Basic idea is that I have a server that hosts a Java EE 7 websocket. The EV3 brick acts as a websocket client so that a message can be sent from brick to server and the other way around.

Because I'm not to familiar with the leJOS API, I started with a very simple app. When the app starts a message is shown. When pushing enter the app should display another message. In a later stage, pushing enter would trigger the websocket connection to be initiated. But I can't even get this simple app to work as expected. Below you can see the code that is based on the nxt tutorial: http://www.lejos.org/nxt/nxj/tutorial/ListenersAndEvents/Listeners_Events.htm (there are no ev3 tutorials/documentation, right?).

Code: Select all
public static void main(String[] args) {
      System.out.println("START BrickInit");
      Button.ENTER.addButtonListener(new EnterButtonListener());
      showMessage("Push ENTER to connect");
      Button.ESCAPE.waitForPressAndRelease();
   }

   public static void showMessage(String message) {
      System.out.println("showMessage: " + message);
      LCD.clear();
      LCD.drawString(message, 0, 0);
      LCD.refresh();
   }

   public static class EnterButtonListener implements ButtonListener {

      @Override
      public void buttonPressed(Button b) {
         System.out.println("ENTER button pressed");
      }

      @Override
      public void buttonReleased(Button b) {
         System.out.println("ENTER button released");
         BrickInit.showMessage("ENTER button released");
      }

   }


The output of this program is on the display: "Push ENTER to connect". When I push the ENTER button, nothing happens except a beep sound is produced on the brick. According to http://www.lejos.org/nxt/nxj/tutorial/ListenersAndEvents/Listeners_Events.htm new threads are being made for every listener that is added, but it seems that this listener is never called? Any ideas on this?

My next attempt was to create a ButtonTest class that extends Thread. In the main method of BrickInit I create a new instance of it and make sure it's a daemon thread. But this gives me the same results: a beep when I click the enter button, but no message is shown on the display.

To conclude, do you have any thought on the websocket program itself? My plan is to alter the EnterButtonListener to:
Code: Select all
public static class EnterButtonListener implements ButtonListener {

      @Override
      public void buttonPressed(Button b) {
         System.out.println("ENTER button pressed");
      }

      @Override
      public void buttonReleased(Button b) {
         System.out.println("ENTER button released");
         BrickInit.showMessage("CONNECTING");
          final ClientEndpointConfig cec = ClientEndpointConfig.Builder.create().build();
          ClientManager client = ClientManager.createClient();
          BrickClientEndPoint brickEndPoint = new BrickClientEndPoint();
         
          try {
            session = client.connectToServer(brickEndPoint, cec, URI.create("ws://10.0.1.10:8080/legoxx/brick"));
          } catch (DeploymentException | IOException e) {
            BrickInit.showMessage("ERROR when connecting: " + e.getMessage());
          }
      }

   }


The BrickClientEndPoint class would then be responsible for the receiving of messages sent by the server:
Code: Select all
import javax.websocket.ClientEndpoint;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

@ClientEndpoint
public class BrickClientEndPoint extends Endpoint {

   @Override
   public void onOpen(final Session session, EndpointConfig endpointConfig) {

      // Add a listener to capture the returning event
      session.addMessageHandler(new MessageHandler.Whole<String>() {

         @Override
         public void onMessage(String message) {
            BrickInit.showMessage("RECEIVED: " + message);
         }
      });

   }
}


It seemed to me like a very simple app, but it's frustrating I even can't get the basics to work. So any help is much appreciated!

By the way, I'm running on version 0.4.0 alpha.
sander
New User
 
Posts: 1
Joined: Sat Sep 21, 2013 3:53 pm

Re: Simple WebSocket client on EV3 brick

Postby skoehler » Thu Nov 07, 2013 6:46 pm

I'm not sure how far the NXJ tutorial still applies. I'm somewhat sure the code for spawning threads for listeners is still in place, and I can't recommend using them. The underlying hardware is _based_ on polling. The kernel modules don't even expose an event based interface, just a state based interface. So we can't have listeners without threads that poll in userspace at the moment. We could have one thread for all events or whatever - but even better would be if you write exactly the polling thread you need.

Also, as far as WebSockets, SOAP, REST, and whatnot are concerned, the Java running on the EV3 is not a pretty standard JDK (Java SE embedded). I expect that pretty much every library you download from the web for such network stuff might work out of the box.
skoehler
leJOS Team Member
 
Posts: 1406
Joined: Thu Oct 30, 2008 4:54 pm

Re: Simple WebSocket client on EV3 brick

Postby esmetaman » Fri Nov 08, 2013 9:07 pm

Hi sander, the problem with a WebSocket in EV3 is the way to deploy the server.
I was trying to use websockets in the same way that you and I couldn't.

Later, I discovered the following project:
https:// github.com/TooTallNate/Java-WebSocket

With this WebSocket implemenation, it is possible to use WebSockets without any problem in EV3.

I give you some examples:

Server:
https://github.com/jabrena/liverobots/b ... ocket.java
https://github.com/jabrena/liverobots/b ... lTest.java

Client:
https://github.com/jabrena/EV3_WebUI/bl ... /index.htm

Try this way.

Cheers
Juan Antonio Breña Moral
http://www.juanantonio.info/lejos-ebook/
http://www.iloveneutrinos.com/
User avatar
esmetaman
Advanced Member
 
Posts: 290
Joined: Wed Sep 13, 2006 12:16 am
Location: Madrid, Spain


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff