当检测到运动时,我正在使用Python程序单击Raspberry Pi 3B+上的图片,并将此图像发送到firebase存储.

import RPi.GPIO as GPIO
import gpiozero
import datetime
import picamera
import time
import os
import pyrebase

firebase_config = {
  "apiKey": "...",
  "authDomain": "x.firebaseapp.com",
  "databaseURL": "https://x.firebaseio.com",
  "projectId": "...",
  "storageBucket": "x.appspot.com",
  "messagingSenderId": "...",
  "appId": "..."
}
      
firebase = pyrebase.initialize_app(firebase_config)
storage = firebase.storage()

# Camera config
camera = picamera.PiCamera()

# Motion sensor
pir = gpiozero.MotionSensor(4)

print("Waiting for motion")
pir.wait_for_motion()
print(f"Motion detected")

filename = datetime.datetime.now().strftime("%d%m%y%H%M%S")+".jpg"
print(filename)
camera.capture(filename)
print(f"{filename} saved")

storage.child(filename).put(filename)
print("Image sent to firebase")

os.remove(name)
sleep(5)

图片被点击并保存在Pi上,但由于以下错误而不会发送到Firebase存储:

Waiting for motion
Motion detected
080524135451.jpg
080524135451.jpg saved
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/requests/packages/urllib3/connection.py", line 141, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.9/dist-packages/requests/packages/urllib3/util/connection.py", line 75, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.9/socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/raspi/Desktop/firebase.py", line 42, in <module>
    storage.child(filename).put(filename)
  File "/usr/local/lib/python3.9/dist-packages/pyrebase/pyrebase.py", line 405, in put
    request_object = self.requests.post(request_ref, data=file_object)
  File "/usr/local/lib/python3.9/dist-packages/requests/sessions.py", line 522, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.9/dist-packages/requests/sessions.py", line 596, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/requests/adapters.py", line 487, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='firebasestorage.googleapis.com', port=443): Max retries exceeded with url: /v0/b/devilberry0.appspot.com/o?name=080524135451.jpg (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x72df5268>: Failed to establish a new connection: [Errno -2] Name or service not known'))

我使用现有照片在本地计算机上try 了同样的任务,并且成功了=帐户设置很好.

推荐答案

由于您已经能够使用本地计算机成功传输数据,因此这纯粹是Pi和/或网络问题.The error "socket. gaierror: [Errno -2] Name or service not known" typically points to a DNS resolution issue. This error indicates that the hostname used in your application cannot be resolved to an IP address.

话虽如此:

  1. 重新判断您的互联网连接.
  2. 确保如果您使用受限网络(例如办公室/学校wifi),您的Raspberry Pi已注册到该网络.

希望这有帮助!

Python相关问答推荐

调试回归无法解决我的问题

如何才能将每个组比上一组增加N %?

使用argsorted索引子集索引数组

强制venv在bin而不是收件箱文件夹中创建虚拟环境

将行从一个DF添加到另一个DF

NumPy中的右矩阵划分,还有比NP.linalg.inv()更好的方法吗?

Class_weight参数不影响RandomForestClassifier不平衡数据集中的结果

try 与gemini-pro进行多轮聊天时出错

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

max_of_three使用First_select、second_select、

对于一个给定的数字,找出一个整数的最小和最大可能的和

如何从具有不同len的列表字典中创建摘要表?

如何在类和classy-fastapi -fastapi- followup中使用FastAPI创建路由

使用@ guardlasses. guardlass和注释的Python继承

如何在Python脚本中附加一个Google tab(已经打开)

OR—Tools CP SAT条件约束

如何设置视频语言时上传到YouTube与Python API客户端

Python逻辑操作作为Pandas中的条件

从Windows Python脚本在WSL上运行Linux应用程序

在Admin中显示从ManyToMany通过模型的筛选结果