Shop OBEX P1 Docs P2 Docs Learn Events
Need some help to Control a robot via website — Parallax Forums

Need some help to Control a robot via website

charleyshfcharleyshf Posts: 165
edited 2012-04-25 18:45 in Propeller 1
Good morning,

I apologize if this has been covered before but I have been searching the forums for a solution and am trying to get a better understanding of what I need to do. I have a mobile robot controlled by a prop and a Lantronix Matchport b/g WiFi module as well on the bot. Right now I have a text menu that the prop sends through the Wifi to a terminal on my pc, while I can control my robot this way, I would like to make it controllable through a webpage. This is where I am running into a problem.

My thought was I would have a webserver running on my pc (windows7) and then create webpage(s) that would in turn control my robot (EG arrow buttons to control going left/right/forward/reverse). I am not that great at making web pages, however what else do I need to make this work? From what I have found so far I think I would would need some sort of java program or script that would take the commands from the webpage and send them through my pc's serial port that in turn sends the command to my prop via the WiFi module, I don't need anything fancy to start with, but I would appreciate any ideas on this.

Thank You
«1

Comments

  • John BoardJohn Board Posts: 371
    edited 2012-04-18 16:28
    G'day,

    Well, as many can testify, I'm no good with electronics, but I can do some pretty neat stuff with computer programming ;) So, how I would tackle this... What your saying is that the robot is networked with the computer? Right? Well that means that the robot has an IP, right? Well, in that case what I'd do is make a Java applet that can be embedded into a webpage, then I'd have that java applet take commands from the using (via arrows on the webpage or whatever), and then connect to the robot by opening a network connection with the robot. If you didn't understand a word of that, well.... I don't know what to do from there :} But anyway, tell me how this helps you.

    -John
  • charleyshfcharleyshf Posts: 165
    edited 2012-04-18 18:01
    Hi, and thank you for responding.

    I think I do understand what you are talking about, the Matchport b/g module I have you can telnet to it and in turn the Matchport b/g module talks to the prop, where I messed up is that there is also some software from lantronix (comport redirector) which basically assigns a com port # to the Matchport module, and I was trying to figure out how to get "something" to send commands to that assigned com port.(software is glitchy however)

    So, yes it's just a matter of making a simple webpage, where I am stuck is at the Java applet part, no experience there.. I guess it's time to start googling...

    Thank You



    John Board wrote: »
    G'day,

    Well, as many can testify, I'm no good with electronics, but I can do some pretty neat stuff with computer programming ;) So, how I would tackle this... What your saying is that the robot is networked with the computer? Right? Well that means that the robot has an IP, right? Well, in that case what I'd do is make a Java applet that can be embedded into a webpage, then I'd have that java applet take commands from the using (via arrows on the webpage or whatever), and then connect to the robot by opening a network connection with the robot. If you didn't understand a word of that, well.... I don't know what to do from there :} But anyway, tell me how this helps you.

    -John
  • RaymanRayman Posts: 14,838
    edited 2012-04-18 18:12
    I think you need a PHP or CGI script running on your server to handle the post requests from visitors to the web page.
    (I have no idea how to do any of that though :) )
  • charleyshfcharleyshf Posts: 165
    edited 2012-04-18 18:18
    Thanks Rayman, don't feel bad I have no clue either, it might get worse, I also have other onboard sensors on the robot that I would like to get readings from, HOWEVER I think I am going to start with the basics, wondering if there is some authoring software out that that makes things less "painful"......


    Rayman wrote: »
    I think you need a PHP or CGI script running on your server to handle the post requests from visitors to the web page.
    (I have no idea how to do any of that though :) )
  • Mike GMike G Posts: 2,702
    edited 2012-04-18 20:31
    charleyshf, you don't need a web server running on a PC, just a simple client socket application. I'm assuming you have the prop talking to the MatchPort via serial, right?

    Let's say you want to send "Hello World" to the from a PC to the MatchPort/Propeller. Well, open a socket using the MatchPort IP address, serialize "Hello World" into bytes, and send the byte stream to the IP address. the MatchPort forwards "Hello World" to the Propeller serial style - if that's how you have your stuff configured.


    Here's a simple C# console application. You must update the IP and port in the Main() method close to the bottom of the code block. C# express is a free download from Microsoft.
    using System;
    using System.Text;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    
    namespace SocketConsole
    {
        public class GetSocket
        {
            private static Socket ConnectSocket(string server, int port)
            {
                Socket socket = null;
                IPHostEntry hostEntry = null;
                IPAddress ip;
    
                if (IPAddress.TryParse(server, out ip))
                {
                    try
                    {
                        //C:\WINDOWS\system32\drivers\etc\hosts
                        hostEntry = Dns.GetHostEntry(ip);
                    }
                    catch(Exception ex) 
                    {
                        Console.WriteLine(@"Create an entery in your HOSTS file: C:\WINDOWS\system32\drivers\etc\hosts"); 
                        Console.WriteLine(ex.ToString());
                    }
                }
                else
                {
                    hostEntry = Dns.GetHostEntry(server);
                }
    
    
                foreach (IPAddress address in hostEntry.AddressList)
                {
                    IPEndPoint ipe = new IPEndPoint(address, port);
                    Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
    
                    try
                    {
                        tempSocket.Connect(ipe);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
    
                    if (tempSocket.Connected)
                    {
                        socket = tempSocket;
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                return socket;
            }
    
            private static string SocketSendReceive(string server, int port)
            {
                // Sample request data
                string request = "This is a test";
                Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
                Byte[] bytesReceived = new Byte[256];
    
                // Create a socket connection with the specified server and port.
                Socket s = ConnectSocket(server, port);
    
                if (s == null)
                    return ("Connection failed");
    
                // Send request to the server.
                s.Send(bytesSent, bytesSent.Length, 0);
    
                // Receive the server home page content.
                int bytes = 0;
                string page = "Resposne from " + server + ":\r\n";
                s.ReceiveTimeout = 1000;
                try
                {
                    do
                    {
                        bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                        page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
                    }
                    while (bytes > 0);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
    
                return page;
            }
    
            public static void Main(string[] args)
            {
                // IP or host name and port
                string host = "192.168.1.107";
                int port = 10001;
    
                string result = SocketSendReceive(host, port);
                Console.WriteLine(result);
            }
        }
    
    }
    

    Later, if you decide to... you can use the same code above on an IIS web server. The same concept applies to a LAMP (Linux, Apache, MySQL, PHP) setup.
  • Heater.Heater. Posts: 21,230
    edited 2012-04-18 20:53
    Forget about Java applets. That is 90's and nobody likes them. You still have to put something at the web page server end to get through to your Prop any way.

    Forget a web server with CGI scripts in PHP or whatever to talk to your Prop. That is messy and complex.

    My plan for doing this, which has been brewing for a few weeks now is:

    1. Start with a simple web page. A few buttons for thing like start, stop, left, right, whatever it is you wanto control. Plus perhaps initially a simple text box to display feed back from the Prop.

    2. Use some simple Javascript (not java) in the web page to respond to button events and communicate button events back to the web server.

    3. The web server is not a common or garden web server like Apache but rather a few lines of Javascript being run by node.js.

    4. That communication from javascript in the web page and some javascript on the server end happens over HTML 5 web sockets. Which is very easy to program.

    5. The server end javascript can pass those messages between web page and Prop via serial port or socket connection. Whatever you have.

    All in all it's a couple of hundred lines of javascript and node.js to get the job done. At least as a proof of concept.

    There are guys already doing this with AVRs and such.

    Have google for node.js and node serialport. You will be amazed how simple this can be.

    I'm hoping to find time to do some work on this soon.
  • xanaduxanadu Posts: 3,347
    edited 2012-04-18 21:01
    You can do something very basic with PHP if you want web page controls- http://boebot.netbotic.com:12000/roboweb/

    Or you can use C#. One thing you mentioned was the web server reading an input, that is hard on Windows without C# you would need to develop it on a Linux platform. I have not tried any of this with IIS I use XAMPP.
  • xanaduxanadu Posts: 3,347
    edited 2012-04-18 21:20
    charleyshf wrote: »
    Hi, and thank you for responding.

    I think I do understand what you are talking about, the Matchport b/g module I have you can telnet to it and in turn the Matchport b/g module talks to the prop, where I messed up is that there is also some software from lantronix (comport redirector) which basically assigns a com port # to the Matchport module, and I was trying to figure out how to get "something" to send commands to that assigned com port.(software is glitchy however)

    So, yes it's just a matter of making a simple webpage, where I am stuck is at the Java applet part, no experience there.. I guess it's time to start googling...

    Thank You

    If you want to use telnet over the internet into a bot check out serproxy it can output a telnet session directly into something like X-CTU via virtual port on your Win7 host.
  • Heater.Heater. Posts: 21,230
    edited 2012-04-18 21:26
    Having a web server and PHP is messy and complex. Not to mention that PHP is one of those things you end up wishing you had never learned anything about.

    The node.js solution will also work on Windows. Although I'm not sure if the serialport module works there.

    With the node.js solution you only have to get your head around one language for the webpage and server ends of the problem. Javascript. It's not the worlds cleverest language but gets the job done rather painlessly.

    I'll see if I can knock up some example code over the weekend.
    Prop to web page is something I want to do as well and this might spur me on a bit.


    I must stress that the new HTML 5 websocket standard makes things much easier.
  • Mike GMike G Posts: 2,702
    edited 2012-04-18 21:53
    Or you can use C#. One thing you mentioned was the web server reading an input, that is hard on Windows without C# you would need to develop it on a Linux platform. I have not tried any of this with IIS I use XAMPP.

    It's actually really easy with the MatchPost. Just fire off a serial stream to the MatchPort from the Prop. The MatchPort does all the work through configuration. Of course, you need a listener on the remote socket but that pretty simple too. As long as the language handles sockets, it pretty easy. The hard part is digesting concept and getting a working model.

    Heater, I gota' mess around more with the HTML 5 stuff. I'm surprised JavaScript can open a serial port. For some reason I thought that was a no no - In Windows.
  • Heater.Heater. Posts: 21,230
    edited 2012-04-18 22:12
    Javascript running in a web page in your browser cannot open a serial port.

    What I am talking about is javascript running as a stand alone program under node.js.

    Node.js is built on the V8 javascript interpreter engine as used in Google chrome.

    So, you end up with javascript running standalone rather like python or such languages.

    But on top of that there are a lot of node.js modules for serving up web pages, making websocket or normal socket communications using serial ports etc etc etc.

    So there you have it, javascript at both the client web page and the server end. Small, fast and easy.
  • Mike GMike G Posts: 2,702
    edited 2012-04-18 22:24
    Ah... that's cool.
  • Heater.Heater. Posts: 21,230
    edited 2012-04-18 22:47
    Yep. It is.
    We have just adopted it for a project at work.
    Out in the field we have 100s of embedded systems that can be sending XML messages over the net to a node.js server. 10 Messages per second. You can then connect to that server from your browser and request one of those data streams
    The requested data is transformed to JSON format, much easier to work with than XML, and streamed to the browser via a websocket connection. In the browser an animation is created from the data stream using webgl. It's all about as real-time as you can get over the web.
  • RaymanRayman Posts: 14,838
    edited 2012-04-19 06:17
    BTW: If you just want to control your robot wirelessly from a computer on your own LAN, that's a whole lot easier problem than controlling it from a web page over the internet...

    In the former case, you could just use (as an example) a Visual Basic form and WinSock to open a port and talk to the robot...
  • charleyshfcharleyshf Posts: 165
    edited 2012-04-19 06:35
    Good Morning!

    WOW, thank you everyone for the responses!!

    I guess I should of been more descriptive in what I am trying to do. Right now I am using PST to control my robot, which connects to a virtual comport with the lantronix software(com15), in turn the prop and the matchport b/g on my robot, the prop serves up a menu to drive the robot to go forward/reverse/turn/stop. The lantronix software(com port redirector) is glitchy and crashes often.

    The main reason I want to control my robot over a webpage was so I could be anywhere and be able to access it.

    With the Matchport b/g module I can either connect to it through a com port, or I can telnet to it.

    I spent some time playing around with XAMPP, but I think I am making this way too complicated.
    @Heater - I just looked at node.js and actually installed it on my win7 pc and used the test example and it works. I am am trying to find a serial port module and see if it would work on my win7 pc (you mentioned something about it possibly not working on a windows pc). IF something like this could work it would be great!

    Thank you again everyone for the responses.
  • max72max72 Posts: 1,155
    edited 2012-04-19 06:49
    I tested the roving networks serial wireless modules, and with little configuration effort they can be used as if you were working from the PST. I'm confident you can do the same with yours.

    Only thing you must use a telnet client in place of PST, after that everything is the same, so if your PST interface suits you simply switch to a terminal.
    I personally tested:
    Under windows teraterm or putty
    Under linux putty or telnet
    Under android lunaterm

    You can probably use many other clients.
    If you have a router and do a port forwarding you can control your prop from anywhere. In case of a dynamic DNS you need a "dyndns style" service.

    Massimo
  • RaymanRayman Posts: 14,838
    edited 2012-04-19 07:20
    If you just want to control your robot from anywhere, I also think telnet would be the easiest way to go...
  • Heater.Heater. Posts: 21,230
    edited 2012-04-19 07:58
    Rayman,

    Telnet would be very easy. Only thing is very soon anyone in the world can control your robot! A web based system would enable you to build some security into it.

    Also having the feed back from the robot displayed graphically in an animation in the page is going to be way cooler and make it easy to use.

    For my first experiment I imagine something simple like having 8 buttons on the page and 8 images of LEDs. The buttons control the Prop to light real LEDs and the Prop tells the page to update the display accordingly. Simple steps.

    Charleyshf,

    I just have a sneaking suspicion that the serial port module does not work for windows. If you have the latest stable node.js installed you could try installing the serial port module using node built in package manager. From the command line:
    node install -g serialport

    But I think it needs a compiler installed to build the C part of the package.

    You could try to install cygwin and install node.js and serial port there. But then life is getting complex. As is often the case for Windows users:)

    https://github.com/voodootikigod/node-serialport#readme
  • Heater.Heater. Posts: 21,230
    edited 2012-04-19 08:05
    Looks like node serialport might work a bit. At least there is some support for it in the git repository and someone is working on it:

    https://github.com/jaredhanson/node-serialport/tree/windows-support/serialport_native/win

    Might be a pain getting it to compile though.
  • max72max72 Posts: 1,155
    edited 2012-04-19 09:07
    The roving networks module has a password option. You could also have a password coded in the pst prompt.
    For sure a graphical interface is more appealing...
  • Heater.Heater. Posts: 21,230
    edited 2012-04-19 09:38
    Yes but I imagine any such password is crossing the net unencrypted so it can be sniffed.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-04-19 10:46
    Not like I need another project to contemplate now, BUT

    What if you take a beagle bone for example (or a RPi) and put it on your robot as the higher level brain. This would allow the robot itself to be the server and with some WiFi connectivity, it could connect and present itself to you on anything that ran a browser (phone, pad, PC, anything 1/2 way around the world). It would carry its own web application, data files, low-level robot interfaces, etc. for $50 to $100, you've just put a big brain (pardon the expression) onto your robot and added an enormous area of expanded capability.

    With HTML5 you could open up socket communication and have real-time interactive control and feedback.

    Take it a step further, with one of the controller boards salvaged from an AR Drone Parrot instead of a Rpi or beagle Bone, you could project your own WiFi network that you could pair your phone/pad controller to.

    If I missed this line of discussion someplace above, I appologize!
  • Heater.Heater. Posts: 21,230
    edited 2012-04-19 11:30
    Mindrobots,
    Not sure if you have missed anything yet but there seems to be many here converging on that idea. It's time has come.
  • mindrobotsmindrobots Posts: 6,506
    edited 2012-04-19 11:33
    Time to have an inflight accident with my AR Drone! (or buy a spare board)

    @Heater, I've been following your HTML5/javascript tales with interest. I'm far from understanding it but everything looks and sounds intriguing!
  • RaymanRayman Posts: 14,838
    edited 2012-04-19 16:41
    the javascript solution sounds interesting...

    I forgot there was a password option for the roving network device. That solves the security problem.
    I think the easy way is just to write a Visual Basic GUI that uses WinSock and the telnet port...
    You could have it automatically send the password for you...
  • charleyshfcharleyshf Posts: 165
    edited 2012-04-19 17:20
    You were right on the serial port module, it's currently not working for windows, so that idea isn't going to work.....

    Okay, so I know I can set a password for the telnet to the matchport b/g module, at least that part is good.

    I haven't used this in a while, but I do have oracle vm with an installation of Ubuntu that is working on my win7 pc, looks like it would make things easier :)

    I am going to get that fired up and try installing node.js with the serial port module and maybe go from there...

    Thanks again

    Heater. wrote: »
    Rayman,

    Telnet would be very easy. Only thing is very soon anyone in the world can control your robot! A web based system would enable you to build some security into it.

    Also having the feed back from the robot displayed graphically in an animation in the page is going to be way cooler and make it easy to use.

    For my first experiment I imagine something simple like having 8 buttons on the page and 8 images of LEDs. The buttons control the Prop to light real LEDs and the Prop tells the page to update the display accordingly. Simple steps.

    Charleyshf,

    I just have a sneaking suspicion that the serial port module does not work for windows. If you have the latest stable node.js installed you could try installing the serial port module using node built in package manager. From the command line:



    But I think it needs a compiler installed to build the C part of the package.

    You could try to install cygwin and install node.js and serial port there. But then life is getting complex. As is often the case for Windows users:)

    https://github.com/voodootikigod/node-serialport#readme
  • John BoardJohn Board Posts: 371
    edited 2012-04-19 17:34
    Eyeyeeye.... wait up now... Python has the pySerial library that handles COM ports as easy as ABC.... All you need is javascript code that sends commands to a python app which sends commands to the COM port, and your laughing!

    Hmm, if I have time (very unlikely) I will look into this a bit further later on, I've had a python project on the go for a few weeks now which is a python library that simplifies computer app comms to a prop.

    -John
  • charleyshfcharleyshf Posts: 165
    edited 2012-04-19 20:37
    Okay, so I just got done firing up Ubuntu in a virtual window and had to do some updating, I also installed nodejs as well as the serial module, have to call it quits for the night, i'm beat. Hopefully tomorrow I can start to get this going...
  • Heater.Heater. Posts: 21,230
    edited 2012-04-19 21:45
    John Board,
    Yes the is always Python. We use Python a lot at work so I have thought about it. For my web client communications I selected node.js over Python because:

    1. Javascript under node.js is much faster than Python. This may not matter for a single prop serial connection but in my comms problems at work it is important.

    2. With the event based programming model of javascript and node.js being totally non-blocking it is much easier to work with multiple connection dynamically comming and going as is the nature of web interaction.

    3. It's great to be working in the same language at both the server and client web page ends. Often you can migrate code from one end to another if you find it makes sense and no rewriting is required.

    4. You don't need a separate web server to get your web page up and web socket communications going. node.js can do all that easily.

    5. Python has that annoying white space block delimiting.

    Charleyshf,

    We are sort of on the same page.I also have node.js running in Ubuntu in a VM.
  • John BoardJohn Board Posts: 371
    edited 2012-04-20 14:16
    Hmm, well, maybe JS is right, I just have never programmed in it, so I don't know what it can/can't do. On the other hand I use python nearly every day (along with java), so they are the first things I think of using to accomplish such tasks :}

    Well, I hope you get this website thingy working ;)

    -John
Sign In or Register to comment.