68 lines
1 KiB
C++
68 lines
1 KiB
C++
|
|
#include <OneWire.h>
|
|
#include <DallasTemperature.h>
|
|
|
|
// Pin donde se conecta el bus 1-Wire
|
|
const int pinDatosDQ = 2;
|
|
|
|
// Instancia a las clases OneWire y DallasTemperature
|
|
OneWire oneWireObjeto(pinDatosDQ);
|
|
DallasTemperature sensorDS18B20(&oneWireObjeto);
|
|
|
|
|
|
// Dispositivo
|
|
const int devID = 1;
|
|
char devIDstr[5];
|
|
int inQuery;
|
|
|
|
|
|
|
|
|
|
float getData() {
|
|
|
|
sensorDS18B20.requestTemperatures();
|
|
return (sensorDS18B20.getTempCByIndex(0));
|
|
}
|
|
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
sensorDS18B20.begin();
|
|
|
|
|
|
|
|
|
|
}
|
|
void loop() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( Serial.available() > 0) {
|
|
inQuery = Serial.read();
|
|
if ( inQuery == devID) {
|
|
|
|
Serial.write(itoa(devID, devIDstr, 10));
|
|
Serial.write(" ");
|
|
Serial.write("T");
|
|
Serial.write(" ");
|
|
|
|
|
|
char lecturaStr[6]; // Buffer big enough for 7-character float
|
|
dtostrf(getData(), 2, 4, lecturaStr); // Leave room for too large numbers!
|
|
|
|
|
|
Serial.write(lecturaStr);
|
|
Serial.write(" ");
|
|
Serial.write("C");
|
|
Serial.write(" ");
|
|
Serial.write("99");
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|