Handle multiple bytes via Serial Server to / from MQTT

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
trabi-basti
New user
Posts: 2
Joined: 19 Apr 2023, 00:03

Handle multiple bytes via Serial Server to / from MQTT

#1 Post by trabi-basti » 17 Jul 2023, 15:06

Hello everyone,

my current project is to control my projector UHD370X (manufacturer: Optoma) per remote and to integrate it into my SmartHome automation.
The projector has a serial port (RS232, D-Sub9) which I want to use for that. Unfortunately I didn't found pretty much information about using the "Serial Server" of ESP Easy.
I want to use a "WEMOS D1 Mini", currently flashed with release "ESP_Easy_mega_20230623_normal_ESP8266_4M1M" (Jun 23 2023) and use the hardware serial port "0" (GPIO-3 + GPIO-1).
I connected the WEMOS via a level shifter (Wemos: TTL level, RS232 +/- 15V) to the projector and tried to send a command via MQTT.

I need to send Hex strings like "7E 30 30 31 35 30 20 31 0D" to the projector, it will answer like "4F 6B 30 30 30 30 32 38 30 30 30 43 30 38 30 30 0D".
This works as I tested it before with direct connection from laptop to projector using a serial terminal tool (e.g. "Realterm" or "Hterm").

Now I have serveral problems.
1. Generic - MQTT Import: If data is received it is converted to a float value (MQTT topic 1, value 1). This huge string above can not be converted correctly into an integer value (range overflow). So I tried to send every single byte via an own MQTT message and store it in max. 12 different variables ("Let,3,%eventvalue%" .. "Let,13,%eventvalue%") until all bytes are transmitted. I used twi different MQTT topics for that "Bytewert" - the value to be transmitted (stored in int#1) and "Byteposition" (stored in int#2) to tell the ruIe to which variable the value have to be assigned to. Is there a more comfortable way to handle this?
2. If the last received byte matches "OD" the string is completed and I sent it out via "serialsendmix" command from "Communication - Serial Server", adding each byte from before ("[int#3]" .. "[int#12]"). Since "OD" received via MQTT and convered to integer (0x0D = 13 dec.) before and converting it back to hex for sendig it via the command, the leading "0" of "0D" is lost. Reading back the request above via MQTT for debugging results in "0x7e30303135302031d", as it can be seen, the last "byte" is only "d" instead of "0d". So the string is wrong now and the projector does not understand by request. Is there a possibility to get the leading "0" at the conversation from int to hex? I used "{tohex:[int#3]}".

Rules Set 1:

Code: Select all

On MQTT2Serial#Byteposition* do
 Let,2,abs(%eventvalue%)  // Variable 2 = Byteposition (Variable 1 = Bytewert, siehe RuleSet 2)
 If [int#2] = 1
  If [int#1] = 0x7E  // 1. Byte muss bei valider Anfrage den Wert 0x7E haben
   Let,3,[int#1]
   Let,15,0
  Else
   Let,3,0xFF
   Let,15,0
  Endif
 Endif 
 If [int#2] = 2
  Let,4,[int#1]
 Endif
 If [int#2] = 3
  Let,5,[int#1]
 Endif
 If [int#2] = 4
  Let,6,[int#1]
 Endif
 If [int#2] = 5
  Let,7,[int#1]
 Endif
 If [int#2] = 6
  Let,8,[int#1]
 Endif
 If [int#2] = 7
  Let,9,[int#1]
 Endif
 If [int#2] = 8
  Let,10,[int#1]
 Endif
 If [int#2] = 9
  Let,11,[int#1]
  If [int#1] = 13
   serialsendmix,0x{tohex:[int#3]}{tohex:[int#4]}{tohex:[int#5]}{tohex:[int#6]}{tohex:[int#7]}{tohex:[int#8]}{tohex:[int#9]}{tohex:[int#10]}{tohex:[int#11]}
   Publish, %sysname%/debugging,0x{tohex:[int#3]}{tohex:[int#4]}{tohex:[int#5]}{tohex:[int#6]}{tohex:[int#7]}{tohex:[int#8]}{tohex:[int#9]}{tohex:[int#10]}{tohex:[int#11]}
   Let,15,1
  Endif 
 Endif
 If [int#2] = 10
  Let,12,[int#1]
  If [int#1] = 13
   serialsendmix,0x{tohex:[int#3]}{tohex:[int#4]}{tohex:[int#5]}{tohex:[int#6]}{tohex:[int#7]}{tohex:[int#8]}{tohex:[int#9]}{tohex:[int#10]}{tohex:[int#11]}{tohex:[int#12]}
   Let,15,1
  Endif
 Endif
 If [int#2] = 11
  Let,13,[int#1]
  If [int#1] = 13
   serialsendmix,0x{tohex:[int#3]}{tohex:[int#4]}{tohex:[int#5]}{tohex:[int#6]}{tohex:[int#7]}{tohex:[int#8]}{tohex:[int#9]}{tohex:[int#10]}{tohex:[int#11]}{tohex:[int#12]}{tohex:[int#13]}
   Let,15,1
  Endif
 Endif
 If [int#2] = 12
  Let,14,[int#1]
  If [int#1] = 13
   serialsendmix,0x{tohex:[int#3]}{tohex:[int#4]}{tohex:[int#5]}{tohex:[int#6]}{tohex:[int#7]}{tohex:[int#8]}{tohex:[int#9]}{tohex:[int#10]}{tohex:[int#11]}{tohex:[int#12]}{tohex:[int#13]}{tohex:[int#14]}
   Let,15,1
  Endif
 Endif
 If [int#15] = 1
  Let,1,255
  Let,2,0
  Let,3,255
  Let,4,255
  Let,5,255
  Let,6,255
  Let,7,255
  Let,8,255
  Let,9,255
  Let,10,255
  Let,11,255
  Let,12,255
  Let,13,255
  Let,14,255
  Let,15,0
 Endif
endon
Rules Set 2:

Code: Select all

On MQTT2Serial#Bytewert* do
 Let,1,abs(%eventvalue%)
endon

On !Serial#* do
 Publish, %sysname%/serial, {substring:8:25:%eventvalue%}
endon

Devices:
Image

Serial Server Setup:
Image

MQTT Setup:
Image

MQTT debugging message:
Image



Thanks a lot for your help and investigations!

Best regards,
Sebastian

User avatar
Ath
Normal user
Posts: 3521
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Handle multiple bytes via Serial Server to / from MQTT

#2 Post by Ath » 17 Jul 2023, 15:26

I don't currently see a way to solve that nicely, but as a workaround, you can insert the 0 hard-coded in every sendserialmix command, by inserting the extra 0 between the last 2 bytes, like this:

Code: Select all

...{tohex:[int#13]}0{tohex:[int#14]}
(1 example only, apply to other lines too)
as you know that the last byte will always be 0xD ;)

I'll see if I can add an extra argument to the toHex function to specify the minimum width of the output.
/Ton (PayPal.me)

User avatar
Ath
Normal user
Posts: 3521
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Handle multiple bytes via Serial Server to / from MQTT

#3 Post by Ath » 17 Jul 2023, 21:47

Ath wrote: 17 Jul 2023, 15:26 I'll see if I can add an extra argument to the toHex function to specify the minimum width of the output.
I've created a PR #4736 to add an optional extra min. hex digits arguments. A GH Actions build is running, and once completed you can download from there for testing. (You'll need a free Github account to be able to download)
/Ton (PayPal.me)

trabi-basti
New user
Posts: 2
Joined: 19 Apr 2023, 00:03

Re: Handle multiple bytes via Serial Server to / from MQTT

#4 Post by trabi-basti » 17 Jul 2023, 22:13

Hi Ath,

thanks a lot for your replies and effort!
I already have a Github account so I will have a look there for any news.

Best regards,
Sebastian

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests