Compare commits
	
		
			16 Commits
		
	
	
		
			v1.21
			...
			69cb07f721
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 69cb07f721 | |||
| 5fafe9a538 | |||
| cf32071ee0 | |||
| e4572cc31a | |||
| 57d0bc6481 | |||
| 1e745724e4 | |||
| 25ce7e419a | |||
| 15472ab36b | |||
| 7a74c4f2df | |||
| 24ba0a09f4 | |||
| eebaca38b7 | |||
| 8e9082f05e | |||
| 1c306e031b | |||
| 74c6279f50 | |||
| 8c3b51304e | |||
| 7b60f9efb3 | 
							
								
								
									
										53
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								README.md
									
									
									
									
									
								
							@@ -1,46 +1,65 @@
 | 
				
			|||||||
# ESP-NOW gateway for ESP8266/ESP32
 | 
					# ESP-NOW gateway for ESP8266/ESP32
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi.
 | 
					Gateway for data exchange between ESP-NOW devices and MQTT broker via WiFi/LAN.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Features
 | 
					## Features
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. Creates an access point named "ESP-NOW Gateway XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1).
 | 
					1. Creates an access point named "ESP-NOW gateway XXXXXXXXXXXX" with password "12345678" (IP 192.168.4.1).
 | 
				
			||||||
2. Possibility a device search through the Windows Network Environment via SSDP.
 | 
					2. Possibility a device search through the Windows Network Environment via SSDP (at ESP_NOW_WIFI mode).
 | 
				
			||||||
3. Periodically transmission of system information to the MQTT broker (every 60 seconds) and availability status to the ESP-NOW network and to the MQTT broker (every 10 seconds).
 | 
					3. Periodically transmission of system information to the MQTT broker (every 60 seconds), availability status to the ESP-NOW network and to the MQTT broker (every 10 seconds) and current date and time to the ESP-NOW network (every 10 seconds).
 | 
				
			||||||
4. Automatically adds gateway configuration to Home Assistan via MQTT discovery as a binary_sensor.
 | 
					4. Automatically adds gateway configuration to Home Assistan via MQTT discovery as a binary_sensor.
 | 
				
			||||||
5. Automatically adds supported ESP-NOW devices configurations to Home Assistan via MQTT discovery.
 | 
					5. Automatically adds supported ESP-NOW devices configurations to Home Assistan via MQTT discovery.
 | 
				
			||||||
6. Possibility firmware update over OTA.
 | 
					6. Automatically adds supported nRF24 devices configurations to Home Assistan via MQTT discovery.
 | 
				
			||||||
7. Web interface for settings.
 | 
					7. Possibility firmware update over OTA (at ESP_NOW_LAN mode via access point only).
 | 
				
			||||||
  
 | 
					8. Web interface for settings (at ESP_NOW_LAN mode via access point only).
 | 
				
			||||||
 | 
					9. 3 operating modes:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					ESP_NOW       ESP-NOW node only. Default mode after flashing.
 | 
				
			||||||
 | 
					ESP_NOW_WIFI  Gateway between ESP-NOW devices and MQTT broker via WiFi.
 | 
				
			||||||
 | 
					ESP_NOW_LAN   Gateway between ESP-NOW devices and MQTT broker via Ethernet. Preferred mode.
 | 
				
			||||||
 | 
					 ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Notes
 | 
					## Notes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. ESP-NOW mesh network based on the library [ZHNetwork](https://github.com/aZholtikov/ZHNetwork).
 | 
					1. ESP-NOW mesh network based on the library [ZHNetwork](https://github.com/aZholtikov/ZHNetwork).
 | 
				
			||||||
2. Regardless of the status of connections to WiFi or MQTT the device perform ESP-NOW node function.
 | 
					2. Regardless of the status of connections to WiFi or MQTT the device perform ESP-NOW node function.
 | 
				
			||||||
3. For restart the device (without using the Web interface and only if MQTT connection established) send an "restart" command to the device's root topic (example - "homeassistant/espnow_gateway/70039F44BEF7").
 | 
					3. For restart the device (without using the Web interface and only if MQTT connection established) send an "restart" command to the device's root topic (example - "homeassistant/espnow_gateway/70039F44BEF7").
 | 
				
			||||||
 | 
					4. W5500 connection:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```text
 | 
				
			||||||
 | 
					ESP8266 (GPIO05 - CS, GPIO14 - SCK, GPIO12 - MISO, GPIO13 - MOSI).
 | 
				
			||||||
 | 
					ESP32   (GPIO05 - CS, GPIO18 - SCK, GPIO19 - MISO, GPIO23 - MOSI).
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Attention
 | 
					## Attention
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
 | 
					1. ESP-NOW network name must be set same of all another ESP-NOW devices in network.
 | 
				
			||||||
2. Upload the "data" folder (with web interface) into the filesystem before flashing.
 | 
					2. If encryption is used, the key must be set same of all another ESP-NOW devices in network.
 | 
				
			||||||
3. WiFi router must be set on channel 1.
 | 
					3. Upload the "data" folder (with web interface) into the filesystem before flashing.
 | 
				
			||||||
 | 
					4. At ESP_NOW_WIFI mode WiFi router must be set on channel 1.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Tested on
 | 
					## Tested on
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. NodeMCU 1.0 (ESP-12E Module). ESP-NOW + WiFi mode. Unstable work.
 | 
					1. NodeMCU 1.0 (ESP-12E Module). ESP_NOW_WIFI mode. Unstable work.
 | 
				
			||||||
2. AZ-Delivery ESP-32 Dev Kit C V4. ESP-NOW + WiFi mode. Stable work.
 | 
					2. AZ-Delivery ESP-32 Dev Kit C V4. ESP_NOW_WIFI mode. Stable work.
 | 
				
			||||||
 | 
					3. NodeMCU 1.0 (ESP-12E Module) + W5500. ESP_NOW_LAN mode. Stable work.
 | 
				
			||||||
 | 
					4. AZ-Delivery ESP-32 Dev Kit C V4 + W5500. ESP_NOW_LAN mode. Stable work.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Supported devices
 | 
					## Supported devices
 | 
				
			||||||
 | 
					
 | 
				
			||||||
1. [RF Gateway](https://github.com/aZholtikov/RF-Gateway) (coming soon)
 | 
					1. [RF Gateway](https://github.com/aZholtikov/RF-Gateway)
 | 
				
			||||||
2. [ESP-NOW Switch](https://github.com/aZholtikov/ESP-NOW-Switch)
 | 
					2. [ESP-NOW Switch](https://github.com/aZholtikov/ESP-NOW-Switch)
 | 
				
			||||||
3. [ESP-NOW Light/Led Strip](https://github.com/aZholtikov/ESP-NOW-Light-Led-Strip)
 | 
					3. [ESP-NOW Light/Led Strip](https://github.com/aZholtikov/ESP-NOW-Light-Led-Strip)
 | 
				
			||||||
4. [ESP-NOW Window/Door Sensor](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor)
 | 
					4. [ESP-NOW Window/Door Sensor](https://github.com/aZholtikov/ESP-NOW-Window-Door-Sensor)
 | 
				
			||||||
5. [ESP-NOW Water Leakage Sensor](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor) (coming soon)
 | 
					5. [ESP-NOW Water Leakage Sensor](https://github.com/aZholtikov/ESP-NOW-Water-Leakage-Sensor)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## To Do
 | 
					## To Do
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- [X] Automatically add ESP-NOW devices configurations to Home Assistan via MQTT discovery.
 | 
					- [X] Automatically add ESP-NOW devices configurations to Home Assistan via MQTT discovery.
 | 
				
			||||||
- [ ] LAN connection support.
 | 
					- [X] LAN connection support.
 | 
				
			||||||
- [ ] nRF24 device support (in current time uses "RF Gateway").
 | 
					- [X] nRF24 device support (via [RF Gateway](https://github.com/aZholtikov/RF-Gateway)).
 | 
				
			||||||
- [ ] BLE device support (for ESP32).
 | 
					- [ ] BLE device support (via BLE Gateway).
 | 
				
			||||||
- [ ] LoRa device support.
 | 
					- [ ] LoRa device support (via LoRa Gateway).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Any feedback via [e-mail](mailto:github@zh.com.ru) would be appreciated. Or... [Buy me a coffee](https://paypal.me/aZholtikov).
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,7 @@ function loadBlock() {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    document.getElementsByTagName('body')[0].innerHTML = newString;
 | 
					    document.getElementsByTagName('body')[0].innerHTML = newString;
 | 
				
			||||||
    setFirmvareValue('version', 'firmware');
 | 
					    setFirmvareValue('version', 'firmware');
 | 
				
			||||||
 | 
					    setGpioValue('workModeSelect', 'workMode');
 | 
				
			||||||
    handleServerResponse();
 | 
					    handleServerResponse();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,6 +38,12 @@ function getValue(id) {
 | 
				
			|||||||
    return value;
 | 
					    return value;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function getSelectValue(id) {
 | 
				
			||||||
 | 
					    var select = document.getElementById(id);
 | 
				
			||||||
 | 
					    var value = select.value;
 | 
				
			||||||
 | 
					    return value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function sendRequest(submit, server) {
 | 
					function sendRequest(submit, server) {
 | 
				
			||||||
    request = new XMLHttpRequest();
 | 
					    request = new XMLHttpRequest();
 | 
				
			||||||
    request.open("GET", server, true);
 | 
					    request.open("GET", server, true);
 | 
				
			||||||
@@ -45,11 +52,14 @@ function sendRequest(submit, server) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function saveSetting(submit) {
 | 
					function saveSetting(submit) {
 | 
				
			||||||
    server = "/setting?ssid=" + getValue('ssid') + "&password=" + encodeURIComponent(getValue('password'))
 | 
					    server = "/setting?ssid=" + getValue('ssid') + "&password=" + encodeURIComponent(getValue('password'))
 | 
				
			||||||
        + "&host=" + getValue('mqttHostName') + "&port=" + getValue('mqttHostPort')
 | 
					        + "&mqttHostName=" + getValue('mqttHostName') + "&mqttHostPort=" + getValue('mqttHostPort')
 | 
				
			||||||
        + "&login=" + getValue('mqttUserLogin') + "&pass=" + encodeURIComponent(getValue('mqttUserPassword'))
 | 
					        + "&mqttUserLogin=" + getValue('mqttUserLogin') + "&mqttUserPassword=" + encodeURIComponent(getValue('mqttUserPassword'))
 | 
				
			||||||
        + "&prefix=" + getValue('topicPrefix')
 | 
					        + "&topicPrefix=" + getValue('topicPrefix')
 | 
				
			||||||
        + "&name=" + getValue('deviceName')
 | 
					        + "&deviceName=" + getValue('deviceName')
 | 
				
			||||||
        + "&net=" + getValue('espnowNetName');
 | 
					        + "&espnowNetName=" + getValue('espnowNetName')
 | 
				
			||||||
 | 
					        + "&workMode=" + getSelectValue('workModeSelect')
 | 
				
			||||||
 | 
					        + "&ntpHostName=" + getValue('ntpHostName')
 | 
				
			||||||
 | 
					        + "&gmtOffset=" + getValue('gmtOffset');
 | 
				
			||||||
    sendRequest(submit, server);
 | 
					    sendRequest(submit, server);
 | 
				
			||||||
    alert("Please restart device for changes apply.");
 | 
					    alert("Please restart device for changes apply.");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -62,3 +72,8 @@ function restart(submit) {
 | 
				
			|||||||
function setFirmvareValue(id, value) {
 | 
					function setFirmvareValue(id, value) {
 | 
				
			||||||
    document.getElementById(id).innerHTML = document.getElementById(value).value;
 | 
					    document.getElementById(id).innerHTML = document.getElementById(value).value;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function setGpioValue(id, value) {
 | 
				
			||||||
 | 
					    var select = document.getElementById(id);
 | 
				
			||||||
 | 
					    select.value = document.getElementById(value).value;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -29,22 +29,39 @@
 | 
				
			|||||||
                title="ESP-NOW network name (1 to 20 characters)" />
 | 
					                title="ESP-NOW network name (1 to 20 characters)" />
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <div class="wrapper">
 | 
				
			||||||
 | 
					            <p class="text-select">Work mode:</p>
 | 
				
			||||||
 | 
					            <input id="workMode" value="{{workMode}}" hidden />
 | 
				
			||||||
 | 
					            <p><select id="workModeSelect">
 | 
				
			||||||
 | 
					                    <option value="0">ESP-NOW</option>
 | 
				
			||||||
 | 
					                    <option value="1">ESP-NOW WIFI</option>
 | 
				
			||||||
 | 
					                    <option value="2">ESP-NOW LAN</option>
 | 
				
			||||||
 | 
					                </select></p>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <p class="text">WiFi settings</p>
 | 
					        <p class="text">WiFi settings</p>
 | 
				
			||||||
        <div class="wrapper">
 | 
					        <div class="wrapper">
 | 
				
			||||||
            <input id="ssid" value="{{ssid}}" placeholder="SSID" label title="WiFi network name" />
 | 
					            <input class="text-inp" id="ssid" value="{{ssid}}" placeholder="SSID" label title="WiFi network name" />
 | 
				
			||||||
            <input id="password" value="{{password}}" onfocus="this.type='text'" type="password" placeholder="Password"
 | 
					            <input id="password" value="{{password}}" onfocus="this.type='text'" type="password" placeholder="Password"
 | 
				
			||||||
                autocomplete="off" label title="WiFi password" />
 | 
					                autocomplete="off" label title="WiFi password" />
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <p class="text">NTP settings</p>
 | 
				
			||||||
 | 
					        <div class="wrapper">
 | 
				
			||||||
 | 
					            <input class="text-inp" id="ntpHostName" value="{{ntpHostName}}" placeholder="URL or IP" label
 | 
				
			||||||
 | 
					                title="NTP server URL or IP" />
 | 
				
			||||||
 | 
					            <input id="gmtOffset" value="{{gmtOffset}}" placeholder="Time zone" label title="Time zone" />
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <p class="text">MQTT settings</p>
 | 
					        <p class="text">MQTT settings</p>
 | 
				
			||||||
        <div class="wrapper">
 | 
					        <div class="wrapper">
 | 
				
			||||||
            <input id="mqttHostName" value="{{mqttHostName}}" placeholder="URL or IP" label
 | 
					            <input class="text-inp" id="mqttHostName" value="{{mqttHostName}}" placeholder="URL or IP" label
 | 
				
			||||||
                title="MQTT server URL or IP" />
 | 
					                title="MQTT server URL or IP" />
 | 
				
			||||||
            <input id="mqttHostPort" value="{{mqttHostPort}}" placeholder="Port" label title="MQTT server port" />
 | 
					            <input id="mqttHostPort" value="{{mqttHostPort}}" placeholder="Port" label title="MQTT server port" />
 | 
				
			||||||
        </div>
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        <div class="wrapper">
 | 
					        <div class="wrapper">
 | 
				
			||||||
            <input id="mqttUserLogin" value="{{mqttUserLogin}}" placeholder="Login" label
 | 
					            <input class="text-inp" id="mqttUserLogin" value="{{mqttUserLogin}}" placeholder="Login" label
 | 
				
			||||||
                title="MQTT server user login" />
 | 
					                title="MQTT server user login" />
 | 
				
			||||||
            <input id="mqttUserPassword" value="{{mqttUserPassword}}" onfocus="this.type='text'" type="password"
 | 
					            <input id="mqttUserPassword" value="{{mqttUserPassword}}" onfocus="this.type='text'" type="password"
 | 
				
			||||||
                placeholder="Password" autocomplete="off" label title="MQTT server user password" />
 | 
					                placeholder="Password" autocomplete="off" label title="MQTT server user password" />
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,7 @@
 | 
				
			|||||||
 | 
					p{
 | 
				
			||||||
 | 
					    margin: 0 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
body {
 | 
					body {
 | 
				
			||||||
    font-family: "Gill Sans", sans-serif;
 | 
					    font-family: "Gill Sans", sans-serif;
 | 
				
			||||||
    background: rgb(255, 255, 255);
 | 
					    background: rgb(255, 255, 255);
 | 
				
			||||||
@@ -21,6 +25,28 @@ h1 {
 | 
				
			|||||||
    font-weight: 600;
 | 
					    font-weight: 600;
 | 
				
			||||||
    flex-shrink: 0;
 | 
					    flex-shrink: 0;
 | 
				
			||||||
    margin-right: 10px;
 | 
					    margin-right: 10px;
 | 
				
			||||||
 | 
					    margin-left: 10px;
 | 
				
			||||||
 | 
					    margin: 10px 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.text-select {
 | 
				
			||||||
 | 
					    width: 35%;
 | 
				
			||||||
 | 
					    font-weight: 600;
 | 
				
			||||||
 | 
					    flex-shrink: 0;
 | 
				
			||||||
 | 
					    margin-right: 10px;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.text-inp {
 | 
				
			||||||
 | 
					    width: 48%;
 | 
				
			||||||
 | 
					    min-height: 30px;
 | 
				
			||||||
 | 
					    border-radius: 5px;
 | 
				
			||||||
 | 
					    border: none;
 | 
				
			||||||
 | 
					    margin-bottom: 10px;
 | 
				
			||||||
 | 
					    padding: 0 10px;
 | 
				
			||||||
 | 
					    color: rgb(0, 0, 0);
 | 
				
			||||||
 | 
					    background: #a3e0f1;
 | 
				
			||||||
 | 
					    transition: .5s;
 | 
				
			||||||
 | 
					    margin-left: 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.wrapper {
 | 
					.wrapper {
 | 
				
			||||||
@@ -39,6 +65,19 @@ input {
 | 
				
			|||||||
    color: rgb(0, 0, 0);
 | 
					    color: rgb(0, 0, 0);
 | 
				
			||||||
    background: #a3e0f1;
 | 
					    background: #a3e0f1;
 | 
				
			||||||
    transition: .5s;
 | 
					    transition: .5s;
 | 
				
			||||||
 | 
					    margin-left: 10px;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					select {
 | 
				
			||||||
 | 
					    width: 140px;
 | 
				
			||||||
 | 
					    min-height: 30px;
 | 
				
			||||||
 | 
					    border-radius: 5px;
 | 
				
			||||||
 | 
					    border: none;
 | 
				
			||||||
 | 
					    margin-bottom: 10px;
 | 
				
			||||||
 | 
					    padding: 0 10px;
 | 
				
			||||||
 | 
					    color: rgb(0, 0, 0);
 | 
				
			||||||
 | 
					    background: #a3e0f1;
 | 
				
			||||||
 | 
					    transition: .5s;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
input:hover {
 | 
					input:hover {
 | 
				
			||||||
@@ -46,11 +85,18 @@ input:hover {
 | 
				
			|||||||
    cursor: pointer;
 | 
					    cursor: pointer;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					select:hover {
 | 
				
			||||||
 | 
					    background: white;
 | 
				
			||||||
 | 
					    cursor: pointer;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.btn {
 | 
					.btn {
 | 
				
			||||||
    width: 48%;
 | 
					    width: 48%;
 | 
				
			||||||
    background: rgb(65, 125, 238);
 | 
					    background: rgb(65, 125, 238);
 | 
				
			||||||
    color: white;
 | 
					    color: white;
 | 
				
			||||||
    transition: .5s;
 | 
					    transition: .5s;
 | 
				
			||||||
 | 
					    margin-left: 0;
 | 
				
			||||||
 | 
					    margin-top: 8px;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.btn:hover {
 | 
					.btn:hover {
 | 
				
			||||||
@@ -65,6 +111,10 @@ input:hover {
 | 
				
			|||||||
    width: 100%;
 | 
					    width: 100%;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#espnowNetName {
 | 
				
			||||||
 | 
					    margin-bottom: 15px;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.wrapper.wrapper--end {
 | 
					.wrapper.wrapper--end {
 | 
				
			||||||
    align-items: baseline;
 | 
					    align-items: baseline;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -1,49 +1,61 @@
 | 
				
			|||||||
[env:esp8266]
 | 
					[env:ESP8266]
 | 
				
			||||||
platform = espressif8266
 | 
					platform = espressif8266
 | 
				
			||||||
board = nodemcuv2
 | 
					board = esp12e
 | 
				
			||||||
framework = arduino
 | 
					framework = arduino
 | 
				
			||||||
 | 
					board_build.filesystem = littlefs
 | 
				
			||||||
lib_deps = 
 | 
					lib_deps = 
 | 
				
			||||||
	https://github.com/aZholtikov/ZHNetwork
 | 
						https://github.com/aZholtikov/ZHNetwork
 | 
				
			||||||
	https://github.com/aZholtikov/ZHConfig
 | 
						https://github.com/aZholtikov/ZHConfig
 | 
				
			||||||
	bblanchon/ArduinoJson@^6.19.4
 | 
						https://github.com/aZholtikov/Async-Web-Server
 | 
				
			||||||
	me-no-dev/ESP Async WebServer@^1.2.3
 | 
						https://github.com/bblanchon/ArduinoJson
 | 
				
			||||||
	marvinroger/AsyncMqttClient@^0.9.0
 | 
						https://github.com/arduino-libraries/Ethernet
 | 
				
			||||||
 | 
						https://github.com/knolleary/pubsubclient
 | 
				
			||||||
 | 
						https://github.com/arduino-libraries/NTPClient
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[env:esp8266-ota]
 | 
					[env:ESP8266-OTA]
 | 
				
			||||||
platform = espressif8266
 | 
					platform = espressif8266
 | 
				
			||||||
board = nodemcuv2
 | 
					board = esp12e
 | 
				
			||||||
framework = arduino
 | 
					framework = arduino
 | 
				
			||||||
upload_port = 192.168.1.113
 | 
					board_build.filesystem = littlefs
 | 
				
			||||||
 | 
					upload_port = 192.168.4.1
 | 
				
			||||||
upload_protocol = espota
 | 
					upload_protocol = espota
 | 
				
			||||||
lib_deps = 
 | 
					lib_deps = 
 | 
				
			||||||
	https://github.com/aZholtikov/ZHNetwork
 | 
						https://github.com/aZholtikov/ZHNetwork
 | 
				
			||||||
	https://github.com/aZholtikov/ZHConfig
 | 
						https://github.com/aZholtikov/ZHConfig
 | 
				
			||||||
	bblanchon/ArduinoJson@^6.19.4
 | 
						https://github.com/aZholtikov/Async-Web-Server
 | 
				
			||||||
	me-no-dev/ESP Async WebServer@^1.2.3
 | 
						https://github.com/bblanchon/ArduinoJson
 | 
				
			||||||
	marvinroger/AsyncMqttClient@^0.9.0
 | 
						https://github.com/arduino-libraries/Ethernet
 | 
				
			||||||
 | 
						https://github.com/knolleary/pubsubclient
 | 
				
			||||||
 | 
						https://github.com/arduino-libraries/NTPClient
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[env:esp32]
 | 
					[env:ESP32]
 | 
				
			||||||
platform = espressif32
 | 
					platform = espressif32
 | 
				
			||||||
board = az-delivery-devkit-v4
 | 
					board = esp32dev
 | 
				
			||||||
framework = arduino
 | 
					framework = arduino
 | 
				
			||||||
 | 
					board_build.filesystem = littlefs
 | 
				
			||||||
lib_deps = 
 | 
					lib_deps = 
 | 
				
			||||||
	https://github.com/aZholtikov/ZHNetwork
 | 
						https://github.com/aZholtikov/ZHNetwork
 | 
				
			||||||
	https://github.com/aZholtikov/ZHConfig
 | 
						https://github.com/aZholtikov/ZHConfig
 | 
				
			||||||
	bblanchon/ArduinoJson@^6.19.4
 | 
						https://github.com/aZholtikov/Async-Web-Server
 | 
				
			||||||
	me-no-dev/ESP Async WebServer@^1.2.3
 | 
						https://github.com/bblanchon/ArduinoJson
 | 
				
			||||||
	marvinroger/AsyncMqttClient@^0.9.0
 | 
						https://github.com/arduino-libraries/Ethernet
 | 
				
			||||||
	luc-github/ESP32SSDP@^1.2.0
 | 
						https://github.com/knolleary/pubsubclient
 | 
				
			||||||
 | 
						https://github.com/arduino-libraries/NTPClient
 | 
				
			||||||
 | 
						https://github.com/luc-github/ESP32SSDP
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[env:esp32-ota]
 | 
					[env:ESP32-OTA]
 | 
				
			||||||
platform = espressif32
 | 
					platform = espressif32
 | 
				
			||||||
board = az-delivery-devkit-v4
 | 
					board = esp32dev
 | 
				
			||||||
framework = arduino
 | 
					framework = arduino
 | 
				
			||||||
upload_port = 192.168.1.143
 | 
					board_build.filesystem = littlefs
 | 
				
			||||||
 | 
					upload_port = 192.168.4.1
 | 
				
			||||||
upload_protocol = espota
 | 
					upload_protocol = espota
 | 
				
			||||||
lib_deps = 
 | 
					lib_deps = 
 | 
				
			||||||
	https://github.com/aZholtikov/ZHNetwork
 | 
						https://github.com/aZholtikov/ZHNetwork
 | 
				
			||||||
	https://github.com/aZholtikov/ZHConfig
 | 
						https://github.com/aZholtikov/ZHConfig
 | 
				
			||||||
	bblanchon/ArduinoJson@^6.19.4
 | 
						https://github.com/aZholtikov/Async-Web-Server
 | 
				
			||||||
	me-no-dev/ESP Async WebServer@^1.2.3
 | 
						https://github.com/bblanchon/ArduinoJson
 | 
				
			||||||
	marvinroger/AsyncMqttClient@^0.9.0
 | 
						https://github.com/arduino-libraries/Ethernet
 | 
				
			||||||
	luc-github/ESP32SSDP@^1.2.0
 | 
						https://github.com/knolleary/pubsubclient
 | 
				
			||||||
 | 
						https://github.com/arduino-libraries/NTPClient
 | 
				
			||||||
 | 
						https://github.com/luc-github/ESP32SSDP
 | 
				
			||||||
							
								
								
									
										483
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										483
									
								
								src/main.cpp
									
									
									
									
									
								
							@@ -1,28 +1,27 @@
 | 
				
			|||||||
#include "ArduinoJson.h"
 | 
					#include "ArduinoJson.h"
 | 
				
			||||||
#include "ArduinoOTA.h"
 | 
					#include "ArduinoOTA.h"
 | 
				
			||||||
#include "ESPAsyncWebServer.h"
 | 
					#include "ESPAsyncWebServer.h" // https://github.com/aZholtikov/Async-Web-Server
 | 
				
			||||||
#include "AsyncMQTTClient.h"
 | 
					#include "Ethernet.h"          // https://github.com/arduino-libraries/Ethernet
 | 
				
			||||||
 | 
					#include "PubSubClient.h"
 | 
				
			||||||
 | 
					#include "LittleFS.h"
 | 
				
			||||||
#include "Ticker.h"
 | 
					#include "Ticker.h"
 | 
				
			||||||
 | 
					#include "NTPClient.h"
 | 
				
			||||||
#include "ZHNetwork.h"
 | 
					#include "ZHNetwork.h"
 | 
				
			||||||
#include "ZHConfig.h"
 | 
					#include "ZHConfig.h"
 | 
				
			||||||
#if defined(ESP8266)
 | 
					#if defined(ESP8266)
 | 
				
			||||||
#include "ESP8266SSDP.h"
 | 
					#include "ESP8266SSDP.h"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#if defined(ESP32)
 | 
					#if defined(ESP32)
 | 
				
			||||||
#include "SPIFFS.h"
 | 
					 | 
				
			||||||
#include "ESP32SSDP.h"
 | 
					#include "ESP32SSDP.h"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void onWifiEvent(WiFiEvent_t event);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void onEspnowMessage(const char *data, const uint8_t *sender);
 | 
					void onEspnowMessage(const char *data, const uint8_t *sender);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void onMqttConnect(bool sessionPresent);
 | 
					void onMqttMessage(char *topic, byte *payload, unsigned int length);
 | 
				
			||||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
 | 
					 | 
				
			||||||
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void sendKeepAliveMessage(void);
 | 
					void sendKeepAliveMessage(void);
 | 
				
			||||||
void sendAttributesMessage(void);
 | 
					void sendAttributesMessage(void);
 | 
				
			||||||
 | 
					void sendConfigMessage(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String getValue(String data, char separator, uint8_t index);
 | 
					String getValue(String data, char separator, uint8_t index);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -32,13 +31,29 @@ void saveConfig(void);
 | 
				
			|||||||
String xmlNode(String tags, String data);
 | 
					String xmlNode(String tags, String data);
 | 
				
			||||||
void setupWebServer(void);
 | 
					void setupWebServer(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void connectToMqtt(void);
 | 
					void checkMqttAvailability(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const String firmware{"1.21"};
 | 
					void mqttPublish(const char *topic, const char *payload, bool retained);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum : uint8_t
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    ESP_NOW,
 | 
				
			||||||
 | 
					    ESP_NOW_WIFI,
 | 
				
			||||||
 | 
					    ESP_NOW_LAN
 | 
				
			||||||
 | 
					} work_mode_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const String firmware{"1.42"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String espnowNetName{"DEFAULT"};
 | 
					String espnowNetName{"DEFAULT"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String deviceName{"ESP-NOW gateway"};
 | 
					uint8_t workMode{ESP_NOW};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#if defined(ESP8266)
 | 
				
			||||||
 | 
					String deviceName = "ESP-NOW gateway " + String(ESP.getChipId(), HEX);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if defined(ESP32)
 | 
				
			||||||
 | 
					String deviceName = "ESP-NOW gateway " + String(ESP.getEfuseMac(), HEX);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String ssid{"SSID"};
 | 
					String ssid{"SSID"};
 | 
				
			||||||
String password{"PASSWORD"};
 | 
					String password{"PASSWORD"};
 | 
				
			||||||
@@ -48,114 +63,162 @@ uint16_t mqttHostPort{1883};
 | 
				
			|||||||
String mqttUserLogin{""};
 | 
					String mqttUserLogin{""};
 | 
				
			||||||
String mqttUserPassword{""};
 | 
					String mqttUserPassword{""};
 | 
				
			||||||
String topicPrefix{"homeassistant"};
 | 
					String topicPrefix{"homeassistant"};
 | 
				
			||||||
 | 
					const char *mqttUserID{"ESP32"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					String ntpHostName{"NTP"};
 | 
				
			||||||
 | 
					uint16_t gmtOffset{10800};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint8_t w5500Mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; // Change it if necessary.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ZHNetwork myNet;
 | 
					ZHNetwork myNet;
 | 
				
			||||||
AsyncWebServer webServer(80);
 | 
					AsyncWebServer webServer(80);
 | 
				
			||||||
AsyncMqttClient mqttClient;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Ticker mqttReconnectTimer;
 | 
					EthernetClient ethClient;
 | 
				
			||||||
bool mqttReconnectTimerSemaphore{false};
 | 
					WiFiClient wifiClient;
 | 
				
			||||||
void mqttReconnectTimerCallback(void);
 | 
					
 | 
				
			||||||
 | 
					PubSubClient mqttEthClient(ethClient);
 | 
				
			||||||
 | 
					PubSubClient mqttWifiClient(wifiClient);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WiFiUDP udpWiFiClient;
 | 
				
			||||||
 | 
					EthernetUDP udpEthClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					NTPClient ntpWiFiClient(udpWiFiClient, ntpHostName.c_str(), gmtOffset);
 | 
				
			||||||
 | 
					NTPClient ntpEthClient(udpEthClient, ntpHostName.c_str(), gmtOffset);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Ticker mqttAvailabilityCheckTimer;
 | 
				
			||||||
 | 
					bool mqttAvailabilityCheckTimerSemaphore{true};
 | 
				
			||||||
 | 
					bool isMqttAvailable{false};
 | 
				
			||||||
 | 
					void mqttAvailabilityCheckTimerCallback(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Ticker keepAliveMessageTimer;
 | 
					Ticker keepAliveMessageTimer;
 | 
				
			||||||
bool keepAliveMessageTimerSemaphore{true};
 | 
					bool keepAliveMessageTimerSemaphore{true};
 | 
				
			||||||
void keepAliveMessageTimerCallback(void);
 | 
					void keepAliveMessageTimerCallback(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Ticker attributesMessageTimer;
 | 
					Ticker attributesMessageTimer;
 | 
				
			||||||
bool attributesMessageTimerSemaphore{false};
 | 
					bool attributesMessageTimerSemaphore{true};
 | 
				
			||||||
void attributesMessageTimerCallback(void);
 | 
					void attributesMessageTimerCallback(void);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void setup()
 | 
					void setup()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    SPIFFS.begin();
 | 
					#if defined(ESP8266)
 | 
				
			||||||
 | 
					    LittleFS.begin();
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if defined(ESP32)
 | 
				
			||||||
 | 
					    LittleFS.begin(true);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    loadConfig();
 | 
					    loadConfig();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    WiFi.onEvent(onWifiEvent);
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        Ethernet.init(5);
 | 
				
			||||||
 | 
					        Ethernet.begin(w5500Mac);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
#if defined(ESP8266)
 | 
					#if defined(ESP8266)
 | 
				
			||||||
    WiFi.setSleepMode(WIFI_NONE_SLEEP);
 | 
					        WiFi.setSleepMode(WIFI_NONE_SLEEP);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
#if defined(ESP32)
 | 
					#if defined(ESP32)
 | 
				
			||||||
    WiFi.setSleep(WIFI_PS_NONE);
 | 
					        WiFi.setSleep(WIFI_PS_NONE);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					        WiFi.persistent(false);
 | 
				
			||||||
    WiFi.persistent(false);
 | 
					        WiFi.setAutoConnect(false);
 | 
				
			||||||
    WiFi.setAutoConnect(false);
 | 
					        WiFi.setAutoReconnect(true);
 | 
				
			||||||
    WiFi.setAutoReconnect(true);
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    myNet.begin(espnowNetName.c_str(), true);
 | 
					    myNet.begin(espnowNetName.c_str(), true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    myNet.setOnBroadcastReceivingCallback(onEspnowMessage);
 | 
					    if (workMode)
 | 
				
			||||||
    myNet.setOnUnicastReceivingCallback(onEspnowMessage);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    WiFi.softAP(("ESP-NOW Gateway " + myNet.getNodeMac()).c_str(), "12345678");
 | 
					 | 
				
			||||||
    uint8_t scan = WiFi.scanNetworks(false, false, 1);
 | 
					 | 
				
			||||||
    String name;
 | 
					 | 
				
			||||||
    int32_t rssi;
 | 
					 | 
				
			||||||
    uint8_t encryption;
 | 
					 | 
				
			||||||
    uint8_t *bssid;
 | 
					 | 
				
			||||||
    int32_t channel;
 | 
					 | 
				
			||||||
    bool hidden;
 | 
					 | 
				
			||||||
    for (int8_t i = 0; i < scan; i++)
 | 
					 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
#if defined(ESP8266)
 | 
					        // myNet.setCryptKey("VERY_LONG_CRYPT_KEY"); // If encryption is used, the key must be set same of all another ESP-NOW devices in network.
 | 
				
			||||||
        WiFi.getNetworkInfo(i, name, encryption, rssi, bssid, channel, hidden);
 | 
					        myNet.setOnBroadcastReceivingCallback(onEspnowMessage);
 | 
				
			||||||
#endif
 | 
					        myNet.setOnUnicastReceivingCallback(onEspnowMessage);
 | 
				
			||||||
#if defined(ESP32)
 | 
					 | 
				
			||||||
        WiFi.getNetworkInfo(i, name, encryption, rssi, bssid, channel);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
        if (name == ssid)
 | 
					 | 
				
			||||||
            WiFi.begin(ssid.c_str(), password.c_str());
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    mqttClient.onConnect(onMqttConnect);
 | 
					#if defined(ESP8266)
 | 
				
			||||||
    mqttClient.onDisconnect(onMqttDisconnect);
 | 
					    WiFi.softAP(("ESP-NOW gateway " + String(ESP.getChipId(), HEX)).c_str(), "12345678");
 | 
				
			||||||
    mqttClient.onMessage(onMqttMessage);
 | 
					#endif
 | 
				
			||||||
    mqttClient.setServer(mqttHostName.c_str(), mqttHostPort);
 | 
					#if defined(ESP32)
 | 
				
			||||||
    mqttClient.setCredentials(mqttUserLogin.c_str(), mqttUserPassword.c_str());
 | 
					    WiFi.softAP(("ESP-NOW gateway " + String(ESP.getEfuseMac(), HEX)).c_str(), "12345678");
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        uint8_t scan = WiFi.scanNetworks(false, false);
 | 
				
			||||||
 | 
					        String name;
 | 
				
			||||||
 | 
					        int32_t rssi;
 | 
				
			||||||
 | 
					        uint8_t encryption;
 | 
				
			||||||
 | 
					        uint8_t *bssid;
 | 
				
			||||||
 | 
					        int32_t channel;
 | 
				
			||||||
 | 
					        bool hidden;
 | 
				
			||||||
 | 
					        for (int8_t i = 0; i < scan; i++)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					#if defined(ESP8266)
 | 
				
			||||||
 | 
					            WiFi.getNetworkInfo(i, name, encryption, rssi, bssid, channel, hidden);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					#if defined(ESP32)
 | 
				
			||||||
 | 
					            WiFi.getNetworkInfo(i, name, encryption, rssi, bssid, channel);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					            if (name == ssid)
 | 
				
			||||||
 | 
					                WiFi.begin(ssid.c_str(), password.c_str());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ntpWiFiClient.begin();
 | 
				
			||||||
 | 
					        mqttWifiClient.setBufferSize(2048);
 | 
				
			||||||
 | 
					        mqttWifiClient.setServer(mqttHostName.c_str(), mqttHostPort);
 | 
				
			||||||
 | 
					        mqttWifiClient.setCallback(onMqttMessage);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ntpEthClient.begin();
 | 
				
			||||||
 | 
					        mqttEthClient.setBufferSize(2048);
 | 
				
			||||||
 | 
					        mqttEthClient.setServer(mqttHostName.c_str(), mqttHostPort);
 | 
				
			||||||
 | 
					        mqttEthClient.setCallback(onMqttMessage);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    setupWebServer();
 | 
					    setupWebServer();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ArduinoOTA.begin();
 | 
					    ArduinoOTA.begin();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    keepAliveMessageTimer.attach(10, keepAliveMessageTimerCallback);
 | 
					    keepAliveMessageTimer.attach(10, keepAliveMessageTimerCallback);
 | 
				
			||||||
 | 
					    mqttAvailabilityCheckTimer.attach(5, mqttAvailabilityCheckTimerCallback);
 | 
				
			||||||
 | 
					    attributesMessageTimer.attach(60, attributesMessageTimerCallback);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void loop()
 | 
					void loop()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (mqttReconnectTimerSemaphore)
 | 
					 | 
				
			||||||
        connectToMqtt();
 | 
					 | 
				
			||||||
    if (keepAliveMessageTimerSemaphore)
 | 
					    if (keepAliveMessageTimerSemaphore)
 | 
				
			||||||
        sendKeepAliveMessage();
 | 
					        sendKeepAliveMessage();
 | 
				
			||||||
    if (attributesMessageTimerSemaphore)
 | 
					    if (attributesMessageTimerSemaphore)
 | 
				
			||||||
        sendAttributesMessage();
 | 
					        sendAttributesMessage();
 | 
				
			||||||
 | 
					    if (mqttAvailabilityCheckTimerSemaphore)
 | 
				
			||||||
 | 
					        checkMqttAvailability();
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					        mqttWifiClient.loop();
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					        mqttEthClient.loop();
 | 
				
			||||||
    myNet.maintenance();
 | 
					    myNet.maintenance();
 | 
				
			||||||
    ArduinoOTA.handle();
 | 
					    ArduinoOTA.handle();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void onWifiEvent(WiFiEvent_t event)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
#if defined(ESP8266)
 | 
					 | 
				
			||||||
    if (event == WIFI_EVENT_STAMODE_GOT_IP)
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
#if defined(ESP32)
 | 
					 | 
				
			||||||
        if (event == ARDUINO_EVENT_WIFI_STA_GOT_IP)
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
            mqttClient.connect();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
					void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (!mqttClient.connected())
 | 
					    if (!isMqttAvailable)
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    esp_now_payload_data_t incomingData;
 | 
					    esp_now_payload_data_t incomingData;
 | 
				
			||||||
    memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
 | 
					    memcpy(&incomingData, data, sizeof(esp_now_payload_data_t));
 | 
				
			||||||
    if (incomingData.payloadsType == ENPT_ATTRIBUTES)
 | 
					    if (incomingData.payloadsType == ENPT_ATTRIBUTES)
 | 
				
			||||||
        mqttClient.publish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), 2, true, incomingData.message);
 | 
					        mqttPublish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), incomingData.message, true);
 | 
				
			||||||
    if (incomingData.payloadsType == ENPT_KEEP_ALIVE)
 | 
					    if (incomingData.payloadsType == ENPT_KEEP_ALIVE)
 | 
				
			||||||
        mqttClient.publish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), 2, true, "online");
 | 
					        mqttPublish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), "online", true);
 | 
				
			||||||
    if (incomingData.payloadsType == ENPT_STATE)
 | 
					    if (incomingData.payloadsType == ENPT_STATE)
 | 
				
			||||||
        mqttClient.publish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), 2, true, incomingData.message);
 | 
					        mqttPublish((topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/" + getValueName(incomingData.payloadsType)).c_str(), incomingData.message, true);
 | 
				
			||||||
    if (incomingData.payloadsType == ENPT_CONFIG)
 | 
					    if (incomingData.payloadsType == ENPT_CONFIG)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        if (incomingData.deviceType == ENDT_SWITCH)
 | 
					        if (incomingData.deviceType == ENDT_SWITCH)
 | 
				
			||||||
@@ -165,25 +228,23 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			|||||||
            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
            deserializeJson(json, configData.message);
 | 
					            deserializeJson(json, configData.message);
 | 
				
			||||||
            uint8_t unit = json["unit"].as<uint8_t>();
 | 
					            uint8_t unit = json["unit"].as<uint8_t>();
 | 
				
			||||||
            ha_component_type_t type = json["type"].as<ha_component_type_t>();
 | 
					 | 
				
			||||||
            StaticJsonDocument<2048> jsonConfig;
 | 
					            StaticJsonDocument<2048> jsonConfig;
 | 
				
			||||||
            jsonConfig["platform"] = "mqtt";
 | 
					            jsonConfig["platform"] = "mqtt";
 | 
				
			||||||
            jsonConfig["name"] = json["name"];
 | 
					            jsonConfig["name"] = json["name"];
 | 
				
			||||||
            jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
 | 
					            jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
 | 
				
			||||||
            jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>());
 | 
					            jsonConfig["device_class"] = getValueName(json["class"].as<ha_switch_device_class_t>());
 | 
				
			||||||
            jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
 | 
					            jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
 | 
				
			||||||
            jsonConfig["value_template"] = "{{ value_json.state }}";
 | 
					            jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
 | 
				
			||||||
            jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
 | 
					            jsonConfig["command_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/set";
 | 
				
			||||||
            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
					            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
				
			||||||
            jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
 | 
					            jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
 | 
				
			||||||
            jsonConfig["payload_on"] = json["reverse"] == "true" ? "OFF" : "ON";
 | 
					            jsonConfig["payload_on"] = json["payload_on"];
 | 
				
			||||||
            jsonConfig["payload_off"] = json["reverse"] == "true" ? "ON" : "OFF";
 | 
					            jsonConfig["payload_off"] = json["payload_off"];
 | 
				
			||||||
            jsonConfig["optimistic"] = "false";
 | 
					            jsonConfig["optimistic"] = "false";
 | 
				
			||||||
            jsonConfig["qos"] = 2;
 | 
					 | 
				
			||||||
            jsonConfig["retain"] = "true";
 | 
					            jsonConfig["retain"] = "true";
 | 
				
			||||||
            char buffer[2048]{0};
 | 
					            char buffer[2048]{0};
 | 
				
			||||||
            serializeJsonPretty(jsonConfig, buffer);
 | 
					            serializeJsonPretty(jsonConfig, buffer);
 | 
				
			||||||
            mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
 | 
					            mqttPublish((topicPrefix + "/" + getValueName(json["type"].as<ha_component_type_t>()) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), buffer, true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (incomingData.deviceType == ENDT_LED)
 | 
					        if (incomingData.deviceType == ENDT_LED)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -192,7 +253,6 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			|||||||
            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
            deserializeJson(json, configData.message);
 | 
					            deserializeJson(json, configData.message);
 | 
				
			||||||
            uint8_t unit = json["unit"].as<uint8_t>();
 | 
					            uint8_t unit = json["unit"].as<uint8_t>();
 | 
				
			||||||
            ha_component_type_t type = json["type"].as<ha_component_type_t>();
 | 
					 | 
				
			||||||
            esp_now_led_type_t ledClass = json["class"];
 | 
					            esp_now_led_type_t ledClass = json["class"];
 | 
				
			||||||
            StaticJsonDocument<2048> jsonConfig;
 | 
					            StaticJsonDocument<2048> jsonConfig;
 | 
				
			||||||
            jsonConfig["platform"] = "mqtt";
 | 
					            jsonConfig["platform"] = "mqtt";
 | 
				
			||||||
@@ -218,14 +278,13 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
					            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
				
			||||||
            jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
 | 
					            jsonConfig["availability_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
 | 
				
			||||||
            jsonConfig["payload_on"] = "ON";
 | 
					            jsonConfig["payload_on"] = json["payload_on"];
 | 
				
			||||||
            jsonConfig["payload_off"] = "OFF";
 | 
					            jsonConfig["payload_off"] = json["payload_off"];
 | 
				
			||||||
            jsonConfig["optimistic"] = "false";
 | 
					            jsonConfig["optimistic"] = "false";
 | 
				
			||||||
            jsonConfig["qos"] = 2;
 | 
					 | 
				
			||||||
            jsonConfig["retain"] = "true";
 | 
					            jsonConfig["retain"] = "true";
 | 
				
			||||||
            char buffer[2048]{0};
 | 
					            char buffer[2048]{0};
 | 
				
			||||||
            serializeJsonPretty(jsonConfig, buffer);
 | 
					            serializeJsonPretty(jsonConfig, buffer);
 | 
				
			||||||
            mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
 | 
					            mqttPublish((topicPrefix + "/" + getValueName(json["type"].as<ha_component_type_t>()) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), buffer, true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (incomingData.deviceType == ENDT_SENSOR)
 | 
					        if (incomingData.deviceType == ENDT_SENSOR)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
@@ -240,23 +299,85 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			|||||||
            jsonConfig["name"] = json["name"];
 | 
					            jsonConfig["name"] = json["name"];
 | 
				
			||||||
            jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
 | 
					            jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
 | 
				
			||||||
            jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
 | 
					            jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/state";
 | 
				
			||||||
            jsonConfig["value_template"] = "{{ value_json.state }}";
 | 
					            jsonConfig["value_template"] = "{{ value_json." + json["template"].as<String>() + " }}";
 | 
				
			||||||
            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
					            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
				
			||||||
            jsonConfig["force_update"] = "true";
 | 
					            jsonConfig["force_update"] = "true";
 | 
				
			||||||
            jsonConfig["qos"] = 2;
 | 
					 | 
				
			||||||
            jsonConfig["retain"] = "true";
 | 
					            jsonConfig["retain"] = "true";
 | 
				
			||||||
 | 
					            if (type == HACT_SENSOR)
 | 
				
			||||||
 | 
					                jsonConfig["device_class"] = getValueName(json["class"].as<ha_sensor_device_class_t>());
 | 
				
			||||||
            if (type == HACT_BINARY_SENSOR)
 | 
					            if (type == HACT_BINARY_SENSOR)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                ha_binary_sensor_device_class_t deviceClass = json["class"].as<ha_binary_sensor_device_class_t>();
 | 
					                ha_binary_sensor_device_class_t deviceClass = json["class"].as<ha_binary_sensor_device_class_t>();
 | 
				
			||||||
 | 
					                if (deviceClass == HABSDC_BATTERY || deviceClass == HABSDC_WINDOW || deviceClass == HABSDC_DOOR)
 | 
				
			||||||
 | 
					                    jsonConfig["payload_off"] = json["payload_off"];
 | 
				
			||||||
 | 
					                if (deviceClass == HABSDC_CONNECTIVITY)
 | 
				
			||||||
 | 
					                    jsonConfig["expire_after"] = json["expire_after"];
 | 
				
			||||||
                jsonConfig["device_class"] = getValueName(deviceClass);
 | 
					                jsonConfig["device_class"] = getValueName(deviceClass);
 | 
				
			||||||
                jsonConfig["payload_on"] = json["payload_on"];
 | 
					                jsonConfig["payload_on"] = json["payload_on"];
 | 
				
			||||||
                jsonConfig["payload_off"] = json["payload_off"];
 | 
					 | 
				
			||||||
                if (deviceClass == HABSDC_BATTERY)
 | 
					 | 
				
			||||||
                    jsonConfig["value_template"] = "{{ value_json.battery }}";
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            char buffer[2048]{0};
 | 
					            char buffer[2048]{0};
 | 
				
			||||||
            serializeJsonPretty(jsonConfig, buffer);
 | 
					            serializeJsonPretty(jsonConfig, buffer);
 | 
				
			||||||
            mqttClient.publish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), 2, true, buffer);
 | 
					            mqttPublish((topicPrefix + "/" + getValueName(type) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), buffer, true);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (incomingData.deviceType == ENDT_RF_SENSOR)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            esp_now_payload_data_t configData;
 | 
				
			||||||
 | 
					            memcpy(&configData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message));
 | 
				
			||||||
 | 
					            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
 | 
					            deserializeJson(json, configData.message);
 | 
				
			||||||
 | 
					            uint8_t unit = json["u"].as<uint8_t>();
 | 
				
			||||||
 | 
					            ha_component_type_t type = json["t"].as<ha_component_type_t>();
 | 
				
			||||||
 | 
					            rf_sensor_type_t rf = json["r"].as<rf_sensor_type_t>();
 | 
				
			||||||
 | 
					            uint16_t id = json["i"].as<uint16_t>();
 | 
				
			||||||
 | 
					            String tmp = json["v"].as<String>();
 | 
				
			||||||
 | 
					            StaticJsonDocument<2048> jsonConfig;
 | 
				
			||||||
 | 
					            jsonConfig["platform"] = "mqtt";
 | 
				
			||||||
 | 
					            jsonConfig["name"] = getValueName(rf) + " " + id + " " + tmp;
 | 
				
			||||||
 | 
					            jsonConfig["unique_id"] = String(id) + "-" + unit;
 | 
				
			||||||
 | 
					            jsonConfig["state_topic"] = topicPrefix + "/rf_sensor/" + getValueName(rf) + "/" + id + "/state";
 | 
				
			||||||
 | 
					            jsonConfig["value_template"] = "{{ value_json." + tmp + " }}";
 | 
				
			||||||
 | 
					            if (type == HACT_SENSOR)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                jsonConfig["device_class"] = getValueName(json["h"].as<ha_sensor_device_class_t>());
 | 
				
			||||||
 | 
					                jsonConfig["unit_of_measurement"] = json["m"];
 | 
				
			||||||
 | 
					                jsonConfig["expire_after"] = json["e"];
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (type == HACT_BINARY_SENSOR)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                ha_binary_sensor_device_class_t deviceClass = json["h"].as<ha_binary_sensor_device_class_t>();
 | 
				
			||||||
 | 
					                if (deviceClass == HABSDC_MOISTURE)
 | 
				
			||||||
 | 
					                    jsonConfig["expire_after"] = json["e"];
 | 
				
			||||||
 | 
					                jsonConfig["device_class"] = getValueName(deviceClass);
 | 
				
			||||||
 | 
					                jsonConfig["payload_on"] = json["o"];
 | 
				
			||||||
 | 
					                jsonConfig["payload_off"] = json["f"];
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            jsonConfig["force_update"] = "true";
 | 
				
			||||||
 | 
					            jsonConfig["retain"] = "true";
 | 
				
			||||||
 | 
					            char buffer[2048]{0};
 | 
				
			||||||
 | 
					            serializeJsonPretty(jsonConfig, buffer);
 | 
				
			||||||
 | 
					            mqttPublish((topicPrefix + "/" + getValueName(type) + "/" + id + "-" + unit + "/config").c_str(), buffer, true);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (incomingData.deviceType == ENDT_RF_GATEWAY)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            esp_now_payload_data_t configData;
 | 
				
			||||||
 | 
					            memcpy(&configData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message));
 | 
				
			||||||
 | 
					            StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
 | 
					            deserializeJson(json, configData.message);
 | 
				
			||||||
 | 
					            uint8_t unit = json["unit"].as<uint8_t>();
 | 
				
			||||||
 | 
					            StaticJsonDocument<2048> jsonConfig;
 | 
				
			||||||
 | 
					            jsonConfig["platform"] = "mqtt";
 | 
				
			||||||
 | 
					            jsonConfig["name"] = json["name"];
 | 
				
			||||||
 | 
					            jsonConfig["unique_id"] = myNet.macToString(sender) + "-" + unit;
 | 
				
			||||||
 | 
					            jsonConfig["device_class"] = getValueName(json["class"].as<ha_binary_sensor_device_class_t>());
 | 
				
			||||||
 | 
					            jsonConfig["state_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/status";
 | 
				
			||||||
 | 
					            jsonConfig["json_attributes_topic"] = topicPrefix + "/" + getValueName(incomingData.deviceType) + "/" + myNet.macToString(sender) + "/attributes";
 | 
				
			||||||
 | 
					            jsonConfig["payload_on"] = json["payload_on"];
 | 
				
			||||||
 | 
					            jsonConfig["expire_after"] = json["expire_after"];
 | 
				
			||||||
 | 
					            jsonConfig["force_update"] = "true";
 | 
				
			||||||
 | 
					            jsonConfig["retain"] = "true";
 | 
				
			||||||
 | 
					            char buffer[2048]{0};
 | 
				
			||||||
 | 
					            serializeJsonPretty(jsonConfig, buffer);
 | 
				
			||||||
 | 
					            mqttPublish((topicPrefix + "/" + getValueName(json["type"].as<ha_component_type_t>()) + "/" + myNet.macToString(sender) + "-" + unit + "/config").c_str(), buffer, true);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (incomingData.payloadsType == ENPT_FORWARD)
 | 
					    if (incomingData.payloadsType == ENPT_FORWARD)
 | 
				
			||||||
@@ -265,50 +386,17 @@ void onEspnowMessage(const char *data, const uint8_t *sender)
 | 
				
			|||||||
        memcpy(&forwardData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message));
 | 
					        memcpy(&forwardData.message, &incomingData.message, sizeof(esp_now_payload_data_t::message));
 | 
				
			||||||
        StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					        StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
        deserializeJson(json, forwardData.message);
 | 
					        deserializeJson(json, forwardData.message);
 | 
				
			||||||
        mqttClient.publish((topicPrefix + "/rf_sensor/" + getValueName(json["type"].as<rf_sensor_type_t>()) + "/" + json["id"].as<uint16_t>()).c_str(), 2, false, incomingData.message);
 | 
					        if (incomingData.deviceType == ENDT_RF_GATEWAY)
 | 
				
			||||||
 | 
					            mqttPublish((topicPrefix + "/rf_sensor/" + getValueName(json["type"].as<rf_sensor_type_t>()) + "/" + json["id"].as<uint16_t>() + "/state").c_str(), incomingData.message, false);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void onMqttConnect(bool sessionPresent)
 | 
					void onMqttMessage(char *topic, byte *payload, unsigned int length)
 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    mqttClient.subscribe((topicPrefix + "/espnow_gateway/#").c_str(), 2);
 | 
					 | 
				
			||||||
    mqttClient.subscribe((topicPrefix + "/espnow_switch/#").c_str(), 2);
 | 
					 | 
				
			||||||
    mqttClient.subscribe((topicPrefix + "/espnow_led/#").c_str(), 2);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    StaticJsonDocument<1024> json;
 | 
					 | 
				
			||||||
    json["platform"] = "mqtt";
 | 
					 | 
				
			||||||
    json["name"] = deviceName;
 | 
					 | 
				
			||||||
    json["unique_id"] = myNet.getNodeMac() + "-1";
 | 
					 | 
				
			||||||
    json["device_class"] = "connectivity";
 | 
					 | 
				
			||||||
    json["state_topic"] = topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/status";
 | 
					 | 
				
			||||||
    json["json_attributes_topic"] = topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/attributes";
 | 
					 | 
				
			||||||
    json["payload_on"] = "online";
 | 
					 | 
				
			||||||
    json["expire_after"] = 30;
 | 
					 | 
				
			||||||
    json["force_update"] = "true";
 | 
					 | 
				
			||||||
    json["qos"] = 2;
 | 
					 | 
				
			||||||
    json["retain"] = "true";
 | 
					 | 
				
			||||||
    char buffer[1024]{0};
 | 
					 | 
				
			||||||
    serializeJsonPretty(json, buffer);
 | 
					 | 
				
			||||||
    mqttClient.publish((topicPrefix + "/binary_sensor/" + myNet.getNodeMac() + "-1" + "/config").c_str(), 2, true, buffer);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    sendKeepAliveMessage();
 | 
					 | 
				
			||||||
    sendAttributesMessage();
 | 
					 | 
				
			||||||
    attributesMessageTimer.attach(60, attributesMessageTimerCallback);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
    mqttReconnectTimer.once(5, mqttReconnectTimerCallback);
 | 
					 | 
				
			||||||
    sendKeepAliveMessage();
 | 
					 | 
				
			||||||
    attributesMessageTimer.detach();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total)
 | 
					 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    String mac = getValue(String(topic).substring(0, String(topic).length()), '/', 2);
 | 
					    String mac = getValue(String(topic).substring(0, String(topic).length()), '/', 2);
 | 
				
			||||||
    String message;
 | 
					    String message;
 | 
				
			||||||
    bool flag{false};
 | 
					    bool flag{false};
 | 
				
			||||||
    for (uint16_t i = 0; i < len; ++i)
 | 
					    for (uint16_t i = 0; i < length; ++i)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        message += (char)payload[i];
 | 
					        message += (char)payload[i];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -317,8 +405,6 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
 | 
				
			|||||||
    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
    if (message == "update" || message == "restart")
 | 
					    if (message == "update" || message == "restart")
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        mqttClient.publish(topic, 2, true, "");
 | 
					 | 
				
			||||||
        mqttClient.publish((String(topic) + "/status").c_str(), 2, true, "offline");
 | 
					 | 
				
			||||||
        if (mac == myNet.getNodeMac() && message == "restart")
 | 
					        if (mac == myNet.getNodeMac() && message == "restart")
 | 
				
			||||||
            ESP.restart();
 | 
					            ESP.restart();
 | 
				
			||||||
        flag = true;
 | 
					        flag = true;
 | 
				
			||||||
@@ -326,7 +412,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
 | 
				
			|||||||
    if (String(topic) == topicPrefix + "/espnow_switch/" + mac + "/set" || String(topic) == topicPrefix + "/espnow_led/" + mac + "/set")
 | 
					    if (String(topic) == topicPrefix + "/espnow_switch/" + mac + "/set" || String(topic) == topicPrefix + "/espnow_led/" + mac + "/set")
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        flag = true;
 | 
					        flag = true;
 | 
				
			||||||
        json["set"] = message == "ON" ? "ON" : "OFF";
 | 
					        json["set"] = message;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    if (String(topic) == topicPrefix + "/espnow_led/" + mac + "/brightness")
 | 
					    if (String(topic) == topicPrefix + "/espnow_led/" + mac + "/brightness")
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -349,9 +435,7 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
 | 
				
			|||||||
            outgoingData.payloadsType = ENPT_RESTART;
 | 
					            outgoingData.payloadsType = ENPT_RESTART;
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
            outgoingData.payloadsType = message == "update" ? ENPT_UPDATE : ENPT_SET;
 | 
					            outgoingData.payloadsType = message == "update" ? ENPT_UPDATE : ENPT_SET;
 | 
				
			||||||
        char buffer[sizeof(esp_now_payload_data_t::message)]{0};
 | 
					        serializeJsonPretty(json, outgoingData.message);
 | 
				
			||||||
        serializeJsonPretty(json, buffer);
 | 
					 | 
				
			||||||
        memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message));
 | 
					 | 
				
			||||||
        char temp[sizeof(esp_now_payload_data_t)]{0};
 | 
					        char temp[sizeof(esp_now_payload_data_t)]{0};
 | 
				
			||||||
        memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
 | 
					        memcpy(&temp, &outgoingData, sizeof(esp_now_payload_data_t));
 | 
				
			||||||
        uint8_t target[6];
 | 
					        uint8_t target[6];
 | 
				
			||||||
@@ -363,14 +447,30 @@ void onMqttMessage(char *topic, char *payload, AsyncMqttClientMessageProperties
 | 
				
			|||||||
void sendKeepAliveMessage()
 | 
					void sendKeepAliveMessage()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    keepAliveMessageTimerSemaphore = false;
 | 
					    keepAliveMessageTimerSemaphore = false;
 | 
				
			||||||
    if (mqttClient.connected())
 | 
					    if (isMqttAvailable)
 | 
				
			||||||
        mqttClient.publish((topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/status").c_str(), 2, true, "online");
 | 
					        mqttPublish((topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/status").c_str(), "online", true);
 | 
				
			||||||
    esp_now_payload_data_t outgoingData;
 | 
					    esp_now_payload_data_t outgoingData;
 | 
				
			||||||
    outgoingData.deviceType = ENDT_GATEWAY;
 | 
					    outgoingData.deviceType = ENDT_GATEWAY;
 | 
				
			||||||
    outgoingData.payloadsType = ENPT_KEEP_ALIVE;
 | 
					    outgoingData.payloadsType = ENPT_KEEP_ALIVE;
 | 
				
			||||||
    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
    json["MQTT"] = mqttClient.connected() ? "online" : "offline";
 | 
					    json["MQTT"] = isMqttAvailable ? "online" : "offline";
 | 
				
			||||||
    json["frequency"] = 10; // For compatibility with the previous version. Will be removed in future releases.
 | 
					    json["frequency"] = 10; // For compatibility with the previous version. Will be removed in future releases.
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ntpWiFiClient.update();
 | 
				
			||||||
 | 
					        uint64_t epochTime = ntpWiFiClient.getEpochTime();
 | 
				
			||||||
 | 
					        struct tm *time = gmtime((time_t *)&epochTime);
 | 
				
			||||||
 | 
					        json["time"] = ntpWiFiClient.getFormattedTime();
 | 
				
			||||||
 | 
					        json["date"] = String(time->tm_mday) + "." + String(time->tm_mon + 1) + "." + String(time->tm_year + 1900);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        ntpEthClient.update();
 | 
				
			||||||
 | 
					        uint64_t epochTime = ntpEthClient.getEpochTime();
 | 
				
			||||||
 | 
					        struct tm *time = gmtime((time_t *)&epochTime);
 | 
				
			||||||
 | 
					        json["time"] = ntpEthClient.getFormattedTime();
 | 
				
			||||||
 | 
					        json["date"] = String(time->tm_mday) + "." + String(time->tm_mon + 1) + "." + String(time->tm_year + 1900);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    char buffer[sizeof(esp_now_payload_data_t::message)]{0};
 | 
					    char buffer[sizeof(esp_now_payload_data_t::message)]{0};
 | 
				
			||||||
    serializeJsonPretty(json, buffer);
 | 
					    serializeJsonPretty(json, buffer);
 | 
				
			||||||
    memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message));
 | 
					    memcpy(&outgoingData.message, &buffer, sizeof(esp_now_payload_data_t::message));
 | 
				
			||||||
@@ -381,13 +481,15 @@ void sendKeepAliveMessage()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void sendAttributesMessage()
 | 
					void sendAttributesMessage()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					    if (!isMqttAvailable)
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    attributesMessageTimerSemaphore = false;
 | 
					    attributesMessageTimerSemaphore = false;
 | 
				
			||||||
    uint32_t secs = millis() / 1000;
 | 
					    uint32_t secs = millis() / 1000;
 | 
				
			||||||
    uint32_t mins = secs / 60;
 | 
					    uint32_t mins = secs / 60;
 | 
				
			||||||
    uint32_t hours = mins / 60;
 | 
					    uint32_t hours = mins / 60;
 | 
				
			||||||
    uint32_t days = hours / 24;
 | 
					    uint32_t days = hours / 24;
 | 
				
			||||||
    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
					    StaticJsonDocument<sizeof(esp_now_payload_data_t::message)> json;
 | 
				
			||||||
    json["Type"] = "ESP-NOW Gateway";
 | 
					    json["Type"] = "ESP-NOW gateway";
 | 
				
			||||||
#if defined(ESP8266)
 | 
					#if defined(ESP8266)
 | 
				
			||||||
    json["MCU"] = "ESP8266";
 | 
					    json["MCU"] = "ESP8266";
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
@@ -397,11 +499,32 @@ void sendAttributesMessage()
 | 
				
			|||||||
    json["MAC"] = myNet.getNodeMac();
 | 
					    json["MAC"] = myNet.getNodeMac();
 | 
				
			||||||
    json["Firmware"] = firmware;
 | 
					    json["Firmware"] = firmware;
 | 
				
			||||||
    json["Library"] = myNet.getFirmwareVersion();
 | 
					    json["Library"] = myNet.getFirmwareVersion();
 | 
				
			||||||
    json["IP"] = WiFi.localIP().toString();
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					        json["IP"] = WiFi.localIP().toString();
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					        json["IP"] = Ethernet.localIP().toString();
 | 
				
			||||||
    json["Uptime"] = "Days:" + String(days) + " Hours:" + String(hours - (days * 24)) + " Mins:" + String(mins - (hours * 60));
 | 
					    json["Uptime"] = "Days:" + String(days) + " Hours:" + String(hours - (days * 24)) + " Mins:" + String(mins - (hours * 60));
 | 
				
			||||||
    char buffer[sizeof(esp_now_payload_data_t::message)]{0};
 | 
					    char buffer[sizeof(esp_now_payload_data_t::message)]{0};
 | 
				
			||||||
    serializeJsonPretty(json, buffer);
 | 
					    serializeJsonPretty(json, buffer);
 | 
				
			||||||
    mqttClient.publish((topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/attributes").c_str(), 2, true, buffer);
 | 
					    mqttPublish((topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/attributes").c_str(), buffer, true);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void sendConfigMessage()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    StaticJsonDocument<1024> json;
 | 
				
			||||||
 | 
					    json["platform"] = "mqtt";
 | 
				
			||||||
 | 
					    json["name"] = deviceName;
 | 
				
			||||||
 | 
					    json["unique_id"] = myNet.getNodeMac() + "-1";
 | 
				
			||||||
 | 
					    json["device_class"] = "connectivity";
 | 
				
			||||||
 | 
					    json["state_topic"] = topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/status";
 | 
				
			||||||
 | 
					    json["json_attributes_topic"] = topicPrefix + "/espnow_gateway/" + myNet.getNodeMac() + "/attributes";
 | 
				
			||||||
 | 
					    json["payload_on"] = "online";
 | 
				
			||||||
 | 
					    json["expire_after"] = 30;
 | 
				
			||||||
 | 
					    json["force_update"] = "true";
 | 
				
			||||||
 | 
					    json["retain"] = "true";
 | 
				
			||||||
 | 
					    char buffer[1024]{0};
 | 
				
			||||||
 | 
					    serializeJsonPretty(json, buffer);
 | 
				
			||||||
 | 
					    mqttPublish((topicPrefix + "/binary_sensor/" + myNet.getNodeMac() + "-1" + "/config").c_str(), buffer, true);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
String getValue(String data, char separator, uint8_t index)
 | 
					String getValue(String data, char separator, uint8_t index)
 | 
				
			||||||
@@ -421,9 +544,9 @@ String getValue(String data, char separator, uint8_t index)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
void loadConfig()
 | 
					void loadConfig()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (!SPIFFS.exists("/config.json"))
 | 
					    if (!LittleFS.exists("/config.json"))
 | 
				
			||||||
        saveConfig();
 | 
					        saveConfig();
 | 
				
			||||||
    File file = SPIFFS.open("/config.json", "r");
 | 
					    File file = LittleFS.open("/config.json", "r");
 | 
				
			||||||
    String jsonFile = file.readString();
 | 
					    String jsonFile = file.readString();
 | 
				
			||||||
    StaticJsonDocument<1024> json;
 | 
					    StaticJsonDocument<1024> json;
 | 
				
			||||||
    deserializeJson(json, jsonFile);
 | 
					    deserializeJson(json, jsonFile);
 | 
				
			||||||
@@ -436,6 +559,9 @@ void loadConfig()
 | 
				
			|||||||
    mqttUserLogin = json["mqttUserLogin"].as<String>();
 | 
					    mqttUserLogin = json["mqttUserLogin"].as<String>();
 | 
				
			||||||
    mqttUserPassword = json["mqttUserPassword"].as<String>();
 | 
					    mqttUserPassword = json["mqttUserPassword"].as<String>();
 | 
				
			||||||
    topicPrefix = json["topicPrefix"].as<String>();
 | 
					    topicPrefix = json["topicPrefix"].as<String>();
 | 
				
			||||||
 | 
					    workMode = json["workMode"];
 | 
				
			||||||
 | 
					    ntpHostName = json["ntpHostName"].as<String>();
 | 
				
			||||||
 | 
					    gmtOffset = json["gmtOffset"];
 | 
				
			||||||
    file.close();
 | 
					    file.close();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -452,8 +578,11 @@ void saveConfig()
 | 
				
			|||||||
    json["mqttUserLogin"] = mqttUserLogin;
 | 
					    json["mqttUserLogin"] = mqttUserLogin;
 | 
				
			||||||
    json["mqttUserPassword"] = mqttUserPassword;
 | 
					    json["mqttUserPassword"] = mqttUserPassword;
 | 
				
			||||||
    json["topicPrefix"] = topicPrefix;
 | 
					    json["topicPrefix"] = topicPrefix;
 | 
				
			||||||
 | 
					    json["workMode"] = workMode;
 | 
				
			||||||
 | 
					    json["ntpHostName"] = ntpHostName;
 | 
				
			||||||
 | 
					    json["gmtOffset"] = gmtOffset;
 | 
				
			||||||
    json["system"] = "empty";
 | 
					    json["system"] = "empty";
 | 
				
			||||||
    File file = SPIFFS.open("/config.json", "w");
 | 
					    File file = LittleFS.open("/config.json", "w");
 | 
				
			||||||
    serializeJsonPretty(json, file);
 | 
					    serializeJsonPretty(json, file);
 | 
				
			||||||
    file.close();
 | 
					    file.close();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -480,7 +609,7 @@ void setupWebServer()
 | 
				
			|||||||
        ssdpDescription += xmlNode("friendlyName", deviceName);
 | 
					        ssdpDescription += xmlNode("friendlyName", deviceName);
 | 
				
			||||||
        ssdpDescription += xmlNode("presentationURL", "/");
 | 
					        ssdpDescription += xmlNode("presentationURL", "/");
 | 
				
			||||||
        ssdpDescription += xmlNode("serialNumber", "0000000" + String(random(1000)));
 | 
					        ssdpDescription += xmlNode("serialNumber", "0000000" + String(random(1000)));
 | 
				
			||||||
        ssdpDescription += xmlNode("modelName", "ESP-NOW Gateway");
 | 
					        ssdpDescription += xmlNode("modelName", "ESP-NOW gateway");
 | 
				
			||||||
        ssdpDescription += xmlNode("modelNumber", firmware);
 | 
					        ssdpDescription += xmlNode("modelNumber", firmware);
 | 
				
			||||||
        ssdpDescription += xmlNode("modelURL", "https://github.com/aZholtikov/ESP-NOW-Gateway");
 | 
					        ssdpDescription += xmlNode("modelURL", "https://github.com/aZholtikov/ESP-NOW-Gateway");
 | 
				
			||||||
        ssdpDescription += xmlNode("manufacturer", "Alexey Zholtikov");
 | 
					        ssdpDescription += xmlNode("manufacturer", "Alexey Zholtikov");
 | 
				
			||||||
@@ -493,19 +622,22 @@ void setupWebServer()
 | 
				
			|||||||
        request->send(200, "text/xml", ssdpSend); });
 | 
					        request->send(200, "text/xml", ssdpSend); });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
 | 
					    webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
 | 
				
			||||||
                 { request->send(SPIFFS, "/index.htm"); });
 | 
					                 { request->send(LittleFS, "/index.htm"); });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
 | 
					    webServer.on("/setting", HTTP_GET, [](AsyncWebServerRequest *request)
 | 
				
			||||||
                 {
 | 
					                 {
 | 
				
			||||||
        ssid = request->getParam("ssid")->value();
 | 
					        ssid = request->getParam("ssid")->value();
 | 
				
			||||||
        password = request->getParam("password")->value();
 | 
					        password = request->getParam("password")->value();
 | 
				
			||||||
        mqttHostName = request->getParam("host")->value();
 | 
					        mqttHostName = request->getParam("mqttHostName")->value();
 | 
				
			||||||
        mqttHostPort = request->getParam("port")->value().toInt();
 | 
					        mqttHostPort = request->getParam("mqttHostPor")->value().toInt();
 | 
				
			||||||
        mqttUserLogin = request->getParam("login")->value();
 | 
					        mqttUserLogin = request->getParam("mqttUserLogin")->value();
 | 
				
			||||||
        mqttUserPassword = request->getParam("pass")->value();
 | 
					        mqttUserPassword = request->getParam("mqttUserPassword")->value();
 | 
				
			||||||
        topicPrefix = request->getParam("prefix")->value();
 | 
					        topicPrefix = request->getParam("topicPrefix")->value();
 | 
				
			||||||
        deviceName = request->getParam("name")->value();
 | 
					        deviceName = request->getParam("deviceName")->value();
 | 
				
			||||||
        espnowNetName = request->getParam("net")->value();
 | 
					        espnowNetName = request->getParam("espnowNetName")->value();
 | 
				
			||||||
 | 
					        workMode = request->getParam("workMode")->value().toInt();
 | 
				
			||||||
 | 
					        ntpHostName = request->getParam("ntpHostName")->value();
 | 
				
			||||||
 | 
					        gmtOffset = request->getParam("gmtOffset")->value().toInt();
 | 
				
			||||||
        request->send(200);
 | 
					        request->send(200);
 | 
				
			||||||
        saveConfig(); });
 | 
					        saveConfig(); });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -516,26 +648,73 @@ void setupWebServer()
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    webServer.onNotFound([](AsyncWebServerRequest *request)
 | 
					    webServer.onNotFound([](AsyncWebServerRequest *request)
 | 
				
			||||||
                         { 
 | 
					                         { 
 | 
				
			||||||
        if (SPIFFS.exists(request->url()))
 | 
					        if (LittleFS.exists(request->url()))
 | 
				
			||||||
        request->send(SPIFFS, request->url());
 | 
					        request->send(LittleFS, request->url());
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
        request->send(404, "text/plain", "File Not Found");
 | 
					        request->send(404, "text/plain", "File Not Found");
 | 
				
			||||||
        } });
 | 
					        } });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    SSDP.begin();
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					        SSDP.begin();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    webServer.begin();
 | 
					    webServer.begin();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void connectToMqtt()
 | 
					void checkMqttAvailability()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    mqttReconnectTimerSemaphore = false;
 | 
					    mqttAvailabilityCheckTimerSemaphore = false;
 | 
				
			||||||
    mqttClient.connect();
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					        if (WiFi.isConnected())
 | 
				
			||||||
 | 
					            if (!mqttWifiClient.connected())
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                isMqttAvailable = false;
 | 
				
			||||||
 | 
					                if (mqttWifiClient.connect(mqttUserID, mqttUserLogin.c_str(), mqttUserPassword.c_str()))
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    isMqttAvailable = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    mqttWifiClient.subscribe((topicPrefix + "/espnow_gateway/#").c_str());
 | 
				
			||||||
 | 
					                    mqttWifiClient.subscribe((topicPrefix + "/espnow_switch/#").c_str());
 | 
				
			||||||
 | 
					                    mqttWifiClient.subscribe((topicPrefix + "/espnow_led/#").c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    sendConfigMessage();
 | 
				
			||||||
 | 
					                    sendAttributesMessage();
 | 
				
			||||||
 | 
					                    sendKeepAliveMessage();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					        if (Ethernet.linkStatus() == LinkON)
 | 
				
			||||||
 | 
					            if (!mqttEthClient.connected())
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                isMqttAvailable = false;
 | 
				
			||||||
 | 
					                if (mqttEthClient.connect(mqttUserID, mqttUserLogin.c_str(), mqttUserPassword.c_str()))
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    isMqttAvailable = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    mqttEthClient.subscribe((topicPrefix + "/espnow_gateway/#").c_str());
 | 
				
			||||||
 | 
					                    mqttEthClient.subscribe((topicPrefix + "/espnow_switch/#").c_str());
 | 
				
			||||||
 | 
					                    mqttEthClient.subscribe((topicPrefix + "/espnow_led/#").c_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    sendConfigMessage();
 | 
				
			||||||
 | 
					                    sendAttributesMessage();
 | 
				
			||||||
 | 
					                    sendKeepAliveMessage();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void mqttReconnectTimerCallback()
 | 
					void mqttPublish(const char *topic, const char *payload, bool retained)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    mqttReconnectTimerSemaphore = true;
 | 
					    if (workMode == ESP_NOW_WIFI)
 | 
				
			||||||
 | 
					        mqttWifiClient.publish(topic, payload, retained);
 | 
				
			||||||
 | 
					    if (workMode == ESP_NOW_LAN)
 | 
				
			||||||
 | 
					        mqttEthClient.publish(topic, payload, retained);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void mqttAvailabilityCheckTimerCallback()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    mqttAvailabilityCheckTimerSemaphore = true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void keepAliveMessageTimerCallback()
 | 
					void keepAliveMessageTimerCallback()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user