Need suggestions to introduce Javascript variable to Arduino usb connected

Hello every body,

I'm looking for somes idea to realize the joined image project.

Project description :
I connect an Arduino Uno R3 via USB to my computer (Windows 10).
I launch my browser and go to http://mysite.com/test, where lives a Javascript variable named in an original way : "myVariable".
I would like to introduce this Javascript variable to my arduino program, in order to do somes amazing stuff.

Please see my joined image.

Some one have somes idea to do this connection ?

Thanks a lot

I would like to introduce this Javascript variable to my arduino program, in order to do somes amazing stuff.

Basically you can't. Code does not work like that.

What you have to do is to send a value to the Arduino and the Arduino reads it in the setup function and puts it into a variable. But you need the code at the PC end to send the variable a short time after you start the serial port.

Rather than access the website with your browser I reckon you need to access it with a program on your PC which can parse the webpage (screen-scraping is the common jargon) to get the value from the variable and can also communicate with your Arduino over the USB connection.

Of course if the server that produces the webpage is also on your PC the process could be much simpler - the server program could probably send the data to the Arduino. I do that using Python and the Bottle web framework

...R

@Robin2: Yes the server of http://www.mysite/test is on my pc where Arduino is connected. Could you please list the name of elements used in your workflow ?

Your server is an Apache2 ? Your distribution is an Linux Debian 7 ? What is your Python version used ?

Some image or url or codes to see your project ?

Thanks a lot

Found this on stackoverflow : javascript - Call python function from JS - Stack Overflow

function postData(input) {
    $.ajax({
        type: "POST",
        url: "/reverse_pca.py",
        data: { param: input },
        success: callbackFunc
    });
}

function callbackFunc(response) {
    // do something with the response
    console.log(response);
}

postData('data to process');

I assume that this is a serious security hole, but it can resolve my case. By using a library to control Arduino usb connected via Python, like GitHub - ln-nicolas/Exode: Exode is a Python library for communication between Arduino microcontroller boards and a connected computer. Write Python scripts and take control of your board., I could control my Arduino from Javascript.

Am I right ?

I posted a cut down version of my complete Python/Bottle web server and the Arduino connection for another Forum user. Feel free to use it if it helps.

I developed it on Linux (one of the Ubuntu derivatives) but I suspect it will work on Mac and Windows if you change the serial port references. I use Python 2.7

The Python program uses the Bottle library to simplify the web programming. There is also some CSS and Javascript. It takes data from the browser and stores it in variables. There is a separate Python Thread for the communication with the Arduino. It takes data from the variables and sends it to the Arduino. It also listens for messages from the Arduino and stores them where they can be accessed by the web program. In practice it is two programs running in parallel and communicating through some common variables.

...R

I will study your solution.

I'll be back here to return my results.

Thanks a lot :slight_smile: !