Effacer les filtres
Effacer les filtres

how to send data to firebase and also receive data using matlab

16 vues (au cours des 30 derniers jours)
Pedro
Pedro le 1 Mar 2024
Réponse apportée : Chetan le 10 Mai 2024
I want to send data from some variables to the firebase database and then I also want to receive this data, I used this code:
% Load the Firebase credentials
firebaseCredentials = jsondecode(fileread('path/to/firebase_credentials.json')); %I exchanged this for my data
% Specify the Firebase URL and the data to send
firebaseURL = 'https://your-firebase-project.firebaseio.com/'; %I exchanged this for my data
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);
But whenever I send data it creates a new node, but I wanted it to replace the information that was already there in a specific node, because later I want to use an application that I created to read these values ​​in Firebase and create another variable in the same node that will be used to send data to change variable data in Matlab, and now if you could also help me with how to read the node that It's in Firebase through Matlab and writing it in a variable would be appreciated

Réponses (1)

Chetan
Chetan le 10 Mai 2024
Hi @Pedro,
It appears you're attempting to upload sensor data to Firebase, but each attempt results in the creation of a new node. Additionally, you're looking to retrieve the updated data.
To modify data in an existing node in Firebase without generating a new node each time, you must specify the precise path to the desired node in your Firebase URL. Utilizing the 'post' method causes Firebase to create a new child location for every new data set. To update or replace data at a specific location, you should opt for the 'put' method over 'post' in your `weboptions`.
Here's an example code for this process:
% Load Firebase credentials
firebaseCredentials = jsondecode(fileread('path/to/firebase_credentials.json')); % Replace with your data
% Define the Firebase URL and data to send
firebaseURL = 'https://your-firebase-project.firebaseio.com/sensorData'; % Replace with your data
data = struct('current', 110, 'voltage', 1120); % Substitute with your actual sensor data
% Encode the data as JSON
% Set up HTTP request options
options = weboptions('RequestMethod', 'put', 'HeaderFields', {'Content-Type' 'application/json'}, 'Timeout', 10);
% Transmit the data to Firebase
response = webwrite([firebaseURL '.json'], data, options)
response = struct with fields:
current: 110 voltage: 1120
To fetch the updated data, you can use the `webread` function:
firebaseURL = 'https://your-firebase-project.firebaseio.com/sensorData.json'; % Replace with your data
% Configure HTTP request options, if necessary
options = weboptions('ContentType', 'json', 'Timeout', 10);
% Retrieve data from Firebase
dataRead = webread(firebaseURL, options)
dataRead = struct with fields:
current: 110 voltage: 1120
% `dataRead` now holds the data from your Firebase node, ready for use
For further information, you may consult the following MathWorks Documentation:
Hope this helps.

Community Treasure Hunt

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

Start Hunting!

Translated by