i am trying to figure out a way to store data from my rasberry Pi 5 sensors in an array on MatLab, i am new and wanted some guidance as to how i can add that.

5 views (last 30 days)
I'm not looking for the wholw answer, just some guidance or even exapmles on how to do it, anything will be helpful. my end goal is to be able to pull data from a certain time, or even display data over a period of time
Below is what i have so far
clear
clc
% Get user input on what data they want
Pref = input ('Enter what data you want to see, enter "altitude", "pressure", "temperature" or "all": ', 's');
Alt = 'altitude';
Pres = 'pressure';
Temp = 'temperature';
All_1 = 'all';
% Compare strings to determine which one was inputed
tf1 = strcmp(Alt,Pref);
tf2 = strcmp(Pres,Pref);
tf3 = strcmp(Temp,Pref);
tf4 = strcmp(All_1,Pref);
while tf1 == 0 && tf2 == 0 && tf3 == 0 && tf4 == 0
fprintf ('Error, the string inputed is nor recognized, try again\n');
% Get user input on what data they want
Pref = input ('Enter what data you want to see, enter "altitude", "pressure", "temperature" or "all": ', 's');
Alt = 'altitude';
Pres = 'pressure';
Temp = 'temperature';
All_1 = 'all';
% Compare strings to determine which one was inputed
tf1 = strcmp(Alt,Pref);
tf2 = strcmp(Pres,Pref);
tf3 = strcmp(Temp,Pref);
tf4 = strcmp(All_1,Pref);
end
% connect to Pi device
mypi = raspi;
% Get avialable I2C Buses on Pi device
mypi.AvailableI2CBuses;
% Display I2C Bus speed
mypi.I2CBusSpeed;
% Scan what devices are connected to I2c bus
scanI2CBus(mypi,'i2c-1');
% Read data from the BMP sensor on Pi device
bmp = raspi.internal.bmp180(mypi, 'i2c-1');
temperature = readTemperature(bmp);
pressure = readPressure(bmp);
temp_f = ceil (temperature * (9/5)) +32;
% calculate altitude
Altitude = BalAltFunc (pressure, temp_f);
% Display only temp
if tf3 == 1
fprintf (' current temperature is: %d F \n', temp_f);
end
% display only pressure
if f2 == 1
fprintf (' current pressure is: %f0.1 hPa \n', pressure);
end
% display only altitude
if tf1 == 1
fprintf (' current altitude based on pressure and temperature is: %d meters \n', a);
end
% display all data
if tf4 == 1
fprintf (' current temperature is: %d F \n', temp_f);
fprintf (' current pressure is: %f0.1 hPa \n', pressure);
fprintf (' current altitude based on pressure and temperature is: %d meters \n', a);
end
  2 Comments
Walter Roberson
Walter Roberson on 19 Feb 2025
% Get avialable I2C Buses on Pi device
mypi.AvailableI2CBuses;
% Display I2C Bus speed
mypi.I2CBusSpeed;
% Scan what devices are connected to I2c bus
scanI2CBus(mypi,'i2c-1');
It does not make much sense to run those commands and immediately throw away the output; at best it is a waste of time.
Alexander
Alexander on 19 Feb 2025
understood, i only played arround with MatLab for a couple months so i am still working on my efficiency. also, i need to compress the if statements into one.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 19 Feb 2025
near the top insert
temperatures = [];
pressures = [];
temp_fs = [];
Altitudes = [];
Then before
temperature = readTemperature(bmp);
pressure = readPressure(bmp);
temp_f = ceil (temperature * (9/5)) +32;
% calculate altitude
Altitude = BalAltFunc (pressure, temp_f);
insert
while true
and after the assignment to Altitude
temperatures(end+1) = temperature;
pressures(end+1) = pressure;
temp_fs(end+1) = temp_f;
Altitudes(end+1) = Altitude;
end
This will try to keep on reading from the sensor until it runs out of memory.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!