/* Simple 1-wire device reading Pass T over serial to get temperatures Pass L to list 1-wire devices */ #include #include #define CONVERT 0x44 #define READSCRATCH 0xBE #define SKIP_ROM 0xCC #define MATCH_ROM 0x55 OneWire ds(10); // The DS18S20 is connected on pin 10 int ledPin = 13; // flash an led on 13 - we all like flashing lights void setup(void) { // initialize inputs/outputs // start serial port Serial.begin(9600); pinMode(ledPin, OUTPUT); //we'll use the led to output a heartbeat } void loop(void) { int incomingByte = 0; if (Serial.available() > 0) { // read the incoming byte: incomingByte = Serial.read(); // say what you got: //Serial.print("I received: "); //Serial.println(incomingByte, DEC); if (incomingByte == 84) { getStuff(); Serial.print("\r\n^"); } else if (incomingByte == 76) { listDevices(); } } } void listDevices(void) { byte addr[8]; ds.reset_search(); while(ds.search(addr)) { digitalWrite(ledPin, HIGH); // sets the LED on if ( OneWire::crc8( addr, 7) == addr[7]) { if ( addr[0] == 0x10) { // Make sure it is a DS18S20 device char buffer[512]; sprintf(buffer,"1-Wire ID:%02x%02x%02x%02x%02x%02x%02x%02x\r\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); Serial.print(buffer); } } } Serial.print('^'); } // commented out the xml output just using raw, means very little // to process it void getStuff(void) { byte i; byte present = 0; byte data[12]; byte addr[8]; char buff[15]; float real_temp; float temp_count; float read_temp; ds.reset_search(); while(ds.search(addr)) { digitalWrite(ledPin, HIGH); // sets the LED on if ( OneWire::crc8( addr, 7) != addr[7]) // Check CRC is valid { Serial.print("CRC is not valid\r\n"); } if ( addr[0] != 0x10) { // Make sure it is a DS18S20 device Serial.print("Device is not a DS18S20 family device.\r\n"); } //Serial.print(" "); ds.reset(); // Reset device ds.select(addr); // Select device ds.write(CONVERT,1); // Issue Convert command delay(1000); // maybe 750ms is enough, maybe not // we might do a ds.depower() here, but the reset will take care of it. digitalWrite(ledPin, LOW); // sets the LED off present = ds.reset(); // Reset device ds.select(addr); // Select device ds.write(READSCRATCH); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = ds.read(); //Serial.print(data[i], HEX); //Serial.print(":"); } if(OneWire::crc8( data, 8) == data[8]) // Check CRC is valid { // CRC is ok read_temp=((data[1]<<8) | data[0]) >> 1 ; // Divide the temperature by 2 temp_count=float(data[7] - data[6])/(float)data[7]; // Convert to real temperature real_temp = ((float)read_temp-0.25)+temp_count; tempToAscii(real_temp,buff); // Convert float to ascii char buffer[512]; sprintf(buffer,"ARDUINO TEMP id %02x%02x%02x%02x%02x%02x%02x%02x %s\r\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7], buff); Serial.print(buffer); }else { //CRC failed Serial.println("CRC Failed\r\n"); // } } } void tempToAscii(double temp, char *buff) { int frac; frac=(unsigned int)(temp*1000)%1000; //get three numbers to the right of the deciaml point itoa((int)temp,buff,10); strcat(buff,"."); itoa(frac,&buff[strlen(buff)],10); //put the frac after the deciaml }