Problem with Sensor Switch with Domoticz

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
grazza
New user
Posts: 5
Joined: 14 Jun 2017, 12:45

Problem with Sensor Switch with Domoticz

#1 Post by grazza » 14 Jun 2017, 13:25

My goal is to use an ESP-01 module with a DS18B20 and a float switch for monitoring the temperature and water level in my aquarium.

At the moment I'm just at the breadboard stage trying to get everything working. The DS18B20 is working fine and communicating to Domoticz without drama. My issue is with the switch sensor.

I have the switch connected to GPIO-1 with a 10k pull-up resistor, the switch goes to ground through a 1k resistor to protect the pin in case it is accidentally driven high while the switch is active (because blowing this pin would effectively brick the device). Serial is disabled so I can use GPIO-1 as an IO. If I use an IDX/Var value that doesn't exist in Domoticz, and monitor the ESP module by refreshing the web browser, the switch works perfectly - the value changes from 0 to 1 and back to 0 as I would expect. When I look in the Log, I get entries of State:1 and State:0 as I would expect. Therefore I am confident that my hardware is fine, and the problem I'm having is a software issue.

My problem occurs when I then try to link the switch to Domoticz. I have a "Dummy" hardware device in Domoticz, to which I have added the virtual sensors - one for temperature, and one for the switch. As soon as I set the IDX/Var in ESP-Easy to match the switch ID in Domoticz I get the following behaviour:

Initially, both Domoticz and ESP-Easy will report the switch value as Off/0.
When I activate the switch, both Domoticz and ESP-Easy report the switch as On/1. So far so good.
The problem arises when I release the switch - Both Domoticz and ESP-Easy continue to report the switch as On/1. From this point onward, everything I do with the switch is ignored. ESP-Easy constantly reports 1, and Domoticz constantly reports the switch as on. If I look in the Log on ESP-Easy it reports a new entry of "State 1" each time I activate the switch, but never reports an entry of "State 0".
If I press the switch in Domoticz, it turns off, and then the ESP will report the switch as 0. From then on, it will always report the switch as 0, regardless of whether the switch is activated or not. Now, in the log, it reports a "State 0" every time I reset the switch, but never reports "State 1".
It's as if, once the ESP is connected to Domoticz, it completely ignores what the hardware says, and only believes what Domoticz says. Domoticz becomes in control instead of the hardware being in control.

I've tried to search forums etc to see how to set up this kind of thing, but either my search terms are wrong, or no-one else is trying to do anything similar. Is there something simple I'm doing wrong?

randytsuch
Normal user
Posts: 33
Joined: 05 Jun 2017, 05:32

Re: Problem with Sensor Switch with Domoticz

#2 Post by randytsuch » 14 Jun 2017, 20:45

I'm guessing it's because you're using GPIO1.

I just setup a ESP with Domticz for my spa, and am using a DS18B20 and some switches and relays with no problem.

Try a different IO pin, but stay away from GPIO0, 2 and 15. 0 and 2 have to be high at boot, and 15 low.

I am using GPIO4, 5, 12, 13, 14 and 16 for switches or relays.

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: Problem with Sensor Switch with Domoticz

#3 Post by Shardan » 14 Jun 2017, 20:56

randytsuch wrote: 14 Jun 2017, 20:45 I am using GPIO4, 5, 12, 13, 14 and 16 for switches or relays.
How should these be used with a ESP-01?
Regards
Shardan

randytsuch
Normal user
Posts: 33
Joined: 05 Jun 2017, 05:32

Re: Problem with Sensor Switch with Domoticz

#4 Post by randytsuch » 14 Jun 2017, 21:33

Shardan wrote: 14 Jun 2017, 20:56
randytsuch wrote: 14 Jun 2017, 20:45 I am using GPIO4, 5, 12, 13, 14 and 16 for switches or relays.
How should these be used with a ESP-01?
I don't know. Its why I only bought one ESP-01. I'd rather spend a few more dollars for a Wemos mini d1 and not deal with the ESP-01 limitations.

grazza
New user
Posts: 5
Joined: 14 Jun 2017, 12:45

Re: Problem with Sensor Switch with Domoticz

#5 Post by grazza » 14 Jun 2017, 23:58

Tried it temporarily on GPIO 3 & 0 with same behaviour. I don't think it is a hardware issue, because ESPEasy behaves perfectly when it is independent of Domoticz. The problem only arises once the ESP-Easy is set to talk to the Domoticz switch device. Once that happens it seems Domoticz becomes in control of ESPEasy instead of the hardware.

As for using an ESP-01 - I also have a mix of 07's, 12's, and 201's, but am using the 01 because it has adequate IO for what I want to do. It's one I've had in my parts bin since the days when the 01 was the only module readily available. I'm a believer in using what is suitable for the job, and the limited 01 will fill this task perfectly. I'll save my higher powered ESPs for other projects in the pipeline where I need more IOs. This project is a bit of a test case for other plans I have in mind.

grazza
New user
Posts: 5
Joined: 14 Jun 2017, 12:45

Re: Problem with Sensor Switch with Domoticz

#6 Post by grazza » 16 Jun 2017, 12:59

I've done a little more digging into this problem. Seems if the ESP is configured to talk to Domoticz with HTTP it works as expected. The problem only occurs with MQTT. Since it seems no-one can shed any light, I'll keep digging through the source to try to find the where the bug is.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Problem with Sensor Switch with Domoticz

#7 Post by grovkillen » 16 Jun 2017, 13:44

grazza wrote: 16 Jun 2017, 12:59 I've done a little more digging into this problem. Seems if the ESP is configured to talk to Domoticz with HTTP it works as expected. The problem only occurs with MQTT. Since it seems no-one can shed any light, I'll keep digging through the source to try to find the where the bug is.
You should investigate the MQTT messages. I use MQTT Snooper (Android) to listen to all topics. That way I can tell if the topics are correctly formatted.

This might guide you some: https://www.letscontrolit.com/wiki/inde ... th_own_IDX
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

grazza
New user
Posts: 5
Joined: 14 Jun 2017, 12:45

Re: Problem with Sensor Switch with Domoticz

#8 Post by grazza » 17 Jun 2017, 03:41

I have got somewhere with this by playing with the source. In file _P001_Switch.ino at line 324 is the following:

Code: Select all

        if (command == F("inputswitchstate"))
        {
          success = true;
          UserVar[event->Par1 * VARS_PER_TASK] = event->Par2;
          outputstate[event->Par1] = event->Par2;
        }
By commenting out the lines that start with "UserVar" and "outputstate", I was able to get the switch behaviour to work as expected. I don't know enough about what this bit of code does to know for sure what (if any) side effects this will have, but if Domoticz now changes the state of the switch, the ESPEasy will ignore it, and continue to react to the hardware.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: Problem with Sensor Switch with Domoticz

#9 Post by papperone » 17 Jun 2017, 08:46

I'm not familiar with Domoticz but I do use MQTT for my home automation system (then I've NodeRed and GUI build on it).
I do have plenty of "physical" switches (which are effectively pushbuttons!) and realys around the house and all works as expected.
Are you setting up any rules in ESPEasy in order to have the behaviour you need? If not then it's point less to go around the code and delete lines trying to figure out what's going on.
Yes issues is software but I'm 100% it's solvable using proper rules.

PS: can you confirm you are using a latching switch (on/off) or a push button?
PS2: can you post some pictures on how you configure your devices?

BTW: why the switch is pulled high? I connect always switch with pulldown resistor do GND e the other side to 3.3V as I need to have then HIGH when pressed.
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

grazza
New user
Posts: 5
Joined: 14 Jun 2017, 12:45

Re: Problem with Sensor Switch with Domoticz

#10 Post by grazza » 18 Jun 2017, 11:25

It sounds like you are drastically over-thinking the problem. Rules? I just want the ESP to report whether the switch is open or closed. Just like the temp sensor just reports what the temperature is. It isn't like a light switch, where it turns the light on depending on the switch OR the other software, it is just a simple On/Off sensor. The problem is that by default ESPEasy stops responding to the physical hardware. If there was a rule, it would be to report the value of the switch, which wouldn't work anyway, because ESPEasy starts ignoring the value of the switch.

The switch isn't a push-on/push-off, it is a basic momentary action switch.
EE095-D-5-1.jpg
EE095-D-5-1.jpg (53.7 KiB) Viewed 8768 times
When the float rises it turns the switch off, when the float sinks it turns the switch on - they are normally used to switch small pumps, so that it would fill up a trough if the water level fell. Internally I think it is just a reed switch, activated by a magnet in the float. As I said, all worked correctly when the ESPEasy was standalone, the failure only occurred when linked to Domoticz. My code change above effectively stops the ESPEasy responding to Domoticz commands to change an input-switch, and fixes the problem, but the root problem is, IMO a bug in ESPEasy where an input switch becomes over-ridden by external software.

The circuit is pretty straightforward...

V+
|
[10k Res]
|
+ ----[1k Res]-----> IO PIN
|
[Switch]
|
Ground

The 1K Res is just to limit current into the pin in the event of it accidentally being driven while the switch is closed (eg at boot).
IMG_2532.PNG
IMG_2532.PNG (83.8 KiB) Viewed 8768 times
(now on GPIO-3 after trying other IO ports and experiencing same behaviour)


Finally to answer your question about the use of pull-up vs pull-down. While in this case, using a pull-up vs pull-down and switching to ground vs VCC makes little practical difference, it is in general, good engineering practice to use pull-ups and switching to ground for a number of reasons and is therefore my default design unless there is some specific reason to do otherwise. When switching to ground, the switch can easily be replaced by an open-collector NPN, or open-drain N-channel FET. Furthermore, in that scenario, the driving voltage of the open-collector circuit can be different (eg 5V), and distinct from the uC supply voltage. While a PNP or P-Channel FET can be used in a pull-down/switched + scenario, the designs become more complicated - especially with FETs where the driving voltage might have to go below 0V to ensure switch-on. But this is getting very off-topic.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Problem with Sensor Switch with Domoticz

#11 Post by grovkillen » 18 Jun 2017, 12:17

But you have not reported what you find in the MQTT message. I believe​ that the sent MQTT message is causing the mismatch.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Post Reply

Who is online

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