-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_lcd_v1.ino
124 lines (94 loc) · 2.76 KB
/
mqtt_lcd_v1.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
120
121
122
123
124
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <LiquidCrystal.h>
const int RS = D2, EN = D3, d4 = D5, d5 = D6, d6 = D7, d7 = D8;
LiquidCrystal lcd(RS, EN, d4, d5, d6, d7);
#define ONE_WIRE_BUS_1 14 //2
#define ONE_WIRE_BUS_2 5 //4
const char* ssid = "<WIFI SSID>";
const char* password = "<WIFI PASSWORD>";
const char* mqtt_server = "<MQTT SERVER IP ADDRESS>";
const char* mqtt_username = "<MQTT_BROKER_USERNAME>";
const char* mqtt_password = "<MQTT_BROKER_PASSWORD>";
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char* topic, byte* payload, unsigned int length) {
char *payload_string = (char *) payload;
payload_string[length] = NULL;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
// for (int i=0;i<length;i++) {
// Serial.print((char)payload[i]);
Serial.print(payload_string);
// }
Serial.println();
// Serial.print(topic[length]);
if(strcmp(topic,"<MQTT TOPIC FOR AIRTEMP>") == 0)
{
lcd.setCursor(0, 1);
lcd.print("A: ");
lcd.print(payload_string);
}
if(strcmp(topic,"<MQTT TOPIC FOR WATERTEMP>") == 0)
{
lcd.setCursor(9, 1);
lcd.print("W: ");
lcd.print(payload_string);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("MQTTView")) {
Serial.println("connected");
client.subscribe("<MQTT TOPIC FOR AIRTEMP>");
client.subscribe("<MQTT TOPIC FOR WATERTEMP>");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("MQTT DATA LCD Viewer");
Serial.println("====================");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.setCursor(2, 0);
lcd.print("MQTTLCD Data");
lcd.setCursor(0, 1);
lcd.print("A: --- W: --- ");
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
float airtemp, watertemp;
char airtemp_string[10], watertemp_string[10];
if (!client.connected()) {
reconnect();
}
client.loop();
}