PC和树莓派通过XBee模块通讯实例

前言

    PC端连接WiFi模块产生的局域网,并用python把WiFi模块摄像头的视频流爬取下来。最后,对该视频进行实时人脸检测。一旦检测到人脸,则通过XBee向树莓派发送数据。树莓派接收到数据后,驱动步进电机运行。

PC端python代码如下

from digi.xbee.devices import XBeeDevice
import cv2

PORT = "COM3"
BAUD_RATE = 9600

DATA_TO_SEND = "1"
REMOTE_NODE_ID = "XBEE_A"

cap = cv2.VideoCapture("http://192.168.1.1:8080/?action=stream")

def main():
    while (True):
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Our operations on the frame come here
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        xmlfile = 'C:\\Users\\13161\\PycharmProjects\\untitled1\\venv\\Lib\\site-packages\\cv2\data\\haarcascade_frontalface_default.xml'

        face_cascade = cv2.CascadeClassifier(xmlfile)

        faces = face_cascade.detectMultiScale(
            gray,
            scaleFactor=1.15,
            minNeighbors=5,
            minSize=(5, 5),
        )
        print("发现{0}个目标!".format(len(faces)))
        for (x, y, w, h) in faces:
            cv2.rectangle(frame, (x, y), (x + w, y + w), (0, 255, 0), 2)

        cv2.imshow("frame", frame)

        aaa=[0,0,0]
        aaa.append(len(faces))
        #xxx +=len(faces)

        if max(aaa)==1:
            print(111111111)
            device = XBeeDevice(PORT, BAUD_RATE)
            try:
                device.open()

                # Obtain the remote XBee device from the XBee network.
                xbee_network = device.get_network()
                remote_device = xbee_network.discover_device(REMOTE_NODE_ID)
                if remote_device is None:
                    print("Could not find the remote device")
                    exit(1)

                print("Sending data to %s >> %s..." % (remote_device.get_64bit_addr(), DATA_TO_SEND))

                device.send_data(remote_device, DATA_TO_SEND)

                print("Success")

            finally:
                if device is not None and device.is_open():
                    device.close()
        # Display the resulting frame
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()

树莓派端代码如下

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import serial
 
delay=2 #delay 2ms
 
pin_4 = 4
pin_17 = 17
pin_23 = 23
pin_24 = 24
 
GPIO.setmode(GPIO.BCM) #设置引脚的编码方式
    
def init():
    GPIO.setwarnings(False)
    GPIO.setup(pin_4, GPIO.OUT)
    GPIO.setup(pin_17, GPIO.OUT)
    GPIO.setup(pin_23, GPIO.OUT)
    GPIO.setup(pin_24, GPIO.OUT)
 
 
def forward(delay):  
    setStep(1, 0, 0, 0)
    time.sleep(delay)
    setStep(0, 1, 0, 0)
    time.sleep(delay)
    setStep(0, 0, 1, 0)
    time.sleep(delay)
    setStep(0, 0, 0, 1)
    time.sleep(delay)
 
def setStep(w1, w2, w3, w4):
  GPIO.output(pin_4, w1)
  GPIO.output(pin_17, w2)
  GPIO.output(pin_23, w3)
  GPIO.output(pin_24, w4)
  
def main():
    init() 
    ser=serial.Serial('/dev/ttyUSB0',9600)
    x= 'n'
    y= 'n'
    while x !='q':
          x=ser.read(1)
          print(x)
          if x!=y:
               while True:
                     forward(int(delay) / 1000.0)
         
main() # 调用main


  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值