{"id":13802634,"url":"https://github.com/ckoever/micropython-firebase-realtime-database","last_synced_at":"2025-05-13T13:32:32.488Z","repository":{"id":45616718,"uuid":"356716963","full_name":"ckoever/micropython-firebase-realtime-database","owner":"ckoever","description":"Firebase implementation for Micropython optimized for ESP32","archived":false,"fork":false,"pushed_at":"2024-08-06T12:27:40.000Z","size":137,"stargazers_count":24,"open_issues_count":3,"forks_count":18,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-08T14:31:43.536Z","etag":null,"topics":["firebase","firebase-realtime-database","micropython","micropython-esp32","micropython-lib"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ckoever.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-10T23:03:23.000Z","updated_at":"2024-11-18T06:26:53.000Z","dependencies_parsed_at":"2024-11-18T18:42:31.867Z","dependency_job_id":"2c399325-c9e3-47d4-bd14-96bec47619bb","html_url":"https://github.com/ckoever/micropython-firebase-realtime-database","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckoever%2Fmicropython-firebase-realtime-database","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckoever%2Fmicropython-firebase-realtime-database/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckoever%2Fmicropython-firebase-realtime-database/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckoever%2Fmicropython-firebase-realtime-database/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ckoever","download_url":"https://codeload.github.com/ckoever/micropython-firebase-realtime-database/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253950272,"owners_count":21989333,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["firebase","firebase-realtime-database","micropython","micropython-esp32","micropython-lib"],"created_at":"2024-08-04T00:01:49.006Z","updated_at":"2025-05-13T13:32:32.160Z","avatar_url":"https://github.com/ckoever.png","language":"Python","readme":"# [micropython-firebase-realtime-database](https://github.com/ckoever/micropython-firebase-realtime-database)\n**Firebase implementation** based on [REST API](https://firebase.google.com/docs/reference/rest/database) optimized for the [ESP32 version of Micropython](https://github.com/micropython/micropython-esp32) based on [firebase-micropython-esp32](https://github.com/vishal-android-freak/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.**\n\n![status](https://img.shields.io/badge/%20status-%F0%9F%9F%A1%20Further%20development%20on%20request-yellow?style=for-the-badge)\n\n\n### Commands that are implemented\n```\n- get (equal GET)\n- getfile (equal GET)*\n- put (equal PUT)\n- patch (equal PATCH)\n- addto (equal POST)\n- delete (equal DELETE)\n```\n\u003e *getfile writes the data to a file to avoid RAM overflow\n\n### Required modules\n```\nujson, usocket, ussl, _thread, time\n```\n\n### Preparations [YouTube Tutorial](https://www.youtube.com/watch?v=T35U8zwTe40)\n1. Create a [Firebase Realtime Database](https://firebase.google.com/products/realtime-database). (Console\u003eAdd Project\u003eRealtime Database\u003eCreate Database)\n\nIn the end it should look something like this:\n\n![image](https://user-images.githubusercontent.com/77546092/114287154-f6071b00-9a64-11eb-9214-de75753a71c3.png)\n\n2. Set rules to **public** * *(from now the data can be read and changed by everyone ⚠️)* *\n```java\n{\n  \"rules\": {\n    \".read\": true,\n    \".write\": true\n  }\n}\n```\n3. Note the URL of the database\n```\nhttps://[PROJECT_ID].firebaseio.com/\n```\n### Connect to Wifi\n```python\nimport os\nimport network\nwlan = network.WLAN(network.STA_IF)\nif not wlan.active() or not wlan.isconnected():\n  wlan.active(True)\n  wlan.connect(\"SSID\", \"PASSWD\")\n  while not wlan.isconnected():\n    pass\n```\n### Set the URL of the database\n```python\nimport ufirebase as firebase\nfirebase.setURL(\"https://[PROJECT_ID].firebaseio.com/\")\n```\n## Functions\n### setURL --- do this first!\n```python\nfirebase.setURL(URL)\n```\nSet the current Firebase URL.\n### get --------------------------------------\n```python\nfirebase.get(PATH, DUMP, bg=False, id=0, cb=None, limit=False)\n```\nTakes 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]`.\n  - Optional run in the background with the keyword `bg`.\n  - 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.\n  - Set an callback function after getting the `DATA`. \n    - Example: \n    ```python\n    def hereweare(name, id, varname):\n      print(\"\\nname: \"+str(name)+\", id: \"+str(id)+\", value: \"+str(eval(\"firebase.\"+varname)))\n    firebase.get(\"testtag1\", \"VAR1\", bg=True, id=0, cb=(hereweare, (\"testtag1\", \"0\", \"VAR1\")))\n    firebase.get(\"testtag2\", \"VAR2\", bg=True, id=1, cb=(hereweare, (\"testtag2\", \"1\", \"VAR2\"))) #runs at the same time\n    ```\n  - Limit the depth of the data to 1 with `limit` ⚠️ ONLY USE True/False (not 1/0). \n    - Example:\n\n    ![image](https://user-images.githubusercontent.com/77546092/115153400-f6f80800-a075-11eb-8c50-5814a96309df.png)\n    ```python\n    firebase.get(\"a\", \"VAR1\")\n    print(firebase.VAR1) \n    #returns {'testlarge2': 'KJIHGFEDCBA', 'lol': 'ok', 'a': {'-MY_ntFnAhiTYygcraC6': [2, 2], '-MY_novcmzHOyexwij8B': '2', '-MY_nlKoV7jcYbTJMpzT': '2'}, 'testlarge1': 'ABCDEFGHIJK', 'testtag1': 1, 'testtag2': 2}\n    firebase.get(\"a\", \"VAR2\", limit=True)\n    print(firebase.VAR2)\n    #returns {'testlarge2': True, 'lol': True, 'testtag2': True, 'testlarge1': True, 'testtag1': True, 'a': True} \n    ```\n### getfile --------------------------------------\n```python\nfirebase.getfile(PATH, FILE, bg=False, id=0, cb=None, limit=False)\n```\nTakes 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.\n  - Optional run in the background with the keyword `bg`.\n  - 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.\n  - Set an callback function after getting the `DATA`. \n    - Example: \n    ```python\n    def herewefile(name, id, filename):\n       LOCAL_FILE=open(str(filename))\n       print(\"\\nname: \"+str(name)+\", id: \"+str(id)+\", value: \"+str(LOCAL_FILE.read()))\n       LOCAL_FILE.close()\n    firebase.getfile(\"testlarge1\", \"FILE1.txt\", id=0, bg=1, cb=(herewefile, (\"testlarge1\", \"0\", \"FILE1.txt\")))\n    firebase.getfile(\"testlarge2\", \"FILE2.txt\", id=1, bg=1, cb=(herewefile, (\"testlarge2\", \"1\", \"FILE2.txt\"))) #runs at the same time\n    ```\n  - Limit the depth of the data to 1 with `limit` ⚠️ ONLY USE True/False (not 1/0). \n### put --------------------------------------\n```python\nfirebase.put(PATH, DATA, bg=True, id=0, cb=None)\n```\nTakes the given storage location `PATH` and uploads the given value `DATA` there.\n  - Optional run in the background with the keyword `bg`.\n  - 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)\n  - Set an callback function after getting the `DATA`. \n    - Example: \n    ```python\n    firebase.put(\"testtag1\", \"1\", id=0)\n    firebase.put(\"testtag2\", \"2\", id=1) #runs at the same time\n    ```\n### patch --------------------------------------\n```python\nfirebase.patch(PATH, DATATAG, bg=True, id=0, cb=None)\n```\nTakes the given storage location `PATH` and patches the given key `DATATAG` there, without touching any other tag in the Database.\n  - Example:\n  ```python\n  firebase.put(\"teststruct\", {\"tag1\": \"val1\", \"tag2\": \"val2\"})\n  firebase.patch(\"teststruct\", {\"tag1\": \"new1\"}) #only tag 1 will be changed\n  ```\n  ![image](https://user-images.githubusercontent.com/77546092/114471016-30e98a00-9bf0-11eb-90ec-baec7f10e03c.png)\n  - Optional run in the background with the keyword `bg`.\n  - 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)\n  - Set an callback function after patching the `DATA`. \n### addto --------------------------------------\n```python\nfirebase.addto(PATH, DATA, DUMP=None, bg=True, id=0, cb=None)\n```\nTakes the given storage location `PATH` and adds the given value `DATA` there, the randomly generated tag can be optionally stored in the DUMP variable.\n  - Example:\n  ```python\n  firebase.addto(\"testsensor\", 128)\n  firebase.addto(\"testsensor\", 124)\n  firebase.addto(\"testsensor\", 120, DUMP=\"tagname\")\n  print(firebase.tagname) #returns '-MY7GTy4pp2LSpQp5775' (example)\n  ```\n  ![image](https://user-images.githubusercontent.com/77546092/114472221-1fa17d00-9bf2-11eb-804d-21e0ac425a87.png)\n  - Optional run in the background with the keyword `bg`.\n  - 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)\n  - Retuns the tag under which the data was saved.\n  - Set an callback function after adding the `DATA`. \n### delete --------------------------------------\n```python\nfirebase.delete(PATH, bg=True, id=0, cb=None)\n```\nTakes the given storage location `PATH` deletes the data there.\n  - Optional run in the background with the keyword `bg`.\n  - 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)\n  - Set an callback function after deleting the `DATA`. \n## Constants\n### FIREBASE_GLOBAL_VAR.GLOBAL_URL\n```python\nfirebase.FIREBASE_GLOBAL_VAR.GLOBAL_URL\n```\nReturns the current URL as string, do not change directly insted use `firebase.setURL(URL)`\n### FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO --------------------------------------\n```python\nfirebase.FIREBASE_GLOBAL_VAR.GLOBAL_URL_ADINFO\n```\nAdditional information needed by usocket as list.\n### FIREBASE_GLOBAL_VAR.SLIST --------------------------------------\n```python\nfirebase.FIREBASE_GLOBAL_VAR.SLIST\n```\nDict of sokets for background process.\n## Simple examples\n### Get data from the database\n```python\nfirebase.get(\"testtag\", \"DATAvariable\")\nprint(firebase.DATAvariable) #None if no data found\n\nfirebase.getfile(\"testtag\", \"DATAfile.txt\")\nmyfile=open(\"DATAfile.txt\")\nprint(myfile.read())\nmyfile.close()\n```\n### Upload data to the database --------------------------------------\n```python\nfirebase.put(\"testtag\", \"testtdata\")\nfirebase.put(\"testtag\", {\"tag1\": \"data1\", \"tag2\": \"data2\"})\n\nfirebase.addto(\"testtag\", \"data1\")\n```\n### Delete data from the database --------------------------------------\n```python\nfirebase.delete(\"testtag\")\n```\n## Functionality\nA 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**. \n\nIf you make 4 get commands, id=0, these are processed **one after the other**, which means that the last command is executed much later. \n\nIf 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.\n\u003e*exception if bg = False\n\n\u003cmeta name=\"google-site-verification\" content=\"FTs6IR_lrQ_1XqCMMtQI_AUInQqW3qCF3H7TV1QgqUY\" /\u003e\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Storage"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckoever%2Fmicropython-firebase-realtime-database","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fckoever%2Fmicropython-firebase-realtime-database","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckoever%2Fmicropython-firebase-realtime-database/lists"}