nodemecu/0x01-temperatura/0x01-temperatura.ino

69 lines
1 KiB
Arduino
Raw Permalink Normal View History

2022-03-23 19:21:36 +00:00
2022-04-05 20:08:52 +00:00
#include <OneWire.h>
#include <DallasTemperature.h>
2022-03-23 19:21:36 +00:00
2022-04-05 20:08:52 +00:00
// Pin donde se conecta el bus 1-Wire
const int pinDatosDQ = 2;
2022-03-23 19:21:36 +00:00
2022-04-05 20:08:52 +00:00
// Instancia a las clases OneWire y DallasTemperature
OneWire oneWireObjeto(pinDatosDQ);
DallasTemperature sensorDS18B20(&oneWireObjeto);
2022-03-23 19:21:36 +00:00
2022-03-23 19:11:54 +00:00
2022-04-05 20:08:52 +00:00
// Dispositivo
const int devID = 1;
char devIDstr[5];
int inQuery;
2022-03-23 19:21:36 +00:00
2022-03-23 19:11:54 +00:00
2022-04-05 20:08:52 +00:00
float getData() {
2022-03-23 19:11:54 +00:00
2022-04-05 20:08:52 +00:00
sensorDS18B20.requestTemperatures();
return (sensorDS18B20.getTempCByIndex(0));
2022-03-23 19:58:30 +00:00
}
2022-03-23 19:11:54 +00:00
2022-03-25 20:09:45 +00:00
2022-04-05 20:08:52 +00:00
void setup() {
Serial.begin(9600);
sensorDS18B20.begin();
2022-03-25 20:09:45 +00:00
2022-04-05 20:08:52 +00:00
}
2022-03-23 19:58:30 +00:00
void loop() {
2022-04-05 20:08:52 +00:00
2022-03-23 19:58:30 +00:00
if ( Serial.available() > 0) {
2022-04-05 20:08:52 +00:00
inQuery = Serial.read();
2022-04-05 21:42:54 +00:00
if ( inQuery == devID) {
2022-03-23 19:58:30 +00:00
2022-04-05 20:08:52 +00:00
Serial.write(itoa(devID, devIDstr, 10));
2022-03-23 19:58:30 +00:00
Serial.write(" ");
2022-04-05 20:08:52 +00:00
Serial.write("T");
2022-04-05 21:42:54 +00:00
Serial.write(" ");
2022-04-05 20:08:52 +00:00
2022-04-05 21:42:54 +00:00
char lecturaStr[6]; // Buffer big enough for 7-character float
dtostrf(getData(), 2, 4, lecturaStr); // Leave room for too large numbers!
2022-04-05 20:08:52 +00:00
Serial.write(lecturaStr);
2022-03-23 19:58:30 +00:00
Serial.write(" ");
2022-04-05 20:08:52 +00:00
Serial.write("C");
2022-03-23 19:58:30 +00:00
Serial.write(" ");
Serial.write("99");
2022-04-05 20:08:52 +00:00
}
2022-03-23 19:58:30 +00:00
}
2022-04-05 20:08:52 +00:00
}