需求:
做一个小型的监控,类似电子猫眼,监测到人之后,取一张图 然后发送到自己的邮箱。
架构:
1.sr602 传感器监测到人
2. esp32 cam 取图 并通过mqtt协议传到远端服务器
3, 服务器利用python 搭建一个mqtt客户端,订阅到数据后 将图片保存,并发送到指定邮箱
硬件连接:
结构设计:
软件:
服务端代码
#mqtt客户端
import paho.mqtt.client as mqtt
import logging
import os
import email_send
APPEAR_TOPIC = "APPEAR_TOPIC"
DETAIL_TOPIC = "DETAIL_TOPIC"
mqtt_server = 'xxxxxxxxxx'
image_path="images"
image_index=0
def on_connect(client, userdata, flags, rc):
logging.info('连接到MQTT服务器 '+str(rc))
def on_message(client, userdata, msg):
print(msg.topic + " " + str(msg.payload))
global image_index
if msg.topic ==APPEAR_TOPIC:
if image_index!=0:
#发送全部图片
image_index=0
email_send.send_email(image_path)
logging.info('发送到邮箱完毕')
if msg.topic == DETAIL_TOPIC:
logging.info('收到图片,准备保存到本地')
save_location = "images/"+str(image_index)+".jpg"
f = open(save_location, 'wb')