Read data from DS18B20 digital temperature sensor connected to a Raspberry Pi into Simulink

조회 수: 33 (최근 30일)
I wanted to implement temperature control using DS18B20 temperature sensor in Raspberry Pi 3 using Simulink external mode. Has anyone created SFunction to acquire temperature sensor readings connected to a Raspberry Pi? The code shown below shows error when I build in external mode.
#ifndef MATLAB_MEX_FILE
int main (void) {
DIR *dir;
DIR *dir1;
DIR *dir2;
char buf[256]; // Data from device
char buf1[256];
char buf2[256];
char tmpData[5]; // Temp C * 1000 reported by device
char tmpData1[5];
char tmpData2[5];
const char path[] = "/sys/bus/w1/devices/28-00000784ac50";
const char path1[] = "/sys/bus/w1/devices/28-00000783ea53";
const char path2[] = "/sys/bus/w1/devices/28-000007840c97";
char devPath[128]; // Path to device
char devPath1[128]; // Path to device
char devPath2[128]; // Path to device
ssize_t numRead;
int i = 0;
dir = opendir (path);
dir1 = opendir (path1);
dir2 = opendir (path2);
sprintf(devPath, "%s/%s/w1_slave", path);
sprintf(devPath1, "%s/%s/w1_slave", path1);
sprintf(devPath2, "%s/%s/w1_slave", path2);
while(1) {
int fd = open(path, O_RDONLY);
int fd1 = open(path1, O_RDONLY);
int fd2 = open(path2, O_RDONLY);
if(fd == -1){
y0=0;
}
if(fd1 == -1) {
y1=0;
}
if(fd2 == -1) {
y2=0;
}
while((numRead = read(fd, buf, 256)) > 0) {
strncpy(tmpData, strstr(buf, "t=") + 2, 5);
float tempC = strtof(tmpData, NULL);
y0 = tempC/1000;
}
while((numRead = read(fd1, buf1, 256)) > 0) {
strncpy(tmpData1, strstr(buf1, "t=") + 2, 5);
float tempC1 = strtof(tmpData1, NULL);
y1 = tempC1/1000;
}
while((numRead = read(fd2, buf2, 256)) > 0) {
strncpy(tmpData2, strstr(buf2, "t=") + 2, 5);
float tempC2 = strtof(tmpData2, NULL);
y2 = tempC2/1000;
}
}
}
# endif
The error shows -
"Error executing command "touch -c /home/pi/DS18B20_Trial2_ert_rtw/*.*;make -f DS18B20_Trial2.mk all -C /home/pi/DS18B20_Trial2_ert_rtw". Details:
STDERR: cc1: fatal error: DS18B20_Trial2.c.d: No such file or directory
compilation terminated.
make: *** [DS18B20_Trial2.c.o] Error 1"
I think the problem is with Matlab 2016a compiler? Any idea to solve this? Thanks a ton!!!!

답변 (1개)

Abdelghafar Elkhaoui
Abdelghafar Elkhaoui 2022년 3월 29일
These tutorials will help you to understand how you can integrate DS18B20 , in your program there is no library declared as i see, also you need to make change to a mandatory file related to DS18B20 from C to C++ , another tip is to look for the directory of your workspace in matlab , the folder should be in the right directory
Good Luck

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by