summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2018-08-01 09:52:26 +0200
committerFlorian Pritz <bluewind@xinu.at>2018-08-01 09:52:26 +0200
commitfc7724a0b892fe2407443e417988baa50de6284b (patch)
tree51b7c93c131552eb18bd73c0836c410c9cdc9d3c
parent463aeb281230f8768c660b9fb0d18069d157d8ef (diff)
downloadtemplogger-v2-fc7724a0b892fe2407443e417988baa50de6284b.tar.gz
templogger-v2-fc7724a0b892fe2407443e417988baa50de6284b.tar.xz
Support variable number of ds18b20 sensors
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--src/main.cpp52
1 files changed, 30 insertions, 22 deletions
diff --git a/src/main.cpp b/src/main.cpp
index d8122a6..85fe3fa 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,6 +3,7 @@
#include <DallasTemperature.h>
#include <DHT.h>
#include <stdlib.h>
+#include <inttypes.h>
// Pin setup
const int oneWirePin = 7;
@@ -18,35 +19,42 @@ DHT dht(dhtPin, DHTTYPE);
void setup () {
Serial.begin(9600);
dht.begin();
+ sensors.begin();
}
-void loop () {
- sensors.requestTemperatures();
-
- float t1 = sensors.getTempCByIndex(0);
-
- float t2 = dht.readTemperature();
- float h1 = dht.readHumidity();
-
- char temp[BUFSIZE];
- // format: <timestamp|0> t:1=<temp> t:2=<temp> h:1=<humidity>
- String line = "";
- line += 0;
+void print_float2serial(float val) {
+ char buf[BUFSIZE];
+ dtostrf(val, 5, 2, buf);
+ Serial.print(buf);
+}
+void print_value(char* type, uint8_t id, float val) {
+ Serial.print(" ");
+ Serial.print(type);
+ Serial.print(":");
+ Serial.print(id);
+ Serial.print("=");
+ print_float2serial(val);
+}
- line += " t:1=";
- dtostrf(t1, 5, 2, temp);
- line += temp;
+void loop () {
+ sensors.requestTemperatures();
- line += " t:2=";
- dtostrf(t2, 5, 2, temp);
- line += temp;
+ uint8_t devcounter = 0;
- line += " h:1=";
- dtostrf(h1, 5, 2, temp);
- line += temp;
+ // output format: <timestamp|0> t:1=<temp> t:2=<temp> h:1=<humidity>
+ Serial.print("0");
+ for (uint8_t i = 0; i < sensors.getDeviceCount(); ++i) {
+ float temp = sensors.getTempCByIndex(i);
+ print_value("t", devcounter++, temp);
+ }
+ float t1 = dht.readTemperature();
+ float h1 = dht.readHumidity();
- Serial.println(line);
+ print_value("t", devcounter, t1);
+ print_value("h", devcounter, h1);
+ devcounter++;
+ Serial.println();
delay(10000);
}