Jeb24
Posts: 7
Joined: Tue Dec 05, 2017 12:04 am

Home automation project

Wed Mar 06, 2019 3:46 pm

Hi ,

I'm a beginner and I am presently working on an home automation project inspired by this one:

https://maker.pro/raspberry-pi/tutorial ... dHSyN3pbtQ

I have my own website, which consist of an ON and OFF button, from which I am able to turn on and off a light, which is attached to a pi zero W through a relay.

On the pi zero, there is a python code constantly checking the status of the url address of my website,. Here is the code:

Code: Select all

import RPi.GPIO as GPIO
import urllib.request
import urllib.error
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5,GPIO.OUT)
true = 1
while(true):
                try:
                        response = urllib.request.urlopen('https://blablabla.com/buttonStatus.php?id=1')
                        status = response.read()
                except urllib.error.HTTPError as e:
                                        print(e.code)
                
                except urllib.error.URLError as e:
                                        print(e.args)

                print (status)
                if status==b'ON':
                                GPIO.output(5,True)
                    
                elif status==b'OFF':
                                GPIO.output(5,False)
Everything works perfectly, but I would now like to push a bit further my project...

I recently had an idea but I don't really know how to achieve it. I would like to wire a simple pushbutton or a matrix keyboard to another pi zero W (completely independent) and when I would click on the button it would change the php status of my button, just as my website page does. It would be a sort of wi-fi button to control the light.

To do this I know I need to write a python code but I have no idea how it is supposed to look like.

Correct me on that point, but I don't think I need add stuff in my php code...

It would be very nice if someone could put me on the right track

Best regards,

Andyroo

Re: Home automation project

Wed Mar 06, 2019 5:16 pm

Have a look at https://www.raspberrypi.org/magpi/issue ... o-zero-v1/ - free on the Pi site for a PDF but please buy a copy if you find it useful as the funds help support the foundation.

This will cover the very basics of controlling things and getting button presses in Python.

You may need to link back to the PHP script as it could show if the light is on or off and that could change by the button and not the script.

I hope you are using low voltage or have had an electrician check the circuit (unless you are one) - mains voltage kills...

Jeb24
Posts: 7
Joined: Tue Dec 05, 2017 12:04 am

Re: Home automation project

Wed Mar 06, 2019 5:54 pm

Thanks for the reply Andryoo,

The reference you send me is awesome but it is not exactly what I need...
You may need to link back to the PHP script as it could show if the light is on or off and that could change by the button and not the script.
This is my main challenge and I don't understand what you mean.

When the button will be pressed, it needs to change the status (ON or OFF) in the php script of my website: https://blablabla.com/buttonStatus.php?id=1.

How could my python script do that?

Best regards,

Andyroo

Re: Home automation project

Wed Mar 06, 2019 7:22 pm

Apologies, I though you where stuck on the hardware side aswell as the Python :oops:

Simplest way is to use the page sample as a post message from Python.

Python could use the curl command line but the best way (and more Python) is the request module as per https://www.pythonforbeginners.com/requ ... -in-python and http://docs.python-requests.org/en/master/

Jeb24
Posts: 7
Joined: Tue Dec 05, 2017 12:04 am

Re: Home automation project

Wed Mar 06, 2019 8:30 pm

Thank you very much this is exactly what I was looking for!

I now understand what library to use and the function name but I'm still mixed up on how to POST my ''on'' or ''off'' to my php file. :? :? :? :oops: :oops: :oops:

I think that's the line I need:
>>> r = requests.post('https://httpbin.org/post', data = {'key':'value'})

I still have two questions:

First, why do they add /post at the end of the url??? As I said in my first post, my url is https://blablabla.com/buttonStatus.php?id=1. Do I need to add a /post?if yes where?

Second, I don't understand what the hell are ''key'' and ''value'' are standing for. I guess my ''on'' or ''off'' goes somewhere in there??

Maybe it would help if I' give these two line of my html script...
<input type="submit" name="on" id="on" value="Turn On">
<input type="submit" name="off" id="off" value="Turn Off">

If anyone could clarify this it would be much appreciated.

Best regards,

Return to “General discussion”