Page 1 of 1

Simple code to turn on/off relays from buttons on website

Posted: Fri Apr 05, 2019 9:53 pm
by Osprey72
I have been up and down this forum site and it seems there are "many" solutions to this quandary. Of the six attempts I have made, I am still no further in finding a solution.

I tried several while using Ubunto Mate (had astronomy software there to run)....nothing
Dumped U.M. and came back to Stretch to try WebIOPi. Won't work with Stretch.
My latest failure was following a tutorial on AJAX and php to which the buttons pressed just fine, but GPIO pins not changing state. WiringPi will let me change them manually but I don't want to sit and type all that code just to trigger a relay...let alone 8 of them.

This has been frustrating... a week of trial and error.

I am willing to try suggestions but am not well versed in coding. This is supposed to be a hobby...not a PhD :roll:

Thanks in advance!!

Pi3b running Stretch...all updates current, upgrades done. WiringPi installed, apache2 installed...anything else, feel free to ask and I'll try to find you an answer :)

Re: Simple code to turn on/off relays from buttons on website

Posted: Sat Apr 06, 2019 1:46 am
by JohnsUPS
Look this over.
In the example they are only controlling a LED from a web page, but if that can be controlled, so can anything else (like a relay) with the proper interfacing.

https://www.w3schools.com/nodejs/nodejs ... socket.asp

Re: Simple code to turn on/off relays from buttons on website

Posted: Wed Apr 10, 2019 2:16 pm
by Osprey72
Thanks!! I will try it out later this evening.

Re: Simple code to turn on/off relays from buttons on website

Posted: Sat Apr 27, 2019 12:56 pm
by Osprey72
Still nothing. I have reflashed the card, downloaded newest copy of apache2, set it for cgi.... I just don't understand. I can go to the terminal window and type in

gpio write 1 0

and the first relay will trigger. Is this proprietary information to where nobody can tell how to make this happen? Am I infringing on copyrights? What are the odds that 2 Pi's are bad, running 2 separate versions of Raspian?

Re: Simple code to turn on/off relays from buttons on website

Posted: Sat Apr 27, 2019 1:05 pm
by rpiMike
Have you considered using pythons http.server library:

https://www.e-tinkers.com/2018/04/how-t ... eb-server/

Re: Simple code to turn on/off relays from buttons on website

Posted: Sun Apr 28, 2019 10:42 pm
by neilgl
Ah, didn’t we already try on another post my CGI method?

Re: Simple code to turn on/off relays from buttons on website

Posted: Tue Apr 30, 2019 7:01 am
by ankith26
I found the perfect solution that works(personally tested it)..
It is a weird and inefficient method but works well.

This is how i did it:
1) I am using python to read from the web and control my relay
You have to install requests module, gpiozero module and time module(some are installed by default)

2) Copy the python code below and save it to any file

Code: Select all

import requests
import gpiozero
import time

pin = "BOARD11" # put your pin here

#relay = gpiozero.OutputDevice(pin) # uncomment

while True:
    link = requests.get("http://localhost:80/relay/relaytext.txt")
    data = link.text

    if data == "on":
        #relay.on() #uncomment
        print("RELAY: on")
        pass

    else:
        #relay.off() # uncomment
        print("RELAY: off")
        pass
    
    time.sleep(1)

Make sure to change the pin to that which you are using. Use the PHYSICAL PIN SYSTEM
Check out this important link https://gpiozero.readthedocs.io/en/stable/recipes.html

3)Now in your root directory of apache, create a folder called 'relay'(if you are using my code, exactly follow the names)
Create two files namely "index.php" and "relaytext.txt"
Copy this code onto index.php

Code: Select all

<?php 
if ( isset( $_POST["relay"] ) ) {
$relay = $_POST['relay'];
echo "the value of relay is $relay";
$myfile = fopen('relaytext.txt', 'w') or die("Unable to open file!");;
fwrite($myfile, $relay);
fclose($myfile);
}
?>

<html>
<head>
<title>Relay control</title>
</head>
<body>
<h1> RELAY TEST AND CONTROL </h1>
<form name="relayform" method="post" action="index.php">
 <input type="radio" name="relay" value="on"><label for="on"> ON</label><br>
 <input type="radio" name="relay" value="off" checked><label for="off"> OFF</label><br>
 <input type="submit" value="Submit">
</form>
</body>
</html>

You will also want to edit the "relaytext.txt". open it and type one word that is "off"(without spaces or hitting enter)
4) Give php access to edit your file do this by entering this in your command line

Code: Select all

sudo chown -R www-data /var/www/html/relay/relaytext.txt
(this will work if php user is www-data and your file path matches)
This line may differ for you as you are using Stretch (Iuse jesse)

Check out this link from another forumhttps://stackoverflow.com/questions/290 ... -directory
The reply from Grant Miller helped me( the one which says '3 step solution')

5)Nearly done. Now open http://localhost/relay.
you will see a webpage with on/off option. Choose your option and submit

Make sure that your python file be running this while and you see its response from server.

6)When you get required responses, Connect the relay, uncomment those three lines(which are marked) in the python code and run it after properly checking the relay.( i did not check this part myself so good luck for it)

Re: Simple code to turn on/off relays from buttons on website

Posted: Tue Apr 30, 2019 7:21 am
by ankith26
But the more interesting part is the fact that YOU(or me when i am away from my house) could control my relay just by heading over to http://pratt.ml/relay..
This is because my webserver is forwarded to the internet....
However i wont be running the link for you all ;)