|
| 1 | +<html lang="en-US"><head> |
| 2 | + <meta charset="UTF-8"> |
| 3 | + |
| 4 | +<!-- Begin Jekyll SEO tag v2.7.1 --> |
| 5 | +<title>micropython-firebase-realtime-database | Firebase implementation for Micropython optimized for ESP32</title> |
| 6 | +<meta name="generator" content="Jekyll v3.9.0"> |
| 7 | +<meta property="og:title" content="micropython-firebase-realtime-database"> |
| 8 | +<meta property="og:locale" content="en_US"> |
| 9 | +<meta name="description" content="Firebase implementation for Micropython optimized for ESP32"> |
| 10 | +<meta property="og:description" content="Firebase implementation for Micropython optimized for ESP32"> |
| 11 | +<link rel="canonical" href="https://ckoever.github.io/micropython-firebase-realtime-database/"> |
| 12 | +<meta property="og:url" content="https://ckoever.github.io/micropython-firebase-realtime-database/"> |
| 13 | +<meta property="og:site_name" content="micropython-firebase-realtime-database"> |
| 14 | +<meta name="twitter:card" content="summary"> |
| 15 | +<meta property="twitter:title" content="micropython-firebase-realtime-database"> |
| 16 | +<script type="application/ld+json"> |
| 17 | +{"@type":"WebSite","url":"https://ckoever.github.io/micropython-firebase-realtime-database/","headline":"micropython-firebase-realtime-database","description":"Firebase implementation for Micropython optimized for ESP32","name":"micropython-firebase-realtime-database","@context":"https://schema.org"}</script> |
| 18 | +<!-- End Jekyll SEO tag --> |
| 19 | + |
| 20 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 21 | + <meta name="theme-color" content="#157878"> |
| 22 | + <link rel="stylesheet" href="/micropython-firebase-realtime-database/assets/css/style.css?v=c0ab75793f60dbbf0ed221914d845e4be8d5de26"> |
| 23 | + </head> |
| 24 | + <body> |
| 25 | + <section class="page-header"> |
| 26 | + <h1 class="project-name">micropython-firebase-realtime-database</h1> |
| 27 | + <h2 class="project-tagline">Firebase implementation for Micropython optimized for ESP32</h2> |
| 28 | + |
| 29 | + <a href="https://github.com/ckoever/micropython-firebase-realtime-database" class="btn">View on GitHub</a> |
| 30 | + |
| 31 | + |
| 32 | + </section> |
| 33 | + |
| 34 | + <section class="main-content"> |
| 35 | + <h1 id="micropython-firebase-realtime-database">micropython-firebase-realtime-database</h1> |
| 36 | +<p><strong>Firebase implementation</strong> based on <a href="https://firebase.google.com/docs/reference/rest/database">REST API</a> optimized for the <a href="https://github.com/micropython/micropython-esp32">ESP32 version of Micropython</a> based on <a href="https://github.com/vishal-android-freak/firebase-micropython-esp32">firebase-micropython-esp32</a> from vishal-android-freak. It shouldn’t be a problem to run it on other Micropython platforms.</p> |
| 37 | + |
| 38 | +<h3 id="commands-that-are-implemented">Commands that are implemented</h3> |
| 39 | +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>- get (equal GET) |
| 40 | +- getfile (equal GET)* |
| 41 | +- put (equal PUT) |
| 42 | +- patch (equal PATCH) |
| 43 | +- addto (equal POST) |
| 44 | +- delete (equal DELETE) |
| 45 | +</code></pre></div></div> |
| 46 | +<blockquote> |
| 47 | + <p>*getfile writes the data to a file to avoid RAM overflow</p> |
| 48 | +</blockquote> |
| 49 | + |
| 50 | +<h3 id="required-modules">Required modules</h3> |
| 51 | +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>ujson, usocket, ussl, _thread, time |
| 52 | +</code></pre></div></div> |
| 53 | + |
| 54 | +<h3 id="preparations">Preparations</h3> |
| 55 | +<ol> |
| 56 | + <li>Create a <a href="https://firebase.google.com/products/realtime-database">Firebase Realtime Database</a>. (Console>Add Project>Realtime Database>Create Database)</li> |
| 57 | +</ol> |
| 58 | + |
| 59 | +<p>In the end it should look something like this:</p> |
| 60 | + |
| 61 | +<p><img src="https://user-images.githubusercontent.com/77546092/114287154-f6071b00-9a64-11eb-9214-de75753a71c3.png" alt="image"></p> |
| 62 | + |
| 63 | +<ol> |
| 64 | + <li>Set rules to <strong>public</strong> * <em>(from now the data can be read and changed by anyone ⚠️)</em> * |
| 65 | +_ |
| 66 | + <div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="o">{</span> |
| 67 | + <span class="s">"rules"</span><span class="o">:</span> <span class="o">{</span> |
| 68 | + <span class="s">".read"</span><span class="o">:</span> <span class="kc">true</span><span class="o">,</span> |
| 69 | + <span class="s">".write"</span><span class="o">:</span> <span class="kc">true</span> |
| 70 | + <span class="o">}</span> |
| 71 | +<span class="o">}</span> |
| 72 | +</code></pre></div> </div> |
| 73 | + </li> |
| 74 | + <li>Note the URL of the database |
| 75 | + <div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>https://[PROJECT_ID].firebaseio.com/ |
| 76 | +</code></pre></div> </div> |
| 77 | + <h2 id="example">Example</h2> |
| 78 | + <h3 id="1-connect-to-wifi">1. Connect to Wifi</h3> |
| 79 | + </li> |
| 80 | +</ol> |
| 81 | + |
| 82 | +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span> |
| 83 | +<span class="kn">import</span> <span class="nn">network</span> |
| 84 | +<span class="n">wlan</span> <span class="o">=</span> <span class="n">network</span><span class="p">.</span><span class="n">WLAN</span><span class="p">(</span><span class="n">network</span><span class="p">.</span><span class="n">STA_IF</span><span class="p">)</span> |
| 85 | +<span class="k">if</span> <span class="ow">not</span> <span class="n">wlan</span><span class="p">.</span><span class="n">active</span><span class="p">()</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">wlan</span><span class="p">.</span><span class="n">isconnected</span><span class="p">():</span> |
| 86 | + <span class="n">wlan</span><span class="p">.</span><span class="n">active</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> |
| 87 | + <span class="n">wlan</span><span class="p">.</span><span class="n">connect</span><span class="p">(</span><span class="s">"SSID"</span><span class="p">,</span> <span class="s">"PASSWD"</span><span class="p">)</span> |
| 88 | + <span class="k">while</span> <span class="ow">not</span> <span class="n">wlan</span><span class="p">.</span><span class="n">isconnected</span><span class="p">():</span> |
| 89 | + <span class="k">pass</span> |
| 90 | +</code></pre></div></div> |
| 91 | + |
| 92 | +<h3 id="2-set-the-url-of-the-database">2. Set the URL of the database</h3> |
| 93 | +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">ufirebase</span> <span class="k">as</span> <span class="n">firebase</span> |
| 94 | +<span class="n">firebase</span><span class="p">.</span><span class="n">setURL</span><span class="p">(</span><span class="s">"https://[PROJECT_ID].firebaseio.com/"</span><span class="p">)</span> |
| 95 | +</code></pre></div></div> |
| 96 | + |
| 97 | +<h3 id="get-data-from-the-database-optional-run-in-the-background">Get data from the database (optional run in the background)*</h3> |
| 98 | +<blockquote> |
| 99 | + <p>*In this case you have to pay attention to the timing yourself, because the data needs time to be downloaded. |
| 100 | +```python |
| 101 | +firebase.get(“testtag”, “DATAvariable”[, bg=False]) |
| 102 | +print(firebase.DATAvariable) #None if no data found</p> |
| 103 | +</blockquote> |
| 104 | + |
| 105 | +<p>firebase.getfile(“testtag”, “DATAfile.txt”[, bg=False]) |
| 106 | +myfile=open(“DATAfile.txt”) |
| 107 | +print(myfile.read()) |
| 108 | +myfile.close()</p> |
| 109 | +<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>### Upload data to the database (runs in the background) |
| 110 | +```python |
| 111 | +firebase.put("testtag", "testtdata") |
| 112 | +firebase.put("testtag", {"tag1": "data1", "tag2": "data2"}) |
| 113 | + |
| 114 | +firebase.addto("testtag", "data1") |
| 115 | +</code></pre></div></div> |
| 116 | +<h3 id="delete-data-from-the-database-runs-in-the-background">Delete data from the database (runs in the background)</h3> |
| 117 | +<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">firebase</span><span class="p">.</span><span class="n">delete</span><span class="p">(</span><span class="s">"testag"</span><span class="p">)</span> |
| 118 | +</code></pre></div></div> |
| 119 | +<h2 id="functionality">Functionality</h2> |
| 120 | +<p>A thread is created for each command* entered. There is a kind of waiting loop for these commands, so <strong>only one connection can be executed at a time</strong>. So if you make 10 get commands, these are processed <strong>one after the other</strong>, which means that the <em>last command is executed much later</em>.</p> |
| 121 | +<blockquote> |
| 122 | + <p>*with the exception of get and getfile with bg = False</p> |
| 123 | +</blockquote> |
| 124 | + |
| 125 | +<meta name="google-site-verification" content="FTs6IR_lrQ_1XqCMMtQI_AUInQqW3qCF3H7TV1QgqUY"> |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + <footer class="site-footer"> |
| 130 | + |
| 131 | + <span class="site-footer-owner"><a href="https://github.com/ckoever/micropython-firebase-realtime-database">micropython-firebase-realtime-database</a> is maintained by <a href="https://github.com/ckoever">ckoever</a>.</span> |
| 132 | + |
| 133 | + <span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a>.</span> |
| 134 | + </footer> |
| 135 | + </section> |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +</body></html> |
0 commit comments