socket.gaierror: [Errno -2] Name or service not known | Firebase x Raspberry Pi

Question:

I am using a Python program to click a picture on my Raspberry Pi 3B+ when motion is detected and send this image to firebase storage.

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)

The picture gets clicked and saved on Pi but does not get sent to Firebase storage due to the following error:

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'))

I tried the same task on my local computer using an existing photo and it worked => the account settings are fine.

Asked By: peacer

||

Answers:

Since you have been able to successfully transfer data using your local computer, this is purely a Pi and/or network issue. 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.

With that said:

  1. Recheck your internet connection.
  2. Ensure that if you’re on restricted network (like office/school wifi), your Raspberry Pi is registered to it.

Hope this helps!

Answered By: Himanshu saraswat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.