Sending Data sensor from MATLAB Simulink to firebase

7 ビュー (過去 30 日間)
Hamza hamu
Hamza hamu 2023 年 5 月 14 日
コメント済み: Roberto 2024 年 8 月 9 日
Hi,
I have a MATLAB Simulink model of an EV station electric with sensors such as a current sensor. I want to send the real-time sensor data to Firebase. Is there a methode to do connect MATLAB with Firebase databse?
Thanks,

採用された回答

Shaik
Shaik 2023 年 5 月 14 日
Hi Hamza,
Yes, you can connect MATLAB to the Firebase Realtime Database using the Firebase REST API. The REST API allows you to interact with the Firebase Database using HTTP requests, which can be easily implemented in MATLAB.
Here are the general steps to connect MATLAB to the Firebase Realtime Database:
  1. Obtain the Firebase project credentials: Go to your Firebase project settings in the Firebase console and navigate to the "Service Accounts" tab. Generate a new private key and download the JSON file containing the credentials. This file will be used to authenticate your MATLAB application with Firebase.
  2. Install the JSONLab toolbox: MATLAB does not have built-in support for JSON parsing, so you'll need to install a third-party toolbox like JSONLab. JSONLab provides functions to encode and decode JSON data.
  3. Write MATLAB code to send data to Firebase: Here's an example of how you can send real-time sensor data to the Firebase Realtime Database using MATLAB:
% Load the Firebase credentials
firebaseCredentials = jsondecode(fileread('path/to/firebase_credentials.json'));
% Specify the Firebase URL and the data to send
firebaseURL = 'https://your-firebase-project.firebaseio.com/';
data = struct('current', 10, 'voltage', 220); % Replace with your actual sensor data
% Encode the data as JSON
jsonData = savejson('', data);
% Create the HTTP request options
options = weboptions('RequestMethod', 'post', 'HeaderFields', {'Content-Type' 'application/json'}, 'Timeout', 10);
% Send the data to Firebase
response = webwrite([firebaseURL '.json'], jsonData, options);
Make sure to replace 'path/to/firebase_credentials.json' with the actual path to your Firebase credentials JSON file, and 'https://your-firebase-project.firebaseio.com/' with the URL of your Firebase Realtime Database.
In this example, data is a MATLAB struct containing the sensor data you want to send. The savejson function from the JSONLab toolbox is used to encode the data as JSON. The webwrite function is then used to send an HTTP POST request to Firebase, including the JSON data.
  3 件のコメント
Pedro
Pedro 2024 年 3 月 1 日
Hey @Shaik,
I used the code and whenever I send new information it creates a new node with a different code, would it be possible that it just overwrites an existing node? And how do I, for example, read the information from a node that is in Firebase and write these values ​​into a variable in Matlab?
Roberto
Roberto 2024 年 8 月 9 日
Excuse me @Shaik, how do you use the firebase credentials in this example?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJSON Format についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by