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
|
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include "DHT.h"
#define TOPIC_T "data/here/temperature"
#define TOPIC_H "data/here/humidity"
#define TOPIC_A "data/here/available"
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 1, 33 };
byte ip[] = { 192, 168, 1, 72 };
EthernetClient ethClient;
PubSubClient client(server, 1883, 0, ethClient);
void setup() {
Ethernet.begin(mac, ip);
dht.begin();
Serial.begin(9600);
if (client.connect("arduinoClient")) {
client.publish(TOPIC_A,"1");
}
}
void loop() {
float t = dht.readTemperature();
float h = dht.readHumidity();
//Serial.println(t);
//Serial.println(h);
char temp[6];
dtostrf(t, 5, 2, temp);
//Serial.println(temp);
char hum[6];
dtostrf(h, 5, 2, hum);
//Serial.println(hum);
client.publish(TOPIC_T,temp);
client.publish(TOPIC_H,hum);
delay(5000);
}
|