Picomotion
Angelegt Samstag 20 Mai 2023
Ein frei programmierbarer PIR Bewegungsmelder mit MQTT Interface, WLAN, telnet, TFTP für 10.- €
Raspberry Pi Pico W mit PIR Bewegungsmelder
Eigenschaften
- WLAN Interface
- Telnet Interface
- TFTP Interface
- NTP Zeit
- MQTT Client
- WATCHDOG
- BASIC programmierbar
- Onboard Dateisystem
- Kosten ohne Stromversorgung < € 10.-
- Kosten mit Steckernetzteil und z.B. 5m USB Kabel < € 20.-
Der PIR Sensor hat 3 Anschlüsse und ist wie folgt mit dem Pico verbunden:
PIR . . . . . . PICO
VCC ---------- VSYS
OUT ---------- GP6
GND --------- GND
Die Software
OPTIONEN des Pico W
WebMite MMBasic Version 5.07.07 <- Firmware
OPTION AUTORUN 3
OPTION LIBRARY_FLASH_SIZE 14000
OPTION COLOURCODE ON
OPTION HEARTBEAT OFF
OPTION DISPLAY 50, 100
OPTION WIFI FritzBox, *********************
OPTION TELNET CONSOLE ON
Das Programm: motion.bas
' Sensor C SetPin gp6, din, PULLDOWN ' PIR sensor On ERROR SKIP 3 WEB ntp 2,"192.168.178.1" ' Set RTC from ntp server MESZ (+2) If MM.Errno Then WEB NTP 2,"192.168.178.1" If MM.Errno Then Print "Verbindungsfehler" : CPU RESTART Open "logfile.bas" For append As #1 If Lof(#1) < 5 Then Print #1, "New Logfile: "+Date$+" "+Time$ EndIf If Lof(#1) > 20000 Then Close #1 Rename "logfile.bas" As "logfile-old.bas" Open "logfile.bas" For append As #1 Print #1, "New Logfile: "+Date$+" "+Time$ EndIf Print #1, "A: "+Date$+" "+Time$ Close #1 Pause 1000 Sub ThereIsMotion() ' connect to MQTT broker (mosquitto) on a raspberry pi 4 ON ERROR SKIP 3 WEB MQTT CONNECT "192.168.178.54",1883,"anon","anon" WEB MQTT PUBLISH "motion",Chr$(34)+Date$+","+Time$+", Bewegung Raum C"+Chr$(34) WEB MQTT CLOSE End Sub Do ' main loop DoConnect WatchDog 60000 ' restart cpu if this line is not passed within 60 seconds. If Pin(gp6) = 1 Then ' PIR sensor has detected a motion ThereIsMotion EndIf forcedreboot("02:00:05") Pause 1500 ' slow down Loop
Hinweis: Das onboard Logfile ist nur dazu da, die Einschaltvorgänge zu protokollieren, nicht die Bewegungen. Diese werden zum MQTT Server gesendet.
Als NTP Server verwende ich meine FritzBox.
Die Library
Die Library ist ein WebMite Feature, welches die Programmierung eigener BASIC-Befehle gestattet und automatisch geladen wird. Diese "Autostart" Eigenschaft kann dafür genutzt werden den Pico sicher mit einem WLAN zu verbinden. Zusätzlich muss dem Pico über die OPTION WIFI "SSID","PASSWORT" mitgeteilt werden, mt welchem WLAN er sich verbinden soll. Dies muss nur einmal geschehen und wird bei folgenden Starts automatisch durchgeführt.
Function st(t$) As integer st = Val(Mid$(t$,1,2)) End Function Function mi(t$) As integer mi = Val(Mid$(t$,4,2)) End Function Function se(t$) As integer se = Val(Mid$(t$,7,2)) End Function Sub forcedreboot t$ If st(t$) = st(Time$) And mi(t$) = mi(Time$) And Abs(se(Time$) - se(t$)) < 3 Then CPU restart EndIf End Sub Sub DoConnect Do While MM.Info(TCPIP STATUS) <> 3 If Timer > 6000 Then CPU restart Loop End Sub DoConnect
Die Flashbelegung
> flash list
Slot 1 in use: "'#A:/library.bas" <- Quellcode der Library
Slot 2 available
Slot 3 in use: "' Sensor C" <- Autostart Programm ("motion.bas")
Slot 4 in use: Library <- aktive Library
Das Dateisystem
> files
A:/
<DIR> .
<DIR> ..
00:00 01-01-2000 4 bootcount
00:00 01-01-2000 265 library.bas
15:13 22-05-2023 202 logfile.bas
12:32 22-05-2023 939 motion.bas
2 directories, 4 files, 602112 bytes free
Die Logdatei: logfile.bas
> list "logfile.bas"
New Logfile: 22-05-2023 13:04:15
C: 22-05-2023 13:04:15
C: 22-05-2023 13:21:28
C: 22-05-2023 13:31:26
C: 22-05-2023 13:38:34
C: 22-05-2023 13:45:42
C: 22-05-2023 15:07:40
C: 22-05-2023 15:13:28
Der MQTT CLient (IoT MQTT panel) auf meinem Android Handy
Das klappt via wireguard vpn von beliebigen Standorten ;)
Eine telnet Session zum Sensor
andreas@weizenbaum:~$ telnet webmitec Trying 192.168.178.85... Connected to webmitec.fritz.box. Escape character is '^]'. > list ' Sensor C SetPin gp6, din, PULLDOWN ' PIR sensor On ERROR SKIP 3 WEB ntp 2,"192.168.178.1" ' Set RTC from ntp server MESZ (+2) If MM.Errno Then WEB NTP 2,"192.168.178.1" If MM.Errno Then Print "Verbindungsfehler" : CPU RESTART Open "logfile.bas" For append As #1 If Lof(#1) < 5 Then Print #1, "New Logfile: "+Date$+" "+Time$ EndIf If Lof(#1) > 20000 Then Close #1 Rename "logfile.bas" As "logfile-old.bas" Open "logfile.bas" For append As #1 Print #1, "New Logfile: "+Date$+" "+Time$ EndIf Print #1, "C: "+Date$+" "+Time$ Close #1 Pause 1000 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$+", Bewegung Raum C"+Chr$(34) WEB MQTT CLOSE End Sub Do ' main loop DoConnect WatchDog 60000 ' restart cpu if this line is not passed within 60 seconds. If Pin(gp6) = 1 Then ' PIR sensor has detected a motion ThereIsMotion EndIf forcedreboot("22:00:05") Pause 1500 ' slow down Loop > option list WebMite MMBasic Version 5.07.07 OPTION AUTORUN 3 OPTION LIBRARY_FLASH_SIZE 14000 OPTION COLOURCODE ON OPTION HEARTBEAT OFF OPTION DISPLAY 50, 100 OPTION WIFI FlitzBox, ********************* OPTION TELNET CONSOLE ON > run ntp address 192.168.178.1 got ntp response: 27/05/2023 12:45:14 telnet> quit Connection closed. andreas@weizenbaum:~$
Installation eines MQTT-Brokers auf einem Raspberry Pi 4
sudo apt update && sudo apt upgrade sudo apt install -y mosquitto mosquitto-clients sudo systemctl enable mosquitto.service sudo nano /etc/mosquitto/mosquitto.conf diese zwei Zeilen an das Ende anfügen: listener 1883 allow_anonymous true save&exit with 'ctrl x' and 'y' sudo systemctl restart mosquitto.service
Nicht vergessen den Firewall Port 1883 zu öffnen, falls eine Firewall auf dem MQTT Server läuft!
Das Handbuch zur Firmware und dem MMBASIC: https://geoffg.net/webmite.html
Bezug Hardware: Raspberry Pi Pico W , PIR Sensor
Viel Spaß!