How to ask a ESPeasy device of a GPIO state?
Moderators: grovkillen, Stuntteam, TD-er
How to ask a ESPeasy device of a GPIO state?
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?
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?
Re: How to ask a ESPeasy device of a GPIO state?
same question for me, is there a solution or cant espeasy send the state of GPIOs?
Re: How to ask a ESPeasy device of a GPIO state?
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"
noox: In device config check the "Send Data"
Minimal it works with OpenHub Protocol (almost generic MQTT).
Development versions has also "Send Boot state"
Re: How to ask a ESPeasy device of a GPIO state?
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
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
Re: How to ask a ESPeasy device of a GPIO state?
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.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.
See also http://www.esp8266.nu/index.php/Power
Re: How to ask a ESPeasy device of a GPIO state?
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;"
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
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
//*****************************************************************************************
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
Re: How to ask a ESPeasy device of a GPIO state?
This feature request was listed as #34 and build in R85.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?
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
Code: Select all
{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "output",
"state": 0
}
Code: Select all
{
"log": "",
"plugin": 1,
"pin": 2,
"mode": "PWM",
"state": 512
}
For mqtt, send the command as payload to <subscription template>/cmd
For mqtt, the result is send on <subscription template>/status
Re: How to ask a ESPeasy device of a GPIO state?
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.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)
Re: How to ask a ESPeasy device of a GPIO state?
I am trying to detect a gpio state using MQTT with but I get nothing back when changing the gpio state with mqtt.fx. Is that syntax right? Board is nodemcu running R105.
Code: Select all
/ESP01/status/gpio/14
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
Re: How to ask a ESPeasy device of a GPIO state?
[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.xbmcnut wrote:I am trying to detect a gpio state using MQTT withbut I get nothing back when changing the gpio state with mqtt.fx. Is that syntax right? Board is nodemcu running R105.Code: Select all
/ESP01/status/gpio/14
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
Re: How to ask a ESPeasy device of a GPIO state?
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.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
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
Re: How to ask a ESPeasy device of a GPIO state?
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
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
Re: How to ask a ESPeasy device of a GPIO state?
Thank you, that is awesome to know! I now get the following when I publish to /ESP01/cmd with gpio,14,0.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
Code: Select all
{
"log": "GPIO 14 Set to 0",
"plugin": 1,
"pin": 14,
"mode": "output",
"state": 0
}
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
Re: How to ask a ESPeasy device of a GPIO state?
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)
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.
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.
Re: How to ask a ESPeasy device of a GPIO state?
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 ... RqwZPCNbawxbmcnut 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.
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.
Re: How to ask a ESPeasy device of a GPIO state?
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.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.
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
Re: How to ask a ESPeasy device of a GPIO state?
I didn't knew that (or even worse: i don't know anything at all from MQTT yet
).

Re: How to ask a ESPeasy device of a GPIO state?
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
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
Re: How to ask a ESPeasy device of a GPIO state?
Hi Martinus,
I am testing the feature you have enhanced a few weeks ago with a similar lab setup as yours:
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
I am testing the feature you have enhanced a few weeks ago with a similar lab setup as yours:
but I believe I have found a bug somewhere in how the publish template is used somewhere in the code.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
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
Who is online
Users browsing this forum: No registered users and 18 guests