Golang and Arduino

Small arduino and golang programs

Control relay with Raspberry PI in python in web

Control relay via web on raspberry Pi

First you need to install some dependencies

apt-get install python-rpi.gpio python3-rpi.gpio python-flask python3-flask

after that, create file: app.py and paste following code to it. edit MYRELAY to your pin ( in my example i am using 8 physical pin ) 


from flask import Flask
import RPi.GPIO as GPIO

MYRELAY = 8

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MYRELAY, GPIO.OUT, initial=GPIO.LOW)

app = Flask(__name__)

@app.route("/on")
def turnon():
    GPIO.output(MYRELAY, GPIO.HIGH)
    return "OK"

@app.route("/off")
def turnoff():
    GPIO.output(MYRELAY, GPIO.LOW)
    return "OK"

if __name__ == "__main__":
    app.run(host="0.0.0.0",port='8080')

run

python3 app.py

now, ping your Raspberry Pi via http://ip:8080/on to turn Relay ON and http://ip:8080/off to turn relay off