InfluxDB and Grafana With Openhabian
by clee105 in Workshop > Home Improvement
5326 Views, 1 Favorites, 0 Comments
InfluxDB and Grafana With Openhabian
This project is going to show you how to do the basic data logging to a built-in database and show it hose data on a user-friendly dashboard. This will be the first step toward more interesting IOT project that you probably have later on. In this demo, only simulated temperature and humidity value are graphed but there is no limit to this.
Sources:
- https://www.smarthomeblog.net/openhab-persistence-...
- OpenHAB 2 basics by BK Hobby on Youtube
Supplies
- Raspberry Pi 4 ( I'm not sure older version working but this is the one I'm using)
- SD flash drive (8-16 GB is recommended)
- USB flash drive ( 8-16 GB is recommended)
- Ethernet Cable
- Router connected to the internet
- Laptop or computer
Install Openhabian V1.5 on Your Raspberry Pi
This step we will do it on our computer. This step you should do it right before your bed time. Hear me out and thank me later.
First of all, use the link https://github.com/openhab/openhabian/releases to download the latest release of Openhabian OS. Make sure you click on the image file. In this case, it is "openhabian-pi-raspbian-201908050414-gitca0976f-crc6a66b5a1.img.xz". It will downloaded as a zip file.
Next, while waiting for the zip file to download, go to this website to download a tool to flash your SD card with the Openhabian OS image. Here is the one I recommend since it is pretty straight-forward and free :https://www.balena.io/etcher/. Make sure you choose the right options for OS to download. Speaking of the OS, I mean your Operating System (either MacOS, Linux, or Windows as can be seen from my screenshot). In my case, I chose to download the one made for Windows.
After it has been installed, run the installer file for Balena Etcher and you will see a window popping up. That means Etcher is ready. At this point, insert your SD card into your computer. Balena Etcher will auto-recognized your flash drive. You can click on "change" to see and choose your desired drive to be flashed.
At this point, the OS img ZIP file should have been installed. With the Balena Etcher window opened, choose "select image", then choose the Openhabian image ZIP file. After choosing that, make sure again that the selected flash drive is right and then click "Flash". It will take approximately 3-10 minutes, depending on the speed of your SD flash drive.
When this is done, take your flash drive out of your computer and insert it in the SD card slot on your Raspberry Pi. For my Raspberry Pi 4, the slot is under DSI Display Port. Plugging in the Ethernet cable into your Pi's Ethernet port and the Router's LAN port (Ethernet Pod also works, as long as they have the Ethernet socket). One important notice, make sure powering the Pi is the last thing on the list. Inserting SD card into your already powered Pi could lead to damage on the OS image on your SD card.
This is probably the most boring step. Wait for 2-3 hours until it's done. Raspberry Pi will do its job and run the OS. This is why I recommended that you should do this at night, closing to your bed time.
Install Putty SSH Client on Your Computer
If you have a separate screen supporting HDMI port or you already had a SSH client on your computer, skip this step.
If not, welcome! You are probably not familiar with the SSH client. I am not either, in a way. I have not used any thing like this before, but trust me on this one. It will be easy and fun. You will probably figure out a lot of things on your own when you play around with it. I will try to list out tips as many as I can on the instruction. Enough talking (or reading), let's get to it.
Use this link here: https://www.chiark.greenend.org.uk/~sgtatham/putty... to download Putty SSH Client App. Select the right option for your Windows OS; mine is x64. After the installation, the app will look like the illustration above. Enter "openhab" in the Host Name box or IP address of your Pi if you know it ( I will show you how to get it later) and hit "Open". The username and password for it would be: "openhabian" - "openhabian". You can change it later and I recommend doing that for security purposes. The login screen would look like the illustration above. As you can see, the IP address of your Pi will show up on the screen. There is also another way to do it using ifconfig command.
There you go! You have successfully worked on your Pi remotely for the first time. You can do pretty much the same thing with SSH app on your tablets and smartphones. My only tip would be save the Host Name in the Saved Session so you don't have to type in "openhab" everytime.
Configure Openhabian on Your Raspberry Pi
There won't be a lot of talking from now on since the code is pretty straight-forward. Please CAREFULLY follow these steps.
Type in "sudo openhabian-config" (enter your password)
Choose "Update" and then "Upgrade System" option.
Next, we will move root to our USB drive. Choose "System Settings/Move root to USB". It will ask you to reboot the device. So just back to main login screen and type "sudo reboot now". Log in your Pi again via SSH and start to move root to USB. Type "mount" later to check. If it shows "/dev/sda1", it's done. The reason for this step is to prevent crashes, which have been recorded so many times.
Next, going back to the openhabian-config. Choose to install "NodeRED" and "InfluxDB+Grafana". There should not be any problem installing NodeRed.
For InfluxDB and Grafana, they will prompt you for the password of the account: admin, openhab and grafana. In my case, I will use names as passwords, like "admin" - "admin". You can always change it later with VS Code, as can be seen from above screenshot. You will use the credentials later and I will show how to do that. If you think the **username** password is not cool enough, feel free to enter your own. Just make sure you note it down; otherwise, it would be a mess later on.
Note: default database is openhab_db. It can be accessed by both "admin" account and "openhab" account. Admin account has all the privileges and it can be used for both InfluxDB and Grafana. As long as you keep the same password for both account, you can pretty access them and play around with configurations.
Node-Red Configuration.
First, we need to download the palette needed for this project. Go to Manage Palette/ Install/search modules. In there, type Influx and select and install "node-red-contrib-influxdb".
Next, paste the content of the text file "FlowJson" into Clipboard. How to access it? Good question. Follow steps in the illustration above. After you have imported my Flow, feel free to click deploy and play around with it. It will show some errors since it needs a few more steps. However, you can still get some ideas of how my flow works.
For those who have experience with Node-red before, I insert random value into the InfluxDB with Math.random function. Feel free to inject Json String from either your MQTT source or from a HTTP source.
If you're new to Node-red, it's recommended to Google it first and then play around with it in the flow page. It's very straight forward when it comes to basic functions.
Note: the database name I use for this is "weather" and the Measurement is "station". You can change them but they have to be consistent during the project. Server is usually localhost:8086 or (**your IP address**:8086, in my case: 192.168.2.39:8086)
Downloads
InfluxDB Configuration
Follow these code line. I would assume you have logged in your Pi. "//" is for comments
- Influx //this is to initiate InfluxDB on your Pi, wait a few minute.
- auth //this is to start the authentication process
- admin //it will prompt you for username. Remember Step 3 when we set up the account? we will use the administrator credential for this.
- admin //this is my password for my admin account, use yours
- create weather //create a new database names weather. You can name it Bob or Jack or whatever, just make sure it is consistent during the project.
show databases // remember the "S" in databases. This will show you all the databases you having with Influx. Right now, you should have weather and openhab_db
exit
At this point, we're pretty much done. You can use admin credentials to login with influxDB and Grafana. In case there are many users that wish to share the database but you trust none of them, check these command out.
- create user admin with password 'Password1' with all privileges // this step to create a administrator account like admin
- create user Bob with password 'Password2' // this step to create a regular user account
- create user Tom with password 'Password3'
- grant all on openhab_db to Bob // this step to grant permission to read and write on openhab_db database for user account Bob
- grant read on openhab_db to Tom // this step to grant permission to read only to the user account Tom
Grafana Configuration.
It will be easier to follow this video here. His project is more complicated; however, it's basically the same when it comes to configure Grafana for Influx.
That's it! You have completed the tutorial! One last thing to notice. You have to make sure the repeat time for Inject node in the Node-red flow should match the "time" in "Group by" in Grafana.
I have another Node-red project that implement storing data online via Google Cloud API. I will have a different instruction for that. It would be fun to check that out!
Assessment
- Challenges for this project and solution:
Originally, we form a team to start a project to record live data from sensor using Arduino and using MQTT to transfer it over to Raspberry Pi. I will take care of the Pi while my buddy will configure the sensor. However, because of the Covid-19 lockdown, I don't have access to the real data from his sensors via MQTT. After that we figure out we have to use Google Cloud Firestore services as an online database. It works out fine until my partner Ethernet Shield broke and his data could not transfer over the Internet. After that, we have to simulate the data using the Math.random() function.
We also came up with a problem during installing InfluxDB and Grafana. I took the account for granted and use different credentials for all of the accounts. After that, it is a pain in the butt to login and use Influx and Grafana. I tried to correct the source code in services/influxdb.conf file of Openhabian OS. It did not turn out well and a bunch of problems regarding authentication appearing afterward. Since we're pretty new to Linux, we decided to reinstall the OS with the closest backup we have. Fortunately this time, with my being careful, it works and I figure out a lot of things with influxDB.
The last challenge we are still facing is that we are trying to implement the psychrometric chart into the node-red dashboard. To be honest, we don't have enough time to do this so it will be a goal for our summer project.
- Tools I'm using and assessment:
For operating system, I'm using Openhabian OS right now. The reason for that is because Openhabian is optimized for IOT projects with all the built-in bindings, persistent, misc, and straight forward web based GUI for configuring your smart home. Disadvantages of the OS is that there are literally nothing to do if it cannot access the internet like the original raspianbuster OS made for Pi.
For the database, we decided to use Google Cloud services. Google Cloud, AWS and Microsoft Azure are getting more and more popular right now. Since we have already had an account with Google, we chose to work with Google Cloud services. Advantages of using the services is that your first 2 million HTTP requests are pretty much free and you will be provided with over $400 credit to use other services. When you create a project, it has its own Python environment, logs, etc. We have used Fire Store with python and we like the interface. However, the only disadvantage is that its GUI is not user-friendly. We have not played around with it enough to find out more; therefore, those are all the experience that we had.
About Node-red, it's an awesome tool. I managed to used Json for it and it's not that hard to understand.
About InfluxDB and Grafana, they are great tools and they have a lot of community support. Most of the small problem during the settings that we have can be solved from reading posts on their community blogs. Drawbacks of Influx is that it doesn't have a proper user interface.
Conclusion
Overall, the project did work out in a way. Before my partner's equipment broke, it did get the data from sensors and upload those to a cloud-based database via Node-red. We also managed to get data graphed from our Influx database also using Node-red. Despite using two different database, it is a proof of concept that we can transfer data onto an online database and get it graphed. We will use only one database in our future project.
This project allow us to know more about the IOT concepts. With helps from our professors and peers, especial Prof. Paul O'Brien and Prof. Jamshedshod, we have figured what SSH client, HTTP request and API is and how to use them. We also know more about node-red and its possibilities. I'm also glad to learn some Json that helped us with our simulated data.
Future goal:
- Bring data from Fire Store to Grafana
- Transfer data from Fire Store into Openhabian
- Incorporate psychrometric chart into our project in order to manipulate plants
Again, thanks to all of those who have helped us during the project.