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

I’m trying this out and so far so good, but I have one issue on an RPI2. I get {java.lang.NullPointerException @ line 314 } in the live logging and no data appears on the android app.

any ideas on what or where I went wrong, the web app works ok?

thanks
andre

Few things that can go wrong… try changing the POST/GET methods, etc. Lastly, make sure that you don’t have a duplicate IP (device address) for some other device.

When I look at line 314 is where I take the date variable and store it in the JSON array. Try putting some lame string in line 314 or just comment it out… very weird as this works great on RPI 1st gen and RPI3 for me.

Thanks!

Hey,

I did comment out line 314 and the previous jsonlist.put statement and it all worked - especially if I set to use JSON instead of HTML - using the latter it fails, but it working so I’m happy :smile:

Agree it was odd but being as JSON had the date I didn’t see that as an issue.

Thanks for the response on this - I was going crazy till I went thru the debugging and commented it out and your confirmation helps out!

thanks
Andre

Sadly it doesn’t matter that the RPI/PHP code shows you the time/date and even the JSON because I don’t use it :slight_smile: I cheat and just use Groovy/SmartThings to give me time & date just so I don’t have to parse what RPI/Arduino returns… easier to let ST do its job. Here’s the line you commented out. Notice that I just use the built-in Java function Date() but I do try to offset the time based on location/timezone:

jsonlist.put ("Date", new Date().format("yyyy-MM-dd h:mm:ss a", location.timeZone))

Pretty strange that I haven’t heard from anyone else about this failing and it certainly works for me :slight_smile: make sure that your time/date and timezone are properly set on the ST hub. Try something simpler like “new Date()” rather than the offset/formatted value I use.

All the best!

Hello all,

Finally got around to getting this setup and it works great! Question I have (pardon my ignorance of if this has already been answered) how can I tie this switch into a open/close sensor. For example, how can I have the switch turn on/off momentarily as part of a routine only if the sensor is open.

Thanks in advance

Roman

You need a “rule” engine inside of SmartThings.

The simplest one is the one offered by Samsung/ST — called Smart Lighting… go to your SmartApps and just install it.

The old favorite was Rule Machine but the developer of it killed the project and now we have CoRE — comminity’s own rule engine. It’s SUPER-NICE for advanced rules — but for your miniscule requirement of sensor then switch — Smart Lighting should do the trick and I’d highly recommend it given that Samsung fully supports it.

Totally agree that what I’m trying to do isn’t advanced by any means and you would think there would be a way to do this built into ‘routines’. I think I accomplished what I wanted with smart lighting and using routines to switch to custom modes based on open/close sensor. Still needs some tuning and testing

Thanks so much for the response.

Roman

Can we use this to run a python script on a Pi? I have a cat feeder that has 2 servos connected to the pins on the Pi, with a python script that runs to operate the servos and feed the cats. the problem, though, is its not a simple high/low signal from the pin, we have to use PWM to control the servo. The other HTTP interfaces ive found just turn the pin to high or low, which works awesome for my relays, but not for the servos. Can i use it to run a script? For example, its here:
/var/www/html/scripts/feeder.py

the feeder.py is the script that runs the servos, but i cant figure out how to run that script via smartthings. I have a small webpage built with some PHP code, and from that you can click on/touch a button that runs the code (so i can do it from my phone), but i would love to be able to run the same script from a virtual button in smartthings.

Thanks!!

Yes, in fact, initially the sample that I was working with for the Pi used python. However, I replaced it with GPIO commands just to MANAGE less code files. So if you were to grab the GitHub index.php and simply search for where I run gpio shell commands and replace it with your python command but the FULL COMMAND not just python file name /var/www/html/scripts/some.py — I believe full would be “python /var/www/html/scripts/some.py” You can replace that with ANY linux command supported on that Pi OS. Enjoy!

thats awesome. ill work on that in a little bit. follow up question, can i create a virtual button that presses that button so i can schedule it through smartthings?

Of course, extend as you please. Keep in mind that you have the POLL and REFRESH functions where you leave their name alone but change the logic to do whatever you want. Then use a SmartApp called Pollster to run that Poll or Refresh every X.

Hey Folks. I’m attempting to use this setup with the 8266. I have the 8266 all setup correctly and can function the IO ports on that module without any issue using the webpage. I seem to be having problems with the device handler. The requests are coming into the webpage as I see them in the serial monitor but it doesnt seem to be posting the correct relay1=on / relay1=off in the request. If I hardcode the RELAY1=ON in the additional URL info, it will turn that pin on but will not turn it off (obviously).

My expertise in device handlers is probably pretty evident. Anyone able to give me any insight on what I’m missing or some direction?

Suggestions:

  1. Monitor SmartThings IDE > Logs and filter for your HTTP device. You can see full URL of what is being attempted via the HTTP device handler.
  2. Verify that you try a different HTTP method GET vs. POST… my default is POST for RaspberryPi/PHP while the Arduino/ESP8266 implementation is coded for a GET.

Thanks!

Great feedback. Thank you.

I did change to GET and was able to turn the pin ON. It doesnt register in the app as on and the debug shows the following line:

GET /MainTriggerOn=&MainPin=1&CustomPin=2 HTTP/1.1

I see where it pulls the main trigger HIGH. How come it never registers ON and wont let me turn it off.

Change it to a momentary in the Device Handler’s settings. You want to see /MainTriggerMomentary rather than /MainTriggerOn as the first parameter.

Thanks!

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

© 2019 SmartThings, Inc. All Rights Reserved. Terms of Use | Privacy Policy

SmartThings; SmartApps®; Physical Graph; Hello, Home; and Hello, Smart Home are all trademarks of the SmartThings, Inc.