#include #include #include //--------------------------------------------------------------------------- // Copyright (C) 2000 Dallas Semiconductor Corporation, All Rights Reserved. // // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES // OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // // Except as contained in this notice, the name of Dallas Semiconductor // shall not be used except as stated in the Dallas Semiconductor // Branding Policy. //--------------------------------------------------------------------------- // // tempC.c - Application to find and read the 1-Wire Net // DS1920/DS1820/DS18S20/DS1822/DS18B20 - temperature measurement. // // This application uses the files from the 'Public Domain' // 1-Wire Net libraries ('general' and 'userial'). // // // Version: 2.00 // #include #include #include #include #include "ownet.h" #include "temp10.h" #include "findtype.h" // defines #define MAXDEVICES 20 // global serial numbers uchar FamilySN[MAXDEVICES][8]; // variables int family_code; //---------------------------------------------------------------------- // Log results to files whose names are YYYY/MM/DD. // Files are created with first sample after midnight GMT. // int main(int argc, char **argv) { float current_temp; int i = 0; int NumDevices=0; int TotalDevs; int portnum = 0; time_t now, tomorrow = 0; struct tm *now_tm; const int FNL = 20; char fn[FNL]; int rslt; // check for required port name if (argc != 2) { printf("1-Wire Net name required on command line!\n" " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" " "(Linux DS2480),\"1\" (Win32 TMEX)\n"); exit(1); } do // (stops on CTRL-C) { now = time(0); if (now > tomorrow){ now_tm = gmtime(&now); rslt = strftime(fn, FNL, "%Y%m%d", now_tm); if (rslt < 0) { fprintf(stderr,"Cannot create new logfile name\n"); exit(-1); } if ( freopen(fn,"a",stdout) == NULL){ fprintf(stderr,"Cannot open new logfile\n"); exit(-1); } // determine when tomorrow starts now_tm = localtime(&now); now_tm->tm_mday += 1; now_tm->tm_sec = now_tm->tm_min = 0; now_tm->tm_hour = now_tm->tm_gmtoff/3600; /* ********************************************* if (now_tm->tm_isdst){ fprintf(stderr,"adjust for DST\n"); now_tm->tm_hour -= 1; } ********************************************* */ tomorrow = mktime(now_tm); fprintf(stderr, "%d\n", tomorrow); } // attempt to acquire the 1-Wire Net while((portnum = owAcquireEx(argv[1])) < 0) { OWERROR_DUMP(stderr); sleep(1); } // Find the device(s) DS1820, DS1920 TotalDevs = NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES); //fprintf(stderr, "[%d]", NumDevices); fflush(stderr); // read the temperature and print serial number and temperature for (i = 0; i < NumDevices; i++) { if (ReadTemperature(portnum, FamilySN[i],¤t_temp)) { now = time(0); printf("%ld ", now); PrintSerialNum(FamilySN[i]); printf(" %6.2f \n", current_temp); // converting temperature from Celsius to Fahrenheit } else { printf(" Error reading "); PrintSerialNum(FamilySN[i]); printf(" temperature, verify device present:%d\n", (int)owVerify(portnum, FALSE)); } fflush(stdout); //sleep(1); } // Find more device(s) DS1822 TotalDevs += NumDevices = FindDevices(portnum, &FamilySN[0], 0x22, MAXDEVICES); //fprintf(stderr, "[%d]", NumDevices); fflush(stderr); // read the temperature and print serial number and temperature for (i = 0; i < NumDevices; i++) { if (ReadTemp22(portnum, FamilySN[i],¤t_temp)) { now = time(0); printf("%ld ", now); PrintSerialNum(FamilySN[i]); printf(" %6.2f \n", current_temp); // converting temperature from Celsius to Fahrenheit } else { printf(" Error reading "); PrintSerialNum(FamilySN[i]); printf(" temperature, verify device present:%d\n", (int)owVerify(portnum, FALSE)); } fflush(stdout); //sleep(1); } // Find even more device(s) DS18B20 TotalDevs += NumDevices = FindDevices(portnum, &FamilySN[0], 0x28, MAXDEVICES); //fprintf(stderr, "[%d]", NumDevices); fflush(stderr); // read the temperature and print serial number and temperature for (i = 0; i < NumDevices; i++) { if (ReadTemp28(portnum, FamilySN[i],¤t_temp)) { now = time(0); printf("%ld ", now); PrintSerialNum(FamilySN[i]); printf(" %6.2f \n", current_temp); // converting temperature from Celsius to Fahrenheit } else { printf(" Error reading "); PrintSerialNum(FamilySN[i]); printf(" temperature, verify device present:%d\n", (int)owVerify(portnum, FALSE)); } fflush(stdout); //sleep(1); } // release the 1-Wire Net owRelease(portnum); sleep(1); if (TotalDevs == 0) { printf("\n\n\nERROR, no DS1920/DS18S20/DS1822/DS18B20 found!\n"); sleep(15); } } while (!key_abort()); return 0; }