0

i want to submit a value from my website to a python script. ive got a raspberry pi, running with apche2 and php7. there is an light sensor connected and my script does switch on or off a light, when a definid value has reached. is it possible, to change the value of an running python script through my website with php?

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

import smbus
import time
import RPi.GPIO as gpio
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
gpio.setup(29, gpio.OUT)
gpio.output(29, gpio.HIGH)

while True:
lux = 200


bus = smbus.SMBus(1)
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)
time.sleep(0.5)
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]

print (ch0 - ch1)
if (ch0 - ch1) < lux:    
  gpio.output(29, gpio.LOW)   
  print("Licht AN")
  time.sleep(60)
if (ch0 - ch1) > lux:       
  gpio.output(29, gpio.HIGH)      
  print("Licht AUS")
  time.sleep(60)

thanks, Matthias

1 Answers1

0

There is two ways to do this task
First:
-You can locate your script in the webroot of your project then using php function exec() you can run your script.
-in your php code you need to set up a route for the function that will execute this code.

Second (best practise way):
-you can build a simple api using Flask (Python web framework) to your script read THIS blog i wrote about building a simple api for your scripts.
-then you need to make a call for the api to run the script using php you can use curl or if it is too simple you can just use a static link

aa-Ahmed-aa
  • 363
  • 4
  • 14
  • Thanks for your answer, but i know how to execute the script, but i want to change the value "lux = 200" to maybe 300. And this i want to do in my website with php – Matthias B Apr 01 '18 at 16:50
  • then you need to use the second way and customize the parameter in the url with lux value you want please read this blog to do the task https://dev.to/aaahmedaa/create-restapi-for-your-python-scripts-using-flask-with-15-line-of-code-10ml – aa-Ahmed-aa Apr 01 '18 at 17:00