-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HIoT_MQTTView_v2
167 lines (134 loc) · 3.87 KB
/
HIoT_MQTTView_v2
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#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 = "192.168.1.52";
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 TEMP#1>") == 0)
{
lcd.setCursor(0, 0);
lcd.print("1:");
// if(payload[4]!='.') {
lcd.print(payload_string);
lcd.print(" C");
// }
// else lcd.print("-na-");
}
if(strcmp(topic,"<MQTT TOPIC FOR TEMP#2>") == 0)
{
lcd.setCursor(9, 0);
lcd.print("2:");
// if(payload[4]!='.') {
lcd.print(" -- "); //lcd.print(payload_string);
lcd.print(" C");
// }
// else lcd.print("-na-");
}
if(strcmp(topic,"<MQTT TOPIC FOR TEMP#3>") == 0)
{
lcd.setCursor(0, 1);
lcd.print("3:");
// if(payload[4]!='.') {
lcd.print(" -- "); //lcd.print(payload_string);
lcd.print(" C");
// }
// else lcd.print("-na-");
}
if(strcmp(topic,"<MQTT TOPIC FOR TEMP#4>") == 0)
{
lcd.setCursor(9, 1);
lcd.print("4:");
// if(payload[4]!='.') {
lcd.print(payload_string);
lcd.print(" C");
// }
// else lcd.print("-na-");
}
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");
// Once connected, publish an announcement...
// client.publish("outTopic","hello world");
// client.publish("home/temperature","s");
// ... and resubscribe
// client.subscribe("inTopic");
client.subscribe("<MQTT TOPIC FOR TEMP#1>");
client.subscribe("<MQTT TOPIC FOR TEMP#2>");
client.subscribe("<MQTT TOPIC FOR TEMP#3>");
client.subscribe("<MQTT TOPIC FOR TEMP#4>");
} 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 V2");
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("MQTTView V2");
lcd.setCursor(5, 1);
lcd.print("by HobbyIoT");
delay(5000);
lcd.setCursor(0, 0);
lcd.print("1:----C 2:----C ");
lcd.setCursor(0, 1);
lcd.print("3:----C 4:----C ");
// 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 tx2_string[10], tx3_string[10];
if (!client.connected()) {
reconnect();
}
client.loop();
}