Problem receiving many MQTT messages

Recurrent H/W and software problems
Post Reply
ArminW
Posts: 3
Joined: Tue Nov 21, 2023 10:49 pm

Problem receiving many MQTT messages

Post by ArminW »

Hello all

As this is my first post here, a quick intro: I am using ESP8266 and ESP32 for somewhat like 5 years now for various home automation stuff.
So far I always used the Arduino IDE to feed them but the compile-upload-run cycle is a bit longish and so it's not well suitable for playing around.
Searching for an interpreter language I stumbled across Annex32 and I am pretty impressed what's all in there.

Excellent job cicciocb!

After playing a bit with Annex32, I now tried to create the first "serious" application. I subscribes to 16 topics from a MQTT broker (ioBroker in my case) and displays 16 icons depending on the state of each topic.

I am using Annex32 CAN 1.51.5 LFS

In my first test I did the drawing of the bitmap directly in the message handler after receiving the MQTT message. That horribly failed and only a few of the icons were displayed.
After implementing a "ring buffer" where MQTT messages are written into and processed outside of the event handler, it got better.
After adding a 200ms delay between the subscriptions, at least all is displayed correctly after startup.

But when a display mode change happens in ioBroker (which can change a lot of the states at once), Annex is always missing some of the messages.

Am I doing something wrong or is it a problem of Annex32?

My code below.

Thanks in advance!
Armin

Code: [Local Link Removed for Guests]

tft.init 0
tft.fill 0
'touch.calib

testdata:
data "javascript/0/faces/val00", "Test00", "door"
data "javascript/0/faces/val01", "Test01", "door"
data "javascript/0/faces/val02", "Test02", "door"
data "javascript/0/faces/val03", "Test03", "door"
data "javascript/0/faces/val10", "Test10", "door"
data "javascript/0/faces/val11", "Test11", "door"
data "javascript/0/faces/val12", "Test12", "door"
data "javascript/0/faces/val13", "Test13", "door"
data "javascript/0/faces/val20", "Test20", "door"
data "javascript/0/faces/val21", "Test21", "door"
data "javascript/0/faces/val22", "Test22", "door"
data "javascript/0/faces/val23", "Test23", "door"
data "javascript/0/faces/val30", "Test30", "door"
data "javascript/0/faces/val31", "Test31", "door"
data "javascript/0/faces/val32", "Test32", "door"
data "javascript/0/faces/val33", "Test33", "door"

arrSize=15
dim states$(arrSize)
dim labels$(arrSize)
dim images$(arrSize)
dim values(arrSize)
restore testdata
for i=0 to arrSize
  read states$(i), labels$(i), images$(i)
  values(i)=-1
next i

queueSize=31
dim topics$(queueSize)
dim messages$(queueSize)
wptr=0
rptr=0

onmqtt mqttEvent
j=mqtt.setup("mqtt://172.28.2.12:1884", 0)
j=mqtt.connect("", "")
pause 500
for i=0 to arrSize
  s$=states$(i)
  j=mqtt.subscribe(s$, 0)
  pause 200
next i

while 1=1
  if rptr<>wptr then
    's$=time$+" Topic: "+topics$(rptr)+" Message:"+messages$(rptr)
    'wlog s$
    for i=0 to arrSize
      if topics$(rptr)=states$(i) then
        j=val(messages$(rptr))
        if values(i)<>j then
          values(i)=j
          drawPic i
        end if  
        exit for
      end if
    next i
    if rptr>=queueSize then rptr=0 else rptr=rptr+1
  end if
  pause 10  
wend

wait

sub drawPic(picToDraw)
  k=values(picToDraw)
  s$="/"+images$(picToDraw)+chr$(48+k)+".bmp"
  x=(picToDraw mod 4)*60
  y=(picToDraw \ 4)*80
  tft.bmp s$, x, y
  if k=0 then
    tft.rect x, y+60, 60, 20, tft.rgb(76, 231, 60), 1
  else
    tft.rect x, y+60, 60, 20, tft.rgb(204, 46, 80), 1
  end if
  tft.text.color tft.rgb(0, 0, 0)
  tft.text.align align_top_mid
  tft.text.draw labels$(i), x+30, y+60, 2
end sub

mqttEvent:
  if mqtt.message$<>"" then
    topics$(wptr)=mqtt.topic$
    messages$(wptr)=mqtt.message$
    if wptr>=queueSize then wptr=0 else wptr=wptr+1
  end if
return 
User avatar
cicciocb
Site Admin
Posts: 2195
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 470 times
Been thanked: 1461 times
Contact:

Re: Problem receiving many MQTT messages

Post by cicciocb »

Hello, welcome to the group.

If I remember well, there is no buffer inside annex for the messages, this means that if a new message arrives, if the previous one has not been read, it is lost.
ArminW
Posts: 3
Joined: Tue Nov 21, 2023 10:49 pm

Re: Problem receiving many MQTT messages

Post by ArminW »

Thanks for your quick reply.

Well, that explains it... So I will compose all the values in one topic in the broker, only subscribe to that and make sure there is a minimum delay between changes. That should solve it.

Have a nice day
Armin
User avatar
cicciocb
Site Admin
Posts: 2195
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 470 times
Been thanked: 1461 times
Contact:

Re: Problem receiving many MQTT messages

Post by cicciocb »

[Local Link Removed for Guests] wrote: [Local Link Removed for Guests]Sun Jan 07, 2024 1:44 pm Thanks for your quick reply.

Well, that explains it... So I will compose all the values in one topic in the broker, only subscribe to that and make sure there is a minimum delay between changes. That should solve it.

Have a nice day
Armin
I'll see if is possible to put a queue inside, avoiding to lose messages
ArminW
Posts: 3
Joined: Tue Nov 21, 2023 10:49 pm

Re: Problem receiving many MQTT messages

Post by ArminW »

That would be wonderful :)

Thanks
Armin
Post Reply