elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Running python script from webpage

Wed Jul 20, 2016 9:10 pm

Hi guys, sorry if this is an easy answer, but none of my findings or trials has worked so far.

Ultimately what I am trying to achieve is to control an arduino from a webpage hosted on a raspberry pi. All the arduino controls at the moment is 3 led light strips, simple on/off. Will be looking to advance it but just want it simple for now.
I have them connected via usb and can control it with python scripts, I now want just a simple webpage that will either run the python script to send the command, or run the command directly from the site.

Thanks in advance

elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Re: Running python script from webpage

Wed Jul 20, 2016 9:48 pm

Right, so have gone back to my original config, checked the error log for apache and got

Code: Select all

Traceback (most recent call last):
  File "/var/www/html/1.py", line 6, in <module>
    ser = serial.Serial('/dev/ttyACM0', 9600) # Establish the connection on a specific port
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open
    raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyACM0: [Errno 13] Permission denied: '/dev/ttyACM0'
do I need to add apache to a usergroup to use it, dialout??

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: Running python script from webpage

Wed Jul 20, 2016 9:54 pm

An "ls -l /dev/ttyACM0" should show you the permissions on the device file. This should give you a hint what group you would have to add the user under which apache runs (I think it's www-data by default).

elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Re: Running python script from webpage

Wed Jul 20, 2016 10:02 pm

thank you, adding www-data to dial out did the trick. Its working to a degree, but not reliable, works like 2 out of 10 times.
Anyone fancy scanning over my code to see where I boo booed.

Code: Select all

<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('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('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('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>

Code: Select all

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

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


cheers

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: Running python script from webpage

Wed Jul 20, 2016 10:47 pm

I don't see any errors on first glance. Have you checked the log file again? That should at least provide some hint as to whether the script only gets executed occasionally or whether it is executed every time and runs into errors on some tries.

elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Re: Running python script from webpage

Wed Jul 20, 2016 10:52 pm

The log file is clear. I've come to find out that everytime the page was loading all 3 python scripts were getting ran. So am going to look into that, but dont know really where to start with that one.

Cheers

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Running python script from webpage

Wed Jul 20, 2016 10:58 pm

elephunk84 wrote:thank you, adding www-data to dial out did the trick. Its working to a degree, but not reliable, works like 2 out of 10 times.

Code: Select all

#!/bin/python
# -*- coding: utf-8 -*-
from time import sleep
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('1')
When you say it works 2 out of 10 times, you are saying all the way to the lights on the Arduino, right? Because how often does it work right before the ser.write('1') command? If you answer that, you'll know which side of the divide the problem is on. If that is still 2 out of 10, back up to something watching just before you call the python code.

elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Re: Running python script from webpage

Thu Jul 21, 2016 8:34 am

stderr wrote:
elephunk84 wrote:thank you, adding www-data to dial out did the trick. Its working to a degree, but not reliable, works like 2 out of 10 times.

Code: Select all

#!/bin/python
# -*- coding: utf-8 -*-
from time import sleep
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('1')
When you say it works 2 out of 10 times, you are saying all the way to the lights on the Arduino, right? Because how often does it work right before the ser.write('1') command? If you answer that, you'll know which side of the divide the problem is on. If that is still 2 out of 10, back up to something watching just before you call the python code.

Ok, so upon further investigation, it works everytime, but there is a massive delay between the button being pressed and the lights turning on. Then if I have any of the three lights on, when i go to either turn another light on/off or refresh the page, they all go out.....

elephunk84
Posts: 11
Joined: Sun May 15, 2016 10:41 am

Re: Running python script from webpage

Thu Jul 21, 2016 10:36 am

Ok, so now I know that when I was running the python script to send the serial command, it opened the serial port on the arduino which resets it. which is why I was getting intermittent results.

Now I think what I need is a python script that runs on boot that serves the webpage and can send commands over the serial connection.

Code: Select all

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

from time import sleep
import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while True:
    num = raw_input('Number:')
    if num == '1':
        ser.write('1')
    if num == '2':
        ser.write('2')
    if num == '3':
        ser.write('3')
This is what I'm starting with and going to see where I go from here.

dddomodossola
Posts: 42
Joined: Thu Jan 22, 2015 8:56 am

Re: Running python script from webpage

Thu Jul 21, 2016 10:05 pm

Hello, here is your solution. An interface with three buttons that shows in browser and is accessible from all devices in your network.

Code: Select all

import remi.gui as gui
from remi import start, App
import serial

class MyApp(App):
    def __init__(self, *args):
        super(MyApp, self).__init__(*args)

    def main(self):
        self.ser = serial.Serial('/dev/ttyACM0', 9600)
    
        mainContainer = gui.VBox(width=200, height=200)
        
        self.bt1 = gui.Button('Button1', width=150, height=30)
        # setting the listener for the onclick event of the button
        self.bt1.set_on_click_listener(self, 'on_button1_pressed')
        # appending a widget to another, the first argument is a string key
        mainContainer.append(self.bt1)
        
        self.bt2 = gui.Button('Button2', width=150, height=30)
        self.bt2.set_on_click_listener(self, 'on_button2_pressed')
        mainContainer.append(self.bt2)
        
        self.bt3 = gui.Button('Button3', width=150, height=30)
        self.bt3.set_on_click_listener(self, 'on_button3_pressed')
        mainContainer.append(self.bt3)

        # returning the root widget
        return mainContainer

    # listener function, called when button1 gets pressed
    def on_button1_pressed(self):
        self.ser.write('1')
        
    def on_button2_pressed(self):
        self.ser.write('2')
        
    def on_button3_pressed(self):
        self.ser.write('3')


if __name__ == "__main__":
    # optional parameters
    # start(MyApp,address='127.0.0.1', port=8081, multiple_instance=False, enable_file_cache=True, update_interval=0.1, start_browser=True)
    
    # starts the webserver
    start(MyApp, debug=True, address='0.0.0.0', port=8081)
In order to use this, download and install Remi library: https://github.com/dddomodossola/remi
;)

Return to “Python”