我正在使用 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'))
我在本地计算机上使用现有照片尝试了相同的任务,它有效 => 帐户设置很好。
由于您已经能够使用本地计算机成功传输数据,因此这纯粹是 Pi 和/或网络问题。 错误“socket.gaierror: [Errno -2] 名称或服务未知”通常表示 DNS 解析问题。此错误表明您的应用程序中使用的主机名无法解析为 IP 地址。
话虽如此:
希望这有帮助!