-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtechReader.ino
78 lines (64 loc) · 1.53 KB
/
techReader.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
#include <ArduinoMqttClient.h>
#include <ESP8266WiFi.h>
#include "settings.h"
#include "keyValuePairs.h"
#include "mappings_1.h"
#include "UARTKeyValueReader.h"
WiFiClient wifiClient;
MqttClient mqttClient(wifiClient);
UARTKeyValueReader reader1(rxPin, txPin, &mqttClient, &nameMapping1, "Port_1");
#ifdef useSecondSerial
#include "mappings_2.h"
UARTKeyValueReader reader2(rxPin2, txPin2, &mqttClient, &nameMapping2, "Port_2");
#endif
bool reader1IsCurrent = true;
void setup() {
Serial.begin(serialBaud);
reader1.begin(serialBaud);
reader1.listen();
#ifdef useSecondSerial
reader2.begin(serialBaud);
#endif
setup_wifi();
setup_mqtt();
}
void setup_wifi() {
// Attempt to connect to Wifi network
Serial.print("Attempting to connect to Wifi SSID: ");
Serial.println(wifiSsid);
while (WiFi.begin(wifiSsid, wifiPassword) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the wifi network");
}
void setup_mqtt() {
mqttClient.setId(mqttId);
mqttClient.setUsernamePassword(mqttUsername, mqttPassword);
mqttConnect(&mqttClient, true);
}
void loop() {
if (reader1IsCurrent) {
if (reader1.isPublished()) {
#ifdef useSecondSerial
reader1IsCurrent = false;
reader2.listen();
#else
reader1.listen();
#endif
} else {
reader1.loop();
}
}
#ifdef useSecondSerial
if (!reader1IsCurrent) {
if (reader2.isPublished()) {
reader1IsCurrent = true;
reader1.listen();
} else {
reader2.loop();
}
}
#endif
}