Giter VIP home page Giter VIP logo

micropython-firebase-realtime-database's Introduction

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].

  • Optional run in the background with the keyword bg.

  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background.

  • Set an callback function after getting the DATA.

    • Example:
    def hereweare(name, id, varname):
      print("\nname: "+str(name)+", id: "+str(id)+", value: "+str(eval("firebase."+varname)))
    firebase.get("testtag1", "VAR1", bg=True, id=0, cb=(hereweare, ("testtag1", "0", "VAR1")))
    firebase.get("testtag2", "VAR2", bg=True, id=1, cb=(hereweare, ("testtag2", "1", "VAR2"))) #runs at the same time
  • Limit the depth of the data to 1 with limit ⚠️ ONLY USE True/False (not 1/0).

    • Example:

    image

    firebase.get("a", "VAR1")
    print(firebase.VAR1) 
    #returns {'testlarge2': 'KJIHGFEDCBA', 'lol': 'ok', 'a': {'-MY_ntFnAhiTYygcraC6': [2, 2], '-MY_novcmzHOyexwij8B': '2', '-MY_nlKoV7jcYbTJMpzT': '2'}, 'testlarge1': 'ABCDEFGHIJK', 'testtag1': 1, 'testtag2': 2}
    firebase.get("a", "VAR2", limit=True)
    print(firebase.VAR2)
    #returns {'testlarge2': True, 'lol': True, 'testtag2': True, 'testlarge1': True, 'testtag1': True, 'a': True} 

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.

  • Optional run in the background with the keyword bg.
  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background.
  • Set an callback function after getting the DATA.
    • Example:
    def herewefile(name, id, filename):
       LOCAL_FILE=open(str(filename))
       print("\nname: "+str(name)+", id: "+str(id)+", value: "+str(LOCAL_FILE.read()))
       LOCAL_FILE.close()
    firebase.getfile("testlarge1", "FILE1.txt", id=0, bg=1, cb=(herewefile, ("testlarge1", "0", "FILE1.txt")))
    firebase.getfile("testlarge2", "FILE2.txt", id=1, bg=1, cb=(herewefile, ("testlarge2", "1", "FILE2.txt"))) #runs at the same time
  • Limit the depth of the data to 1 with limit ⚠️ ONLY USE True/False (not 1/0).

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

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

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

  • Optional run in the background with the keyword bg.
  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background. (Example at get)
  • Set an callback function after getting the DATA.
    • Example:
    firebase.put("testtag1", "1", id=0)
    firebase.put("testtag2", "2", id=1) #runs at the same time

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.

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

image

  • Optional run in the background with the keyword bg.
  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background. (Example at get)
  • Set an callback function after patching the DATA.

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.

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

image

  • Optional run in the background with the keyword bg.
  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background. (Example at get)
  • Retuns the tag under which the data was saved.
  • Set an callback function after adding the DATA.

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

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

Takes the given storage location PATH deletes the data there.

  • Optional run in the background with the keyword bg.
  • Set socket id with the keyword id. This makes it possible to establish multiple connections to the server instead of just one. Make sure the command runs in the background. (Example at get)
  • Set an callback function after deleting the DATA.

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

micropython-firebase-realtime-database's People

Contributors

ckoever avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

micropython-firebase-realtime-database's Issues

Beta branch

What is the status of the beta branch? Is it in a usable state? What are the differences between the master branch and the beta?

Firebase Security - Add Credentialized Setup

First, thank you for creating this project. How difficult would it be to add the ability to run the DB connection secured. In your setup the connection is established in "TEST" meaning that FB will reset the security to AUTH every 30 days. It would be nice to have the ability to use email/password connection security so that a project could be used more permanently. If I understand FB for Python there is a firebase-admin library that uses API Keys. The values are stored in a .env and then used as the initial connect routine. Given the credentials and URL path are all that is required the additional memory should be almost zero. ESP32-WROVER-E has 4MB flash and the WROVER -IE has 8MB. Plenty of room.

What makes FBRTDB so interesting to me is the serverless aspect allowing for easy data collection on the MC and easy dashboard creation for control and monitoring using almost any web framework... ie.. React, Angular, Flask, Vue, etc.. Within maybe one hour you can have all the ESP32 side and a hosted dashboard up and running.

You could have an async set up to read the DB on the ESP every so often to check for commands pushed into the DB from the hosted dashboard. Based on my experience with FB you could do so with latency under 100ms(my best was 18ms). This makes it almost possible to get a realtime(almost as RT would be < 10ms) roundtrip. ORM connections take too long for such low latency.

I would be willing to work on creating the required additions to your library to allow for the secured connection. Let me know if there are any hurdles that cannot be crossed and this is why you have chosen to stop at open connection protocols.

Again, thank you for the effort.

ESP32: OSError: ECONNABORTED

Problem with the library running it in an esp32 module. Simple code that captures data through the Inventor App that turns a led on and off. The application works wonderfully but after a while (40-60 seconds) the connection is terminated and the following error appears.

Traceback (most recent call last):
   File "<stdin>", line 21, in <module>
   File "ufirebase.py", line 124, in get
   File "urequests.py", line 116, in get
   File "urequests.py", line 60, in request
OSError: [Errno 113] ECONNABORTED

_thread issue

When I run a script containing this I am getting "ImportError: no module named '_thread'". Can't figure out how to install _thread.

No Module _thread

import ufirebase
Traceback (most recent call last):
File "", line 1, in
File "ufirebase.py", line 4, in
ImportError: no module named '_thread'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.