用树莓派几分钟搭建一个网络监控

原文地址:https://pimylifeup.com/raspberry-pi-webcam-server/

This Raspberry Pi webcam server tutorial will take you through on how to have your very own Webcam that is visible on a web page.

If you’re after more of a security like system, then check out the Raspberry Pi security cameratutorial as it features fully-fledged web streaming, motion detection, recording and multi-camera functionality.

This tutorial is marked intermediate as it can be challenging to get working especially if you have hardware that differs to the versions used in this tutorial. If you find that you can’t get this to work, then the security camera solution linked above is much easier to get going.

Ultimate Books of Pi

If you’re more of a visual person and would prefer to watch how to set this up, then you check out my video below. If you like the video, then please subscribe to me, so you’re kept up to date with all the latest videos.

Unfortunately, the video is now out of date and uses an older method then what’s explained in the text version of this tutorial.

 

 

 Equipment

Below are some of the pieces of equipment that you will need to be able to complete this tutorial.

Recommended:

 Raspberry Pi (Latest version recommended for best performance)

 Micro SD Card or SD Card (If you’re using an old version of the Pi)

 Ethernet Cord or WiFi dongle

 Raspberry Pi Camera or USB Webcam

Optional:

 USB Keyboard

 USB Mouse

 HDMI Cable

 How to Setup a Raspberry Pi Webcam Server

Firstly, we will need to install Raspbian onto the Raspberry Pi. If you haven’t already done, this then check out my awesome guide on installing NOOBs Raspberry Pi. It will take you through all the steps that you will need to do to get up and running.

In this tutorial, we will be using a package called Motion if you want to learn more about it you can check out their website at Motion.

Since the latest version of Raspbian is missing dependencies that Motion requires for it to work correctly, we will need to install a different precompiled version. The package maintainer (Currently: Mr Dave | Previously: Calin Crisan) of Motioneye provides a package that has everything we need, and it works just fine with the Raspberry Pi.

1. We will be using the terminal, so open the terminal on the Pi or connect to it via SSH.

2. To begin, first, update the Raspberry Pi, so you’re running on the latest version.

sudo apt-get update
sudo apt-get upgrade

Depending on the version of Raspbian you’re using you will need to do some different steps. For this tutorial, we will be installing Motion for Raspbian Stretch. If you’re not already on it then I highly recommend that you upgrade.

3. First, install the following packages. This command will work both on the full and lite version of Raspbian Stretch.

sudo apt-get install libjpeg-dev gettext libmicrohttpd-dev libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev libwebp-dev mysql-common libmariadbclient18 libpq5

4.Next, download the Motion deb file from the GitHub and install it using the dpkg command.

sudo wget https://github.com/Motion-Project/motion/releases/download/release-4.2.1/pi_stretch_motion_4.2.1-1_armhf.deb
sudo dpkg -i pi_stretch_motion_4.2.1-1_armhf.deb

That’s all you need to do before moving on to configuring Motion so that it will run on your Pi.

 Configuring Motion

1. Now we need to make some edits to the configuration file, motion.conf.

sudo nano /etc/motion/motion.conf

2. Find the following lines and update them to the following.

  • daemon on
  • stream_localhost off

Note: Change the following two lines from on to off if you’re having issues with the stream freezing whenever motion occurs.

  • picture_output off
  • movie_output off

Optional (Don’t include the text after the #)

  • stream_maxrate 100 This change will allow for real-time streaming but requires more bandwidth & resources. Needs to be added to the config file, default is 1
  • framerate 100 Changing this option will allow for 100 frames to be captured per second allowing for smoother video, default is 50
  • width 640 #This line changes the width of the image displayed, default is 640
  • height 480 #This option changes the height of the image displayed, default is 480

Altering these options can drastically affect the performance. You may need to tinker to get the best results.

3. Now we need to setup up the daemon. First, we need to edit the motion file.

sudo nano /etc/default/motion

4. Find the following line and change it to the following:

start_motion_daemon=yes

5. Once you’re done, simply save and exit by pressing ctrl+x then y.

6. Now make sure the camera is connected and run the following line:

sudo service motion start

7. If you need to stop the service, simply run the following command:

sudo service motion stop

8. Now you should be able to check out the Webcam Stream at the IP address of our Pi so in your browser go to the following address.

192.168.1.103:8081

9. If the web page isn’t loading, try restarting the service.

sudo service motion restart

10. If you’re using a Raspberry Pi camera, then you will need to do a few extra steps that are mentioned below.

 

There we have it a fully working Raspberry Pi Webcam server that you can place wherever you would like (Given it is within WiFi range) and be able to view the stream via the web browser. If you want to allow external access to it, then check out my instructions towards the bottom of this tutorial.

raspberry pi USB webcam in action

 Extra Steps for the Raspberry Pi Camera

If you want to use the Raspberry Pi camera module, then you will need to do a few additional steps to set it up.

 Installing the Hardware

1. First, go to the CSI (Camera Serial Interface) ribbon cable slot on the Pi and with two fingers on each side of the connector pull up. This technique should now open the connector, now insert the ribbon cable with the metal leads facing away from the Ethernet Port.

2. Once you have lined it up gently press the connector back down and the cable should now be locked in one place.

Clips and Ribbon Cable

 Configuring the Software

To get the Raspberry Pi camera to work with Motion, we will need to do a few extra steps.

1. First make sure the camera is switched on within raspi config, enter the following command and then enable the camera (Found in interfacing options). (You will need to restart once you have done this).

sudo raspi-config

2. Enter the following line to had the camera to modules. Make sure the camera is connected correctly before running this line otherwise it will thrown an error.

sudo modprobe bcm2835-v4l2

3. Alternatively, the following process should also work. First, open up the modules file by entering the following line.

sudo nano /etc/modules

4. Enter the following line at the bottom of the file if it doesn’t already exist. Once done, save & exit by pressing ctrl+x then y.

bcm2835-v4l2

5. Now reboot the Pi, and the stream should now work.

sudo reboot

6. You should now be able to access the Raspberry Pi webcam stream by going to the Pi’s IP address on port 8081.

192.168.1.103:8081

You should now have a fully working Raspberry Pi camera server that is accessible within your local network. If you want to allow external access to the camera, then please follow my instructions below.

Webcam Server Browser

 Saving Motion Footage and Photos

By default, you will likely run into permission problems for saving the photos and video triggered by motion. I will go through the steps below on how to set this up to work correctly.

1. Firstly, let’s make a new directory for our photos and videos.

sudo mkdir /motion

2. Next, we need to change the group of the motion folder so motion can write into it. After that, we give the group; read, write and execute permissions.

sudo chgrp motion /motion 
sudo chmod g+rwx /motion

3. We now need to go back into the motion configuration file and change the target_dir to our new directory. First, open up the file.

sudo nano /etc/motion/motion.conf

4. Update the target_dir line, so it looks like the one below. Make sure you remove the from the start of the line.

target_dir /motion

5. Next, restart the motion service, so the configuration is loaded correctly.

sudo service motion restart

6. Now, whenever the camera detects motion, it should be saved into the folder we specified above. You can check this by running the following command.

ls /motion

 Setting up outside External Access

In order to enable external access to the Raspberry Pi webcam server, we will need to change some settings on the router. However, all routers are designed differently so you may need to look up instructions for your brand of router. Please note, opening ports to the internet comes with a security risk.

If you need a more in-depth guide, then be sure to take a look at my guide on how to set up Raspberry Pi port forwarding and dynamic DNS.

The following steps are what I did on mine in order to get it to work. My router is an AC1750 TP-Link Router.
1. Go to the router admin page (This will typically be 192.168.1.1 or 192.168.254)

2. Enter the username and password. Default typically is admin & admin.

3. Once in go to forwarding->Virtual Server and then click on add new.

4. In here enter:

  • Service port: In this case 48461
  • IP Address: 192.168.1.103 (Address of your Pi)
  • Internal Port: We want this to be the same as the webcam server so make it 8081
  • Protocol: All
  • Status: Enabled

5. These settings will route all traffic destined for port 48461 to the webcam server located at the IP address and port you provided. For example: 192.168.1.103:8081

6. You should now be able to connect to the Raspberry Pi webcam stream outside your network. You may need to restart the router for changes to take effect.
Router Port Forwarding

If you’re unable to connect outside your local network then you can try the following:

  • Check your router settings and confirm they are correct.
  • Check your IP hasn’t changed. Some internet service providers will provide you with a dynamic IP rather than a static IP) You can set up something called dynamic DNS to counter this you can find out more information via the link mentioned above.
  • Restart the router.

I hope that you have enjoyed this tutorial on how to build a Raspberry Pi webcam server if you want more then, please check out all my Raspberry Pi projects I currently have completed. There will be much more coming in the future.

<think>好的,我现在需要帮用户设计一个基于树莓派和STM32的智能交通灯控制系统。首先,用户之前已经询问过STM32和树莓派的区别及协同应用场景,现在他们想具体做一个智能交通灯系统,可能需要更详细的实现思路。 用户可能的需求是什么?他们可能希望系统能够根据实时交通流量调整信号灯,提高交通效率,同时具备远程监控和管理功能。作为智能系统,可能还需要集成传感器、通信模块以及用户交互界面。 首先,我需要确定系统的整体架构。树莓派适合处理复杂的计算任务,比如图像识别、数据分析、网络通信等,而STM32擅长实时控制,可以精确管理交通灯的切换和传感器数据的采集。因此,可以将树莓派作为主控中心,负责处理数据和决策,STM32作为下位机执行具体的控制任务。 接下来需要考虑硬件部分。交通灯需要红黄绿灯,可能需要使用LED模块,STM32的GPIO可以控制这些灯。传感器方面,可能需要车辆检测传感器,比如红外、超声波或者摄像头。树莓派处理摄像头数据做车辆识别,STM32处理红外或超声波传感器的实时数据。通信方面,两者之间可以通过UART、SPI或者I2C进行数据交换,确保实时性。 软件部分,树莓派可能需要运行Linux系统,使用Python或C++编写程序,处理图像识别(如OpenCV)、数据分析,并通过网络将数据上传到服务器或远程监控端。STM32则需要用C语言编写固件,实时读取传感器数据,控制交通灯状态,并与树莓派通信。 还需要考虑实时性的问题。交通灯控制对时间敏感,STM32的硬实时特性可以确保信号切换的精确性,而树莓派可能处理较慢的任务,比如图像处理和数据传输,因此需要设计好两者之间的通信协议,确保指令及时传达。 另外,电源管理也很重要。STM32和树莓派的功耗不同,可能需要独立的电源设计,特别是如果系统需要长时间运行,可能需要考虑低功耗模式或备用电源。 用户可能还会需要远程控制功能,比如通过手机APP或网页调整信号灯配时,或者查看实时交通状况。树莓派可以搭建Web服务器,提供API接口,而STM32负责执行具体的调整指令。 测试和调试阶段,需要分模块验证。先测试STM32的交通灯控制和传感器数据采集是否准确,再测试树莓派的图像识别和数据处理是否正确,最后整合两者,确保通信顺畅,系统稳定。 可能遇到的挑战包括通信协议的稳定性、实时数据处理的延迟、以及系统的可靠性。需要设计冗余机制,比如在通信中断时,STM32可以按照预设的默认方案运行,避免交通混乱。 总结来说,系统需要结合树莓派的高性能计算和STM32的实时控制,合理分配任务,设计可靠的通信和电源方案,同时考虑扩展性和用户交互需求。</think>--- ### 智能交通灯控制系统设计方案(树莓派 + STM32) #### 一、系统架构设计 **核心分工**: - **树莓派**:智能决策层 ✓ 运行交通流量分析算法 ✓ 摄像头图像识别(车辆/行人检测) ✓ 4G/WiFi远程数据传输(对接云平台) ✓ 人机交互界面(触摸屏/网页控制) - **STM32**:实时控制层 ✓ 交通灯状态切换(精确到毫秒级时序) ✓ 车辆检测传感器(地磁/红外)数据采集 ✓ 应急车辆优先信号触发(RFID识别) ✓ 故障自检与安全保护机制 **通信方案**: ```mermaid graph LR A[树莓派] -- UART/SPI --> B[STM32] B -- 中断信号 --> A C[摄像头] -- USB --> A D[地磁传感器] -- ADC --> B E[RFID读卡器] -- SPI --> B ``` --- #### 二、硬件选型建议 | **模块** | **推荐型号** | **功能说明** | |---------------|-----------------------------|--------------------------------| | 主控 | 树莓派4B + STM32F407 | 双核协同控制 | | 交通灯驱动 | WS2812B LED灯带 + MOSFET电路 | 支持PWM调光,单总线控制 | | 车辆检测 | AMS AS3935地磁传感器 | 检测车辆停留/通过状态 | | 应急识别 | MFRC522 RFID模块 | 识别救护车/消防车优先通行权限 | | 环境监测 | BME280气象传感器 | 监测雨雾天气自动调整信号周期 | | 备用电源 | TP4056锂电池管理模块 | 市电中断时维持基础运行30分钟 | --- #### 三、软件实现流程 1. **STM32实时控制核心**: ```c // 交通灯状态机示例(4相位控制) typedef enum { PhaseA_Green, PhaseB_Red, // 主路通行 PhaseA_Yellow, PhaseB_Red, // 过渡期 PhaseA_Red, PhaseB_Green, // 支路通行 PhaseA_Red, PhaseB_Yellow // 过渡期 } TrafficPhase; void Traffic_LED_Update() { switch(current_phase) { case PhaseA_Green: HAL_GPIO_WritePin(GPIOA, MAIN_GREEN_PIN, GPIO_PIN_SET); if(sensor_data.main_road_congestion > 80%) { Send_Alert_to_RPi(); // 向树莓派发送拥堵信号 } break; // ...其他相位控制逻辑 } } ``` 2. **树莓派智能决策算法**: - **动态配时策略**: $$ T_{green} = T_{base} + \alpha \cdot \frac{N_{current}}{N_{max}} + \beta \cdot W_{emergency} $$ 其中: - $T_{base}$=基础绿灯时间(20s) - $N_{current}$=当前车道车辆数(通过摄像头YOLO识别) - $W_{emergency}$=应急车辆权重(RFID触发时设为1) - **OpenCV车辆检测代码片段**: ```python import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() detections = yolo_model(frame) # 使用YOLOv5-tiny模型 car_count = len([obj for obj in detections if obj.class == 'car']) uart_send_to_stm32(car_count) # 通过串口发送给STM32 ``` --- #### 四、关键实现细节 1. **安全冗余设计**: - STM32内置看门狗定时器(IWDG),树莓派异常时自动切换至离线模式 - 双路传感器校验(地磁+摄像头),检测误差>15%时触发校准流程 2. **通信协议优化**: - 自定义二进制协议(包头0xAA + 数据长度 + CRC校验) - 树莓派每500ms发送配时参数,STM32每100ms反馈传感器数据 3. **低功耗模式**: - 夜间车流量少时,STM32切换至Stop模式(仅保留RTC唤醒) - 树莓派按需启动摄像头(通过STM32的PIR人体感应信号) --- #### 五、扩展功能建议 1. **V2X车路协同**: - 通过树莓派广播SPAT(信号灯相位时间)消息(DSRC/LTE-V2X) - 车载OBU接收数据,实现车速引导 2. **违章抓拍系统**: - 树莓派联动摄像头抓拍闯红灯车辆(触发条件:红灯后0.5s内压线) - 车牌识别结果通过4G上传至交管平台 3. **可视化监控**: - 使用Grafana搭建实时看板,展示: ✓ 各方向车流量热力图 ✓ 信号灯状态时序图 ✓ 设备健康度监控(CPU温度/网络延迟) --- ### 开发路线图 1. **第一阶段(1-2周)**: - 完成STM32基础灯控+传感器采集 - 验证树莓派与STM32的UART通信稳定性 2. **第二阶段(3-4周)**: - 部署YOLO车辆检测模型(量化至INT8精度) - 实现动态配时算法原型 3. **第三阶段(5-6周)**: - 添加应急车辆优先通行功能 - 搭建Web管理界面(Flask/Django) 4. **第四阶段(7-8周)**: - 通过JTAG进行EMC测试(确保抗干扰能力) - 路侧实际场景72小时压力测试 --- 通过这种架构设计,既能发挥STM32在实时控制方面的可靠性(确保信号切换零失误),又能利用树莓派的强大算力实现智能优化。建议优先实现核心控制逻辑,再逐步增加AI决策层功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值