Read data from DS18B20 digital temperature sensor connected to a Raspberry Pi into Simulink
29 views (last 30 days)
Show older comments
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 Comment
Thomas Lundberg
on 8 May 2020
So how did the project turn out? Did you manage to get the temp into simulink from the 1wire connection
Answers (1)
Abdelghafar Elkhaoui
on 29 Mar 2022
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
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!