-2

I wrote python program that can be blink led on raspberry pi .

import RPi.GPIO as GPIO   ## Import GPIO library 
import time               ## import Delay library 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(26,GPIO.OUT)
while True:
     GPIO.output(26,True)  ##Turn on LED
     time.sleep(1)         ## wait for one second
     GPIO.output(26,False) ## Turn of LED
     time.sleep(1)

Now, I want to blink led with help of web page on apache server. So whenever I click on button, led should be blink.

HTML Page

<html>
<body>
<title>Raspberry PI</title>
<h1>LED</h1>
<form>
        <input type="Button" value="Blink LED" onclick="href='led.py'">
</form>
</body>
</html>

I have just started to working on it. This is my effort, I have doubt on html Can you tell me, What's wrong in html page. How to blink led using web page?

  • 2
    Plenty of tutorials. Find one, follow it, report a problem if you have one. – joan Dec 31 '17 at 9:11
  • Yes there are so many example available on google. I have followed some tutorials. I would like to follow method where I want make page using html and then I want to embedded python program into html page. As I was trying to do in question – abhimanyu143 Dec 31 '17 at 14:47
0

Just putting "href='led.py'" in your HTML isn't going to work - you need a web server that listens for requests from your web browser and then exectutes the code to blink the light.

Looking around online a bit I found this article which shows how to blink an LED from a python web server, that might be a good starting point for you

  • I know how to setup server on Pi. My question is related to programming. I have asked what's the wrong in html program. I am sure there are something wrong. I couldn't embedded python program into html page. specially I have doubt in this line <input type="Button" value="Blink LED" onclick="href='led.py'"> – abhimanyu143 Dec 31 '17 at 6:37
  • Basically HTML doesn't work like that, there is no way to execute a program on your pi directly from the HTML. That's why you need a web server - your browser sends a message to the server, and then the server runs your python code to blink the light. The browser itself isn't directly connected to your hardware. – Tim Gilbert Dec 31 '17 at 8:45
  • I am totally agree with you but if you have a read my question carefully I have mention apache server in question and I know how does apache server work on PI. I have told you main problem in previous comment – abhimanyu143 Dec 31 '17 at 14:40
  • Oh, sorry, you're right, I misread your question. What you need is a way to make Apache execute the led.py script. As I said, just putting the script name in the HTML won't work. There are a few ways to do this, you could use CGI or a PHP extension, but I still think your best bet is going to be replacing Apache with a Python web server like in the article I linked to, then you can just call the code directly. You also want to make a specific url like /blink that will execute the code instead of serving HTML. Then you can use javascript to hit that URL when you press the button. – Tim Gilbert Jan 7 '18 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.