Store Arduino data to Firebase database [Howto]

The last few weeks I was playing with Firebase mostly because I wanted to get familiar with this technology. So at some point I thought that it will be a great idea to connect Arduino with Firebase. Imagine that you have a simple temperature sensor that measures the boiler’s water temperature every few minutes and you want to keep a history log of it in order to make some adjustments to thermostat. If you also want those measurements to be available online so you will be able to access them at any time then each sensor reading has to be stored somewhere.

One great way that I was also using for that kind of things is Xively. Originally it was Pachube , then Cosm and now Xively. I hope the guys don’t decide to change the name again! With Xively client for arduino you simply connect to their Api and you send them the readings. Its fast, its simple and they also have a free plan. But there are a few things that might not work for you. One of them is the limit of readings that you can store, at the moment in free plan you can have up to 3 months history. Another thing is that you can’t send data every 1 sec, so its impossible to store almost real time readings. And last but not least you need to consider that you are storing YOUR data in a database that you do not have control.

So what are the alternatives? Having your own sql database server is an overkill, not only because of the cost but also for maintenance reasons. For such simple projects you don’t have to spend a fortune. So here is where Firebase comes. The Firebase guys lets you connect to your own firebase database and do  all the CRUD operations through simple calls to their api. You can visit their website and they will give you a 100mb database free. Which is enough to store at least a years readings. This is what I thought but quickly I came up with a huge problem. Firebase uses only HTTPS for security reasons and my Arduino Uno’s microprocessor don’t have enough power for that. In this link you can read what a member of firebase team says about the ssl support. The idea to bypass this issue is to proxy the requests through a server that can speak ssl. In order to achieve this I used the Firebase PHP library that you can find here. Think of it like this Arduino->Php website->Firebase. Once the data are in Firebase then you reatrive them in many different ways.  Let’s start building our project and you will see how all the parts connect together.

First the hardware part. In my case I used the popular DS1820 temperature sensor. By connecting it to my Arduino Uno I was getting  the room temperature readings. I also had to use an Ethernet shield. I had a spare W5100 so I made a small stack: Arduino Uno-Ethernet Sheild W5100-Prototyping Sheild. The circuit diagram is this:

DS18S20_arduino

And here is mine with all the parts connected:

arduino-ds1820

Next step is to create your free firebase database in http://www.firebase.com. Then log into the database and go at the last menu item “Secrets”. From there you will get your secret token. Write it down as we will need it later. So now we have the hardware and the database ready. If you dont already have a domain, or sub domain go get one from your favorite registar and host the Firebase php library file in one of the thousand hosting providers that offer free plans. Personally I have a windows azure account so I just went there and created a new website.

Then by ftp I uploaded the firebaseLib.php:

And now the most importand file that you also have to upload to your hosting directory. Its the file that will respond to Arduino requests. I named it firebaseTest.php

So now if you do a manual request to http://www.yourdomain.com/firbaseTest.php?arduino_data=21.00 the value 21.00 should be stored in your firebase database. And you should see it straight away. Pretty cool ah? Now the only thing that is left is to make the Arduino send the actually readings. So instead of the dummy “21.00” that we put manually before we will have the real temp readings.  The arduino code is this one:

The above code is not that complicated as it may look. I used parts of the web client example code from arduino.cc. So in a few words what we are doing is we are making a request and we pass the current temperature reading. As you can see i set it to do the request every 5 seconds. The most difficult part for me was to convert the float value in string. If I didn’t do this I was getting an error message.

If all went well then you are almost ready. Your arduino is probably uploading the readings and you probably see your database filling. Here is a video from mine:

Now that you have a database full of readings you can use them as you like. Me, because this was only a small test project I created a simple html file and with some javascript and a jquery chart plugin I am just showing the current temperature from my room and a graph of it.

The index.html:

And the javascript file (controller.js)

Probably for your convenience you might want  to upload also those to your hosting directory.

temperature_arduino_ds1820_firebase

I hope you enjoyed this small project as I did. You can see the temperature in my room live in this link: http://ardufirebase.azurewebsites.net/

Special thanks to my colleague Fabrizio for his valuable help!

Thanks for reading

58 thoughts on “Store Arduino data to Firebase database [Howto]”

  1. Pingback: arduino (UNO) helppppp
  2. Trackback: arduino (UNO) helppppp
  3. Phice says:

    Awesome project!
    I’m making a project with Arduino and a Telit GPRS board, I want to datalog in Firebase but I’m having problems, I cannot use external libs to make http requests because I have to follow the GPRS AT commands syntax. Do you think is possible to have a workaround for SSL protocol (it’s a f.. BIG certificate)? I hope that your experience can give me a big hand!
    Thank you !

  4. Damith says:

    You are AWESOME. This is the most useful and workable example project I’ve found so far for sending Arduino data to cloud. One more thing. I’ve sending data using WiFI shield and following code worked. Hope it will be useful for you.

    Cheers!

    String request = “GET “;//
    request.concat(WEBPAGE);
    request.concat(tempC);
    request.concat(” HTTP/1.0\r\n”);
    request.concat(“Host: “);
    request.concat(WEBSITE);
    request.concat(“\r\n\r\n”);

    Serial.println(request);

    //now send data
    Adafruit_CC3000_Client www = cc3000.connectTCP(ip, 80);
    if (www.connected()) {
    Serial.println(F(“successfully connected!”));

    http://www.println(request);
    } else {
    Serial.println(F(“Connection failed”));
    return;
    }

    1. Damith says:

      Forgot to put my site which collects my home temperature. Thanks again!

      http://dsignhome.netau.net/measure_temparature/

      1. admin says:

        Very nice Damith! Well done 🙂

    2. Fuat says:

      Hi Damith, can you send your all codes that using wifi? By the way, your link does not work.

  5. Sebastian says:

    Hello, i´m working on a similar project and i need to store values on a previously defined table in firebase, i noticed that this one generates new ones with random names. How can i just update an existing one?

  6. Jefferson says:

    Greate tutorial,

    But I think this don’t make sense because you are doing pooling for your server and you aren’t using the benefits o firebase through websocket or server events.

    1. admin says:

      Hi Jefferson,

      Yes you are right, back in 2014 when I wrote this tutorial I didn’t have the knowledge to do it in a different way.

  7. Yatin Varachhia says:

    Hi,

    Why didn’t you use the REST APIs for accessing firebase ? I am trying out that and having some issue.

    1. admin says:

      Hi Yatin,

      Please see below.

      “Firebase uses only HTTPS for security reasons and my Arduino Uno’s microprocessor don’t have enough power for that. In this link you can read what a member of firebase team says about the ssl support.”

  8. Achintya Gupta says:

    Would you mind making a video tutorial from scratch, i am not new to firebase, but i am new to arduino… Would be great 🙂

    1. Achintya Gupta says:

      I have no idea on how to Then by ftp I uploaded the firebaseLib.php:

      1. admin says:

        Hi,

        You need to use an ftp client like ‘Filezilla’ in order to upload the file to your web server

        1. Fuat says:

          Did you use filezilla to upload the file to Azure?

        2. Fuat says:

          Did you use filezilla to upload the file to Azure? How can i use Azure? By the way, thanks for that tutorial.

          1. admin says:

            Yes I used filezilla but it wont make a difference if you use a different ftp client. For azure go to https://azure.microsoft.com and create an account.

  9. Phinephas says:

    Hi there,

    Thank you very much for your tutorial, I am working on a similar project too.

    I am having trouble finding my token from firebase. Where can I find it?

    Thank you in advance.

    1. admin says:

      In firebase admin console. Few months ago they changed it so not sure where exactly you can find it now. It shouldn’t be hard though.

      1. Muhammad Akram Karimi says:

        Hi,
        I am unbale to find the token.
        Do you have any idea, how can I find it now?

        1. admin says:

          1. Click on the settings/cog wheel icon next to your project name at the top of the new Firebase Console
          2. Click Project settings
          3. Click on the Service Accounts tab
          4. Click on Database Secrets
          5. Hover over the non-displayed secret and click Show

  10. abedze says:

    hello admin, i’m very confuse with our tutorial, but i only want to connect my arduino ethrnet shield with firebase, i followed your tutorial, but until now cannot, please help me 🙁

    1. admin says:

      You need to write what have you done so far, where you are getting an error and what error. Otherwise I can’t help you.

      1. abedze says:

        your arduino code i uploaded in my firebasse, but not working, what false step?

        1. admin says:

          Hi abedze,

          I am struggling to understand what exactly is your problem. You need to explain at which point you stuck. In the article I don’t say anywhere that you have to upload arduino code to firebase. Firebase is for storing the data, nothing else. Please go through the article again. I understand that the process is quite complex but is the only way I found. Maybe you will need to look into using a raspberry pi which has the power for https calls.

  11. Lana says:

    Hey, do you know any link or script to send data from a raspberry pi which has the Arduino sensor data getting collected on it? I am currently using pyserial to view the data. But I want to upload it to firebase. A link or some code would help.

    1. admin says:

      If the data is in raspberry then you can communicate with Firebase from there. Raspberry has the power to make https requests unlike arduino. In a small project I am working on I used nodejs to make the requests. I am reading arduino data with this https://www.npmjs.com/package/serialport

  12. Maverick says:

    What exactly goes into the firebase path? Could you please give me an example or is it something like … $firebasepath = ‘firebase.database().ref() ???

  13. Maverick says:

    Does the path go exactly into the single quotes –>> ‘firebase.database().ref()’ . And I tried this before hosting my website by simply calling the url on a web browser and inputing a value for arduino_data but it failed to update….is it really necessary to host your website first?

  14. eoin says:

    Hey I have the firebaseTest.php and firebaseLib.php hosted on a server but when I go to run http://www.yourdomain.com/firbaseTest.php?arduino_data=21.00 the value 21.00 doesn’t appear in the firebase database. I dont seem to be getting any errors in the console do you know what the issue could be please?

    1. eoin says:

      I had the baseURI linkint to the project instead of the database, thanks 🙂

  15. Bhargav B L says:

    I am following this tutorial. When I am sending data manually through get method url, it is storing data in firebase with a new unique id every time. Help me to modify the code.
    Note:-line in firebaseTest.php I modified, $firebasePath = ‘/UID/demo’;
    So its creating unique id under “demo” for every manual test.
    Is there any modification to be done for not creating any unique id under “demo” path.
    Thanks in advance.

    1. Bhargav B L says:

      Even I tried changing push method to update method, still not working

    2. admin says:

      This is how firebase works. It creates a unique id for every record

      1. Fortune says:

        Replace line 14 with this line of code :
        $response = $fb->set($firebasePath, $arduino_data);

  16. Feri says:

    hey admin ,, your code make value always in random name at firebase ,,
    how if i want to place the value in same name at firebase ?

    1. admin says:

      You need to think of your db structure. Mine here was just for demoing. It would be better to create a child for each day

  17. johan says:

    hi, im having problem with this code, when i run the code, i get this error

    Class ‘fireBase’ not found in firebaseTest.php line 13

  18. Sam says:

    Thank you so much for your tutorial. It works perfectly. But I have a question, how can I get the data from Firebase to Arduino?

  19. Yudha says:

    how can I if I want to push 2 or more data ?

  20. Javier says:

    Hello, I know it is an old post.
    I have done all the steps but I do not receive any answer.
    First I have realized that you were wrong in “So now if you do a manual request to http://www.yourdomain.com/firbaseTest.php?arduino_data=21.00” it should be “firebaseTest.php” instead of “firbaseTest.php” I corrected this, but I do not receive nothing from my server. I have checked I have php installed correctly on my server with phpinfo.php (https://mediatemple.net/community/products/dv/204643880/how-can-i-create-a-phpinfo.php-page)

  21. Javier says:

    Hello, I know it is an old post.
    I have done all the steps but I do not receive any answer.
    First I have realized that you were wrong in “So now if you do a manual request to http://www.yourdomain.com/firbaseTest.php?arduino_data=21.00” it should be “firebaseTest.php” instead of “firbaseTest.php” I corrected this, but I do not receive nothing from my server. I have checked I have php installed correctly on my server with phpinfo.php (https://mediatemple.net/community/products/dv/204643880/how-can-i-create-a-phpinfo.php-page).
    I have tried it with POSTMAN (Chrome extension), doing a POST to http://www.mywebsite.net/firebaseTest.php?arduino_data=21.00

    1. Javier says:

      I have forgotten saying I have download the firebaseLib.php (maybe it is a new version and that it could be why it does not work). I installed firebaseInstance.php and I tried with your Arduino code receiving:

      HTTP/1.1 301 Moved Permanently
      Server: nginx
      Date: Thu, 22 Jun 2017 11:12:26 GMT
      Content-Type: text/html
      Content-Length: 178
      Connection: close
      Location: http://www.smartsens.net/firebaseTest.php?arduino_data=21.3

      301 Moved Permanently

      301 Moved Permanently
      nginx

      1. Javier Rizzitano says:

        yo baje una version de firebaseLib.php nueva que no funciona porque la class Firebase no existe, usen la version de firebaseLib que muestra el que hico este post, esa si funciona!!!

  22. Ashutosh says:

    How to do the reverse thing?that is how to get data from the firebase to arduino and have a logic to compare the data with a value?

  23. MasterChief says:

    I had a doubt, you are sending your sensor data to firebase, but is there a way I can get data from firebase to my arduino

  24. ronak sahu says:

    How to use same code to receive data from firebase to arduino??

    1. Javier Rizzitano says:

      lo mismo estoy intentando hacer

  25. sweety says:

    Thanks fro this tutorial.i just want to ask do I have to make any changes to firebaseLib.php file?

    1. Some says:

      Nope

  26. Chathuranga says:

    Hi there,

    Thank you very much for your tutorial,
    if i want to use a “obstacle avoiding sensor” and want to send a binary value like 0 or 1 to the database, what should i do?

  27. Ved says:

    I am making a project where I check the water level and if it’s low the arduino uno using the Ethernet shield send notification to android app through firebase. However client.connect() function doesn’t work for https://fcm.googleapis.com as it uses https. So what should I do.

  28. Nimra says:

    I want to upload image from nodemcu to firebase-store.Is that possible or not?

  29. Javier Rizzitano says:

    Hola, pude implemantar esta solucion para guardar datos en firebase !! y Funciona!!!
    Como puedo hacer para guardas etiquetas tambien y que no las genere automaticamente el firebase?

  30. xiao says:

    how to connect it using esp-01

  31. Prabodh Thombare says:

    Actually, i want to update my values in firebase database.. So i have created website on 000webhost and i have upload firebaseLib.php and firebaseTest.php and i used update inside of push but i am fail .. Even i am fail to push data .. Can u tell me “$fb = new fireBase($url, $token);” where this call goes??

  32. xiao says:

    sir I had successfully connect my devices in the firebase using those files
    but I have a problem actually or a month
    I can’t send the data of 3 arduino devices simultaneously in the firebase

    do you know the solution? I am using I web server for those 3 devices and same library
    but these devices has ID for them to post data on a specific field in the firebase

    what should I do to simultaneously update the data of 3 devices on their each specific field in firebase?

  33. fifah says:

    Does it possible to connect to firebase if i am using bluetooth module instead of the ethernet?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.