[Digi-Key Follow me Issue 1] +5. Extension Project: Intrusion Detection Alarm Based on PicoW
[Copy link]
This post was last edited by qinyunti on 2023-4-10 16:08
78afb5e5d54b7e4804f394c1dc8f64a1
Preface
In the previous articles, we played with PicoW through various modules and conducted tests on LED, buzzer, OLED, network, etc.
There are also some interesting ways to play, such as using a buzzer to play "Two Tigers".
In this article, we will implement a comprehensive demo based on the above peripherals: an intrusion detection monitor based on PicoW.
Summary Design
The overall block diagram is as follows
The TOF module detects the approach of a person.
Then the GPS location time and personnel approach information are reported to the server via WIFI.
At the same time, alarm indications are given through sound and light such as OLED, BEEP, LED, etc.
accomplish
TOF personnel approach detection
The TOF module model used is HLK-LD116S-24G
When human movement is detected, the output will be high, otherwise the output will be low.
Use GPIO18 as IO input to detect TOF output
The test code is as follows
from machine import Pin
import time
led = Pin("LED",Pin.OUT)
tof = Pin(18,Pin.IN)
led.value(0)
while True:
time.sleep(0.1)
print(tof.value())
The test is as follows
LED Indication
When a person approaches, the LED lights up to indicate an alarm, otherwise it goes out.
The test code is as follows
from machine import Pin
import time
led = Pin("LED",Pin.OUT)
tof = Pin(18,Pin.IN)
led.value(0)
while True:
time.sleep(0.1)
print(tof.value())
if(tof.value()):
led.value(1)
else:
led.value(0)
When the test hand approaches, the LED lights up, and goes out when it is removed.
Buzzer alarm
When a person approaches, the buzzer sounds to warn, otherwise it does not sound.
The test code is as follows
from machine import Pin
import time
from machine import PWM
led = Pin("LED",Pin.OUT)
tof = Pin(18,Pin.IN)
pwm = PWM(Pin(16))
pwm.freq(1000)
led.value(0)
while True:
time.sleep(0.1)
print(tof.value())
if(tof.value()):
led.value(1)
pwm.duty_u16(1000)
else:
led.value(0)
pwm.duty_u16(0)
OLED Display
When a person approaches, the OLED displays a warning message.
The test code is as follows
from machine import Pin
import time
from machine import PWM
from machine import I2C
from ssd1306 import SSD1306_I2C
WIDTH = 128
HEIGHT = 64
i2c = I2C(0)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
led = Pin("LED",Pin.OUT)
tof = Pin(18,Pin.IN)
pwm = PWM(Pin(16))
pwm.freq(1000)
led.value(0)
pwm.duty_u16(0)
while True:
time.sleep(0.1)
print(tof.value())
if(tof.value()):
led.value(1)
pwm.duty_u16(1000)
oled.fill(0)
oled.text("Warning!Intruder!",5,20)
oled.show()
else:
led.value(0)
pwm.duty_u16(0)
oled.fill(0)
oled.text("Safety!",5,20)
oled.show()
When Juve is detected approaching, it displays "arning! Intruder!", otherwise it displays "Safety!"
Online reporting
When a person approaches, the OLED will report the detected status, GPS location, current time and other information to the server.
test
The complete code is as follows
from machine import Pin
import time
from machine import PWM
from machine import I2C
from ssd1306 import SSD1306_I2C
from micropyGPS import MicropyGPS
from machine import UART
import network
import socket
ssid = 'qiqiqiqi'
password = 'cqmygysdss'
host='192.168.31.236' #服务器IP地址
port = 10000 #服务器端口
my_gps = MicropyGPS()
WIDTH = 128
HEIGHT = 64
i2c = I2C(0)
oled = SSD1306_I2C(WIDTH, HEIGHT, i2c)
uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))
led = Pin("LED",Pin.OUT)
tof = Pin(18,Pin.IN)
pwm = PWM(Pin(16))
pwm.freq(1000)
led.value(0)
pwm.duty_u16(0)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# 网络连接
# Wait for connect or fail
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else:
print('connected')
status = wlan.ifconfig()
print( 'ip = ' + status[0] )
#建立Socket连接
s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#Socket属性
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
while True:
time.sleep(0.1)
#print(tof.value())
if uart0.any():
stat = my_gps.update(uart0.read(1).decode('utf-8')) # Note the conversion to to chr, UART outputs ints normally
if stat:
print('Latitude:', my_gps.latitude_string())
print('Longitude:', my_gps.longitude_string())
print('Speed:', my_gps.speed_string('kph'), 'or', my_gps.speed_string('mph'), 'or', my_gps.speed_string('knot'))
print('Date (Long Format):', my_gps.date_string('long'))
print('Date (Short D/M/Y Format):', my_gps.date_string('s_dmy'))
print('timestamp (Short [H,M,S] Format):', my_gps.timestamp)
sstr = 'Warning!Intruder!' + my_gps.latitude_string() + my_gps.longitude_string() + my_gps.date_string('s_dmy') + '\r\n'
s.sendto(sstr,(host,port))
stat = None
if(tof.value()):
led.value(1)
pwm.duty_u16(1000)
oled.fill(0)
oled.text("Warning!Intruder!",5,20)
oled.show()
else:
led.value(0)
pwm.duty_u16(0)
oled.fill(0)
oled.text("Safety!",5,20)
oled.show()
Summarize
In the above, we first carried out a rough design, then tested each module according to the module division, and finally achieved the design goals comprehensively.
From the whole process, since each module has been fully tested before, this article is actually the 'assembly' process.
With the design block diagram, the implementation was carried out according to the outline design, which was relatively smooth.
This activity is very meaningful. I became familiar with the corresponding development of PicoW and drove each module.
We also implemented some interesting tests and finally realized a comprehensive Demo, which was very rewarding.
Finally, I have a small suggestion. You can add more free peripheral modules to give full play to your creativity.
|