Home

Awesome

micropython-firebase-realtime-database

Firebase implementation based on REST API optimized for the ESP32 version of Micropython based on firebase-micropython-esp32 from vishal-android-freak. It shouldn't be a problem to run it on other Micropython platforms. A board with SPIRAM is recommended.

status

Commands that are implemented

- get (equal GET)
- getfile (equal GET)*
- put (equal PUT)
- patch (equal PATCH)
- addto (equal POST)
- delete (equal DELETE)

*getfile writes the data to a file to avoid RAM overflow

Required modules

ujson, usocket, ussl, _thread, time

Preparations YouTube Tutorial

  1. Create a Firebase Realtime Database. (Console>Add Project>Realtime Database>Create Database)

In the end it should look something like this:

image

  1. Set rules to public * (from now the data can be read and changed by everyone ⚠️) *
{
  "rules": {
    ".read": true,
    ".write": true
  }
}
  1. Note the URL of the database
https://[PROJECT_ID].firebaseio.com/

Connect to Wifi

import os
import network
wlan = network.WLAN(network.STA_IF)
if not wlan.active() or not wlan.isconnected():
  wlan.active(True)
  wlan.connect("SSID", "PASSWD")
  while not wlan.isconnected():
    pass

Set the URL of the database

import ufirebase as firebase
firebase.setURL("https://[PROJECT_ID].firebaseio.com/")

Functions

setURL --- do this first!

firebase.setURL(URL)

Set the current Firebase URL.

get --------------------------------------

firebase.get(PATH, DUMP, bg=False, id=0, cb=None, limit=False)

Takes the given storage location PATH, gets the data from there and stores it as DUMP. The data can later be read out by firebase.[DUMP].

getfile --------------------------------------

firebase.getfile(PATH, FILE, bg=False, id=0, cb=None, limit=False)

Takes the given storage location PATH, gets the data from there and stores it as file at the location FILE. Recommeded to download larger amounts of data to avoid ram overflow.

put --------------------------------------

firebase.put(PATH, DATA, bg=True, id=0, cb=None)

Takes the given storage location PATH and uploads the given value DATA there.

patch --------------------------------------

firebase.patch(PATH, DATATAG, bg=True, id=0, cb=None)

Takes the given storage location PATH and patches the given key DATATAG there, without touching any other tag in the Database.

firebase.put("teststruct", {"tag1": "val1", "tag2": "val2"})
firebase.patch("teststruct", {"tag1": "new1"}) #only tag 1 will be changed

image

addto --------------------------------------

firebase.addto(PATH, DATA, DUMP=None, bg=True, id=0, cb=None)

Takes the given storage location PATH and adds the given value DATA there, the randomly generated tag can be optionally stored in the DUMP variable.

firebase.addto("testsensor", 128)
firebase.addto("testsensor", 124)
firebase.addto("testsensor", 120, DUMP="tagname")
print(firebase.tagname) #returns '-MY7GTy4pp2LSpQp5775' (example)

image

delete --------------------------------------

firebase.delete(PATH, bg=True, id=0, cb=None)

Takes the given storage location PATH deletes the data there.

Constants

FIREBASE_GLOBAL_VAR.GLOBAL_URL

firebase.FIREBASE_GLOBAL_VAR.GLOBAL_URL

Returns the current URL as string, do not change directly insted use firebase.setURL(URL)

FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO --------------------------------------

firebase.FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO

Additional information needed by usocket as list.

FIREBASE_GLOBAL_VAR.SLIST --------------------------------------

firebase.FIREBASE_GLOBAL_VAR.SLIST

Dict of sokets for background process.

Simple examples

Get data from the database

firebase.get("testtag", "DATAvariable")
print(firebase.DATAvariable) #None if no data found

firebase.getfile("testtag", "DATAfile.txt")
myfile=open("DATAfile.txt")
print(myfile.read())
myfile.close()

Upload data to the database --------------------------------------

firebase.put("testtag", "testtdata")
firebase.put("testtag", {"tag1": "data1", "tag2": "data2"})

firebase.addto("testtag", "data1")

Delete data from the database --------------------------------------

firebase.delete("testtag")

Functionality

A thread is created for each command* entered. There is a kind of waiting loop for these commands, so only one connection can be executed at a time per id.

If you make 4 get commands, id=0, these are processed one after the other, which means that the last command is executed much later.

If you make 4 get commands, half id=0, half id=1, these are processed 2*one after the other, which means that the last command is executed a bit earlier.

*exception if bg = False

<meta name="google-site-verification" content="FTs6IR_lrQ_1XqCMMtQI_AUInQqW3qCF3H7TV1QgqUY" />