Skip to content

Commit 4b1f09b

Browse files
authored
Wifi managers improvements (#503)
* Add parameters to wifi manager: gateway name and base topic Enable to restart the ESP and erase the parameters by MQTT
1 parent a05fd01 commit 4b1f09b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+308
-196
lines changed

docs/.vuepress/config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ module.exports = {
6161
children: [
6262
'upload/binaries',
6363
'upload/pio',
64-
'upload/arduino-ide'
64+
'upload/arduino-ide',
65+
'upload/portal'
6566
]
6667
},
6768
{
@@ -75,7 +76,8 @@ module.exports = {
7576
'use/gsm',
7677
'use/rfm69',
7778
'use/sensors',
78-
'use/actuators'
79+
'use/actuators',
80+
'use/gateway'
7981
]
8082
},
8183
{

docs/upload/arduino-ide.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*Example for the use of RF gateway*
1313
```C++
1414
#define ZgatewayRF "RF" //ESP8266, Arduino, ESP32
15-
//#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge
15+
//#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge
1616
//#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32
1717
//#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32
1818
//#define ZgatewayBT "BT" //ESP8266, ESP32
@@ -24,3 +24,5 @@
2424
* Open the serial monitor and set 115200 bauds
2525
* Upload ➡️
2626
* You should see the logs into the serial monitor
27+
28+
With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md).

docs/upload/binaries.md

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,6 @@ https://github.com/marcelstoer/nodemcu-pyflasher/releases
3232
* Click on *FlashNodeMCU*
3333
The upload details appears.
3434

35-
Note that to reset the wifi and mqtt settings you can check *yes, wipes all data*
36-
37-
Once loaded into your board you have to set your network parameters with wifi manager portal
38-
From your smartphone search for your OpenMQTTGateway wifi network and connect to it, a web page will appear
39-
* Select your wifi
40-
* Set your wifi password
41-
* Set your MQTT Server IP
42-
* Set your MQTT Server username (not compulsory)
43-
* Set your MQTT Server password (not compulsory)
44-
45-
The ESP restart and connect to your network. Note that your credentials are saved into the ESP memory, if you want to redo the configuration you have to erase the ESP memory with the flash download tool.
35+
With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md).
4636

47-
_The default password for wifi manager is "your_password"_
48-
49-
Once done the gateway should connect to your network and your broker, you should see it into the broker in the form of the following messages:
50-
```
51-
home/OpenMQTTGateway/LWT Online
52-
home/OpenMQTTGateway/version
53-
```
37+
Note that to reset the wifi and mqtt settings you can check *yes, wipes all data*

docs/upload/pio.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,5 @@ If you want to use HASS MQTT discovery you need to have
111111
`#define ZmqttDiscovery "HADiscovery"`
112112
uncommented.
113113
Added to that auto discovery box should be selected into your Home Assistant MQTT integration configuration.
114+
115+
With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md).

docs/upload/portal.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Wifi and MQTT configuration portal
2+
3+
Once loaded into your board and if you don't use the manual configuration you have to set your network parameters with wifi manager portal.
4+
From your smartphone search for your OpenMQTTGateway wifi network and connect to it, a web page will appear
5+
* Select your wifi
6+
* Set your wifi password
7+
* Set your MQTT Server IP
8+
* Set your MQTT Server username (not compulsory)
9+
* Set your MQTT Server password (not compulsory)
10+
* Set your MQTT base topic (you must keep the / at the end)
11+
* Set your gateway name
12+
13+
The ESP restart and connect to your network. Note that your credentials are saved into the ESP memory, if you want to redo the configuration you have to erase the ESP memory with the flash download tool.
14+
15+
_The default password for wifi manager is "your_password"_
16+
17+
Once done the gateway should connect to your network and your broker, you should see it into the broker in the form of the following messages:
18+
```
19+
home/OpenMQTTGateway/LWT Online
20+
home/OpenMQTTGateway/version
21+
```
22+
23+
Note that the web portal appears only on first boot, if you want to configure again the setting you can do a long press on TRIGGER_PIN.
24+

docs/use/gateway.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# System commands (ESP only)
2+
3+
## Restart the ESP
4+
5+
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/set" -m '{"cmd":"restart"}'`
6+
7+
## Erase the ESP settings
8+
9+
`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/set" -m '{"cmd":"erase"}'`

main/User_config.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#ifndef Gateway_Name
5151
#define Gateway_Name "OpenMQTTGateway"
5252
#endif
53+
#define Base_Topic "home/"
5354

5455
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
5556
#if defined(ESP8266)||defined(ESP32) // for nodemcu, weemos and esp8266
@@ -71,11 +72,14 @@
7172
/*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/
7273
//MQTT Parameters definition
7374
//#define mqtt_server_name "www.mqtt_broker.com" // instead of defining the server by its IP you can define it by its name, uncomment this line and set the correct MQTT server host name
74-
char mqtt_user[20] = "your_username"; // not compulsory only if your broker needs authentication
75-
char mqtt_pass[30] = "your_password"; // not compulsory only if your broker needs authentication
76-
char mqtt_server[40] = "192.168.1.17";
75+
#define parameters_size 20
76+
#define mqtt_topic_max_size 100
77+
char mqtt_user[parameters_size] = "your_username"; // not compulsory only if your broker needs authentication
78+
char mqtt_pass[parameters_size] = "your_password"; // not compulsory only if your broker needs authentication
79+
char mqtt_server[parameters_size] = "192.168.1.17";
7780
char mqtt_port[6] = "1883";
78-
81+
char mqtt_topic[mqtt_topic_max_size] = Base_Topic;
82+
char gateway_name[parameters_size * 2] = Gateway_Name;
7983
//uncomment the line below to integrate msg value into the subject when receiving
8084
//#define valueAsASubject true
8185

@@ -119,9 +123,8 @@ const byte Dns[] = { 0, 0, 0, 0 }; //ip adress, if first value is different from
119123
const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
120124

121125
/*-------------DEFINE YOUR MQTT ADVANCED PARAMETERS BELOW----------------*/
122-
#define Base_Topic "home/"
123-
#define version_Topic Base_Topic Gateway_Name "/version"
124-
#define will_Topic Base_Topic Gateway_Name "/LWT"
126+
#define version_Topic "/version"
127+
#define will_Topic "/LWT"
125128
#define will_QoS 0
126129
#define will_Retain true
127130
#define will_Message "offline"
@@ -182,9 +185,16 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
182185
#endif
183186
/*--------------MQTT general topics-----------------*/
184187
// global MQTT subject listened by the gateway to execute commands (send RF, IR or others)
185-
#define subjectMQTTtoX Base_Topic Gateway_Name "/commands/#"
186-
#define subjectMultiGTWKey "toMQTT"
187-
#define subjectGTWSendKey "MQTTto"
188+
#define subjectMQTTtoX "/commands/#"
189+
#define subjectMultiGTWKey "toMQTT"
190+
#define subjectGTWSendKey "MQTTto"
191+
192+
// key used for launching commands to the gateway
193+
#define restartCmd "restart"
194+
#define eraseCmd "erase"
195+
196+
// define if we concatenate the values into the topic
197+
//#define valueAsASubject true
188198

189199
//variables to avoid duplicates
190200
#define time_avoid_duplicate 3000 // if you want to avoid duplicate mqtt message received set this to > 0, the value is the time in milliseconds during which we don't publish duplicates
@@ -197,9 +207,10 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
197207
#endif
198208

199209
#define TimeBetweenReadingSYS 120000 // time between system readings (like memory)
200-
#define subjectSYStoMQTT Base_Topic Gateway_Name "/SYStoMQTT"
210+
#define subjectSYStoMQTT "/SYStoMQTT"
211+
#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/set"
201212

202-
//#define subjectTRACEtoMQTT Base_Topic Gateway_Name "/log" //uncomment if you want to see traces on /log topic
213+
//#define subjectTRACEtoMQTT "OpenMQTTGateway/log" //uncomment if you want to see traces on OpenMQTTGateway/log topic
203214

204215
/*-------------------ACTIVATE TRACES----------------------*/
205216
#define TRACE 1 // commented = trace off, uncommented = trace on

main/ZactuatorFASTLED.ino

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,12 @@ void MQTTtoFASTLEDJSON(char *topicOri, JsonObject& jsonData)
127127
{
128128
trc(F("MQTTtoFASTLEDJSON: "));
129129
currentLEDState = GENERAL;
130-
String topic = topicOri;
131-
132-
trc(topic);
130+
trc(topicOri);
133131
//number = (long)strtol(&datacallback[1], NULL, 16);
134132

135133

136134

137-
if (topic == subjectMQTTtoFASTLEDsetled)
135+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetled)) != NULL)
138136
{
139137
trc(F("JSON parsed"));
140138
int ledNr = jsonData["led"];
@@ -160,10 +158,9 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback)
160158
{
161159
trc(F("MQTTtoFASTLED: "));
162160
currentLEDState = GENERAL;
163-
String topic = topicOri;
164161
long number = 0;
165-
trc(topic);
166-
if (topic == subjectMQTTtoFASTLED)
162+
trc(topicOri);
163+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLED)) != NULL)
167164
{
168165
number = (long)strtol(&datacallback[1], NULL, 16);
169166
trc(number);
@@ -173,18 +170,18 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback)
173170
}
174171
FastLED.show();
175172
}
176-
else if (topic == subjectMQTTtoFASTLEDsetbrightness)
173+
else if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetbrightness)) != NULL)
177174
{
178175
number = (long)strtol(&datacallback[1], NULL, 16);
179176
trc(number);
180177
FastLED.setBrightness(number);
181178
FastLED.show();
182179
}
183-
else if (topic == subjectMQTTtoFASTLEDsetanimation)
180+
else if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetanimation)) != NULL)
184181
{
185182
String payload = datacallback;
186183
trc(payload);
187-
if (payload.equals("fire"))
184+
if ( strstr(datacallback,"fire") != NULL)
188185
{
189186
currentLEDState = FIRE;
190187
gPal = HeatColors_p;

main/ZactuatorONOFF.ino

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#ifdef jsonReceiving
3434
void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){
3535

36-
if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){
36+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoONOFF)) != NULL){
3737
trc(F("MQTTtoONOFF json data analysis"));
3838
int boolSWITCHTYPE = ONOFFdata["state"] | 99;
3939
int pin = ONOFFdata["pin"] | ACTUATOR_ONOFF_PIN;
@@ -55,7 +55,7 @@ void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){
5555

5656
#ifdef simpleReceiving
5757
void MQTTtoONOFF(char * topicOri, char * datacallback) {
58-
if ((strstr(topicOri,subjectMQTTtoONOFF) != NULL) ){
58+
if ((strstr(topicOri,catToMainTopic(subjectMQTTtoONOFF)) != NULL) ){
5959

6060
trc(F("MQTTtoONOFF"));
6161
int pin = strtol(datacallback, NULL, 10); // we will not be able to pass values > 4294967295
@@ -69,7 +69,8 @@ void MQTTtoONOFF(char * topicOri, char * datacallback) {
6969

7070
digitalWrite(pin, ON);
7171
// we acknowledge the sending by publishing the value to an acknowledgement topic
72-
pub(subjectGTWONOFFtoMQTT, ON);
72+
char b = ON;
73+
pub(subjectGTWONOFFtoMQTT, &b);
7374
}
7475
}
7576
#endif

main/Zgateway2G.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ bool _2GtoMQTT(){
107107
String data = datacallback;
108108
String topic = topicOri;
109109

110-
if (topic == subjectMQTTto2G) {
110+
if (strstr(topicOri,catToMainTopic(subjectMQTTto2G)) != NULL) {
111111
trc(F("MQTTto2G data analysis"));
112112
// 2G DATA ANALYSIS
113113
String phone_number = "";
@@ -122,7 +122,7 @@ bool _2GtoMQTT(){
122122
if (A6l.sendSMS(phone_number,data) == A6_OK ) {
123123
trc(F("SMS OK"));
124124
// Acknowledgement to the GTW2G topic
125-
pub(subjectGTW2GtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
125+
pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
126126
}else{
127127
trc(F("SMS KO"));
128128
// Acknowledgement to the GTW2G topic
@@ -138,7 +138,7 @@ bool _2GtoMQTT(){
138138
#ifdef jsonReceiving
139139
void MQTTto2G(char * topicOri, JsonObject& SMSdata) {
140140

141-
if (strcmp(topicOri,subjectMQTTto2G) == 0){
141+
if (strstr(topicOri,catToMainTopic(subjectMQTTto2G)) != NULL){
142142
const char * sms = SMSdata["message"];
143143
const char * phone = SMSdata["phone"];
144144
trc(F("MQTTto2G json data analysis"));
@@ -148,7 +148,7 @@ bool _2GtoMQTT(){
148148
if (A6l.sendSMS(String(phone),String(sms)) == A6_OK ) {
149149
trc(F("SMS OK"));
150150
// Acknowledgement to the GTW2G topic
151-
pub(subjectGTW2GtoMQTT, SMSdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
151+
pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also
152152
}else{
153153
trc(F("SMS KO"));
154154
pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also

main/ZgatewayBT.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,12 @@ void MiBandDiscovery(char * mac){
530530
while(true){
531531
trc(taskMessage);
532532
delay(BLEinterval);
533-
BLEscan();
533+
if (client.state() == 0) {
534+
BLEscan();
535+
}else{
536+
trc("MQTT client disconnected no BLE scan");
537+
delay(1000);
538+
}
534539
}
535540
}
536541

@@ -993,12 +998,13 @@ void haRoomPresence(JsonObject& HomePresence){
993998
HomePresence["distance"] = distance;
994999
trc(F("BLE DISTANCE :"));
9951000
trc(distance);
996-
pub(subjectHomePresence,HomePresence);
1001+
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
1002+
pub_custom_topic((char *)topic.c_str(),HomePresence);
9971003
}
9981004
#endif
9991005

10001006
void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding
1001-
if (strcmp(topicOri,subjectMQTTtoBTset) == 0){
1007+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoBTset)) != NULL){
10021008
trc(F("MQTTtoBT json set"));
10031009

10041010
// Black list & white list set

main/ZgatewayIR.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void IRtoMQTT(){
152152
storeValue(MQTTvalue);
153153
if (repeatIRwMQTT){
154154
trc(F("Pub. IR for rpt"));
155-
pub(subjectMQTTtoIR,MQTTvalue);
155+
pubMQTT(subjectMQTTtoIR,MQTTvalue);
156156
}
157157
}
158158
}
@@ -274,7 +274,7 @@ void IRtoMQTT(){
274274
#ifdef jsonReceiving
275275
void MQTTtoIR(char * topicOri, JsonObject& IRdata) {
276276

277-
if (strcmp(topicOri,subjectMQTTtoIR) == 0){
277+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoIR)) != NULL){
278278
trc(F("MQTTtoIR json"));
279279
uint64_t data = IRdata["value"];
280280
const char * raw = IRdata["raw"];

main/ZgatewayLORA.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void LORAtoMQTT(){
8787

8888
#ifdef jsonReceiving
8989
void MQTTtoLORA(char * topicOri, JsonObject& LORAdata) { // json object decoding
90-
if (strcmp(topicOri,subjectMQTTtoLORA) == 0){
90+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoLORA)) != NULL){
9191
trc(F("MQTTtoLORA json"));
9292
const char * message = LORAdata["message"];
9393
int txPower = LORAdata["txpower"]|LORA_TX_POWER;
@@ -120,7 +120,7 @@ void LORAtoMQTT(){
120120
#endif
121121
#ifdef simpleReceiving
122122
void MQTTtoLORA(char * topicOri, char * LORAdata) { // json object decoding
123-
if (strcmp(topicOri,subjectMQTTtoLORA) == 0){
123+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoLORA)) != NULL){
124124
LoRa.beginPacket();
125125
LoRa.print(LORAdata);
126126
LoRa.endPacket();

main/ZgatewayPilight.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) {
7474

7575
int result = 0;
7676

77-
if (strcmp(topicOri,subjectMQTTtoPilight) == 0){
77+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoPilight)) != NULL){
7878
trc(F("MQTTtoPilight json data analysis"));
7979
const char * message = Pilightdata["message"];
8080
const char * protocol = Pilightdata["protocol"];

main/ZgatewayRF.ino

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
132132
trc(valueBITS);
133133
}
134134

135-
if ((topic == subjectMQTTtoRF) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){
135+
if ((strstr(topicOri,catToMainTopic(subjectMQTTtoRF)) != NULL) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){
136136
trc(F("MQTTtoRF dflt"));
137137
mySwitch.setProtocol(1,350);
138138
mySwitch.send(data, 24);
@@ -156,8 +156,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) {
156156

157157
#ifdef jsonReceiving
158158
void MQTTtoRF(char * topicOri, JsonObject& RFdata) { // json object decoding
159-
160-
if (strcmp(topicOri,subjectMQTTtoRF) == 0){
159+
if (strstr(topicOri,catToMainTopic(subjectMQTTtoRF)) != NULL){
161160
trc(F("MQTTtoRF json"));
162161
unsigned long data = RFdata["value"];
163162
if (data != 0) {

0 commit comments

Comments
 (0)