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 CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by