|
Forum Index : Microcontroller and PC projects : PicoMiteWeb V5.07.07a20: Now with added MQTT
| Author | Message | ||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11646 |
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: FinlandPosts: 411 |
Just started to learn about MQTT. QOS=Quality Of Service. |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 411 |
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: SpainPosts: 27 |
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 KingdomPosts: 11646 |
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: FinlandPosts: 411 |
BME280 |
||||
| RonnS Senior Member Joined: 16/07/2015 Location: GermanyPosts: 123 |
and it works fine |
||||
| Pluto Guru Joined: 09/06/2017 Location: FinlandPosts: 411 |
I am also using it since a few years back. Thanks to Peter! |
||||
| LucV Regular Member Joined: 19/02/2023 Location: NetherlandsPosts: 62 |
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: FinlandPosts: 411 |
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: GermanyPosts: 651 |
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 StatesPosts: 1 |
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" 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 KingdomPosts: 11646 |
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 "MQTT external-broker test" Print "-------------------------" Print "Local IP : "; MM.Info$(IP Address) Print "Broker : "; Broker$; " : "; Str$(Port) Print "Topic : "; topic$ Print "Payload : "; msg$ ' --- 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 "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 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 "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 "Done." End |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |