Serial MCU controlled relay/switch

Moderators: Voyager, BertB, rtenklooster, Stuntteam, grovkillen, TD-er

Message
Author
Mravko
Normal user
Posts: 25
Joined: 18 Feb 2019, 04:48

Re: Serial MCU controlled relay/switch

#251 Post by Mravko » 04 May 2019, 07:33

enesbcs wrote:
04 May 2019, 00:53
Mravko wrote:
03 May 2019, 09:07
1. looking into the following code:

line 681:
I'm interested in the byte "btnnum". What's confusing is this line "Serial.write( (btnnum + 1) ); // relay number 1,2,3"
Are we adding "+ 1" because btnnum = 01 for the first relay?
Yes the Tuya addresses buttons from 1, the plugin addresses EVERYTHING from 0.
Incoming switch numbers decoded at line 426:

Code: Select all

byte btnnum = (serial_buf[6] - 1);
Who has worked with arrays knows why starting from 0 is handy.
Mravko wrote:
03 May 2019, 09:07
I'm wondering if the hex of switch 3 and 4 are not read correctly in the read section. you know 0x00, 0x01, 0x0a and 0x0b.
I'm wondering if a and b are not interpreted correctly when reading.
I don't quite understand your point, it does not matter if you write it in hex or decimal, everything is binary. Byte values arriving from serial which do not need any interpreting.
Mravko wrote:
03 May 2019, 09:07
As I don't really follow the reading of the status, I cant figure out what bytes it's expecting for switch 3 and switch 4?
The same as switch 1 and switch 2 as the packet format is equal. You wont find a difference where there are no difference, this is handled by the case from line 434.
Mravko wrote:
03 May 2019, 09:07
Could the dimmer selection be interfering with the reading?
I noticed it's 8 bytes and I think my logs show that it is sending those valies, so when it is processing 2, 3 and 4 is it expecting dimmer values or switch values?
If you see "SerSW : Dimmer" it does not process anything but execute a sendto() command which may update the device status in MQTT.. but if it bother you, i can hide it.
If you see "SerSW : Dimmer d1:" or "SerSW : Dimmer d2:" in your logs, than yes, it may interfere.
Not for now, I’m ok with it reporting.
In fact, soon I may get some wifi dimmers, from the same series and we can deal with it then. I don’t know what the logs will look like but will let you know when I get them

MM

Bohbe
Normal user
Posts: 6
Joined: 02 May 2019, 09:14
Location: Uppsala, Sweden

Re: Serial MCU controlled relay/switch

#252 Post by Bohbe » 04 May 2019, 10:26

enesbcs wrote:
04 May 2019, 00:28
Bohbe wrote:
03 May 2019, 21:29
This is my first post in this forum, so bare with me. I went to Aliexpress and got some LCTech 2ch relays. I have a really hard time to get it to work with ESP Easy. I realized that the 2-channel version I got had a Nuvoton N76E003 MCU instedad of the STM.
Url? At the description page the seller usually writes about the usable commands...
I see now that I in the end bought it from Wish (I went around for best price including the ridiculus post import fee in Sweden, Wish has it included)... But it has all the functionality with the LCTech apps and everything, so I believe it's an "original"
enesbcs wrote:
04 May 2019, 00:28
Bohbe wrote:
03 May 2019, 21:29
Relay 1 on : (Hex) 0D 0A 2B 49 50 44 2C 30 2C 34 3A A0 01 01 A2
Relay 1 off: (Hex) 0D 0A 2B 49 50 44 2C 30 2C 34 3A A0 01 00 A1
Relay 2 on : (Hex) 0D 0A 2B 49 50 44 2C 30 2C 34 3A A0 02 01 A3
Relay 2 off: (Hex) 0D 0A 2B 49 50 44 2C 30 2C 34 3A A0 02 00 A2

They have added 11 bytes of the same data, maybe as an address
If you convert "0D 0A 2B 49 50 44 2C 30 2C 34 3A" to ASCII you'll get
"
+IPD,0,4:" which is a simple AT modem command for the ESP (means transfer the following 4 bytes)... the real commands for the MCU seems to be the same as always: A0 01 01 A2, A0 01 00 A1, etc... which is already supported by this plugin.
Thanks for the interpretation of the message! As I said, I'm a hardware guy... :geek: It seems like the ESP original software just is pushing out on the UART what it got from the command over IP, as it is stated in the Espressif guide: https://www.espressif.com/sites/default ... les_en.pdf

And the Nuvoton N76E003 I got is programmed to interpret just that. When reprogram the ESP with ESP Easy it just sends the "correct" command with the 4 bytes, which is not seen as a "correct" command by theN76E003. Is there a simpler way to test this theory with the "Command" tool in the web interface, or do I have to set up a compiler environment and code it?

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

Re: Serial MCU controlled relay/switch

#253 Post by enesbcs » 04 May 2019, 16:38

Bohbe wrote:
04 May 2019, 10:26
And the Nuvoton N76E003 I got is programmed to interpret just that. When reprogram the ESP with ESP Easy it just sends the "correct" command with the 4 bytes, which is not seen as a "correct" command by theN76E003. Is there a simpler way to test this theory with the "Command" tool in the web interface, or do I have to set up a compiler environment and code it?
Debug:
- Enable serial port usage, but disable serial logging, also disable all plugin (P165) and device that using serial pins.
- Setup "Communication - Serial Server" plugin for debugging (port 23, baud 9600, 8n1)
https://www.letscontrolit.com/wiki/index.php/Ser2Net
- install Realterm to your PC, setup for displaying in hexadecimal in Display menu, in Capture write the ESP device IP address and port (23) and set the serial settings that matches with the ESP Ser2Net plugin

As i remember, you can premade a binary file with the necesarry command bytes, and you can send it with Realterm to the Ser2Net for testing purposes.

Bohbe
Normal user
Posts: 6
Joined: 02 May 2019, 09:14
Location: Uppsala, Sweden

Re: Serial MCU controlled relay/switch

#254 Post by Bohbe » 05 May 2019, 19:26

enesbcs wrote:
04 May 2019, 16:38
Debug:
- Enable serial port usage, but disable serial logging, also disable all plugin (P165) and device that using serial pins.
- Setup "Communication - Serial Server" plugin for debugging (port 23, baud 9600, 8n1)
https://www.letscontrolit.com/wiki/index.php/Ser2Net
- install Realterm to your PC, setup for displaying in hexadecimal in Display menu, in Capture write the ESP device IP address and port (23) and set the serial settings that matches with the ESP Ser2Net plugin

As i remember, you can premade a binary file with the necesarry command bytes, and you can send it with Realterm to the Ser2Net for testing purposes.
Thanks! I have tried to do what you described and ran into two problems:

1. I get 9600 baud rate on the serial port to the MCU and can't find a way to get it to work with 115200, which is the baud rate I communicate with
2. It sends 12 bytes, even though the file is 11 bytes. And the strange thing is that the second byte is the one inserted with "0x00". :? Thank god for a good logic analyzer!

I also realized that serial logging was on originally and thought for a while that that would have interfered with the messaging, but nooo. :cry:

I will work on this more later on, try to figure out what happens. I'm in parallel installing Platformio but haven't a clue right now how to merge in P165 into the rest of the code...

Mravko
Normal user
Posts: 25
Joined: 18 Feb 2019, 04:48

Re: Serial MCU controlled relay/switch

#255 Post by Mravko » 07 May 2019, 07:40

Bohbe wrote:
05 May 2019, 19:26
enesbcs wrote:
04 May 2019, 16:38
Debug:
- Enable serial port usage, but disable serial logging, also disable all plugin (P165) and device that using serial pins.
- Setup "Communication - Serial Server" plugin for debugging (port 23, baud 9600, 8n1)
https://www.letscontrolit.com/wiki/index.php/Ser2Net
- install Realterm to your PC, setup for displaying in hexadecimal in Display menu, in Capture write the ESP device IP address and port (23) and set the serial settings that matches with the ESP Ser2Net plugin

As i remember, you can premade a binary file with the necesarry command bytes, and you can send it with Realterm to the Ser2Net for testing purposes.
Thanks! I have tried to do what you described and ran into two problems:

1. I get 9600 baud rate on the serial port to the MCU and can't find a way to get it to work with 115200, which is the baud rate I communicate with
2. It sends 12 bytes, even though the file is 11 bytes. And the strange thing is that the second byte is the one inserted with "0x00". :? Thank god for a good logic analyzer!

I also realized that serial logging was on originally and thought for a while that that would have interfered with the messaging, but nooo. :cry:

I will work on this more later on, try to figure out what happens. I'm in parallel installing Platformio but haven't a clue right now how to merge in P165 into the rest of the code...
I've just gone through that painstaking exercise of setting up PlatformIO and incorporating the plugin. I have worked it out.
basically there are a couple of things you need to do.

1. Add #ifdef USES_P165 to the first line and #endif // USES_P165 the very last line of the plugin. (This should be done to the plugin itself in github but can only be done once the plugin becomes a core plugin).
2. set up custom set of plugins in the define_plugin_sets.h e.g.:
#ifdef PLUGIN_SET_ONLY_TUYA
#ifndef PLUGIN_SET_NONE
#define PLUGIN_SET_NONE
#endif
#ifndef USES_P001
#define USES_P001 // switch input - Switch
#endif
#ifndef USES_P002
#define USES_P002 // Analogue input - internal
#endif
#ifndef USES_P003
#define USES_P003 // Pulse Counter
#endif
#ifndef USES_P020
#define USES_P020 // Communication - Serial Server
#endif
#ifndef USES_P021
#define USES_P021 // Regulator - Level Control
#endif
#ifndef USES_P026
#define USES_P026 // SysInfo
#endif
#ifndef USES_P029
#define USES_P029 // Output - Domoticz MQTT Helper
#endif
#ifndef USES_P033
#define USES_P033 // Dummy Device
#endif
#ifndef USES_P043
#define USES_P043 // Output - Clock
#endif
#ifndef USES_P165
#define USES_P165 // Serial MCU controlled Switch
#endif
#endif

3. define an environment in platformio.ini that calls the set of plugins. e.g.:
; Easy Tuya (1M) device ----------------------
[env:easy_TUYA_1M]
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
framework = ${common.framework}
platform = ${common.platform}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
board = esp01_1m
build_flags = ${esp8266_1M.build_flags} -D PLUGIN_SET_ONLY_TUYA

4. call that environment at the start of platformio.ini:

[platformio]
;env_default = esp32dev
;env_default = dev_ESP8266_4096
env_default = easy_TUYA_1M
;env_default = normal_ESP8266_4096
; ..etc

5. (oh and obviously! :) ) copy the plugin to the src/ directory


..and that's it.

Mravko
Normal user
Posts: 25
Joined: 18 Feb 2019, 04:48

Re: Serial MCU controlled relay/switch

#256 Post by Mravko » 07 May 2019, 08:17

Hi enesbcs,

Ok, so I've done some major debugging of the plugin and here are my findings:

Everything in the plugin works great except the reading of the tuya MCU.

rationale: First I have to add a delay to the reading. The plugin will eventually read all four lines. It may take up to 20 minutes for it to stumble across the 3rd and fourth line because, at least in my case, the MCU will pump out the 168 bytes every 5 seconds.

55AA0107000501010001000F55AA0107000502010001001055AA0107000503010001001155AA0107000504010001011355AA0107000807020004000000001C55AA0107000808020004000000001D55AA0107000809020004000000001E55AA010700080A020004000000001F55AA0107000865020004000000047E55AA01070008660200040000844E4D5AA01070008670200040000BEA5DF55AA01070008680200040000097A00

The read function doesn't know where it should start reading, it almost randomly starts reading the byte waiting for 55AA (which works) but I think it only processes one (or a couple of) 55AA at a time (this is what I'm trying to figure out now). Then it'll do another one the next time it reads. It may be any of the 12 sets:
1. 55AA0107000501010001000F
2. 55AA01070005020100010010
3. 55AA01070005030100010011
4. 55AA01070005040100010113
5. 55AA0107000807020004000000001C
6. 55AA0107000808020004000000001D
7. 55AA0107000809020004000000001E
8. 55AA010700080A020004000000001F
9. 55AA0107000865020004000000047E
10. 55AA01070008660200040000844E4D
11. 55AA01070008670200040000BEA5DF
12. 55AA01070008680200040000097A00

Conclusion:
I'm trying to think of a better way to handle the reading of the stream. Perhaps before it just starts reading a stream there should be a check for the start of the stream and not just start reading randomly.
Something like: "Wait for when there is no talk from the MCU" - which means wait for it to finish the last pumping of the stream; Then start reading the new stream and loop through each of the 12 sets. so a for loop that processes each 55AA set.
The first 4 sets will be 12 bytes and the consecutive 8 will be 15 bytes.
Perhaps we don't even care about the length as the loop will start processing when it hits the next 55AA.

I have watched the plugin process it and it does seem like it may start reading the stream randomly (anywhere in the sequence) and eventually it'll process them all but it'll take a hundred goes because it doesn't care where the beginning is, it just starts reading.

Reading the code, I'm not convinced that it reads and parses the whole stream every time. In fact, I am positive that it doesn't as it would change all the states at once. I have gone through the code numerous times and cannot find the logic of processing the whole stream in the classic "for loop" style.
Now, I understand that your Tuya send a streal for a changed state. That may be version "00" but, at least for version "01" of the tuya protocol, it sends the whole stream every 5 seconds.

The other thing I'm going to find out is can the plugin be interrupted by the MCU pumping the stream. i.e. Can the function be interrupt driven? this way, each time the MCU sends data, the function will be called and it can parse the stream.

Mravko...

Mravko
Normal user
Posts: 25
Joined: 18 Feb 2019, 04:48

Re: Serial MCU controlled relay/switch

#257 Post by Mravko » 08 May 2019, 16:03

Hi enesbcs,

I've been thinking...

Looking at the code, it seems that in the Read function only ever processes the first 12 or 15 bytes. I don't see that it increments.

First check: if (bytes_read == 0) { // packet start --> check if byte 0 is "0x55"
Second check: if (bytes_read == 1) { // check if packet is valid --> check if byte 1 is "0xAA"

but there are 12 valid packets in my case

second check
if (bytes_read == 12) { // packet start --> check if byte 0 is "0x55"
if (bytes_read == 13) { // check if packet is valid --> check if byte 1 is "0xAA"

third check
if (bytes_read == 25) { // packet start --> check if byte 0 is "0x55"
if (bytes_read == 26) { // check if packet is valid --> check if byte 1 is "0xAA"
...and so on

Then again it only needs to check the first two bytes to determine the validity of the stream, so I'm good with that.

What I cannot follow in the code is when does it ready bytes 12-23, 24-35, 36-47, ....and so on
i.e.
1. 55AA0107000501010001000F 00-11
2. 55AA01070005020100010010 12-23
3. 55AA01070005030100010011 24-35
4. 55AA01070005040100010113 36-47
5. 55AA0107000807020004000000001C 48-62
6. 55AA0107000808020004000000001D 63-77
7. 55AA0107000809020004000000001E 78-92
8. 55AA010700080A020004000000001F 93-107
9. 55AA0107000865020004000000047E 108-122
10. 55AA01070008660200040000844E4D 123-137
11. 55AA01070008670200040000BEA5DF 138-152
12. 55AA01070008680200040000097A00 153-167

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

Re: Serial MCU controlled relay/switch

#258 Post by enesbcs » 08 May 2019, 17:33

Mravko wrote:
08 May 2019, 16:03
third check
if (bytes_read == 25) { // packet start --> check if byte 0 is "0x55"
if (bytes_read == 26) { // check if packet is valid --> check if byte 1 is "0xAA"
You are on a bad track, "bytes_read" will never above 20. As i mentioned before only one package held at a time in the plugin buffer which is rollover on the next package everything else is held on the ESP serial buffer until it.

bkarpe
Normal user
Posts: 2
Joined: 25 Dec 2017, 15:52

Re: Serial MCU controlled relay/switch

#259 Post by bkarpe » 11 May 2019, 22:07

I got the LC-Tech module working using ESP Easy. As I understand it, the Serial MCU device communicates to the controller via TX. Does anyone know if this device is one-way or two-way? In other words, is the RX pin available? Also, are the GPIO0 and/or GPIO2 pins available? I want to attach a DHT22 to the relay to control a heater for a reptile tank on one relay. A light would be controller by a timer on the second relay.

bkarpe
Normal user
Posts: 2
Joined: 25 Dec 2017, 15:52

Re: Serial MCU controlled relay/switch

#260 Post by bkarpe » 12 May 2019, 22:10

Also, are the GPIO0 and/or GPIO2 pins available?
I have determined that both GPIO0 and GPIO2 pins are available. I have two devices (BME280 and an OLED) running on I2C (scl=GPIO2, sda=GPIO0), and the relays still work. I expect there would be no problem using a DHT22, but I'd have to give up the I2C interface. I will probably stick with the BME280. I haven't tested the RX pin, but I saw a schematic for the LC-Tech, and it looks like it's connected to the on-board processor.

It's amazing what you can do with a little ESP-01. I have a load of them. My NodeMCUs and Esp-32s seem like overkill.

Bohbe
Normal user
Posts: 6
Joined: 02 May 2019, 09:14
Location: Uppsala, Sweden

Re: Serial MCU controlled relay/switch

#261 Post by Bohbe » 18 May 2019, 20:08

I've just gone through that painstaking exercise of setting up PlatformIO and incorporating the plugin. I have worked it out.
basically there are a couple of things you need to do.
Mvrako, thanks for helping me out here! Sorry for the silence. One week away from home... I followed your excellent guide, and everything makes sense to me, but I ran in to one problem in the Platformio.ini file when trying to build. It says: "Error: Invalid `platformio.ini`, project configuration file: 'No option 'platform' in section: 'common''"

I can find the option 'platform' in section 'common' but in section 'normal' or other definitions:

[common]
board_build.f_cpu = 80000000L
build_flags = ${debug_flags.build_flags}
build_unflags = -DDEBUG_ESP_PORT
lib_deps = ""
lib_ignore = ESP32_ping, ESP32WebServer, IRremoteESP8266
lib_ldf_mode = chain
lib_archive = false
upload_speed = 115200
framework = arduino
board = esp12e
monitor_speed = 115200
;targets = size, checkprogsize
targets =

[regular_platform]
build_unflags =
build_flags = ${core_2_4_2.build_flags} ${common.build_flags}
platform = ${core_2_4_2.platform}

[beta_platform]
build_unflags =
build_flags = ${core_stage.build_flags} ${common.build_flags} -DARDUINO_ESP8266_RELEASE='"2.6.0-dev"'
platform = ${core_stage.platform}



[normal]
platform = ${regular_platform.platform}
build_flags = ${regular_platform.build_flags}

Are we looking at different Platformio.ini versions or am I stupid? Tried to find out from other builds what to use, but became a bit insecure here... :?

muppet96
Normal user
Posts: 4
Joined: 17 May 2019, 13:31

Re: Serial MCU controlled relay/switch

#262 Post by muppet96 » 18 May 2019, 20:19

Is there any reason that this doesn't work with an LCTECH relay on the latest firmware ESP82xx Core 2_4_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3 PUYA support
It works fine on an older version "Core Version: 2_4_0_RC2"
As far as I can see all the settings are the same, compiled on PlatformIO
Thanks.

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

Re: Serial MCU controlled relay/switch

#263 Post by enesbcs » 18 May 2019, 21:34

muppet96 wrote:
18 May 2019, 20:19
Is there any reason that this doesn't work with an LCTECH relay on the latest firmware ESP82xx Core 2_4_2, NONOS SDK 2.2.1(cfd48f3), LWIP: 2.0.3 PUYA support
It works fine on an older version "Core Version: 2_4_0_RC2"
As far as I can see all the settings are the same, compiled on PlatformIO
Thanks.
I guess that the ESP Core 2.4.2 serial library changed, and into a wrong direction...

Bohbe
Normal user
Posts: 6
Joined: 02 May 2019, 09:14
Location: Uppsala, Sweden

Re: Serial MCU controlled relay/switch

#264 Post by Bohbe » 19 May 2019, 17:18

I guess that the ESP Core 2.4.2 serial library changed, and into a wrong direction...
Ahh, so I need to get back to 2_4_0 as well?

Bohbe
Normal user
Posts: 6
Joined: 02 May 2019, 09:14
Location: Uppsala, Sweden

Re: Serial MCU controlled relay/switch

#265 Post by Bohbe » 20 May 2019, 01:05

Mvrako, I'm still struggling with Platformio to compile. Getting closer though. I think I have some discrepancies in versions. I get the following Error

Code: Select all

Compiling .pioenvs\easy_TUYA_1M\lib69a\SerialDevices\SensorSerialBuffer.cpp.o

lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp: In member function 'bool ESPeasySerial::hasOverrun()':
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp:228:21: error: 'class HardwareSerial' has no member named 'hasOverrun'
return getHW()->hasOverrun();
^
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp: In member function 'bool ESPeasySerial::hasRxError()':
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp:277:19: error: 'class HardwareSerial' has no member named 'hasRxError'
return getHW()->hasRxError();
^
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp: In member function 'void ESPeasySerial::startDetectBaudrate()':
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp:290:12: error: 'class HardwareSerial' has no member named 'startDetectBaudrate'
getHW()->startDetectBaudrate();
^
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp: In member function 'long unsigned int ESPeasySerial::testBaudrate()':
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp:301:19: error: 'class HardwareSerial' has no member named 'testBaudrate'
return getHW()->testBaudrate();
^
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp: In member function 'long unsigned int ESPeasySerial::detectBaudrate(time_t)':
lib\ESPEasySerial\ESPeasySerial_ESP8266.cpp:312:19: error: 'class HardwareSerial' has no member named 'detectBaudrate'
return getHW()->detectBaudrate(timeoutMillis);
^
*** [.pioenvs\easy_TUYA_1M\lib9e9\ESPEasySerial\ESPeasySerial_ESP8266.cpp.o] Error 1
I needed to get the platform defined and currently is 'platform = ${core_2_4_0.platform}', and I have tried several others with no luck. platformio.ini looks like this:

Code: Select all

; Easy Tuya (1M) device ----------------------
[env:easy_TUYA_1M]
upload_speed = ${common.upload_speed}
monitor_speed = ${common.monitor_speed}
framework = ${common.framework}
platform = ${core_2_4_0.platform}
lib_deps = ${common.lib_deps}
lib_ignore = ${common.lib_ignore}
lib_ldf_mode = ${common.lib_ldf_mode}
lib_archive = ${common.lib_archive}
board_upload.maximum_size = ${esp8266_1M.board_upload.maximum_size}
board_build.flash_mode = ${esp8266_1M.board_build.flash_mode}
board = ${esp8266_1M.board}
build_flags = ${esp8266_1M.build_flags} -D PLUGIN_SET_ONLY_TUYA
I'm probably doing some mixup with versions, so could you give me a clue what version you used when getting it into Platformio? Or should i use a differnt build environment? Thanks.

Post Reply

Who is online

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