How to ask a ESPeasy device of a GPIO state?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Stefan
Normal user
Posts: 1
Joined: 29 Nov 2015, 14:34

How to ask a ESPeasy device of a GPIO state?

#1 Post by Stefan » 29 Nov 2015, 15:51

Hi
Is there an API where I can ask for the state of a GPIO?
I've found the command for setting a GPIO: http://<ESP IP address>/control?cmd=GPIO,<pin>,1
Is there another command for getting it back?

noxx
Normal user
Posts: 6
Joined: 23 Feb 2016, 14:26

Re: How to ask a ESPeasy device of a GPIO state?

#2 Post by noxx » 27 Mar 2016, 15:39

same question for me, is there a solution or cant espeasy send the state of GPIOs?

wutu
Normal user
Posts: 35
Joined: 05 Feb 2016, 07:33

Re: How to ask a ESPeasy device of a GPIO state?

#3 Post by wutu » 30 Mar 2016, 00:58

Stefan: You can read from http://ESPEasy/json

noox: In device config check the "Send Data"
Minimal it works with OpenHub Protocol (almost generic MQTT).
Development versions has also "Send Boot state"

bigal
Normal user
Posts: 4
Joined: 17 Mar 2016, 21:00

Re: How to ask a ESPeasy device of a GPIO state?

#4 Post by bigal » 08 Apr 2016, 01:38

I am in the same boat, needing to be sure the state of ESPEasy O/Ps.
I have trouble where I am trying to control a Boiler from Domoticz (http), but some days I find the boiler runs buy itself.
For example currently the ESPEasy device shows an uptime of 362minutes meaning it must have rebooted itself just over 5 hours ago, and the output controlling the boiler went low, which switched on the boiler.

Not sure I can understand your reply Wutu:
I have latest stable build 78 but I do not see any "send data".
If it is in a development version can you explain what it does?
Does it work with http or does it require MQTT protocol to work?
Also I do not understand what you mean by "http://ESPEasy/json". Is this done from the controller to check the ESPEasy output state?
If so can you provide and example please?

If I am wasting my time with Domoticz http please advise, happy to experiment with a different controller.


Al

costo
Normal user
Posts: 490
Joined: 21 Nov 2015, 15:03

Re: How to ask a ESPeasy device of a GPIO state?

#5 Post by costo » 08 Apr 2016, 17:40

bigal wrote: For example currently the ESPEasy device shows an uptime of 362minutes meaning it must have rebooted itself just over 5 hours ago, and the output controlling the boiler went low, which switched on the boiler.
If you experience frequent reboots you may have stability problems with your ESP module. If you run a ESP07/12 then you should solder a 10nF capacitor very close over the reset pin and also you can add a good capacitor, like a tantalium , over the Vcc very close to the ESP module.
See also http://www.esp8266.nu/index.php/Power

namirda
Normal user
Posts: 53
Joined: 22 Jan 2016, 17:09

Re: How to ask a ESPeasy device of a GPIO state?

#6 Post by namirda » 09 Apr 2016, 23:33

This is a question which has been asked a number of times. I see from the 'ESPEasy Feature Wish List' compiled by Martinus

http://www.esp8266.nu/forum/viewtopic.php?t=736 (item 39)

that this will maybe not happen soon. In the meantime, you might like to try this workaround which works great for me.

Paste the following code snipit into routine _C005.ino at line 56 immediately after the line "struct EventStruct TempEvent;"

Code: Select all

//	******************************************************************************************************

//	Beginning of lines inserted by Namirda

//  In addition to the MQTT input facilities provided by the official release, I wanted  :

		//	a)	to be able to prompt a specific plugin for a value		/%sysname%/%tskname%   with payload = ?
		//	b)	to allow MQTT system commands like reboot and reset		/%sysname%/System	   with payload=command

//	The lines inserted below accomplish this

//		First check for a system message of the form /%sysname%/System - if found then try to execute the payload as command
		
		if (topicSplit[1].equalsIgnoreCase("System")) {
			addLog(LOG_LEVEL_INFO,"SYS  : System Command received");
			ExecuteCommand(VALUE_SOURCE_MQTT, event->String2.c_str());
			break;
		}
		
//		Now we check to see if this message is directed at a particular task
			
		byte TaskIndex = getTaskIndex(topicSplit[1]);									// Get TaskIndex from Task Name
		if (TaskIndex != 255) {													

//      We have a task match - now we must construct a temp event for this task

			byte DeviceIndex = getDeviceIndex(Settings.TaskDeviceNumber[TaskIndex]);	// Used to call the plugin

			TempEvent.TaskIndex = TaskIndex;
			TempEvent.BaseVarIndex = TaskIndex * VARS_PER_TASK;;
			TempEvent.idx = Settings.TaskDeviceID[TaskIndex];
			TempEvent.sensorType = Device[DeviceIndex].VType;
			TempEvent.String1 = event->String1;											// Topic of the message
			TempEvent.String2 = event->String2;											// Payload

//		If the payload is ? then read the device

			if (event->String2 == "?")
			{
				String log = F("REQ  : Data Request received for Task ");
				log += ExtraTaskSettings.TaskDeviceName;
				addLog(LOG_LEVEL_INFO, log);

				Plugin_ptr[DeviceIndex](PLUGIN_READ, &TempEvent, topicSplit[2]);    // Update the latest values
				sendData(&TempEvent);                                               // Send the latest values
				success = true;
				break;
			}		
		}

//	End of lines inserted by Namirda.

//	If the MQTT command was not handled by the above code then it is passed back to offical code
//*****************************************************************************************

You will also need to download my file Extra_Utilities.ino from the playground https://github.com/ESP8266nu/ESPEasyPluginPlayground and add it to your project before you recompile.

This code introduces two useful features

a) It allows you to specifically request output values from a plugin rather than wait for a timer or for a contact state to change. For example, if you have a switch named Contact1 then if you send an MQTT message with topic /ESP01-IN/Contact1 with payload ? then the plugin Contact1 will send its status whether it has changed recently or not. Similarly, if you have a DHT22 device with name MYDHT22 then if you send an MQTT message with topic /ESP01-IN/MYDHT22 and payload ? then the DHT22 will read temperature and humidity and send them. In this way it is possible to have your esp modules send data only on request rather than regularly on a timer. Another thing I like about it is that you can request output data by name rather than having to remember which GPIO they are connected to.

b) It allows you to send system commands as an alternative to using the webserver. For example, if you send the MQTT command /ESP01-IN/System with the payload 'Reboot' then your esp will reboot. There are many useful things you can do with this if you think about it for a while.

Note that in the examples above, I have used the topic /ESP01-IN/... Please avoid having your MQTT client subscribe to the same topics that it publishes to - talking to yourself is a sign of insanity! I generally subscribe to /ESP01-IN and publish to /ESP01 - just make sure that the two are different or things get confusing! This can be setup in the Tools/Advanced Settings of the Webserver :

Subscribe Template /%sysname%-IN/#
Publish Template /%sysname%/%tskname%/%valname%

I hope the above works for you - it does for me. Please let me know how you get on with it.

N

Martinus

Re: How to ask a ESPeasy device of a GPIO state?

#7 Post by Martinus » 10 Apr 2016, 09:31

Stefan wrote:Hi
Is there an API where I can ask for the state of a GPIO?
I've found the command for setting a GPIO: http://<ESP IP address>/control?cmd=GPIO,<pin>,1
Is there another command for getting it back?
This feature request was listed as #34 and build in R85.

There's a 'status' command for plugins that handle GPIO's and it returns a json format pinstate

Sample:

Code: Select all

status,gpio,2
results into:

Code: Select all

{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}
It also works for PWM settings and it does not depend on having a specific task. If you have set a certain IO pin with a remote command, you should be able to retrieve status with the status command. So after using "pwm,2,512", running "status,gpio,2" results into:

Code: Select all

{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "PWM",
"state": 512
}
It's a system wide command and should work on serial, http and mqtt.
For mqtt, send the command as payload to <subscription template>/cmd
For mqtt, the result is send on <subscription template>/status

Martinus

Re: How to ask a ESPeasy device of a GPIO state?

#8 Post by Martinus » 10 Apr 2016, 09:38

namirda wrote:This is a question which has been asked a number of times. I see from the 'ESPEasy Feature Wish List' compiled by Martinus

http://www.esp8266.nu/forum/viewtopic.php?t=736 (item 39)
To avoid confusion, #39 is about "Switch input" devices that currently send their state on changes only. R98 will address this in a way that you can optionally set a send interval (delay) also on switch devices (internal GPIO, MCP23017 and PCF8574). At the same time, the "send at boot" option will be added to MCP and PCF plugins.

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#9 Post by xbmcnut » 02 Aug 2016, 04:49

I am trying to detect a gpio state using MQTT with

Code: Select all

/ESP01/status/gpio/14
but I get nothing back when changing the gpio state with mqtt.fx. Is that syntax right? Board is nodemcu running R105.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#10 Post by xbmcnut » 08 Aug 2016, 10:44

xbmcnut wrote:I am trying to detect a gpio state using MQTT with

Code: Select all

/ESP01/status/gpio/14
but I get nothing back when changing the gpio state with mqtt.fx. Is that syntax right? Board is nodemcu running R105.
[SOLVED] I simply added a switch and used the same gpio number as the one I'm controlling with MQTT messages. So I now I get the gpio status back on /ESP01/relay14/state with either a 0 or 1.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#11 Post by xbmcnut » 10 Aug 2016, 00:30

Martinus wrote:
Stefan wrote:
It's a system wide command and should work on serial, http and mqtt.
For mqtt, send the command as payload to <subscription template>/cmd
For mqtt, the result is send on <subscription template>/status
Could you possibly clarify the MQTT command for obtaining a GPIO status? You mention <subscription template>/status but in my case if I subscribe to /ESP01/status/#, nothing is ever received. Tried with R108.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

Martinus

Re: How to ask a ESPeasy device of a GPIO state?

#12 Post by Martinus » 10 Aug 2016, 09:30

Testlab setup:

ESP name: ESPDemo
Template: /%sysname%/#

Publish gpio,2,0 to /ESPDemo/cmd

Receiving this on /ESPDemo/status:

{
"log": "GPIO 2 Set to 0",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}



Publish status,gpio,2 to /ESPDemo/cmd

Receiving this on /ESPDemo/status:

{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}


Every command that's documented for HTTP should also work if send to .../cmd topic using MQTT.
Instead of returning a web page, it reports back to ..../status

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#13 Post by xbmcnut » 10 Aug 2016, 11:27

Martinus wrote:Testlab setup:

Every command that's documented for HTTP should also work if send to .../cmd topic using MQTT.
Instead of returning a web page, it reports back to ..../status
Thank you, that is awesome to know! I now get the following when I publish to /ESP01/cmd with gpio,14,0.

Code: Select all

{
"log": "GPIO 14 Set to 0",
"plugin": 1,
"pin": 14,
"mode": "output",
"state": 0
}
Home Assistant however is expecting either a 1 or 0 from its MQTT components so I'm trying to just get a status back with that data and nothing else. Is that possible? You mention sending MQTT ...cmd (which is what I did above) but I got the json response back. I see I may be able to use the 'value_template' to pull data from the json payload but that's getting a bit over my head.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

JR01
Normal user
Posts: 252
Joined: 14 Feb 2016, 21:04
Location: South Africa
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#14 Post by JR01 » 10 Aug 2016, 12:08

Thank you Martinus ! Now I understand how it works, was not sure about the /cmd part on how to use it.

I prefer the json file format, its the standard moving forward in the industry, and give me flexibility on what I can do with it. It also allows our ESPEasy code and framework not to be bloated with different code for what each individual expect the output needs to be, but that ons does that in the integration layer.

Regarding only expecting 0/1 back from the previous post, I use node-red with mqtt, and thus easy with a function node, after receiving the mqtt message to transform the json to say a zero or 1. I know the other platforms like OpenHab / Domostisz etc can also do it (I do not have experience with it though, but read up how this works).

Thank you Martinus again, this is a huge step forward! (for me - to cover the full arch)
-----------
IOTPLAY. Tinkerer, my projects are @ http://GitHub.com/IoTPlay, and blog https://iotplay.org. Using RPi, Node-Red, ESP8266 to prove Industry 4.0 concepts.

User avatar
ThinkPad
Normal user
Posts: 5
Joined: 20 Jun 2016, 12:04

Re: How to ask a ESPeasy device of a GPIO state?

#15 Post by ThinkPad » 10 Aug 2016, 22:59

xbmcnut wrote: [...]
Home Assistant however is expecting either a 1 or 0 from its MQTT components so I'm trying to just get a status back with that data and nothing else. Is that possible? You mention sending MQTT ...cmd (which is what I did above) but I got the json response back. I see I may be able to use the 'value_template' to pull data from the json payload but that's getting a bit over my head.
This doesn't has to be very difficult, have a look at this where they create a template to do this: https://groups.google.com/forum/#!topic ... RqwZPCNbaw
See also https://home-assistant.io/components/switch.mqtt/ for more information about a MQTT in HA.

Very interesting stuff btw. I have Home Assistant running for a little more than a week now (mostly with Z-Wave stuff) but ESPEasy is a very nice addition i think. Especially with boards like the Wemos D1 Mini which already have the necessary components on board (voltage converter, USB-Serial etc). Haven't done anything with MQTT yet, i think i will need to have a look at it sometime, seems interesting.

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#16 Post by xbmcnut » 11 Aug 2016, 00:13

ThinkPad wrote:
xbmcnut wrote:
This doesn't has to be very difficult, have a look at this where they create a template to do this: https://groups.google.com/forum/#!topic ... RqwZPCNbaw
See also https://home-assistant.io/components/switch.mqtt/ for more information about a MQTT in HA.
This seems significantly more difficult than just adopting the MQTT standard for polling the broker for the 'retain' flag which is how the Home Assistant core is working with their MQTT components. This is the problem I'm having with ESPEasy. I've configured a switch on the same gpio as the one I'm controlling using /ESP01/gpio/14 (0 or 1) and now get a reply back on /ESP01/relay14/state but this message does not contain the retain flag. This all works fine and dandy until Home Assistant is restarted and it polls the broker for the last know state. Without the retain flag, the switch status always drops back to 0 or off.
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

User avatar
ThinkPad
Normal user
Posts: 5
Joined: 20 Jun 2016, 12:04

Re: How to ask a ESPeasy device of a GPIO state?

#17 Post by ThinkPad » 11 Aug 2016, 20:19

I didn't knew that (or even worse: i don't know anything at all from MQTT yet :mrgreen: ).

xbmcnut
Normal user
Posts: 49
Joined: 17 Mar 2016, 08:49
Location: Auckland, NZ
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#18 Post by xbmcnut » 12 Aug 2016, 14:44

This is now working as the retain flag option has been added in R121 and it works a treat.

http://www.esp8266.nu/forum/viewtopic.p ... 8545#p8545
Numerous Sonoff's and Shelly's, Home Assistant on Intel NUC i3, Aeotec Z-Stick (Gen 5), deCONZ Zigbee Stick, 3 x Echo Dot's and 5 x Google Home

User avatar
lucaberta
Normal user
Posts: 59
Joined: 09 May 2016, 11:26
Location: Lausanne, Switzerland
Contact:

Re: How to ask a ESPeasy device of a GPIO state?

#19 Post by lucaberta » 24 Aug 2016, 00:41

Hi Martinus,

I am testing the feature you have enhanced a few weeks ago with a similar lab setup as yours:
Martinus wrote:Testlab setup:

ESP name: ESPDemo
Template: /%sysname%/#

Publish gpio,2,0 to /ESPDemo/cmd

Receiving this on /ESPDemo/status:

{
"log": "GPIO 2 Set to 0",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}


[...]

Every command that's documented for HTTP should also work if send to .../cmd topic using MQTT.
Instead of returning a web page, it reports back to ..../status
but I believe I have found a bug somewhere in how the publish template is used somewhere in the code.

As I have decided to NOT have a leading slash in my template, nothing was working. When I added the slash in the Advanced tab, and changed the MQTT paths accordingly, things started to work.

I did not have any time to check the code of R123 which is what I am using now, but I suspect that somewhere in the code lies an hardcoded slash where it shouldn't be...

I'll do some checking tomorrow. Meanwhile if you have any idea of where the problem lies, let me know!

MvG, Luca

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 33 guests