Skip to main content

IOT-Firebase-Python

Connecting raspberry with the firebase using the python. I will code here how to upload the images or some data to firesebase using python.
Sample code to upload images and data to firebase is there in github : https://github.com/yuvaraj119/IOT-Firebase-Python 

First install pyrebase from https://pypi.org/project/Pyrebase/

Now we are good to go with code.

import pyrebase
import uuid
from uuid import getnode as get_mac
import getpass
import datetime
import os

config = {
    "apiKey":"", #get api key from firebase
    "authDomain":"", #firebase app auth url 
    "databaseURL": "", #add yout db url from firebase where your data is gonna store 
    "storageBucket": "" #storage bucket url from firebase storage 
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
storage = firebase.storage()

is_session=False

#creating unique name for session and device

uid_str = uuid.uuid5(uuid.NAMESPACE_DNS,'python.org')  
session = str(uid_str)+"PI"+str(uuid.uuid4())

mac_addr = get_mac()
pi_username = getpass.getuser()
name = str(mac_addr)+pi_username

#firebase storage path will be created base on this
storage_path = "pi-images/"+name+"/"+session+"/"

#saving the data and image url in firebase db. 
db.child("users").child(name)

data = {"created_date_time":str(datetime.datetime.now()),
        "image_url":"https:google.com/images"}

def pushData(data):
    db.child(session).push(data)
    
def setData(data):
    db.child(session).set(data)

def updateData(data):
    db.child(session).update(data)

def removeData():
    db.child(session).remove()

def uploadImage():
    file=os.path.basename('image1.jpg') #image1.jpg is local storage image
    storage.child(storage_path+"image1.jpg").put(file)
    image_url= storage.child(storage_path+"image1.jpg").get_url(1)
    print(image_url)
    data = {"created_date_time":str(datetime.datetime.now()),
        "image_url":image_url}
    pushData(data)


#this will upload the image to firebase and push data will upload in firebase db.
uploadImage()


Comments

Popular posts from this blog

Vertical AutoScrolling TextView in Android

In android by default we can scroll the text in horizontal using marquee in layout, but if we want to scroll the text in vertical its not possible by default. So here we will learn to create a custom TextView which will auto-scroll in vertical direction. Source Code:  VerticalScrollingTextView-Android Create a AutoScrollingTextView.class which extends TextView: @SuppressLint ( "AppCompatCustomView" ) public class AutoScrollingTextView extends TextView { private static final float DEFAULT_SPEED = 65.0f ; public Scroller scroller ; public float speed = DEFAULT_SPEED ; public boolean continuousScrolling = true; public AutoScrollingTextView (Context context) { super (context) ; init( null, 0 ) ; scrollerInstance(context) ; } public AutoScrollingTextView (Context context , AttributeSet attrs) { super (context , attrs) ; init(attrs , 0 ) ; scr...

Flexbox inside the RecyclerView as a LayoutManager (FlexboxLayoutManager).

Currently google has release the Flexbox which can be used for building flexible layouts using FlexboxLayout, it can be interpreted as an advanced LinearLayout because both layouts align their child views sequentially. For more detail on this flexbox-layout But here we are gonna work on Flexbox with RecyclerView. Flexbox with a large number of items in a scrollable container! Let's first see what are the Supported attributes / features comparison Due to some characteristics of the RecyclerView, some Flexbox attributes are not available/not implemented to the FlexboxLayoutManager. Here is a quick overview of the attributes/features comparison between the two containers. Attribute / Feature FlexboxLayout                FlexboxLayoutManager (RecyclerView) flexDirection flexWrap (except wrap_reverse ) justifyContent alignItems alignContent - layout_order - layout_fle...

Android Customized-FloatSeekBar

A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. SeekBar support only int values for progress: void setMax(int max) Set the upper range of the progress bar max. void setMin(int min) Set the lower range of the progress bar to min. void setProgress(int progress) Sets the current progress to the specified value. int getMax() Return the upper limit of this progress bar's range. int getMin() Return the lower limit of this progress bar's range. int getProgress() Get the progress bar's current level of progress. In some scenario we would need floating point seekbar progress, which is not supported by default in android. Here the floating seekbar which supports floating point value: You can download the full sample and library from https://github.com/yuvaraj119/Cus...