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

Hey JZ, I’m still having problems using smartthings to execute commands to my nodemcu. I can trigger things using the webpage or a curl command, but in smartthings, nothing. In the live logging, I get this -
`9:20:13 PM: debug GET /RELAY2=Momentary HTTP/1.1
Accept: /
User-Agent: Linux UPnP/1.0 SmartThings
HOST: 192.168.6.209:80
Content-Type: application/x-www-form-urlencoded

134c0920-1061-48bf-96ba-c1e417953234 9:20:13 PM: debug The method is GET
134c0920-1061-48bf-96ba-c1e417953234 9:20:13 PM: debug GET path is: /RELAY2=Momentary
134c0920-1061-48bf-96ba-c1e417953234 9:20:13 PM: debug Uses which method: GET
134c0920-1061-48bf-96ba-c1e417953234 9:20:13 PM: debug The Header is [HOST:192.168.6.209:80, Content-Type:application/x-www-form-urlencoded]
134c0920-1061-48bf-96ba-c1e417953234 9:20:13 PM: debug The device id configured is: C0A806D1:0050`

Which I assume looks normal. The nodemcu does nothing in response. Oddly enough, the contact sensor part is working. All network settings are correct, I’m set to GET, and there’s no auth settings. I’ve even tried different nodemcu’s. Any ideas?

I think so… look at your GET path… that’s not correct because the word MOMENTARY is supposed to be capitalized (and really only used when you use the webpage of the NodeMCU, via ST, I just use my older get strings like /CustomTrigger= which the NodeMCU also understands). Which all suggests to me that you’re not using the default ST Groovy device handler that I distribute or you manually changed the path. Notice the difference below of what the path is supposed to look like if you leave all stock defaults.

Last but not least try this path from a browser: http://192.168.6.209/CustomTrigger=

7:13:31 PM: debug whichTile: mainoff
7:13:31 PM: debug [Date:2017-05-18 7:13:31 PM, CustomTrigger:Success, UpTime:00:03:45, Free Mem:34KB of 80KB, SensorPinStatus:Closed, Sensor2PinStatus:Closed, MainPinStatus:0, CustomPinStatus:0]
7:13:29 PM: debug GET /CustomTrigger= HTTP/1.1
	Accept: */*
	User-Agent: Linux UPnP/1.0 SmartThings
	HOST: 192.168.0.241:80
	Content-Type: application/x-www-form-urlencoded

7:13:29 PM: debug The method is GET
7:13:29 PM: debug GET path is: /CustomTrigger=
7:13:29 PM: debug Uses which method: GET
7:13:29 PM: debug The Header is [HOST:192.168.0.241:80, Content-Type:application/x-www-form-urlencoded]
7:13:29 PM: debug The device id configured is: C0A800F1:0050 

That hopefully fixes you up.

You are the man! That was the problem, of course it was something simple like that. Yea, I’ve made a custom device handler from yours to better fit my use case.

What an awesome work @JZst

i’m a noob reagrding smarthings and above all a noob with the nodemcu, even so i was able to do this easily and make it work flawlessly, with very little programing knowloedge, and man this is so cool

Now i’m thinking that i could use this to ease my life a lot more, for instance, my irrigation controler broke so for now i have to go to the electrovalves and manually turn it on, zone by zone, tedious and boring i know, but summer is coming

I have a 4 zone irrigation control so i was thinking that i could hook a 4 channel relay to the electrovalves a ESP and control it within smartthings. i still have to learn how to modify your code to be able to control a 4 channel relay, so any hint you could me give to start my learning process?

and thanks for this again, awesome work :clap: :+1:

Thanks! It’s a pet project :slight_smile: requiring care & feeding every now & again.

Check out this project and search Google for SmartThings, sprinkler, irrigation: ESP8266 Smart Sprinkler System

My recommendation is to actually have two NodeMCU modules because they’re cheap. The reason for TWO is because you want to alleviate yourself from maintaining your own version of my code and just use my prepared stock code from here to eternity. For $3.50 on fleebay, it’s a simple choice. Otherwise you’ll be copying quite a bit of logic from me which is not simple or easy. Remember that for automating (like turning it on/off at certain times like irrigation systems require) SmartThings is limited to only automating the main “capability”, switch in our case… and hence the reason why I needed to create a SmartApp to synchronize the secondary switch to a virtual/fake one. Otherwise no automation worked. In your case, you’d do that for 3 separate switches. Not easy at all…

Lastly, don’t be too cheap :slight_smile: as you’ll pay for it all in your time investment & long-term maintenance. I love my RainMachine Mini8. It was super-simple to install & run. I’m very happy with it and how smart it is adjusting water amount based on local weather/rainfall/humidity, etc.

1 Like

@Perks I’m using this already integrated ESP 4-channel relay https://www.amazon.com/dp/B01FVJ8XSU/ref=sr_1_2?ie=UTF8&qid=1495801475&sr=8-2&keywords=linksprite+r4 for my 4 zone sprinkler system. $10 for a Smart Sprinkler system with minimal assembly required! :slight_smile:

There is a device handler for it and it’s 8-relay big brother on the ESP8266 Smart Sprinkler thread @JZst referred to.

1 Like

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