Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 18:24 23 Jul 2026 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : PicoMiteWeb V5.07.07a20: Now with added MQTT

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11646
Posted: 07:46pm 21 Feb 2023
Copy link to clipboard 
Print this post

V5.07.07a20


PicoMiteWebV5.07.07a20.zip

New commands

WEB MQTT CONNECT ip_address$, port, username$, password$ [, interrupt]


WEB MQTT PUBLISH topic$, message$, [,qos] [,retain]

qos defaults to 1 - valid 0, 1, or 2
retain defaults to 0 - valid 0 or 1

WEB MQTT SUBSCRIBE topic$ [,qos]

qos defaults to 0 - valid 0, 1, or 2 - don't ask me what it does

WEB MQTT UNSUBSCRIBE topic$


WEB MQTT CLOSE


MM.TOPIC$ 'contains the last topic received


MM.MESSAGE$ 'contains the last message received


example program

WEB ntp
WEB mqtt close
WEB mqtt connect "192.168.1.108", 1883, "mqtt", "*****", myint
WEB mqtt subscribe "thermo"
Do
Bitbang humid gp1,a,b,1
WEB mqtt publish "thermo","The temperature at "+Time$+" is "+Str$(a)
Pause 2000
Loop
'
Sub myint
Print mm.topic$,":",mm.message$
End Sub


I've tested it on a MOSQUITTO broker running on my PC (easy to get up and running)

At the moment does not support TLS port 8883


Edited 2023-02-22 05:56 by matherp
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 411
Posted: 08:16pm 21 Feb 2023
Copy link to clipboard 
Print this post

Just started to learn about MQTT. QOS=Quality Of Service.
  Quote  MQTT can send messages with different so-called "Quality of Service" levels.
This means the publisher and subscriber can choose how reliably a message will be sent.
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 411
Posted: 08:19pm 21 Feb 2023
Copy link to clipboard 
Print this post

QoS level 0
Quality of Service (QoS) level 0 means messages are sent without any confirmation from the receiver. This means it is possible for a message to get lost, given an unreliable connection.

QoS level 1
QoS level 1 means the receiver must send a confirmation to let the sender know that the message was received.

However, with QoS 1, the receiver may get a single message multiple times. This QoS level ensures that a message makes it from sender to receiver but fails to ensure that it is received exactly once.

QoS level 2
QoS level 2 uses a four-step communication process to ensure a message is sent exactly once only, which can be necessary depending on the use case.

Source: https://elearning.easygenerator.com/
 
grroel.tech
Newbie

Joined: 09/12/2021
Location: Spain
Posts: 27
Posted: 01:07pm 22 Feb 2023
Copy link to clipboard 
Print this post

Hello,

Good job, the MQTT works fine:



It is a pity that PicoMite does not have support for the BME280 sensor.

Best regards.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11646
Posted: 01:22pm 22 Feb 2023
Copy link to clipboard 
Print this post

There is Basic code for the BME280 on this site somewhere.

My question about QOS is what setting it in Subscribe does. I. Understand its use in publish.
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 411
Posted: 01:54pm 22 Feb 2023
Copy link to clipboard 
Print this post

BME280
 
RonnS
Senior Member

Joined: 16/07/2015
Location: Germany
Posts: 123
Posted: 05:52pm 22 Feb 2023
Copy link to clipboard 
Print this post

and it works fine
  Pluto said  BME280
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 411
Posted: 07:12pm 22 Feb 2023
Copy link to clipboard 
Print this post

I am also using it since a few years back. Thanks to Peter!
 
LucV

Regular Member

Joined: 19/02/2023
Location: Netherlands
Posts: 62
Posted: 07:45pm 22 Feb 2023
Copy link to clipboard 
Print this post

Great job Peter !! Fantastic !!!      

Runs without a problem.
I am using my own Raspberry Pi Zero W as a broker.
Publish and subscribe without a problem.

I only saw that the username and password are required even if the broker does not use them. So I filled 11 in as username and 11 as password.

Thank you
Luc's tech Blog
 
Pluto
Guru

Joined: 09/06/2017
Location: Finland
Posts: 411
Posted: 09:00pm 22 Feb 2023
Copy link to clipboard 
Print this post

  matherp said  My question about QOS is what setting it in Subscribe does. I. Understand its use in publish.


If I understand correctly, a subscriber can also demand the level of "QoS" (Quality of Service). If the subscriber demands QoS=1, the subscriber demands that the message will be repeatedly sent until the subscriber sends an Acknowledge (PUBACK,publish acknowledgment) back to the "broker". For QoS=0 no Acknowledgement is required by the subscriber. Not sure if the MQTT-broker can overrun the QoS wishes of the subscriber.
 
homa

Guru

Joined: 05/11/2021
Location: Germany
Posts: 651
Posted: 06:16pm 29 Jul 2023
Copy link to clipboard 
Print this post

  LucV said  Great job Peter !! Fantastic !!!      

Runs without a problem.
I am using my own Raspberry Pi Zero W as a broker.
Publish and subscribe without a problem.

I only saw that the username and password are required even if the broker does not use them. So I filled 11 in as username and 11 as password.

Thank you



Hi Luc,

Can you have a look here? Thanks.
https://www.thebackshed.com/forum/ViewTopic.php?FID=16&TID=16080

Matthias
 
richard59715
Newbie

Joined: 20/07/2026
Location: United States
Posts: 1
Posted: 02:13am 22 Jul 2026
Copy link to clipboard 
Print this post

I have webmite 6.03 on pico W connected to my local network on IP 192.168.1.157 and an mqtt broker on 192.168.1.107. I can run mosquitto_sub -h 192.168.1.107 -p 1883 -u test -P c16txtxx -t pump/p -v on any machine on the network and it connects indicating the broker is configured correctly. But this code:
WEB MQTT close
WEB MQTT CONNECT "192.168.1.107", 1883, "test", "c16txtxx"

DO
   BME280_Read temp, hum, press

   PRINT "Temperature: ", temp, " F"
   PRINT "Humidity:    ", hum, " %"
   PRINT "Pressure:    ", press, " inHg"
   PRINT

WEB MQTT PUBLISH "Test T", str$(temp)), 1, 0

   PAUSE 5000
LOOP

fails on line WEB MQTT CONNECT
with:Connecting to 192.168.1.107 port 1883
[145] WEB MQTT CONNECT "192.168.1.107", 1883, "test", "c16txtxx",mqttint
Error : Failed to connect

What am I doing wrong?
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11646
Posted: 07:51am 22 Jul 2026
Copy link to clipboard 
Print this post

It is probably a firewall/permissioning issue on the PC, I haven't tested recently but remember it was a nightmare getting it set up when I first implemented the code. The 6.03.00 mqtt code was tested against remote brokers without any issues so I'm reasonably confident in it. Try the following program. Assuming it works (does for me)  change it to point to your local broker
Edit the two Const lines at the top and add credentials to the Connect line if needed:
Const Broker$ = "192.168.1.x"    ' their local broker
Const Port    = 1883
...
Web Mqtt Connect Broker$, Port, "user", "pass", OnMqtt   ' if the broker needs auth

' ---------------------------------------------------------------------------
' mqtt_test.bas  -  MQTT round-trip test against a public external broker
' ---------------------------------------------------------------------------
' Purpose: prove the WebMite MQTT client works end-to-end (CONNECT, SUBSCRIBE,
' PUBLISH, incoming message, UNSUBSCRIBE, CLOSE) against an anonymous broker
' on plain port 1883 (no TLS). If this passes but your LOCAL broker fails,
' the problem is with the local broker's configuration / permissions
' (anonymous access, ACLs, listener binding, firewall) - not the MMBasic code.
'
' The broker below is public and allows anonymous connections on port 1883.
' Swap Broker$ for "test.mosquitto.org" if hivemq is unreachable.
' ---------------------------------------------------------------------------

Const Broker$ = "broker.hivemq.com"      ' public, anonymous, plain MQTT
Const Port    = 1883                     ' plain (non-TLS) MQTT port

Dim mqttGot = 0                          ' set by the OnMqtt interrupt

Sub OnMqtt
 mqttGot = 1
End Sub

' Unique topic + payload so a stale retained message can't fool us
Dim Integer stamp = Timer Mod 1000000
Dim topic$ = "picomite-test/" + Str$(stamp)
Dim msg$   = "hello-" + Str$(stamp)
Dim Integer t

Print
Print "MQTT external-broker test"
Print "-------------------------"
Print "Local IP   : "; MM.Info$(IP Address)
Print "Broker     : "; Broker$; " : "; Str$(Port)
Print "Topic      : "; topic$
Print "Payload    : "; msg$
Print

' --- CONNECT (anonymous: empty user + empty password) ----------------------
mqttGot = 0
On Error Skip 1
Web Mqtt Connect Broker$, Port, "", "", OnMqtt
If MM.ErrNo <> 0 Then
 Print "CONNECT   : FAIL - "; MM.ErrMsg$
 Print
 Print "Could not reach the external broker. Check WiFi / DNS / internet."
 End
EndIf
Print "CONNECT   : OK"

' --- SUBSCRIBE -------------------------------------------------------------
On Error Skip 1
Web Mqtt Subscribe topic$
If MM.ErrNo <> 0 Then
 Print "SUBSCRIBE : FAIL - "; MM.ErrMsg$
 Web Mqtt Close
 End
EndIf
Print "SUBSCRIBE : OK"

' Let the SUBACK land before we publish
Pause 200

' --- PUBLISH ---------------------------------------------------------------
On Error Skip 1
Web Mqtt Publish topic$, msg$
If MM.ErrNo <> 0 Then
 Print "PUBLISH   : FAIL - "; MM.ErrMsg$
 Web Mqtt Close
 End
EndIf
Print "PUBLISH   : OK"

' --- WAIT FOR ROUND-TRIP ---------------------------------------------------
t = Timer + 5000
Do While Timer < t And mqttGot = 0
 Pause 50
Loop

Print
If mqttGot Then
 Print "ROUND-TRIP: OK - message came back"
 Print "  topic   : "; MM.Topic$;   " ("; Choice(MM.Topic$   = topic$, "match", "MISMATCH"); ")"
 Print "  payload : "; MM.Message$; " ("; Choice(MM.Message$ = msg$,   "match", "MISMATCH"); ")"
 Print
 Print "PASS - external MQTT works. If your LOCAL broker fails, it is a"
 Print "       broker permission/config issue, not the firmware."
Else
 Print "ROUND-TRIP: FAIL - no message received within 5 s"
 Print "  Broker accepted CONNECT/SUBSCRIBE/PUBLISH but did not echo back."
EndIf

On Error Skip 1
Web Mqtt Unsubscribe topic$
Web Mqtt Close
Print
Print "Done."
End
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2026