bskari / pi-rc

Turn your Raspberry Pi into a radio controller for RC toys.
GNU General Public License v2.0
139 stars 38 forks source link

[Q] Writing a HTML/Javascript to control the car from a browser #6

Open ghost opened 10 years ago

ghost commented 10 years ago

Hi again,

Now that I have PI-RC working, is it possible to send UDP packets to the pi_pcm server from a web browser using Javascript? If so, what contents does the UDP packet contain exactly? Can you provide an example? Is it also possible for you to provide a simple Javascript that sends the packet?

I'm asking because I've written a web page for my current project which allows me to control a robot from my Nintendo 3DS and I think it would be awesome to create a page that works with Pi-RC too.

Thanks again!

bskari commented 10 years ago

That's a good idea, I like it! I'm pretty sure it's not possible to send UDP packets from JavaScript, but you should be able to send TCP packets using WebSockets. I have some time this weekend so I'll try to add TCP support to the server and add a sample page to the repository that you can use to send messages to the server to drive the var.

I have an example of what messages that you send should look like in the README. I think for your car, you'll want to do something similar to this (I've left out the web socket setup stuff though, I'll get a full example done this weekend. Also, I haven't tested any of this code.):

var forward = [
    // Synchronization burst
    {
        "frequency": 40.0,
        "dead_frequency": 26.995,
        "burst_us": 1200,
        "spacing_us": 400,
        "repeats": 4
    },
    // Command burst
    {
        "frequency": 40.0,
        "dead_frequency": 26.995,
        "burst_us": 400,
        "spacing_us": 400,
        "repeats": 11
    }
];
var message = JSON.stringify(forward);
socket.send(message);
ghost commented 10 years ago

Cool! Thanks!

ghost commented 10 years ago

Managed to create a PHP script that does it, fixed a issue with pi_pcm crashing after a few commands, too - it'd crash after receiving a specific .json command. Here's the script

<?php // Make car stop. $message = '[{ "frequency": 40.0, "dead_frequency": 26.995, "burst_us": 1200, "spacing_us": 400, "repeats": 4 } ]'; $server_ip = '127.0.0.1'; $server_port = 12345; if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) { socket_sendto($socket, $message, strlen($message), 0, $server_ip, $server_port); } else { print("can't create socket\n"); }

?>

bskari commented 10 years ago

Okay, I added WebSocket support to the server. If you start it with ./pi_pcm --tcp, then you should be able to connect to the server by opening the control.html page with a browser (I tested Firefox and Chrome), entering in the control values or loading a JSON control file, and connecting to the server. Then just use the arrow keys.

There is a bug where occasionally the decoding of the message from the browser will fail and the server will ignore the message. I have no idea what's going on with that, but I'll try to fix it later. I'll leave this issue open in the mean time.

ghost commented 10 years ago

Oh wow! Thanks! I'll surely have fun with this nifty piece of html.