@@ -13,21 +13,55 @@ class EasyMqtt : public MqttEntry {
13
13
PubSubClient mqttClient;
14
14
WebPortal webPortal;
15
15
16
- ConfigEntry* config ;
16
+ ConfigEntry* configEntry ;
17
17
18
18
String deviceId = " deviceId" ;
19
- const char * mqtt_username = " N/A" ;
20
- const char * mqtt_password = " N/A" ;
21
19
22
20
protected:
23
21
/* *
24
22
Handle connections to mqtt
25
23
*/
26
- void mqttReconnect () {
27
- // if(WiFi.status() != WL_CONNECTED) {
28
- while (!mqttClient.connected ()) {
29
- if (mqttClient.connect (deviceId.c_str (), mqtt_username, mqtt_password)) {
24
+ void connect () {
25
+ if (WiFi.status () != WL_CONNECTED) {
26
+ const char * ssid = config ().getCString (" wifi.ssid" , " " );
27
+ const char * password = config ().getCString (" wifi.password" , " " );
28
+ WiFi.mode (WIFI_STA);
29
+ WiFi.begin (ssid, password);
30
+
31
+ int timer = 0 ;
32
+ while (WiFi.status () != WL_CONNECTED && timer < 10 ) {
33
+ delay (500 );
34
+ timer++;
35
+ // ToDo: handle timeout, and create AP
36
+ }
37
+ if (timer < 10 ) {
38
+ debug (" WiFi connected" );
39
+ } else {
40
+ debug (" WiFi connection timeout" );
41
+ // ToDo: create AP
42
+ }
43
+ debug (" IP address" , WiFi.localIP ().toString ());
44
+ debug (" devideId" , deviceId);
45
+ webPortal.setup (*this );
46
+ }
47
+ if (mqttClient.state () == MQTT_DISCONNECTED) {
48
+ // Setup MQTT
49
+ const char * host = config ().getCString (" mqtt.host" , " " );
50
+ int port = config ().getInt (" mqtt.port" , 1883 );
51
+ mqttClient.setClient (wifiClient);
52
+ mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
53
+ each ([=](MqttEntry* entry){
54
+ entry->callback (topic, payload, length);
55
+ });
56
+ });
57
+ mqttClient.setServer (host, port);
58
+ }
59
+ if (!mqttClient.connected ()) {
60
+ const char * username = config ().getCString (" mqtt.username" , " " );
61
+ const char * password = config ().getCString (" mqtt.password" , " " );
62
+ if (mqttClient.connect (deviceId.c_str (), username, password)) {
30
63
debug (" Connected to MQTT" );
64
+ debug (" Topic" , getTopic ());
31
65
each ([&](MqttEntry* entry){
32
66
if (entry->isOut ()) {
33
67
mqttClient.subscribe (entry->getTopic ().c_str ());
@@ -42,12 +76,11 @@ class EasyMqtt : public MqttEntry {
42
76
43
77
public:
44
78
EasyMqtt () : MqttEntry(" easyMqtt" , mqttClient) {
45
- Serial.begin (115200 );
46
79
deviceId = String (ESP.getChipId ());
47
80
48
81
debug (" test" );
49
- config = new ConfigEntry (mqttClient, *this );
50
- addChild (config );
82
+ configEntry = new ConfigEntry (mqttClient, *this );
83
+ addChild (configEntry );
51
84
52
85
get (" system" ).setInterval (30 );
53
86
get (" system" )[" deviceId" ] << [this ]() {
@@ -59,6 +92,18 @@ class EasyMqtt : public MqttEntry {
59
92
get (" system" )[" uptime" ] << []() {
60
93
return String (millis () / 1000 );
61
94
};
95
+
96
+ // Setup wifi diag
97
+ get (" system" )[" wifi" ][" rssi" ] << []() {
98
+ return String (WiFi.RSSI ());
99
+ };
100
+ get (" system" )[" wifi" ][" ssid" ] << []() {
101
+ return WiFi.SSID ();
102
+ };
103
+ get (" system" )[" wifi" ][" ip" ] << []() {
104
+ return WiFi.localIP ().toString ();
105
+ };
106
+
62
107
get (" system" )[" restart" ] >> [this ](String value) {
63
108
if (value == " restart" ) {
64
109
debug (" Restart" );
@@ -67,13 +112,13 @@ class EasyMqtt : public MqttEntry {
67
112
};
68
113
get (" system" )[" config" ][" reset" ] >> [this ](String value) {
69
114
if (value == " reset" ) {
70
- getConfig ()-> reset ();
115
+ config (). reset ();
71
116
}
72
117
};
73
118
}
74
119
75
- ConfigEntry* getConfig () {
76
- return config ;
120
+ ConfigEntry & config () {
121
+ return *configEntry ;
77
122
}
78
123
79
124
void debug (String msg) {
@@ -101,53 +146,25 @@ class EasyMqtt : public MqttEntry {
101
146
Setup connection to wifi
102
147
*/
103
148
void wifi (const char * ssid, const char * password) {
104
- WiFi.mode (WIFI_STA);
105
- WiFi.begin (ssid, password);
106
-
107
- while (WiFi.status () != WL_CONNECTED) {
108
- delay (500 );
109
- // ToDo: handle timeout, and create AP
110
- }
111
- debug (" WiFi connected" );
112
- debug (" IP address" , WiFi.localIP ().toString ());
113
- debug (" devideId" , deviceId);
114
-
115
- // Setup wifi diag
116
- get (" system" )[" wifi" ][" rssi" ] << []() {
117
- return String (WiFi.RSSI ());
118
- };
119
- get (" system" )[" wifi" ][" ssid" ] << []() {
120
- return WiFi.SSID ();
121
- };
122
- get (" system" )[" wifi" ][" ip" ] << []() {
123
- return WiFi.localIP ().toString ();
124
- };
125
-
126
- webPortal.setup (*this );
149
+ config ().set (" wifi.ssid" , ssid);
150
+ config ().set (" wifi.password" , password);
127
151
}
128
152
129
153
/* *
130
154
Setup connection to mqtt
131
155
*/
132
156
void mqtt (const char * host, int port, const char * username, const char * password) {
133
- mqttClient.setClient (wifiClient);
134
- mqttClient.setCallback ([&](const char * topic, uint8_t * payload, unsigned int length) {
135
- each ([=](MqttEntry* entry){
136
- entry->callback (topic, payload, length);
137
- });
138
- });
139
- mqttClient.setServer (host, port);
140
- mqtt_username = username;
141
- mqtt_password = password;
142
-
143
- debug (" Topic" , getTopic ());
157
+ config ().set (" mqtt.host" , host);
158
+ config ().set (" mqtt.port" , String (port));
159
+ config ().set (" mqtt.username" , username);
160
+ config ().set (" mqtt.password" , password);
144
161
}
145
162
146
163
/* *
147
164
Handle the normal loop
148
165
*/
149
166
void loop () {
150
- mqttReconnect ();
167
+ connect ();
151
168
mqttClient.loop ();
152
169
webPortal.loop ();
153
170
each ([](MqttEntry* entry){
0 commit comments