Serial MCU controlled relay/switch

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Serial MCU controlled relay/switch

#1 Post by enesbcs » 20 Jun 2017, 01:36

Originally I have developed and tested a plugin for Yewelink/Tuya WiFi Touch wall light switch, but after some time i made it compatible with other serial controlled devices.
Serial controlled devices works with the ESP RX/TX pins connected to a secondary MCU, and this MCU does the real work, ESP only forwards the ON/OFF commands. Tuya protocol is 2way (so reports back their status), other devices seems to be 1way only.
Tuya serial API
Tuya downloads

Compatible device list:
1/Tuya Wifi Touch wall switch (originally controlled by Tuya Smart/Smart Life app)
2/Tuya Wifi Dimmer Switch (originally controlled by Tuya Smart/Smart Life app)
3/ Sonoff Dual v1 only! (R2 has no serial MCU!)
4/ LCTECH compatible 5V WiFi relay 1,2 and 4 relay versions also supported.

Tested with Arduino 1.8.3 and ESPEasy Mega 2.0.0-dev10 and dev11.
https://github.com/enesbcs/ESPEasyPlugi ... Switch.ino

To succesfully compile this plugin:
- use Arduino core for ESP8266 WiFi chip 2.4.0 (Serial.setRxBufferSize – with default buffer size 33 byte length status update packages got defective)

Proposed Setup
Tools->Advanced->Serial log Level: 0
Tools->Advanced->Enable Serial port: enabled
Tools->Advanced->Rules:enabled

Add one "Serial MCU controlled switch" device with multiple Switch input devices.

Select switch type, Disable "Send to Controller" because every relay needs different IDX in Domoticz, so alone this component will never works.
Set number of relays to buttons on the switch. (Tuya MCU may sends non-existent button states also)
Other settings optional.

For Sonoff Dual i used the following rules:

on sonoff#Relay0 do
Publish domoticz/in,{"idx":100,"nvalue":[sonoff#Relay0],"svalue":""}
endon
on sonoff#Relay1 do
Publish domoticz/in,{"idx":101,"nvalue":[sonoff#Relay1],"svalue":""}
endon

sonoff is the main plugin name (Task 1), Relay0 is the first logical relay, addressed 0 by the Dual, but some reason it is physically the 'out2' relay. Relay1 is the second logical button, addressed 0, and interestingly it is the relay named 'out1'.
The above rules reports back the change of the states to the Domoticz, with idx 100 & 101, or anything that you write here.
There are GPIO connector's inside the box for button0 and button1, from plugin v1.3 it's supported, and also the WiFi button status is monitored as the third 'virtual' relay.
The Tuya/Yewelink touch wall light switch on the other hand also reports back it's status when operated manually by it's physical buttons, I've tested 1 and 2 gang models. Tuya dimmer also tested by digiblur. (Thanx for debugging!)

In Domoticz virtual switches the following ON/OFF commands can be used:

The ON command for Relay0:
http://ESPEASY_IP/control?cmd=relay,0,1

The OFF command for Relay0:
http://ESPEASY_IP/control?cmd=relay,0,0

The ON command for Relay1:
http://ESPEASY_IP/control?cmd=relay,1,1

The OFF command for Relay1:
http://ESPEASY_IP/control?cmd=relay,1,0

Download from:
Source from Github
Binary downloads attached for convenience. (4M for Tuya and 1M for Tuya Dimmer/Sonoff Dual/LCTech, ESPEasy source from github)
update: Latest Puya binary
Attachments
ESPEasy_P165_24.zip
ESPEasy Mega with P165 core 2.4.0 (current)
(748.63 KiB) Downloaded 1984 times
P165_22_P.zip
ESPEasy Mega with P165 SerSwitch plugin - PUYA safe versions and the stable R147 for ESP-01S
(1.01 MiB) Downloaded 1764 times
ESPEasy_p165_14_rc2.zip
ESPEasy Mega 2.0 with P165 SerSwitch plugin + 2.4.0rc2 (previous)
(694.71 KiB) Downloaded 2148 times
Last edited by enesbcs on 23 Nov 2018, 17:36, edited 25 times in total.

the cosmic gate
Normal user
Posts: 102
Joined: 14 Nov 2015, 20:17

Re: Serial MCU controlled relay/switch

#2 Post by the cosmic gate » 20 Jun 2017, 15:38

can you maybe upload the compiled *.bin ? So i can easy test this on the Sonoff dual ?

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#3 Post by enesbcs » 20 Jun 2017, 21:14

the cosmic gate wrote: 20 Jun 2017, 15:38 can you maybe upload the compiled *.bin ? So i can easy test this on the Sonoff dual ?
I compiled it for Generic ESP8266, 4M/1M SPIFFS, attached.
Update: I provided 1M version also, Sonoff Dual ESP is 1M/128k SPIFFS according to xoseperez, Tuya is 4M/1M according to me select wisely before flash. :)
Last edited by enesbcs on 26 Jun 2017, 23:41, edited 1 time in total.

the cosmic gate
Normal user
Posts: 102
Joined: 14 Nov 2015, 20:17

Re: Serial MCU controlled relay/switch

#4 Post by the cosmic gate » 21 Jun 2017, 12:34

enesbcs wrote: 20 Jun 2017, 21:14
the cosmic gate wrote: 20 Jun 2017, 15:38 can you maybe upload the compiled *.bin ? So i can easy test this on the Sonoff dual ?
I compiled it for Generic ESP8266, 4M/1M SPIFFS, attached.
Update: I provided 1M version also, Sonoff Dual ESP is 1M/128k SPIFFS according to xoseperez, Tuya is 4M/1M according to me select wisely before flash. :)
Nice , thnx will flash this and test it on a dual

the cosmic gate
Normal user
Posts: 102
Joined: 14 Nov 2015, 20:17

Re: Serial MCU controlled relay/switch

#5 Post by the cosmic gate » 24 Jun 2017, 19:55

just a short / simple update . Flashed the dual this this custom firmware ;
The configurataion is something like:
Image

Image

But for now :
when i want to switch the relais with this commands:
The ON command:
http://ESPEASY_IP/control?cmd=relay,1,1
the one of the relais 2 is changing state nicely, and the LED is turning green
when i try to trun it off:
http://ESPEASY_IP/control?cmd=relay,1,0
nothing happens.

so i'll tried some other combinationms
http://ESPEASY_IP/control?cmd=relay,3,1
and then the state from relais A, and the LED is turning red

So the "ON" command seems to work, but the "OFF" doensn't seem to work .
But when i need to switch on / off i use for relais 2
on : http://ESPEASY_IP/control?cmd=relay,1,1
Off : http://ESPEASY_IP/control?cmd=relay,3,1

And didn;t find this combination for the 2nd / other relais ;(
Did i do someting wrong ,or is there something wrong in the plugin maybe ?

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#6 Post by rira2005 » 26 Jun 2017, 07:25

Hello

Many Thanks für this Plugin! Works but not Pefect.

Have the same Problem as CosmicGate with the Plugin and my Sonoff Dual!
May you can check you Plugin once more.

TIA
Raphael

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#7 Post by enesbcs » 26 Jun 2017, 23:37

Thank you for your feedback, i find extremely hard to debug any ESP code... especially without a Sonoff Dual. :)
Relays are numbered from zero, so Relay0 is the first and Relay1 is the second, Relay2 is the third - which is not applicable for Sonoff Dual, only for Tuya switch.
I renamed them in the plugin to avoid confusion.

I write sent command codes according to Markus Maeder description.

So the first relay can be controlled like this:
http://ESPEASY_IP/control?cmd=relay,0,1
http://ESPEASY_IP/control?cmd=relay,0,0
the second can be controlled like this:
http://ESPEASY_IP/control?cmd=relay,1,1
http://ESPEASY_IP/control?cmd=relay,1,0

If the OFF command did not work, than it is possible that the plugin is unable to read the status of the relay, because switching only executed when the previous state is different, than the asked state of the relay. Now i patched it hopefully..

If something not going as planned, please copy&paste the ESPEasy log.

I compiled a new version for 1M/128k SPIFFS to test, attached.
Last edited by enesbcs on 20 Aug 2017, 18:08, edited 1 time in total.

the cosmic gate
Normal user
Posts: 102
Joined: 14 Nov 2015, 20:17

Re: Serial MCU controlled relay/switch

#8 Post by the cosmic gate » 27 Jun 2017, 21:32

enesbcs wrote: 26 Jun 2017, 23:37 Thank you for your feedback, i find extremely hard to debug any ESP code... especially without a Sonoff Dual. :)
Relays are numbered from zero, so Relay0 is the first and Relay1 is the second, Relay2 is the third - which is not applicable for Sonoff Dual, only for Tuya switch.
I renamed them in the plugin to avoid confusion.

I write sent command codes according to Markus Maeder description.

So the first relay can be controlled like this:
http://ESPEASY_IP/control?cmd=relay,0,1
http://ESPEASY_IP/control?cmd=relay,0,0
the second can be controlled like this:
http://ESPEASY_IP/control?cmd=relay,1,1
http://ESPEASY_IP/control?cmd=relay,1,0

If the OFF command did not work, than it is possible that the plugin is unable to read the status of the relay, because switching only executed when the previous state is different, than the asked state of the relay. Now i patched it hopefully..

If something not going as planned, please copy&paste the ESPEasy log.

I compiled a new version for 1M/128k SPIFFS to test, attached.
I don't know if the code is compared : https://www.letscontrolit.com/forum/vie ... 969#p17196 But here is oneother sonoff dual user with a self made plugin . (maybe some copy paste work ?

If i flashed the dual with this firmware ESPurna https://bitbucket.org/xoseperez/espurna then i can control the relais without any problem and they work fine.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#9 Post by enesbcs » 28 Jun 2017, 20:01

the cosmic gate wrote: 27 Jun 2017, 21:32 I don't know if the code is compared : https://www.letscontrolit.com/forum/vie ... 969#p17196 But here is oneother sonoff dual user with a self made plugin . (maybe some copy paste work ?
It seems a similar and simpler approach, which differs from mine, because my priority is the Tuya switch that i have, and by the way it can send simple serial commands, that Dual also uses. And i am sure that there are some other ESP controlled devices in the wild, that driven by serial/UART commands, and a common plugin may be useful in the future.
the cosmic gate wrote: 27 Jun 2017, 21:32 If i flashed the dual with this firmware ESPurna https://bitbucket.org/xoseperez/espurna then i can control the relais without any problem and they work fine.
ESPurna is a very nice and complex software, but differs from ESPEasy a lot.

the cosmic gate
Normal user
Posts: 102
Joined: 14 Nov 2015, 20:17

Re: Serial MCU controlled relay/switch

#10 Post by the cosmic gate » 28 Jun 2017, 20:31

this sketch written bij Bryan is working nice https://www.letscontrolit.com/forum/vie ... 258#p17196

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#11 Post by rira2005 » 05 Jul 2017, 08:35

HI

the new image(for the sonoff dual) is working, relays are set on/off!
Perfect work, many thanks.

but i get no status in domoticz, if i set the relay manally (with the the
ON command:
http://ESPEASY_IP/control?cmd=relay,1,1
The OFF command:
http://ESPEASY_IP/control?cmd=relay,1,0)
commands...
tried it with the extra switch trick. but didnt works.
any idea?

What can i do to help you?

tia
raphael

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#12 Post by enesbcs » 05 Jul 2017, 12:41

rira2005 wrote: 05 Jul 2017, 08:35 HI
the new image(for the sonoff dual) is working, relays are set on/off!
...
but i get no status in domoticz, if i set the relay manally (
Thank you for your feedback.

Did you create the rules? Please be careful with the relay names, first relay is "Relay0"
If you named your SerSwitch module for example to "sonoff", it's 1st relay named "Relay0", 2nd relay named "Relay1", the 1st dummy switch domoticz idx=2, the 2nd dummy switch idx=3, the following rules needed:

on sonoff#Relay0 do
Publish domoticz/in,{"idx":2,"nvalue":[sonoff#Relay0],"svalue":""}
endon

on sonoff#Relay1 do
Publish domoticz/in,{"idx":3,"nvalue":[sonoff#Relay1],"svalue":""}
endon

Sadly i do not find any other way, to use multiple domoticz idx numbers from one plugin - to do MQTT publishing. Explicit "Publish" command can only be used from rules, if i understand correctly the ESPEasy documentation.

In theory Sonoff Dual reports it's state to ESP with the same serial packets, as the ESP sent to it. Could you please copy from Tools->Log what is happening exactly when you do switching? If Sonoff reports back, the log will be: "SW: State r0:0" or "SW: State r0:1". If only the set command logged, the log will be: "SW: SetSwitch r0,0" or "SW: SetSwitch r0,1".

In case that the "State" log line do not show, i attach an updated image with an extra Senddata line after SetSwitch, which - in theory - will trigger the Relay0/Relay1 changes to the rule engine.
Last edited by enesbcs on 20 Aug 2017, 18:09, edited 1 time in total.

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#13 Post by rira2005 » 18 Jul 2017, 22:51

Hello

Sorry for my late answer was an vacation. Soon testet your last image.
same as before.

my Rules :
on sonoff#Relay0 do
Publish domoticz/in,{"idx":48,"nvalue":[sonoff#Relay0],"svalue":""}
endon

on sonoff#Relay1 do
Publish domoticz/in,{"idx":49,"nvalue":[sonoff#Relay1],"svalue":""}
endon

in domoticz named it "relay0" with idx 48 and "relay1" with idx 49.

with mqttlens i see NO pubilsh messages when i switch the relay0 or relay1 on/off!

the log reports :
2698474 : EVENT: sonoff#Relay1=0.00
2698486 : ACT : Publish domoticz/in,{"idx":49,"nvalue":0,"svalue":""}
2698504 : EVENT: sonoff#Relay2=0.00
2698531 : SW : SetSwitch r0:1
2701637 : EVENT: sonoff#Relay0=0.00
2701644 : ACT : Publish domoticz/in,{"idx":48,"nvalue":0,"svalue":""}
2701668 : EVENT: sonoff#Relay1=0.00
2701679 : ACT : Publish domoticz/in,{"idx":49,"nvalue":0,"svalue":""}
2701697 : EVENT: sonoff#Relay2=0.00
2701725 : SW : SetSwitch r0:0

any idea?

Many thanks
Raphael

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#14 Post by rira2005 » 18 Jul 2017, 23:31

Hi
One Info more.

somtime the mqtt state works in domoticz.... but cant find when.

Only see in the logfile from easyesp:
4765500 : MQTT : Connected to broker
4765501 : Subscribed to: domoticz/out
4787094 : MQTT : Ignored too big message
4794470 : WD : Uptime 77 ConnectFailures 120 FreeMem 22992
4824471 : WD : Uptime 78 ConnectFailures 119 FreeMem 23256
4824471 : MQTT : Connection lost
4825503 : MQTT : Connected to broker
4825504 : Subscribed to: domoticz/out
4847203 : MQTT : Ignored too big message

Using standard mqtt broker (mosquito 1.4.12).

Next:
And tried to test the send2http in the rules
on sonoff#Relay0 do
if [sonoff#Relay0]=0
SendToHTTP 192.168.253.11,80,/json.htm?type=command&param=switchlight&idx=50&switchcmd=Off
else
SendToHTTP 192.168.253.11,80,/json.htm?type=command&param=switchlight&idx=50&switchcmd=On
endif
endon

But every time the rules were cut at the first "&", i think a parser error. tested it with an other easyesp there is no problem with adding the rule....

May that helps you.
tia
raphael

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#15 Post by enesbcs » 19 Jul 2017, 00:31

rira2005 wrote: 18 Jul 2017, 23:31 And tried to test the send2http in the rules
on sonoff#Relay0 do
if [sonoff#Relay0]=0
SendToHTTP 192.168.253.11,80,/json.htm?type=command&param=switchlight&idx=50&switchcmd=Off
else
SendToHTTP 192.168.253.11,80,/json.htm?type=command&param=switchlight&idx=50&switchcmd=On
endif
endon

But every time the rules were cut at the first "&", i think a parser error. tested it with an other easyesp there is no problem with adding the rule....
It maybe related to ESPEasy Mega dev10 version Domoticz bug, in dev11 it is fixed.
https://github.com/letscontrolit/ESPEasy/releases

I compiled with new dev11, but i see some strange characters on the web GUI.. even so the MQTT and Domoticz connection seems flawless.
Last edited by enesbcs on 20 Aug 2017, 18:09, edited 1 time in total.

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#16 Post by rira2005 » 02 Aug 2017, 20:39

HI

Works Perfect! yet i get the mqtt messages!

MANY THANKS for your work! --> should be added to the EASYESP Libary.

One Question : which "gpio" command i need to use it in rules ...
like :
On toogle-r0 do
sonoff#Relay0=1
timerSet 1,5
endon

on Rules#Timer=1 do
sonoff#Relay0=0
endon

need a one second pulse!

many thanks.
Rira

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#17 Post by rira2005 » 03 Aug 2017, 07:31

Forget my last post it allready made it.

relay,0,1 (relay1 on)
relay,0,0 (relay1 off)

relay,1,1 (relay2 on)
relay,1,0 (relay2 off)

But may you can compile a new version without the "dev" digtis in the webinterface for me?

many thanks. RR

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#18 Post by enesbcs » 04 Aug 2017, 07:21

rira2005 wrote: 03 Aug 2017, 07:31

But may you can compile a new version without the "dev" digtis in the webinterface for me?

many thanks. RR
Thank you, for testing it. In the meantime i also win a Sonoff Dual for 8$ i hope it will arrive from China soon. :)
I removed the development bit from the plugin and from espeasy.ino,i hope it's enough to remove the dev digits.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#19 Post by enesbcs » 20 Aug 2017, 18:14

rira2005 wrote: 03 Aug 2017, 07:31 But may you can compile a new version without the "dev" digtis in the webinterface for me?
I've get rid of the 'dev' digits, it's in fact in the webserver.ino for some reason, the numbers were the length of the written chunks.. i do not know what was the purpose, commented out the lines doing it and compiled, the resulted binaries attached in the first post of this thread. I've done some cleanup in thread, so every other uploads deleted, only the latest can be downloaded. (It is not based on the dev11, but on the github ESPEasy Mega source)
'ESPEasy.bin.serialswitch.zip'

PS.: My Sonoff Dual finally arrived from China, tested and it works! :)

rira2005
Normal user
Posts: 30
Joined: 25 Jun 2017, 21:09

Re: Serial MCU controlled relay/switch

#20 Post by rira2005 » 31 Aug 2017, 13:55

hi

yes me too. works fine! Many Thanks for your work.

One Question more...
Bought this ESP8266 5 V WiFi relaismodul in china
https://de.aliexpress.com/item/ESP8266- ... 0.0.5Thz2C
it also works with a serial controled MCU... May you have a idea to get it work with easyesp?

Tia
Raphael

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

Re: Serial MCU controlled relay/switch

#21 Post by papperone » 31 Aug 2017, 15:19

rira2005 wrote: 31 Aug 2017, 13:55 hi

yes me too. works fine! Many Thanks for your work.

One Question more...
Bought this ESP8266 5 V WiFi relaismodul in china
https://de.aliexpress.com/item/ESP8266- ... 0.0.5Thz2C
it also works with a serial controled MCU... May you have a idea to get it work with easyesp?

Tia
Raphael
That's not wokring with serial but it's a plain old ESP-01 module with 1Mb of memory.
It can potentially run R12o or maybe R148 but defintely not V2.0.0 (or at least without loosing OTA functionality).
If you are good at soldering you can upgrade the memory chip to 4Mb and give this module a new life :D
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

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

Re: Serial MCU controlled relay/switch

#22 Post by Shardan » 31 Aug 2017, 16:34

papperone wrote: 31 Aug 2017, 15:19
That's not wokring with serial but it's a plain old ESP-01 module with 1Mb of memory.
It can potentially run R12o or maybe R148 but defintely not V2.0.0 (or at least without loosing OTA functionality).
If you are good at soldering you can upgrade the memory chip to 4Mb and give this module a new life :D
The serial MCU is the 8-pin STC chip on the baseboard connected to a standard ESP-01.
You can see the chip left of the ESP's antenna on the base PCB int he first picture.

The 1MB can use 2.0.0, OTA works if you use a two step upgrade as described in the Wiki.
It just can't take a direct update from one 2.00 to another 2.000 version.

I'd try to remove the STC chip and switch relay directly from an ESP's GPIO and leave the flash rom untouched...
For just a relay and the limited GPIO's of an ESP-01 the ESPEasy-version and updating is nothing i'd take too much care of.

Regards
Shardan
Regards
Shardan

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#23 Post by enesbcs » 31 Aug 2017, 17:24

rira2005 wrote: 31 Aug 2017, 13:55 Bought this ESP8266 5 V WiFi relaismodul in china
https://de.aliexpress.com/item/ESP8266- ... 0.0.5Thz2C
it also works with a serial controled MCU... May you have a idea to get it work with easyesp?
I am sure that it can be done. But first we need to know the commands, that the ESP-01 is sending to drive the relay ON/OFF state.

I see the serial speed is: 9600,8,1,0,0.

And i've found a fantastic article by makerrelay:
https://www.hackster.io/makerrelay/esp8 ... ome-e8a437

According to it:
A00101A2 open relay,
A00100A1 closed relay

If you're willing to try, i can make a test binary with the above commands, but there is no guarantee. :)

Update: attached 1M/128k SPIFFS for black ESP-01
Last edited by enesbcs on 26 Sep 2017, 19:53, edited 1 time in total.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#24 Post by enesbcs » 19 Sep 2017, 19:34

enesbcs wrote: 31 Aug 2017, 17:24 If you're willing to try, i can make a test binary with the above commands, but there is no guarantee. :)
Update: attached 1M/128k SPIFFS for black ESP-01
There are 8 downloads, if anyone had time to try it, please let me know the results. :)
Last edited by enesbcs on 26 Sep 2017, 19:52, edited 1 time in total.

cmcooper123
New user
Posts: 4
Joined: 25 Sep 2017, 09:59

Re: Serial MCU controlled relay/switch

#25 Post by cmcooper123 » 25 Sep 2017, 12:42

I wasn't able to get it working with the test binary but with ESPEasy I was able to make a Serial server with these settings and was able to turn the relay off and on using these commands from my linux computer :D

Code: Select all

echo -n -e '\xA0\x01\x01\xA2' | nc -q 0 192.168.1.50 8080
echo -n -e '\xA0\x01\x00\xA1' | nc -q 0 192.168.1.50 8080

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#26 Post by enesbcs » 25 Sep 2017, 23:09

cmcooper123 wrote: 25 Sep 2017, 12:42 I wasn't able to get it working with the test binary but with ESPEasy I was able to make a Serial server with these settings and was able to turn the relay off and on using these commands from my linux computer :D

Code: Select all

echo -n -e '\xA0\x01\x01\xA2' | nc -q 0 192.168.1.50 8080
echo -n -e '\xA0\x01\x00\xA1' | nc -q 0 192.168.1.50 8080
Thanx, it is very interesting, the init sequence that i used in the plugin is the same:
Serial.begin(9600, SERIAL_8N1);

The relay off command "/control?cmd=relay,0,0" executes:
Serial.write(0xA0);
Serial.write(0x01);
Serial.write(0x00);
Serial.write(0xA1);

The relay on command "/control?cmd=relay,0,1" executes:
Serial.write(0xA0);
Serial.write(0x01);
Serial.write(0x01);
Serial.write(0xA2);

So in theory it should work. It is implemented in a very similar way, that the Sonoff Dual works, maybe the ESP chip is an 512k module? Did the binary starts up at all? Did the log said anything? What ESPEasy version did you use?

cmcooper123
New user
Posts: 4
Joined: 25 Sep 2017, 09:59

Re: Serial MCU controlled relay/switch

#27 Post by cmcooper123 » 25 Sep 2017, 23:26

What I got working was with the binary you provided, I have the 1mb model, I didn't see anything in the logs, I might have configured the plugin wrong "/control?cmd=relay,0,1" loads as a blank page

cmcooper123
New user
Posts: 4
Joined: 25 Sep 2017, 09:59

Re: Serial MCU controlled relay/switch

#28 Post by cmcooper123 » 26 Sep 2017, 12:57

OK I got "/control?cmd=relay,0,1" to work I had to load the page twice because the relay needs to be sent "A00101A2" twice for it to turn on sometimes

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#29 Post by enesbcs » 26 Sep 2017, 19:52

cmcooper123 wrote: 26 Sep 2017, 12:57 OK I got "/control?cmd=relay,0,1" to work I had to load the page twice because the relay needs to be sent "A00101A2" twice for it to turn on sometimes
Interesting, maybe serial logging is enabled in Espeasy?
But can be fixed, new binary attached with command doubling. :)

Anyway the proposed Espeasy setup:
Tools->Advanced->Serial log Level: 0
Tools->Advanced->Enable Serial port: enabled
Tools->Advanced->Rules:enabled
Last edited by enesbcs on 07 Oct 2017, 14:21, edited 1 time in total.

cmcooper123
New user
Posts: 4
Joined: 25 Sep 2017, 09:59

Re: Serial MCU controlled relay/switch

#30 Post by cmcooper123 » 26 Sep 2017, 20:33

I thought about serial logging as well but I think the problem is to do with the relay not the ESP,
because it didn't always turn on the first time when I sent the command from my computer using serial and also when I was using different firmware,
but with firmware you sent it seems to work every time.
Also I didn't need to have the "Enable Serial port" option enabled for it to function.
Thanks for this, hopefully these additions will be merged to the git. :D

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#31 Post by enesbcs » 27 Sep 2017, 21:50

cmcooper123 wrote: 26 Sep 2017, 20:33 I thought about serial logging as well but I think the problem is to do with the relay not the ESP,
because it didn't always turn on the first time when I sent the command from my computer using serial and also when I was using different firmware,
but with firmware you sent it seems to work every time.
Also I didn't need to have the "Enable Serial port" option enabled for it to function.
Thanks for this, hopefully these additions will be merged to the git. :D
Thank you for testing it, i've uploaded the source to github.
Just noticed that the P165 plugin found it's way to the official ESPEasyPluginPlayground thanx to psy0rz. :) I started a new pull request.

Mattley30
New user
Posts: 4
Joined: 04 Oct 2017, 17:41

Re: Serial MCU controlled relay/switch

#32 Post by Mattley30 » 04 Oct 2017, 18:00

Hi enesbcs
Congratulations on your job, I used your plugin successfully with domoticz. Now I have trouble with the rules for 4/5 days that I'm doing tests but not successful so I wanted to write to you.
My project as I think others is to use the Sonoff Dual for the blinds, and make sure that when I turn on relay0 the relay1 is turned off, and when I get the relay1, the relay0 is turned off, using pin “Button 0” and “Button1” present on the Sonoff dual.
I tried different rules and this seemed to me the simplest and most logical but it does not work:

on sonoff#relay0 do
if [sonoff#relay0]=1
relay1,0
endif
endon

on sonoff#relay1 do
if [sonoff#relay1]=1
relay0,0
endif
endon

Could you give me some help?

Thanks
P.s. Sorry for my english

Mattley30

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#33 Post by enesbcs » 04 Oct 2017, 20:44

Mattley30 wrote: 04 Oct 2017, 18:00 Hi enesbcs
Congratulations on your job, I used your plugin successfully with domoticz. Now I have trouble with the rules for 4/5 days that I'm doing tests but not successful so I wanted to write to you.
My project as I think others is to use the Sonoff Dual for the blinds, and make sure that when I turn on relay0 the relay1 is turned off, and when I get the relay1, the relay0 is turned off, using pin “Button 0” and “Button1” present on the Sonoff dual.
I tried different rules and this seemed to me the simplest and most logical but it does not work:

on sonoff#relay0 do
if [sonoff#relay0]=1
relay1,0
endif
endon

on sonoff#relay1 do
if [sonoff#relay1]=1
relay0,0
endif
endon

Could you give me some help?

Thanks
P.s. Sorry for my english

Mattley30
Thanx! Your english is at least as understandable as mine. Currently my Dual is in a box somewhere, but i will do my best to search it and try at the weekend. :) Can you try the following?
//
On sonoff#relay0=1 do
relay,1,0
Endon

On sonoff#relay1=1 do
relay,0,0
Endon
//

If it is not working i can implement an exluding working mode for blinds to the Dual, that can be choosed in the plugin options.. in that mode, switching on a relay will automatically switch off the other one instantly.
When the rules method used, i think there will always be a little time, when both relays states are ON before the other one go OFF.

Mattley30
New user
Posts: 4
Joined: 04 Oct 2017, 17:41

Re: Serial MCU controlled relay/switch

#34 Post by Mattley30 » 05 Oct 2017, 14:44

hi
I did a test with the rules you wrote in your post, but it does not work, it does not switch relay0,1 and relay1,0, I even noticed that with this rule when the Button0 or the Button1 is active the dual janeff crashes and after a few seconds restarts.

The solution to include in your plugin the possibility of using dual with the blinds seems great, if you can be of assistance i have done tests on domoticz using the actions to turn off the switch between one relay and the other 1 second time between switching off and on is a good delay.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#35 Post by enesbcs » 05 Oct 2017, 19:40

Mattley30 wrote: 05 Oct 2017, 14:44 it does not switch relay0,1 and relay1,0, I even noticed that with this rule when the Button0 or the Button1 is active the dual janeff crashes and after a few seconds restarts.
relay1,0 is not a command
relay1,0, is not a command
relay,1,0 is a command

Are you sure that you are using it correctly?

Anyway the implementation of exclusive mode is not so complicated i will try it at the weekend.

Mattley30
New user
Posts: 4
Joined: 04 Oct 2017, 17:41

Re: Serial MCU controlled relay/switch

#36 Post by Mattley30 » 06 Oct 2017, 08:13

enesbcs wrote: 05 Oct 2017, 19:40
Mattley30 wrote: 05 Oct 2017, 14:44 it does not switch relay0,1 and relay1,0, I even noticed that with this rule when the Button0 or the Button1 is active the dual janeff crashes and after a few seconds restarts.
relay1,0 is not a command
relay1,0, is not a command
relay,1,0 is a command

Are you sure that you are using it correctly?

Anyway the implementation of exclusive mode is not so complicated i will try it at the weekend.
the commands I used are relay,0,1 etc.
I made a copy and paste the rules you wrote in your previous post. :P

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#37 Post by enesbcs » 07 Oct 2017, 09:56

Mattley30 wrote: 06 Oct 2017, 08:13 the commands I used are relay,0,1 etc.
I made a copy and paste the rules you wrote in your previous post. :P
Now i understand the problem. This plugin has 3 return value, and everytime when either is changing returns all three value... so if the first relay is on, and the second is also on there were a little endless loop, after little fixing now only the first rule runs, second never fired.. so i do not think that it can be done with rules - easily.

Anyway i implemented blinds mode directly in the plugin, tested with my Dual, it works.

Also fixed the button0 and button1 support (panel pins) and the third relay "Relay2" now holds the value of the WiFi on-board button of the Dual. This button do nothing alone (no relay behind), but can be used in rules, also thinking to implement in the plugin such as "Relay0 ON/OFF", "Relay1 ON/OFF", "Relay0+1 ON/OFF" functions.. look forward to the ideas. :)

Tested with every possible scenario that crossed my mind.. currently there are no delay in case of switching, please let me know it it is needed.
Last edited by enesbcs on 07 Oct 2017, 14:22, edited 1 time in total.

Mattley30
New user
Posts: 4
Joined: 04 Oct 2017, 17:41

Re: Serial MCU controlled relay/switch

#38 Post by Mattley30 » 07 Oct 2017, 12:58

Hi,
Your new plugin with the solution to include the blinds works fine. In fact with this solution there is not even a need to put a delay time between one switch and the other because the communications with domoticz work well and the blind engine has no problems.
Congratulations still on your job, I think other people will use it well.
Now I do other tests with actions on domoticz for up, down and stop.

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

Re: Serial MCU controlled relay/switch

#39 Post by papperone » 07 Oct 2017, 13:36

I think this idea of dual relays control for blinds should be implemented in a more generic plugin.
Like you can select the output (SonoffDual, or 2 GPIOs controlling to normal relays) and have all the logic applied when UP/DOWN (or left/right in case of curtains!) commands are sent to it.
We can call this "Blind Control" plugin... :D

PS: I do not have personally any automated blinds as I dont' have actually blinds in my house.... But my wife has always asked me to automate our curtains (and the logic should be similar).
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

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#40 Post by enesbcs » 07 Oct 2017, 14:14

Mattley30 wrote: 07 Oct 2017, 12:58 Hi,
Your new plugin with the solution to include the blinds works fine. In fact with this solution there is not even a need to put a delay time between one switch and the other because the communications with domoticz work well and the blind engine has no problems.
Congratulations still on your job, I think other people will use it well.
Now I do other tests with actions on domoticz for up, down and stop.
Thank you for testing! Binary attached to first post of this thread, source code uploaded to github!
Last edited by enesbcs on 07 Oct 2017, 14:23, edited 1 time in total.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#41 Post by enesbcs » 07 Oct 2017, 14:16

papperone wrote: 07 Oct 2017, 13:36 I think this idea of dual relays control for blinds should be implemented in a more generic plugin.
Like you can select the output (SonoffDual, or 2 GPIOs controlling to normal relays) and have all the logic applied when UP/DOWN (or left/right in case of curtains!) commands are sent to it.
We can call this "Blind Control" plugin... :D

PS: I do not have personally any automated blinds as I dont' have actually blinds in my house.... But my wife has always asked me to automate our curtains (and the logic should be similar).
Currently i have no blinds at all, but not a bad idea. Anyway the source code is uploaded to github. :)

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

Re: Serial MCU controlled relay/switch

#42 Post by papperone » 08 Oct 2017, 12:41

@enesbcs: watch out that your plugin cannot work properly in all conditions due to limitation design of ESPEasy PLUGIN_WRITE call.
Basically while executing PLUGIN_WRITE you have 2 limits:
1) only first device of the same "plugin type" will execute the commands sent (in this case not relevant as it makes no sense to define more than one :) )
2) when PLUGIN_WRITE is called the EventStruc of the device is not passed interely so you cannot be sure you can access the right Device parameters :(
To test point 2 you can define something as device 1, then setup P165 as device 2, and play a round and you will see a loooooot of issues when sending commands (PLUG_WRITE) :o
It shoudl be as well sufficient while device works to reboot the ESP and try to send commands without doing anythign else before and commands wont' be executed most probably...
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

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#43 Post by enesbcs » 09 Oct 2017, 19:24

papperone wrote: 08 Oct 2017, 12:41 @enesbcs: watch out that your plugin cannot work properly in all conditions due to limitation design of ESPEasy PLUGIN_WRITE call.
Basically while executing PLUGIN_WRITE you have 2 limits:
1) only first device of the same "plugin type" will execute the commands sent (in this case not relevant as it makes no sense to define more than one :) )
2) when PLUGIN_WRITE is called the EventStruc of the device is not passed interely so you cannot be sure you can access the right Device parameters :(
To test point 2 you can define something as device 1, then setup P165 as device 2, and play a round and you will see a loooooot of issues when sending commands (PLUG_WRITE) :o
It shoudl be as well sufficient while device works to reboot the ESP and try to send commands without doing anythign else before and commands wont' be executed most probably...
On point 1: I see, but P165 is made for specific devices, that hardwired to hardware UART, i can not think any real world scenario, when two of the P165 plugin will be necessery on the same physical device, on the same time. In fact, if there are any method to disable multiple usage of the plugin, i will do it. :)

On point 2: it can be a problem, i tought that Eventstruct *event is declared in global scope, so i do not expect any problem.. thank you for pointing to it, i think i fixed it with global variables - ugly but works- tested with sonoff dual 165 as second device. Binaries updated in first post.
https://github.com/enesbcs/ESPEasyPlugi ... Switch.ino

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

Re: Serial MCU controlled relay/switch

#44 Post by papperone » 09 Oct 2017, 21:07

Thanks to the "God" of ESPEasy we have a neat solution here --> https://github.com/letscontrolit/ESPEas ... -335028103
So defintely it's not a blocking limit, just required a bit of code to access proper device parameters specially useful when you could have more than one device of the same plugin (not your case thou)...
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

mcfarlde
New user
Posts: 1
Joined: 20 Oct 2017, 13:05

Re: Serial MCU controlled relay/switch

#45 Post by mcfarlde » 20 Oct 2017, 13:09

Can you share the binary, I have the same boards.

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#46 Post by enesbcs » 20 Oct 2017, 19:11

mcfarlde wrote: 20 Oct 2017, 13:09 Can you share the binary, I have the same boards.
Yes of course, the binary is always being uploaded to the first post of this thread, and the source is uploaded to the github.
download/file.php?id=1581

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#47 Post by enesbcs » 17 Nov 2017, 10:55

I've compiled the binary against ESP8266 Arduino Core 2.4.0 rc2, attached to the first post of this thread:
download/file.php?id=1715

saundersrg
New user
Posts: 5
Joined: 13 Dec 2017, 10:18

Re: Serial MCU controlled relay/switch

#48 Post by saundersrg » 13 Dec 2017, 13:26

Perhaps I've missed the boat completely, but been tearing my hair out with a Sonoff Dual V2.0. Between ESPEasy, ESPurna, and Tasmota, none of them work. Other people seem to be having the same issue on other forums.

I don't know if maybe its a slight difference in the boards, but I just cannot get the relays to fire no matter what I do. Anyone else seeing this issue?

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

Re: Serial MCU controlled relay/switch

#49 Post by grovkillen » 13 Dec 2017, 13:34

saundersrg wrote: 13 Dec 2017, 13:26 Perhaps I've missed the boat completely, but been tearing my hair out with a Sonoff Dual V2.0. Between ESPEasy, ESPurna, and Tasmota, none of them work. Other people seem to be having the same issue on other forums.

I don't know if maybe its a slight difference in the boards, but I just cannot get the relays to fire no matter what I do. Anyone else seeing this issue?
If you take a picture of the board we might see if the ESP MCU is doing the actual pulling of the relay or if they (itead) has started to put in a middle hand MCU which is controlled via the ESP MCU.

EDIT: sorry, I see that it is in fact discussed in this thread. It i a serial controlled MCU. So how are you trying to make the relay trigger? Using serial commands?
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:

User avatar
enesbcs
Normal user
Posts: 587
Joined: 18 Jun 2017, 11:02
Location: Békéscsaba, Hungary
Contact:

Re: Serial MCU controlled relay/switch

#50 Post by enesbcs » 13 Dec 2017, 17:16

saundersrg wrote: 13 Dec 2017, 13:26 Perhaps I've missed the boat completely, but been tearing my hair out with a Sonoff Dual V2.0. Between ESPEasy, ESPurna, and Tasmota, none of them work. Other people seem to be having the same issue on other forums.

I don't know if maybe its a slight difference in the boards, but I just cannot get the relays to fire no matter what I do. Anyone else seeing this issue?
If you have a Sonoff Dual R2 there are no more MCU, relays are directly attached to 8285 GPIO, you have to choose R2 modell in Tasmota, or use GPIO directly in ESPEasy
https://github.com/arendst/Sonoff-Tasmo ... nd-Dual-R2

If you have an old Sonoff Dual, then please read the first post of this thread, and use the binary attached to it. If it is not working for some reason and this board is definitely is not an R2, then you can test with ESPEasy+SER2NET plugin the serial status messages, that this board reply when you short the onboard button0 and button1 pins.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest