-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_mqtt_basic.ino
119 lines (93 loc) · 2.59 KB
/
my_mqtt_basic.ino
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include "DHT.h"
// dht11 and ds18b20 pins
#define DHTTYPE DHT11
#define DHTPIN 2
#define INTERVAL 5000 // interval in ms
#define ONE_WIRE_BUS 8
// RGB led
#define RPIN 3
#define GPIN 5
#define BPIN 6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature dsensor(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
byte mac[] = { 0x90, 0xA2, 0xDA, 0x03, 0x00, 0xC3 };
IPAddress myip;
IPAddress server(192, 168, 1, 10);
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect("arduinoClient")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
setPinColor(255, 0, 0); // red light
delay(5000);
}
}
}
void setPinColor(int red, int green, int blue){
analogWrite(RPIN, red);
analogWrite(GPIN, green);
analogWrite(BPIN, blue);
}
void publishTemp(){
// publish temperature data
float t = dht.readTemperature();
float h = dht.readHumidity();
dsensor.requestTemperatures();
float td = dsensor.getTempCByIndex(0);
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
//mqttClient.publish("temp", "error");
return;
}
Serial.print("Publishing DHT11 temperature: ");
Serial.println(t);
mqttClient.publish("sensors/dht11/temp", String(t).c_str());
Serial.print("Publishing DHT11 humidity: ");
Serial.println(h);
mqttClient.publish("sensors/dht11/humidity", String(h).c_str());
Serial.print("Publishing DS18B20 temperature: ");
Serial.println(td);
mqttClient.publish("sensors/dallas/temp", String(td).c_str());
setPinColor(0, 255, 0); // green light
}
void setup(){
Serial.begin(9600);
mqttClient.setServer(server, 1883); // set mosquitto server
IPAddress myip(192, 168, 1, 2);
Ethernet.begin(mac, myip);
//myip = Ethernet.localIP();
Serial.print("My ip: ");
Serial.println(myip);
Serial.print("Server: ");
Serial.println(server);
delay(1500);
Serial.println("Starting sensors...");
dht.begin();
dsensor.begin();
pinMode(RPIN, OUTPUT);
pinMode(GPIN, OUTPUT);
pinMode(BPIN, OUTPUT);
}
void loop(){
if (!mqttClient.connected()) {
setPinColor(255, 0, 0); // red light
reconnect();
}
mqttClient.loop();
publishTemp();
delay(INTERVAL);
}