gpt4 book ai didi

python - 如何使用 PIR 传感器将信息从客户端发送到服务器端 [WinError 10057]

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:54 24 4
gpt4 key购买 nike

我们是两个学生,正在从事一个学校项目。任务是将 PIR 传感器(运动传感器)连接到树莓派,每当传感器检测到运动时,它应该向运行 Windows Server 2019 的 AD/DNS 服务器上的文本文档发送一个字符串。我们正在使用 Python 进行编码,我们用于客户端(运行 PIR 传感器的 Raspberry Pi)的代码使用以下代码:

import RPi.GPIO as GPIO
import time
import datetime
import socket

GPIO.setmode(GPIO.BCM)
pir = 24

GPIO.setup(pir, GPIO.IN)

print ("Sensor initializing...")
time.sleep(2)
print ("active")
print ("press ctrl+c to end program")

mouvement = "Motion detected at "


while True:
if GPIO.input(pir) == True:
now = datetime.datetime.now()
fb = open("/home/pi/Projekt/pirsensor.txt", "a+")
#print (mouvement)
print(now.strftime("%Y-%m-%d %H:%M:%S "))
fb.write(mouvement)
fb.write(now.strftime("%Y-%m-%d %H:%M:%S "))
fb.write("\n")
fb.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.3', 5007))
s.send(mouvement)
s.send(now.strftime ("%Y-%m-%d %H: %M: %S "))
s.send("\n")
s.close()
#time.sleep(2)
#time.sleep(0.1)
GPIO.cleanup()
print (" program ended")

我们用于 Windows Server 2019 的代码如下:

from socket import *
port = 5007
file = ''
class Server:
gate = socket(AF_INET, SOCK_STREAM)
host = '192.168.1.3'
def __init__(self, port):
self.port = port
self.gate.bind((self.host, self.port))
self.listen()

def listen(self):
self.gate.listen(10)
while True:
print("Lyssnar på anslutningar, via port: ", self.port)
add = self.gate.accept()
self.reciveFileName()
self.reciveFile()


def reciveFileName(self):
while True:
data = self.gate.recv(1024)
self.file = data

def reciveFile(self):
createFile = open("new_"+self.file, "wb")
while True:
data = self.gate.recv(1024)
createFile.write(data)
createFile.close()
server= Server(port)
listen()

两台设备都在同一 LAN 网络上运行并具有静态 IP 地址。我们能够互相 ping 通,并能够将带有 PIR 传感器的 Raspberry pi 的信息发送到 Windows Server 2019 计算机。然而,我们有一个问题。当我们在 Raspberry pi 上运行代码并等待服务器接收数据时,我们收到以下错误:

[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

See image of the error

我们陷入了这个错误,不知道如何修复它。我们一直在网上搜索,但只找到 2006 年的帖子,但没有任何帮助。还值得注意的是,我们已尝试禁用防火墙。我们也没有使用 Python 编码的经验。

最佳答案

这可能是因为每次 while 循环迭代时都会创建一个新的套接字。最好在循环外部声明并连接套接字,并且仅从循环内部发送数据:

mouvement = "Motion detected at "

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.1.3', 5007))

while True:
if GPIO.input(pir) == True:
now = datetime.datetime.now()
fb = open("/home/pi/Projekt/pirsensor.txt", "a+")
#print (mouvement)
print(now.strftime("%Y-%m-%d %H:%M:%S "))
fb.write(mouvement)
fb.write(now.strftime("%Y-%m-%d %H:%M:%S "))
fb.write("\n")
fb.close()
s.send(mouvement)
s.send(now.strftime ("%Y-%m-%d %H: %M: %S "))
s.send("\n")
#time.sleep(2)
#time.sleep(0.1)
s.close()
GPIO.cleanup()
print (" program ended")

这应该给你的套接字足够的时间来初始化、连接、发送和关闭。这样做的另一个好处是循环在等待连接时不会阻塞,从而使程序运行得更快。

关于python - 如何使用 PIR 传感器将信息从客户端发送到服务器端 [WinError 10057],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59322011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com