Controlling Arduino via Serial USB via website on Raspberry PI

Hi guys, long time lurker first time poster. I've run into a problem that I'm having difficulty finding a solution to.

I am trying to control 3 strips of LEDs, for the moment simply on/off, via a website hosted on a raspberry pi the arduino is connected to via usb. Now I have it all set up and working as far as I know, i can control the lights with python scripts like so

#!/bin/python
# -*- coding: utf-8 -*-

from time import sleep
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('1')

So I thought i would try to build a simple site with 3 buttons on it to run the scripts which looks like

<html>

<head>
<link rel="stylesheet" href="style.css">
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<title>IBedroom Lighting Controll</title>
</head>

<body>
<h2 align="center">
Main Lights - 
</h2>

    <?php
    if (isset($_POST['advance'])){
	    exec('/usr/bin/python /var/www/html/1.py');
	}
    ?>
    <form action="" method="post" align="center">
    <button type="submit" name="advance" id="submit">Advance</button>
    </form>
    <h2 align="center">
    Iains Lights - 
    
    <?php
    if (isset($_POST['iainslights'])){
	    exec('/usr/bin/python /var/www/html/2.py');
	}
    ?>
    <form action="" method="post" align="center">
    <button type="submit" name="iainslights" id="iainslights">Iains Lights</button>
    </form>
    <h2 align="center">
    Eloras Lights - 
    
    <?php
    if (isset($_POST['eloraslights'])){
	    exec('/usr/bin/python /var/www/html/3.py');
	}
    ?>
    <form action="" method="post" align="center">
    <button type="submit" name="eloraslights" id="eloraslights">Eloras Lights</button>
    </form>

</div>
</body>

</html>

Which I have had working for another project, but on this one I'm not having it so easy.

So it is working to a degree, I can control the LED's via the webpage, but, there seems to be a delay of ~>10seconds from me pressing the button on the page to the light turning on. Then if I go to press another button for another light, any that were on before go off. Also if i reload the website they all go off.

Now I'm not sure where these symptoms could be originating from so any help would be greatly recieved.

Thanks in advance

Bedroom_Control.ino (4.98 KB)

Now I'm not sure where these symptoms could be originating from

How long does it take for the Pi to start Python and run the script?

What happens when the python script opens the serial port? Typically, that causes the Arduino to reset.

PaulS:
How long does it take for the Pi to start Python and run the script?

What happens when the python script opens the serial port? Typically, that causes the Arduino to reset.

Ah OK now I start to see where I may be going wrong.
The approach I'm using is that that python script is only ran when called from the website. So I'm opening the serial port every time I press a button on the site, so the arduino resets every time I press a button.

So do I need to write a python program that runs on boot that generates the web page and sends commands from input off the site?? Is that the approach I should be looking at

So I'm opening the serial port every time I press a button on the site, so the arduino resets every time I press a button.

Yep.

So do I need to write a python program that runs on boot that generates the web page and sends commands from input off the site?? Is that the approach I should be looking at

If it were me, I'd get an Ethernet shield for the Arduino, and let it serve the page and respond directly to GET requests.

PaulS:
Yep.
If it were me, I'd get an Ethernet shield for the Arduino, and let it serve the page and respond directly to GET requests.

That was my initial thought to begin with, but I need to be able to edit and upload code to the arduino remotely as this system is tucked away in a hard to reach place, and the audio output capabilities of the raspberry. Plus learning to integrate the two could prove handy in the future

I have developed a few projects using the Python Bottle web framework to create a browser based GUI to control an Arduino that is connected to the server by the USB cable.

There is no noticeable latency in my systems.

The basic interface with the Arduino is like this Python - Arduino demo

You can get a simple web app working with Bottle in 15 or 20 minutes.

AFAIK Bottle will work on an RPi. It certainly works on an Arduino Yun which is a lot less powerful.

...R

Cheers Robin2, will certainly look into that

Thanks again robin2 I got it working. I used flask in the end because I found more documentation that was easier for me to understand, but wouldn't of got there without your help.
Thanks again