forked from maragelis/NextionMqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NextionMqtt.ino
311 lines (222 loc) · 6.73 KB
/
NextionMqtt.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <FS.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Nextion.h>
#include <SoftwareSerial.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h>
char mqtt_server[40];
char mqtt_port[6] = "1883";
bool InputSwitch1state=false;
bool InputSwitch2state=false;
const char* root_topicOut = "Nextion/Out";
const char* root_topicIn = "Nextion/In";
const int NextionRX = 13;
const int NextionTX = 15;
const int InputSwitch1 = 5;
const int InputSwitch2 = 4;
const int RelaySwitch1 = 14;
const int RelaySwitch2 = 12;
const int I2busSDA = 16;
const int I2cbusCLA = 2;
const char* Switch1Nextion="LocalSw1";
const char* Switch2Nextion="LocalSw2";
WiFiClient espClient;
PubSubClient client(espClient);
SoftwareSerial nextion(NextionRX, NextionTX);
Nextion myNextion(nextion, 9600);
struct JsonPayload{
String Topic;
String Payload;
} ;
bool shouldSaveConfig = false;
//callback notifying us of the need to save config
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Program Start");
setup_wifi();
pinMode(InputSwitch1,INPUT);
pinMode(InputSwitch2,INPUT);
pinMode(RelaySwitch1,OUTPUT);
pinMode(RelaySwitch2,OUTPUT);
myNextion.init();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
String message = myNextion.listen(); //check for message
if(message != ""){ // if a message is received...
String Component = GetComponent(message);
SendMessage((char*) Component.c_str());
Serial.println(Component); //...print it out
}
else
{
// Serial.println("No message"); //...print it out
}
}
void mountfs()
{
if (SPIFFS.begin()) {
Serial.println("mounted file system");
if (SPIFFS.exists("/config.json")) {
//file exists, reading and loading
Serial.println("reading config file");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("opened config file");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nparsed json");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(mqtt_port, json["mqtt_port"]);
} else {
Serial.println("failed to load json config");
}
}
}
} else {
Serial.println("failed to mount FS");
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String Payload;
for (int i = 0; i < length; i++) {
Payload += (char)payload[i];
}
Serial.println(Payload);
JsonPayload data = Decodejson((char*)Payload.c_str());
Serial.println("JSON Returned!");
RunCommandonNextion(data);
}
bool RunCommandonNextion(struct JsonPayload data){
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(data.Payload);
if (!root.success()) {
Serial.println("RunCommandonNextion JSON parsing failed!");
return false;
}
if (data.Topic == "setComponentText")
{
Serial.println("setComponentText init");
Serial.println((const char *)root["component"]);
Serial.println((const char *)root["text"]);
myNextion.setComponentText(root["component"],root["text"]);
return true;
}
if (data.Topic == "sendCommand")
{
Serial.println("sendCommand init");
Serial.print("With Command =");
Serial.println((const char *) root["command"] );
myNextion.sendCommand( root["command"] );
return true;
}
}
struct JsonPayload Decodejson(char* Payload)
{
JsonPayload data;
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(Payload);
if (!root.success()) {
data = {"",""};
Serial.println("JSON parsing failed!");
return data;
} else
{
String topic = root["topic"];
Serial.println(topic);
String payload = root["payload"];
Serial.println(payload);
JsonPayload data1 = {topic,payload};
return data1;
}
return data;
}
void SendMessage(char* Message)
{
client.publish(root_topicOut, Message);
}
String GetComponent(String NextionMessage)
{
NextionMessage.replace(" ","");
NextionMessage.replace("ffff","");
NextionMessage.trim();
return NextionMessage;
}
void setup_wifi(){
WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40);
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.setConfigPortalTimeout(180);
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.autoConnect("NextionAP");
strcpy(mqtt_server, custom_mqtt_server.getValue());
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
//read updated parameters
strcpy(mqtt_server, custom_mqtt_server.getValue());
//save the custom parameters to FS
if (shouldSaveConfig) {
Serial.println("saving config");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["mqtt_server"] = mqtt_server;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
//end save
}
Serial.println("local ip");
Serial.println(WiFi.localIP());
Serial.println(custom_mqtt_server.getValue());
client.setServer( mqtt_server, 1883);
client.setCallback(callback);
reconnect();
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(root_topicOut, "Connected!");
// ... and resubscribe
client.subscribe(root_topicIn);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}