====== Controlling our entrance gate from android app ======
{{tag>dev iot raspberry}}
Okay so, there is a "little home project" I would like to quickly setup here: we are currently using remote controls to open/close our entrance gate [just as most people do!]. But now I'm thinking I could try to push this a bit further and implement a custom mechanism to control the opening of the gate from the Internet éh éh éh [//I know: not the most secured thing to do on earth, but we'll get back to that point another day//].
If I can do that, then I could create a minimal android app that I could install on our family phones to open/close the gate with a simple button press! That would be great [and fun at the same time]... So where should we start ?
====== ======
===== Setting up the raspberry device for ssh remote access =====
First thing first: I have a couple of spare raspberry pi units here, so I'll be using one of those as the "controller/server" in this project. Thus, I need to ensure I can access it remotely through ssh for quick configuration.
* The current IP address for that device on my network is currently: 192.168.1.23
* => let's make this a static DHCP assignment to IP 192.168.1.2 instead: **Done**
* Now we check if we have an ssh server running there: ssh 192.168.1.2
* And nope, lol it doesn't work. So I have to connect directly on the raspberry to install it. **Done**
* Now to connect remotely from ssh we use the command: ssh pi@192.168.1.2
The default password for the pi user is "raspberry" in raspbian
* Let's change the password just in case... **Done** /* Note: you are using your "default" password here manu */
===== Preparing a minimal test circuit with a relay =====
To be able to test the raspberry with a relay we need a minimal test circuit, let prepare that.
okay, so the circuit itself is pretty simple, with just one battery, one LED, one resistor and cables:
{{ public:projects:gatectrl:circuit_disconnected.jpg?800 }}
{{ public:projects:gatectrl:circuit_connected.jpg?800 }}
But now comes the funny part: how am I going to insert a relay in there (note: I only have a "naked" SRD-05VDC-SL-C component for the moment lol)
=> And I finally found some kind of datasheet for that component, it was not very clear but I could finally figure out how it works, and updated my little test circuit to include the relay, and this seems to work as expected:
{{ public:projects:gatectrl:relay_disabled.jpg?800 }}
{{ public:projects:gatectrl:relay_enabled.jpg?800 }}
As one can see on the picture above, I'm using the battery itself as power source for the relay... This is a 4.5V battery, but on raspberry I will only get 3.3V... let's hope this will still work!
===== Plugging test circuit on raspberry =====
* I found [[https://electronicshobbyists.com/controlling-ac-devices-with-raspberry-pi-raspberry-pi-relay-control/|this nice tutorial]] on how to proceed with the raspberry pi.
* Also found this page on GPIO documentation: https://www.raspberrypi.org/documentation/usage/gpio/
And in fact when connected to the raspberry over ssh, we can use the command "pinout" to get a very useful mapping of the GPIO ports.
* So I decided to connect on the pin GND and GPIO14, so here is the setup I have:
{{ public:projects:gatectrl:connected_to_raspberry.jpg?800 }}
Good, now it's time to build a small program to try this thing :-)!
===== Test python program =====
We have python 2.7.9 with pip 1.5.6 installed on the raspberry. I'm not quite sure I have the package "RPi.GPIO" installed by default, but anyway, let's just try the following minimal test program to see how this behaves first:
import RPi.GPIO as GPIO
from time import sleep
relay_pin =
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
GPIO.output(relay_pin, 1)
try:
while True:
GPIO.output(relay_pin, 0)
sleep(5)
GPIO.output(relay_pin, 1)
sleep(5)
except KeyboardInterrupt:
pass
GPIO.cleanup()
Hmmm.... that doesn't seem to work... sniff... let's try with some debug outputs... and also, I just read on [[https://deusyss.developpez.com/tutoriels/RaspberryPi/PythonEtLeGpio/|this page]] that GPIO is only available as root. So I updated the script to:
import RPi.GPIO as GPIO
from time import sleep
relay_pin = 14
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)
GPIO.output(relay_pin, 1)
try:
while True:
print("Toggling pin to 0")
GPIO.output(relay_pin, 0)
sleep(5)
print("Toggling pin to 1")
GPIO.output(relay_pin, 1)
sleep(5)
except KeyboardInterrupt:
pass
GPIO.cleanup()
And now running as root: sudo python main.py
**Crap...** :-(: So I can see the outputs, and when using a voltmeter I can see the voltage going from 0V to 3.3V and vice versa but the relay will not react at all :-(!
=> So I've been thinking about how to "workaround this problem" but unfortunately, I really cannot find anything that would make sense. Instead I think I should really just order a few SRD-03VDC-SL-C components from amazon and wait a couple of days befor continuing this little experiment!
[//...3 days later...//]
Hmmm! So.... I updated my test circuit to use a **SRD-03VDC-SL-C** component and... this is still **not working** ! :-| I mean, it's working fine when I use the GND pin with the 3V3 pin for instance. But as soon as I try to use a GPIO pin (+GND or +3V3) then it doesn't work anymore. How could that be ?!
... Oh... okay, I see now: just found [[https://www.raspberrypi.org/forums/viewtopic.php?t=192777|this page]]: basically the problem is, we cannot draw any power from the GPIO pin itself (just about 16mA) (but we can draw much more from the 3V3 or 5V pins, that's why those pins will works!).
Conclusion then is: I need to place a transistor on the GPIO pin to control the load activation, as shown for instance on this page: https://raspberrypi.stackexchange.com/questions/41173/how-to-switch-on-off-a-circuit-using-gpio
Question then is of course: what kind of transistor should that be and do I have any of them available ? :-) Let's find out. I have:
* LM350T (x2): these are "Three-Terminal Adjustable Output Positive Voltage Regulators", not what I need.
* [..And other stuff I didn't check because then I realized that...] Actually, maybe that's the point of buying a relay mounted on a PCB with other components ?!
=> And indeed, if we check the relay on PCB, there is a resistance R1, an transistor Q1, a LED, etc: this is definitely what we are after, so I tried using that and it **just worked**! In fact I can provide either 3V3 or 5V as input power and it works in both cases. All good!
{{ public:projects:gatectrl:relay_on_pcb.jpg?800 }}
===== Building a web service =====
Now it's time to move online :-)!
I found [[https://towardsdatascience.com/python-webserver-with-flask-and-raspberry-pi-398423cc6f5d|this article]] which seems to be exactly what I want to do, so let's use that as a starting point.
/*
Also found a far more advanced angular version app here: https://auth0.com/blog/using-python-flask-and-angular-to-build-modern-apps-part-1/
*/
* First we install FLASK for python3 (actually it's already installed): sudo apt-get install python3-flask
* Next we create a folder to store the project files: mkdir gatectrl
* And we create the sub folders: cd gatectrl
mkdir {static,templates}
* We create a basic helloworld version app: vim server.py
* Then we write the following python code: from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello world'
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
* And we run this test app: sudo python3 server.py
=> **OK** we get our hello world message when navigating to the local page: http://192.168.1.2/
Now let's create a minimal HTML template **templates/index.html** with the following content:
{{ title }}
Hello, World!
The date and time on the server is: {{ time }}