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

That does seem to work for the momentary functionality and will work to match the garage door application I was emulating here. THANK YOU!

I was also looking to create a few of these for ON/OFF functionality for other relay applications.
I guess that begs the question if the device handler needs to be re-written for ON/OFF functionality. Obviously it works for on but doesnt seem to hold the state on that or toggle the http get to an off message.

Yep you’re absolutely right… I messed up on the println where I should have used print… that causes string parsing to fail. I modified the NodeMCU code and the Arduino Nano code. Give it a shot and let me know how it goes. It worked great for a sec but the ST API site just went down and I rebooted the hub and now dead in the water :slight_smile: waiting for the service to come back online to finish the few tests that did succeed after the fix.

Fantastic… Works like a charm now. Thank you for your help!

Now I need to re-integrate the temp sensors (I removed them just to get functionality working) and then build my army of devices!

Any thoughts on adding more than 2 pin functionality?

Well, my need brought be to two relays and the rest I’m leaving for folks to customize/enhance. The other reason for settling on 2 is the fact that Amazon Echo can only do ON & OFF which is again 2 commands (to be fair, a slider can be used as workaround for the Echo limitation). While I certainly see the point and usefulness of integrating every pin of the NodeMCU, it’s just not feasible. The other reason why I don’t want to do it is because the Device Handler interface will have countless tiles/buttons for folks not interested in using them and I can’t make it dynamic enough to show extra buttons on the fly, that’s an ST limitation. So for cleanliness I’m leaving as is. If you have any questions on how-to, feel free to ask, but it’s not easy to be honest especially if you want stateful (on/off) instead of momentary and even harder if you want the time-stamp tile of when the trigger was last pressed.

All the best!

Hi JZ,
I’m trying to get this up and running, but don’t have any experience with PHP, HTTP,JSON, POST, GET etc and am a bit confused what I need to fill in in the preferences section. I put my pi’s IP address but suspect something else is needed as it is not connecting to the PI to retrieve details, etc. I am not sure either if/how I need to set up a http user ID and password.
Grateful for any pointers.

Dom.

PS - I worked it out just now - I needed to put /index.php in the path preference. However, grateful for info on the http authorisation part

I get this from the logs:
MainTrigger=

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎20‎:‎04: debug The method is POST

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎20‎:‎04: debug POST body is: MainTrigger=

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎20‎:‎04: debug Uses which method: POST

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎20‎:‎04: debug The Header is [HOST:192.168.0.125:80, Content-Type:application/x-www-form-urlencoded]

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎20‎:‎04: debug The device id configured is: C0A8007D:0050

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎19‎:‎43: debug whichTile:

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎19‎:‎43: debug [Date:2016-12-12 5:19:42 PM]

7f6a1eca-11d3-4063-9783-b765a5bfdde4 ‎17‎:‎19‎:‎42: debug POST / HTTP/1.1
Accept: /
User-Agent: Linux UPnP/1.0 SmartThings
HOST: 192.168.0.125:80
Content-Type: application/x-www-form-urlencoded
Content-Length: 12

Few things:

  • The default behavior of Apache is to run index.htm, index.html or index.php, whichever is found first and usually HTM/HTML is attempted first since it’s not dynamic. So on my pi, I don’t need to use /index.php also when just tested from the desktop. At least you figured that out, but modifying .htaccess on any HTTPD server should give you the same functionality that I see where the default document is indeed index.xyz
  • In the PHP source that I provide as a sample, authentication is turned OFF, so you really don’t need to fill that in the settings of ST. However, you can enable it at the top with a flag/variable, just set it to true and now you will require authentication.
  • Before trying to wrap your brain around the wonders of ST, grab a tool called Fiddler and on the top right you’ll see a Composer tab where you can create HTTP requests. Start with that or just a browser and make sure it works there, only then go back to ST logs once you’ve guaranteed that generic browser on a desktop succeeds. See screenshot for reference.

Thank you JZ for your great starting point into the world of HTTP PHP etc. I’ve been working as a SAP IT consultant in ABAP and functional module for 20 years so reckon it’s time to start working out how the web works !
I got the app working great. I’ve set up my pi to operate a curtain opener - one pin for open, another for close wired up through a transistor, resistor setup to simulate pressing the respective button on the control module. I’ve used main trigger for open and custom for close, but had to modify the php so that it goes momentarily 1-0 rather than 0-1.
I’ve actually got three pi’s for three curtains in different rooms but set up .sh scripts on the central pi to run commands via ssh on the other pi’s to open or close. I plan to adapt your setup to run the respective .sh on my py e.g. open_bedroom.sh or open_lounge.sh etc.
Thanks once again for pointing me in the right direction for this.I think I need to brush up on the string handling/special characters for scripting a bit (&& || \ etc sed `) and then modify the php to run shell scripts and the groovy to have three tiles - lounge, bedroom, matt bedroom with options for open/close.
Final part would be to integrate into homebridge so I can use Siri to open the curtains etc.
thanks once again,
Dom.

Very nice to hear where you’ve taken the project, thanks for that feedback!

You’re likely doing the right thing with sending HIGH/+ vs. LOW/-
The reason is because when a pin is in OUTPUT mode, the signal is flipped. I read that somewhere and obviously had to settle on that for correct functionality :slight_smile:

FYI, my PHP script should give you plenty for string parsing in the shell. However, you’re throwing a big/heavy device like an RPi at your somewhat fixed/small need. Definitely check out the ESP8266 NodeMCU as a very valid option. RPi is a bit of an overkill, however, RPi3 is still stuck in my gate due to RJ45 reliability rather than WiFi and the fact that it survived a SoCal hot summer. Don’t want to fix something that’s not broken.

New version:

v1.0.20161223 - Added UpTime for the Arduino IDE samples. Added device name & IP at the top of SmartThings screen. Added wiring for Arduino Nano & a generic ENC28J60 ethernet board.

Calculating UpTime in Arduino IDE/C++ using the millis() function. Modified the ST device handler screen to have device name & IP at the top for easy identification. Cleaned up wiring and documented generic Arduino Nano & UNO to ENC28J60 wiring.

Enjoy!

Thanks again for putting this together. Finally finished the project this morning and have been enjoying the automation for my garage door.

I have one bug left to figure out. I can see the temp/humidity information on the webpage (sometimes) when refreshing but i do not see in the ST device at all…

My pleasure, glad it’s working well for you!

EDIT: @cypher2001 I see that you’re having issues with temp/humidity… I verified with a WeMos D1 Mini and a standard NodeMCU using a DHT11 on one and a DHT22 on the other. I have no issues other than SOMETIMES when I refresh the page, there’s no temp/humidity. Then I refresh again and it does come back. Not much that I can do about that as I’m no expert at the DHT library :slight_smile:

@JZst

Whats interesting is that I do get readings via the webpage (Although much less frequent than you did. One out of every 10 or so has the temp readings). I never see that temp populate in the ST app, however.

One other thing that I might point out is the uptime calculations. I installed mine yesterday and this morning it shows 48 days uptime. I havent looked at that code myself but thought I would pass along the feedback.

Uploading new fixes today to address many pieces including uptime calculation.

My guess is that you are under-powering your board. I began to get the non-read more often. So I plugged in a quality cable via a higher output USB charger and I got way more frequent responses from the sensors. You definitely have a point with it NOT displaying especially when the DHT has not been read/refreshed for a while. The annoying way of fixing that is to retry the DHT sensor X times every Y milliseconds unless it gives a valid read. I hate loops like that but it’s certainly an option.

FYI you can see the entire json array in your ST API log. Definitely visit that for further troubleshooting and start outputting debug statements in the code (Arduino Sketch and ST Device Handler). I promise you what I am seeing is ST spitting out the temperature :slight_smile: but like you pointed out, it’s not consistent at reading successfully from the DHT. Sometimes it shows nothing until the next read.

New version:
v1.0.20161231 - Arduino sketch enhancements & fixes: Added free memory/heap logic. UpTime reports day/days correctly now. Replaced reboot logic with a simple while loop to cause a soft watchdog reset. Switched to use5Vrelay=true to match wiring diagrams. Added abort when favicon.ico request is sent.

Very important: switched the default use5Vrelay value to TRUE at the top of both Arduino sketches. This is to match the wiring diagrams closer and because I’m guessing 5V ones of more common use. Correct me if a bunch of you are using 3.3V relays.

I’m still having consistency issues with ESP8266 modules especially over multiple days. I have left the 8 hour reboot in the code but feel free to comment it out at the top of the LOOP() function; I’ve also changed the reboot logic to simply be a an infinite loop which makes the watchdog do a soft reset and the device is back up very quickly. My next step is to see how long the NodeMCU and the WeMos D1 Mini clones that I have can truly stay up.

I’ve also spent some time outputting memory/heap space used. It’s pretty nice to verify that memory is not leaking with the code. Expect the Free Mem tile in SmartThings to display for Arduino & ESP8266.

1 Like

JZst, thanks for putting this together!

I am having an issue with the Custom Trigger. I’m using this (as others are) to monitor and control my garage with a Raspberry Pi Zero. The main trigger triggers the door open or closed, and the custom trigger is tied to a dry contact for door state. 0 == closed, 1 == open. Initially the custom trigger appeared to be updating with the refresh button, but now it seems that the pin state of the custom trigger is not refreshed when using the refresh button. I’ve combed over the code and double checked my settings, any idea where to start troubleshooting this issue?

I have a javascript I was previously using to control the garage, but I just got my ST hub and Echo dot and this was the fastest/easiest way to tie everything together to begin my tinkering. Point being, I can verify that pin state updates as it should with the java script.

Thanks again, nice project!

@boogy886 sadly for you I still have an RPi3 in my gate and it’s perfectly reporting ON vs. OFF on the custom trigger. I checked the same on the ESP8266/Arduino’s and they’re also reporting it correctly. This is one of those quality tests that I try to do before releasing. While I love adding features, I hate breaking the obvious necessaries that my project is supposed to do. Therefore, I check the triggers with stateful & momentary modes before releasing code. It has always worked well. My only recommendation is to reset the devices including the RPi and the ST hub.

Last but not least, in the ST device handler code, un-comment the logging for the full HTML response that was sent to ST. You can easily see what’s up. They’re around line 306-307 so just remove the leading slashes from the BODY from the excerpt below:

//log.debug "BODY---" + bodyReturned
//log.debug "HEADERS---" + headersReturned

@JZst As far as the program was concerned, everything was functioning normally. I removed all instances from ST and started from scratch on the RPi, and still had the same result.

Until I manually cycled the trigger once, from there it has been updating as it should. Apparently I’ll need to cycle the custom trigger manually in the event of a Pi reboot.

I can’t come up with an explanation as to why this is the case. Can you?

I’m fine with it, it’s no problem for me. Just for my own curiosity I would like to know why the code can’t accurately pull pin data until the trigger has been cycled once. Or maybe it’s just a fluke, and only a problem that I would experience (that’s typically the way it goes for me! haha)

Well after a reboot, the logic turns the pins OFF for obvious reasons — you don’t want gate opening just because of a reboot. In fact that’s the reason why I chose the RPi pins that I did. Some default to off and some to on.

That’s about all I got :slight_smile: glad you figured it out. Reset, recreate, and try all of the logic’s abilities are always the valid few troubleshooting steps for this stuff.

Question for everyone here, do folks use relays or have others successfully used a transistor or a FET instead? To play the role of a switch or for shorting…

I’ve been toying with the capability of transistor & FET and it’s so much better than relays due to the life expectancy of solid state part vs. the mechanical relay. Then there’s the obvious size difference. In essence, all I ever needed was to SHORT contacts. It seems that either a transistor or a FET can easily accomplish this with my latest testing.

Would love to hear some feedback… success or failure :slight_smile:

I use a transistor to open/close my garage door with a esp8266.