Raspberry Pi with PHP (or Arduino/ESP8266/NodeMCU) to GPIO to Relay to Gate/Garage Trigger

i’ve seen that thread after JZst linked it, but at that time already ordered few nodemcu. I’m not sure how it’d go with that but i’ll denifitely try it

Thanks for the heads up :+1:

I’ve tested on a nodeMCU. Just modify the GPIO numbers to match your set up and you should be good to go.

1 Like

Great, thank you very much :+1:

Now the “waiting game” for the board arrives from China begins

1 Like

MASSIVE NEWB QUESTION!

I’ve installed the PI as per your instructions, i’ve pasted the code into my smartthings developer area so I now have the device handlers and smart apps. BUT…

How do the switch sensor elements work so I can see state in smrtthings. I’m happy for (ON to mean Open and Off to mean closed )? I’ve got it to work where I tap the “on button” in the Smartthings app and this triggers my garage door into action but how do i hookup the switch that is normally closed when the door is closed and open when the door is NOT closed (no logic for potential mid point stops)

I’m sure i’m being completely thick here!!

Does anyone else have any install photos? Trying to think of a box to put this in.

@Dan_Lumbard check out one of my first posts on this forum. I’m a long time computer nerd and I found myself being just as thick as the next guy :slight_smile: basically create the device handler but don’t forget to add a DEVICE not handler which uses the new driver/DTH/device-type-handler ---- now you should see the new device in SmartThings on your phone/tablet and as long as you assigned the correct DTH to it, it should look like my sample screenshots.

@DITPL you don’t like my project box picture from the first post… well I think it’s gorgeous :slight_smile: small plastic project boxes from China — bought three of them. 1 in my garage, 1 doing my TV/cable remote control in the master bedroom and 1 more spare/future project box left. They were cheap and good enough size for what I needed at about $1.75 per box. Search eBay or Amazon for this “Electronic Project Box Clear” and please post the final project when done. I posted mine but nobody cares to show off I guess.

Hi, thanks for the speedy reply but I managed to add a number of devices using the same pi so i can now open 2 garage doors and turn lights on. i use ONE device for each rather than the secondary one so i have a toggle in the main smart things devices section. i have set the gpio pin to the same on both the main and secondary of each of my devices so my pins dont all get used up

But i wanted to connect switches onto the pi so i can detect the state of the door. Closed or not closed should be enough so just one switch needed. I cant see how this part works.

brilliant work though, gonna add it to a pi zero w for another project at the back of the house once i figure out the switch input issue.

Thanks @Dan_Lumbard! Necessity is the mother, as you know :slight_smile:

In the PHP you’ll see this at the top — set it as you please:
$sensor_pin=24;
$sensor_pin_2=25;

That pin is where you can attach an “MC-38 contact switch” between it and ground or VCC — depending on desire and usage of pull-up vs. pull-down resistor, etc. Currently the code expects you to connect it ground. Should mostly work w/o any resistor the only thing to watch out for is floating pin read values. Refresh the PHP page after changing states — BTW, for testing just run a jumper wire between the pin & ground to demo functionality.

Lastly, use my VIRTUAL DEVICE & SENSOR SmartApps — they will sync the SECOND button and 2nd contact sensor to a new device that you create (also from my code) which is empty/fake/virtual button and sensor. The SmartApp keeps all of those virtual devices and the parent in sync. Makes it easy to automate. No need to do what you’re doing although that was my workaround until writing the SmartApp. Be careful using TWO devices in ST that have the same address because you may get a duplicate violation, etc. Although I wrote code to avoid it as much as possible — I change the device address to a random value after every call to it trying to avoid the duplication error I mentioned.

See the very first post for the SmartApp piece & steps.

Enjoy!

Sorry for being a little dense, but this is my first time tinkering with a NodeMCU. I have all of the hardware handy, but don’t know where to go from there. Do I edit the NodeMCU.ino file with a text editor then using something like Esplorer to send the file to the NodeMCU?

Try something like this: https://www.youtube.com/watch?v=K1DE00LKfiY

I’ll also update the OP to mention the fact that the Arduino IDE is what’s used.

FYI, after installing the Arduino IDE, don’t forget to go under File > Preferences and use the proper path for the ESP8266 board like so.

Thanks! You mentioned that you had followed @Casper’s work, so I found that step on his page… Installing now!

This looks like a great app. I’m running into some strange issues and I’m trying to figure out what I am missing or overlooking.

I have a RPi 3 Model.

From the command line, if I write:

gpio mode 4 out
gpio write 4 0
gpio write 4 1

Things work as expected - my multimeter shows the voltage is changing as requested. When I installed the application and I got things working from the web based on the screen shot.

If I click on the “main trigger on” or “main trigger off” or “Main Trigger” and look at the command line (gpio read 4), the state doesn’t seem to change based on the command line.

Looking at the apache2 error log, I’m noticing it’s producing errors such as:

[Mon Jun 12 13:52:23.241551 2017] [:error] [pid 5039] [client x.x.x.x:51276] PHP Notice: Undefined index: MainTrigger in /var/www/html/index.php on line 232, referer: http://x.x.x.x/index.php
[Mon Jun 12 13:52:23.241688 2017] [:error] [pid 5039] [client x.x.x.x:51276] PHP Notice: Undefined index: MainTriggerOff in /var/www/html/index.php on line 234, referer: http://x.x.x.x/index.php

There’s a bunch of undefined index errors…

Weird, I’m using it and so are a bunch of others but to be fair it’s been about half a year since I upgraded Apache. I may wipe my test RPi1 and install from scratch just to make sure all instructions are still accurate.

Few hints… uninstall and reinstall Apache and PHP. Then go through all of my steps in the OP for setting up, specifically the “sudoers” aspect and a few others that will prevent you from running sudo privileged commands via Apache/PHP like we’re doing here.

I previously tried the Webiopi stuff and then switched over to your code. When things weren’t working, I rebuilt my pi from scratch this morning and only used your code - I had the exact same issue, so I must be missing something.

For your instructions, I had to move sudo rpi-update until after installing it otherwise it didn’t work (file not found errors).

Looking at auth.log, it looks like sudo is running as expected. However, I don’t see it making any GPIO write commands - and I suspect it’s related with the PHP errors (Undefined index listed in the above post). I’m going to play with the code a bit to see if I can fix the undefined index errors.

Jun 12 15:17:15 raspberrypi sudo: www-data : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/usr/bin/vcgencmd measure_temp
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session closed for user root
Jun 12 15:17:16 raspberrypi sudo: www-data : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/usr/bin/gpio -g mode 4 out
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session closed for user root
Jun 12 15:17:16 raspberrypi sudo: www-data : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/usr/bin/raspi-gpio get 4
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session closed for user root
Jun 12 15:17:16 raspberrypi sudo: www-data : TTY=unknown ; PWD=/var/www/html ; USER=root ; COMMAND=/usr/bin/raspi-gpio get 21
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session opened for user root by (uid=0)
Jun 12 15:17:16 raspberrypi sudo: pam_unix(sudo:session): session closed for user root

Very strange… when I have a sec I’ll wipe my old RPi. The fact that you can only run rpi-update after all was installed is kinda bad as that firmware update should have zero dependencies other than on the run-times it requires — not Apache or PHP, etc.

I just reinstalled Raspian Jesse Lite following your instructions exactly. This is what happens when I try to do sudo rpi-update:

$ sudo rpi-update
sudo: rpi-update: command not found

$ dpkg -l | grep rpi-update
$

$ sudo apt-get -y install rpi-update
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
rpi-update
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 4,408 B of archives.
After this operation, 45.1 kB of additional disk space will be used.
Get:1 http://archive.raspberrypi.org/debian/ jessie/main rpi-update all 20140705 [4,408 B]
Fetched 4,408 B in 0s (12.7 kB/s)
Selecting previously unselected package rpi-update.
(Reading database … 31514 files and directories currently installed.)
Preparing to unpack …/rpi-update_20140705_all.deb …
Unpacking rpi-update (20140705) …
Setting up rpi-update (20140705) …
$ sudo rpi-update
*** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
*** Performing self-update
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 13403 100 13403 0 0 43250 0 --:–:-- --:–:-- --:–:-- 43375
*** Relaunching after update
*** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom
*** We’re running for the first time
*** Backing up files (this will take a few minutes)
*** Backing up firmware
*** Backing up modules 4.4.50-v7+
#############################################################
This update bumps to rpi-4.9.y linux tree
Be aware there could be compatibility issues with some drivers
Discussion here:
https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=167934
##############################################################
*** Downloading specific firmware revision (this will take a few minutes)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 168 0 168 0 0 2393 0 --:–:-- --:–:-- --:–:-- 2434
100 53.9M 100 53.9M 0 0 6916k 0 0:00:07 0:00:07 --:–:-- 6415k
*** Updating firmware
*** Updating kernel modules
*** depmod 4.9.31+
*** depmod 4.9.31-v7+
*** Updating VideoCore libraries
*** Using HardFP libraries
*** Updating SDK
*** Running ldconfig
*** Storing current firmware revision
*** Deleting downloaded files
*** Syncing changes to disk
*** If no errors appeared, your firmware was successfully updated to 9e6a1a545ef33ac6cc3805845cb3ecac26514a41
*** A reboot is needed to activate the new firmware
$ sudo shutdown -r now

I followed the rest of your instructions.

I ran into the same issue. Let me know when you get a chance to wipe your old RPi.

Bad news @JMitch… working great for me. I definitely needed to alter the instructions which I did in the OP. SSH was closed off on the downloaded Raspbian image, had to use raspi-config to enable it. Obviously I had to change the order of rpi-update which has to come after actually installing it with apt-get — just like you indicated.

That’s about it… worked like a charm after wiring up and uploading my PHP. It’s a bit daunting because I can see you know what you’re doing and you’re starting from scratch just like I finished & tested a few moments ago. However, our results are not the same…

Keep looking at the code and do simple sudo echo commands to the root instead of the current gpio commands. Lastly, add sudo to the chained commands after each semi-colon.

Good luck, let us know how it goes…

Thank you for looking into this. After debugging stuff, it turns out to be user error. Oops. I should have known better.

I got confused with gpio -g and gpio commands. Both gpio -g and gpio control completely different pinouts. When I finally RTFM for gpio, I learned that -g refers to “BCM_GPIO pin numbers rather than standard wiringPi pin numbers.”

Things are now working as expected. Sorry to have you do the legwork to reinstall RPi - at least you updated the documentation about enabling SSH. I hope this helps someone else!

No problem, made me validate & modify my instructions. It’s definitely BCM (Broadcom) pinouts that I use. However, I completely agree, RTFM n00b :slight_smile: thanks for providing the feedback on the fix so we’re all clear on the resolution.

Hey @JZst I just found your project and am hoping to leverage it to bring more value to my latest project found here:

I think doing this will allow for an amazing two-way panel capability - I can envision all sorts of possibilities. Would love to hear your thoughts.