Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 18:35 18 Apr 2024 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 : Connecting a pico w  to a MQTT server (mosquitto,linux)

Author Message
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 08:28pm 19 May 2023
Copy link to clipboard 
Print this post

I have a pico w runniung WebMite 5.07.07 using the options:

WebMite MMBasic Version 5.07.07
OPTION SYSTEM SPI GP10,GP11,GP12
OPTION AUTORUN 3
OPTION LIBRARY_FLASH_SIZE  14000
OPTION COLOURCODE ON
OPTION DISPLAY 50, 100
OPTION WIFI FritzBox, *********************
OPTION TELNET CONSOLE ON
OPTION SDCARD GP15
OPTION AUDIO GP18,GP19, ON PWM CHANNEL 1


This pico w has connected a PIR motion sensor connected to pin gp6

I want to get the motion detection signals on any pc/mobile by using a mqtt subscription to a mqtt message broker.

There are some tasks to do:

1. Connect the pico w reliable to the local wlan without user interaction
2. Install a MQTT server on some local pc i.e. a raspberry pi
3. Generate motion messages and send them to the MQTT server
4. Receive messages on any pc connected to the MQTT server

1. Connect the pico w reliable to the local wlan without user interaction

I'm using a FritzBox 7590 as wifi and internet router. Unfortunatelly not everytime
when I switch on the pico w it is connecting to the wireless lan. So I had to power cycle the pico to get it connected.
Then I used the "library feature" of WebMite to write this code to get a reliable connection without any user interaction:

> library list
Sub Connect
   Do While MM.Info(IP ADDRESS) = "0.0.0.0"
       If Timer > 6000 Then CPU restart
   Loop
   WEB ntp 2
   Print "IP: ";MM.Info(ip address)
End Sub

Sub  DoConnect
   If MM.Info(IP ADDRESS) = "0.0.0.0" Then
       Connect
   EndIf
End Sub

DoConnect


The pico will reboot as long as there is no wireless connection. OPTION WIFI has to be declared before using "SSID" and "wifi password" of the wireless lan.


2. Install a MQTT server on some local pc i.e. a raspberry pi (ip address = 192.168.178.54)

My Raspberry Pi 4 is running 24 hours and serving a nextcloud and adguard-home (DNS filter). Now I have added the MQTT server this way:

sudo apt update && sudo apt upgrade
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service
sudo nano /etc/mosquitto/mosquitto.conf

   adding lines at the end:

   listener 1883
   allow_anonymous true

   save&exit with 'ctrl x' and 'y'

sudo systemctl restart mosquitto.service


and last but not least adding a rule to iptables to allow connections to port 1883:
-A INPUT -d 192.168.178.54 -p tcp --dport 1883 -j ACCEPT
I do this by modifying the /etc/iptables/rules.v4 file.


3. Generate motion messages and send them to the MQTT server

I wrote this program and saved it as flash 3 (flash save 3) so it is auto started when the pico comes to live.

' pir detector
SetPin gp6, din, PULLDOWN ' PIR sensor
Drive "B:" ' Logfile to sd-card
WEB ntp 2 ' Set RTC from ntp server MESZ (+2)

Pause 1000

Sub ThereIsMotion()
   ' connect to MQTT broker (mosquitto) on a raspberry pi 4
   WEB MQTT CONNECT "192.168.178.54",1883,"anon","anon"
   Print Date$, Time$," motion detected"
   Open "logfile.bas" For append As #1
       Print #1, Date$, Time$," motion detected"
   Close #1
   WEB MQTT PUBLISH "motion",Chr$(34)+Date$+","+Time$+", motion detected webmiteb"+Chr$(34)
   WEB MQTT CLOSE
End Sub

Do ' main loop
 If Pin(gp6) = 1 Then ' PIR sensor has detected a motion
   ThereIsMotion
 EndIf
 Pause 3000 ' prevent too many motions in a short time
Loop



4. Receive messages on any pc connected to the MQTT server

Now I can use any other pc/mobile within my lan and subscribe to the motion messages:
first I install the MQTT client:

sudo apt install mosquitto-clients

then I can subscribe to the "motion" messages:

mosquitto_sub -h 192.168.178.54 -t motion

The host 192.168.178.54 is the raspberry pi acting as message broker.

The output is:
andreas@weizenbaum:~$ mosquitto_sub -h 192.168.178.54 -t motion
"19-05-2023,21:14:58, motion detected webmiteb"
"19-05-2023,22:07:00, motion detected webmiteb"


And this is "webmiteb" the sensor pico connected to the PIR, akku and maker pi pico pcb.



I'm open for comments (this is my first experience with MQTT) - I hope there are not
too many errors in the text. At least it is working on my desk ;-)

-andreas
 
PhilP

Newbie

Joined: 25/01/2017
Location: United Kingdom
Posts: 31
Posted: 09:57am 20 May 2023
Copy link to clipboard 
Print this post

Andreas - very interesting and well described process which I will try as I want to try MQTT. I am also having difficulty in always connecting to the WLAN at switch on and have to reapply the power. Your step 1 seems like an excellent solution which I will implement in my WebMite emailing project. Once connected to the WLAN, emailing works well to alert me to various events.
Phil
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3488
Posted: 12:16pm 20 May 2023
Copy link to clipboard 
Print this post

Andreas, thans for this. Nice to have a guide When you want to do this yourself

Volhout
PicomiteVGA PETSCII ROBOTS
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 07:39am 21 May 2023
Copy link to clipboard 
Print this post

  PhilP said  Andreas - very interesting and well described process which I will try as I want to try MQTT. I am also having difficulty in always connecting to the WLAN at switch on and have to reapply the power. Your step 1 seems like an excellent solution which I will implement in my WebMite emailing project. Once connected to the WLAN, emailing works well to alert me to various events.
Phil


Hello PhilP,

I'm glad if you can use parts of my findings  

It seems to be, that the "Library" is only executed when there is at least a single line in the program store, but this is most often the case.

I have added a "DoConnect" from my library within the main loop, to make sure that the pico is always connected. May be the DHCP lease is running out after some hours and I'm not sure whther the tcp-stack is programed in a way to handle that, but after adding this statement I have a constant connection to the pico.

> list
' pir detector
SetPin gp6, din, PULLDOWN ' PIR sensor
Drive "B:" ' Logfile to sd-card
WEB ntp 2 ' Set RTC from ntp server MESZ (+2)

Pause 1000

Sub ThereIsMotion()
 ' connect to MQTT broker (mosquitto) on a raspberry pi 4
 WEB MQTT CONNECT "192.168.178.54",1883,"anon","anon"
 Print Date$, Time$," motion detected"
 Open "logfile.bas" For append As #1
   Print #1, Date$, Time$," motion detected"
 Close #1
 WEB MQTT PUBLISH "motion",Chr$(34)+Date$+","+Time$+", motion detected webmiteb"+Chr$(34)
 WEB MQTT CLOSE
End Sub

Do ' main loop
 DoConnect
 If Pin(gp6) = 1 Then ' PIR sensor has detected a motion
   ThereIsMotion
 EndIf
 Pause 1500 ' depents on the potentiometer settings of the PIR sensor!
Loop


-andreas
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 07:42am 21 May 2023
Copy link to clipboard 
Print this post

  Volhout said  Andreas, thans for this. Nice to have a guide When you want to do this yourself

Volhout


Hello Volhout,

please do it! I like to program and to experiment, but I'm not a good documenter  
I have not much experience using Windows for this kind of job. May be anyone else can add those infos?

-andreas
Edited 2023-05-21 17:44 by andreas
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 08:01am 21 May 2023
Copy link to clipboard 
Print this post

By the way, I found a very nice Android app "IoT MQTT Panel" which works very good with the mosquitto MQTT broker on the raspberry Pi! There one can create a monitoring panel and add all the sensors.



I have a wireguard VPN between my mobile phone and the FritzBox - so I'm allways connected to my lan and can monitor the picos without opening a port or service to the WAN side of the FritzBox. Even my nextcloud is accessible from everywhere without opening a web port. ;-)

-andreas
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 08:46am 21 May 2023
Copy link to clipboard 
Print this post

After reading the posting of matherp
The IP address is undefined unless connected.
about the correct use of checking the connection status I changed the library to this:

> library list
Sub Connect
Do While MM.Info(TCPIP status) <> 3
 If Timer > 6000 Then CPU restart
Loop
WEB ntp 2
Print "IP: ";MM.Info(ip address)
End Sub
Sub  DoConnect
If MM.Info(TCPIP STATUS) <> 3 Then
 Connect
EndIf
End Sub
DoConnect


"3" means connected to wifi AND assigned ip address !

-andreas
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 187
Posted: 07:47am 22 May 2023
Copy link to clipboard 
Print this post

I have a tiny modification to the main loop: the addition of a watchdog timer

Sub ThereIsMotion()
 ' connect to MQTT broker (mosquitto) on a raspberry pi 4
 WEB MQTT CONNECT "192.168.178.54",1883,"anon","anon"
 WEB MQTT PUBLISH "motion",Chr$(34)+Date$+","+Time$+", motion detected webmitec"+Chr$(34)
 WEB MQTT CLOSE
End Sub

Do ' main loop
 DoConnect
 WatchDog 30000 ' restart cpu if this line is not passed every 30 seconds.
 If Pin(gp6) = 1 Then ' PIR sensor has detected a motion
   ThereIsMotion
 EndIf
 Pause 1500 ' prevent too many motions in a short time
Loop


As described in the manuals, the WATCHDOG timer is essential for continuous operation. After this mod I was able to access the Pico 24/7 and hope it is stable now.

-andreas
 
Print this page


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

© JAQ Software 2024