// // Code to read temperature from DLPDesign.com's DLP-TEMP-G // with DS18B20 digital thermometer from a Linux machine. // // Requires: // - Linux 2.6 kernel (newer 2.4's work too, but untested) // - USB core kernel modules // - USB FTDI kernel module for microcontroller on DLP-TEMP-G // // Also Useful: // - 'lsusb' tool for viewing USB bus // // License: // - Copyright (c) 2007 Tim Jensen, tim at jensens d0t org // - This code is FREE, so enjoy it. // #include #include #include #include int fd; void init() { // 9600 baud, 8n1, no flow control struct termios tc; fd = open("/dev/ttyUSB0", O_RDWR); // on my machine is ttyUSB0 if (fd < 0) { perror("/dev/ttyUSB0"); fprintf(stderr, "Cannot connect to temperature sensor\n"); return; } tcgetattr(fd, &tc); tc.c_iflag = IGNPAR; tc.c_oflag = 0; tc.c_cflag = CS8 | CREAD | CLOCAL; tc.c_lflag = 0; cfsetispeed(&tc, B9600); cfsetospeed(&tc, B9600); tcsetattr(fd, TCSANOW, &tc); } int main(int argc, const char **argv) { unsigned char buf[20]; int b; memset(buf,0,sizeof(buf)); init(); if (argc == 2 && !strcmp(argv[1],"-p")) { // // Ping the device expect 'Q' as response // write(fd, "P", 1); read(fd, buf, 1); printf("%c\n", buf[0]); return 0; } // // Probe temperature sensor S1 // write(fd, "S", 1); // // Read 8 bytes back from S1 // // 0 = temperature LSB // 1 = temperature MSB // 2-7 = Not used // for (b=0; b < 8; b++) read(fd, &buf[b], 1); // // Convert to temperature per the DS18B20 specs // int temp = buf[1]; temp = (temp << 8) | buf[0]; // combine MSB and LSB float C = (float)(temp) / 16.0; // adjust the radix to get C float F = (9.0/5.0) * C + 32.0; // convert to F for Americans if (argc == 2 && !strcmp(argv[1],"-i")) { printf("%d\n", (int)(F + 0.5)); // mrtg likes integers } else { printf("%1.2f\n", F); } return 0; }