Filter löschen
Filter löschen

Sending Data sensor from MATLAB Simulink to firebase

15 Ansichten (letzte 30 Tage)
Hamza hamu
Hamza hamu am 14 Mai 2023
Kommentiert: Roberto am 9 Aug. 2024 um 21:59
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,

Akzeptierte Antwort

Shaik
Shaik am 14 Mai 2023
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 Kommentare
Pedro
Pedro am 1 Mär. 2024
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 am 9 Aug. 2024 um 21:59
Excuse me @Shaik, how do you use the firebase credentials in this example?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by