-
Notifications
You must be signed in to change notification settings - Fork 4
/
mqtt.ino
112 lines (86 loc) · 2.69 KB
/
mqtt.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
#include "DHT.h"
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// WiFi configuration
#define WLAN_SSID ""
#define WLAN_PASS ""
// In our scenario the DHCP is disabled
// so we need to configure the network statically
IPAddress ip(192, 168, 1, 10);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
// MQTT configuration
#define MQTT_SERVER "192.168.1.200" // test.mosquitto.org
#define MQTT_SERVERPORT 1883
#define MQTT_USERNAME ""
#define MQTT_KEY ""
#define MQTT_FEED_TEMP "ies/aula20/temperature"
#define MQTT_FEED_HUMI "ies/aula20/humidity"
// WiFi connection
WiFiClient client;
// MQTT connection
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_USERNAME, MQTT_KEY);
// Feed to publish temperature
Adafruit_MQTT_Publish temperatureFeed = Adafruit_MQTT_Publish(&mqtt, MQTT_FEED_TEMP);
// Feed to publish humidity
Adafruit_MQTT_Publish humidityFeed = Adafruit_MQTT_Publish(&mqtt, MQTT_FEED_HUMI);
//----------------------------------------------
void connectWiFi();
//----------------------------------------------
void setup() {
Serial.begin(115200);
Serial.println("IoT demo");
dht.begin();
connectWiFi();
connectMQTT();
}
//----------------------------------------------
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
temperatureFeed.publish(t);
humidityFeed.publish(h);
}
//----------------------------------------------
void connectWiFi() {
WiFi.config(ip, gateway, gateway, subnet);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//----------------------------------------------
void connectMQTT() {
if (mqtt.connected())
return;
Serial.print("Connecting to MQTT... ");
while (mqtt.connect() != 0) {
Serial.println("Error. Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000);
}
}