summaryrefslogtreecommitdiffstats
path: root/src/main.cpp
blob: 85fe3fac339804058a461b1c23d6d1ec5e6172ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <Arduino.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <stdlib.h>
#include <inttypes.h>

// Pin setup
const int oneWirePin = 7;
const int dhtPin = 2;
#define DHTTYPE DHT22

#define BUFSIZE 32

OneWire oneWire(oneWirePin);
DallasTemperature sensors(&oneWire);
DHT dht(dhtPin, DHTTYPE);

void setup () {
	Serial.begin(9600);
	dht.begin();
	sensors.begin();
}


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);
}

void loop () {
	sensors.requestTemperatures();

	uint8_t devcounter = 0;

	// 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();

	print_value("t", devcounter, t1);
	print_value("h", devcounter, h1);
	devcounter++;
	Serial.println();
	delay(10000);
}