RasbperryPi pins control over the internet
Check out the Video Tutorial for more details :-
A very simple tutorial that will show you how you can control Raspberry Pi over the internet.
Here I have attached a LED to Raspberry Pi. State of LED is controlled by website or by mobile application. I have also connected a push button to Raspberry Pi which can also be used to control the LED.
Android application (it’s not mandatory to use the mobile app)
Install Arduino Remote LITE And signup to your RemoteMe account. If you don’t have account create one for free here
After Signup at variable list there are plenty new variables added automatically by application:
- variables tab
- Its good to turn on persistent state – by clicking save icon
We can already change variable state by mobile app:
From Relay tab
After the changes at app, values are sent to RemoteMe.
Adding WebPage (It’s not mandatory you can use mobile app)
WebPages are freely stored at RemoteMe cloud and using them you can control your devices.
We will add one WebPage with two buttons. Buttons are going to change state of relay_01 and relay_02 variables.
Add new WebPage :
- Devices Tab
- New Device at right top corner
- New WebPage
- name : controlWeb
- deviceId: 10
- template – leave default
- more about adding WebPage here
- At new created WebPage click index.html then Edit with wizard
- Insert Component button
- And fill adding window:
- this new created button will change variable Relay_01
- add second button with RELAY_02 as name and label relay2
- close the window
- click at index.html and click open in newTab
- by clicking at relay 1 and relay 2 you can change button states, and If you have opened android application relays states are changing each time buttons at webPage are clicked
Now we will connect our Raspberry Pi zero w and when RELAY_01 variable is true we will turn on a LED and when button at Raspberry Pi is clicked RELAY_02 will change state to opposite.
Install RemoteMe program at you Raspberry Pi instructions here
Connecting LED ans button to your Raspberry Pi:
- Led anode is connected to Pin 19
- Led cathode is connected by resistor to GND
- button one leg is connected to GND second to pin 26
Be careful and double check the connections before you power the Pi.
When connections are made we can add the control script more details here
At Raspberry Pi choose burger menu and “Add external script with wizard”
At first step choose:
Second step device ID:5, name python.
At last step choose “create Device”. when Raspberry Pi program is restarted you will see:
It means that at your Raspberry Pi device you have one script, and both RPi and python script are connected.
Ofcourse now LED and buttons will not work. so lets edit our python script.
- Expand Python script device
- then click at python.py and Edit
- paste code:
Python12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667import loggingimport socketimport structimport sysimport osos.chdir(sys.argv)sys.path.append('../base')import remotemeimport RPi.GPIO as GPIOLED_PIN=19BUTTON_PIN=26relay2=False;def setRELAY_01(b):remoteMe.getVariables().setBoolean("RELAY_01", b)def setRELAY_02(b):remoteMe.getVariables().setBoolean("RELAY_02", b)def changeButtonState(channel):global relay2relay2=not relay2setRELAY_02(relay2)passdef onRELAY_01Change(b):GPIO.output(LED_PIN, GPIO.HIGH if b else GPIO.LOW)passdef onRELAY_02Change(b):global relay2relay2=bpassdef setupPins():GPIO.setmode(GPIO.BCM)GPIO.setup(LED_PIN, GPIO.OUT)GPIO.output(LED_PIN, GPIO.LOW)GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.add_event_detect(BUTTON_PIN,GPIO.RISING,callback=changeButtonState,bouncetime=200)passtry:logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',datefmt='%d.%m %H:%M',filename="logs.log")logger = logging.getLogger('application')setupPins()remoteMe = remoteme.RemoteMe()remoteMe.startRemoteMe(sys.argv)remoteMe.getVariables().observeBoolean("RELAY_01" ,onRELAY_01Change);remoteMe.getVariables().observeBoolean("RELAY_02" ,onRELAY_02Change);remoteMe.wait()finally:pass
How its working
When You open variables tab and expand Relay_01 and Relay_02 you will get:
It means all three devices are observing RELAY_01 and RELAY_02 variable, and if its changed by one of the device, change is propagate to all other devices.
More about Variable, and devices section here , here and here
When the RELAYs variable is changed by the WebPage its propagated to android application and it redraws state of relay. When the change comes to python this functions is called:
GPIO.output(LED_PIN, GPIO.HIGH if b else GPIO.LOW)
and this function just changes output of the pin to turn on or off the LED
When change is received at Raspberry Pi or Relay2 it calls:
we just remember the state of this relay in global variable “relay2”
When the button on RPI is pressed we are calling this function:
this function change global variable rela2 into opposite and this state is sent to RemoteMe by calling function
setRELAY_02(relay2) and then RemoteMe propagate new state of variable to the android app and WebPage.
WebPages and android application automatically recognize this variable and change connected into this component.