Got it! Actually the problem was related to the ESP8266. Here are my full code examples.
First we need a KNX group address as a trigger source for the automation (located in file knx.yaml or can also be stored in configuration.yaml):
event:
- address:
- "6/0/1"
Next the automation code:
alias: Automation1
description: "Kommunication: KNX > HA > ESP8266 > HA > KNX"
trigger:
- platform: event
event_type: knx_event
event_data:
# Group address, which data is send to ESP8266 (KNX button)
destination: 6/0/1
condition: []
action:
- service: shell_command.tcp_esp8266
data_template:
# Extract data from KNX button and send it via shell command
# Important: Use double slash and NO QUOTES!
# Also Important: Format to hex-code with two digits ('%02x')
payload: \\x{{ '%02x' % trigger.event.data.data }}\\x00\\x00
response_variable: return_response
- service: knx.send
data:
address: 6/0/2
# Return ESP8266's answere to the KNX-Button (state for its switching value)
payload: "{{ return_response.stdout[0] | ord }}"
mode: single
Due to the fact that it seems to be impossible to send proper parameters to a shell command, i used a bash script. It is namend send_payload.sh and is located in /config:
# Important: Use double quotes around $1 !!!
# $1 is the parameter from the automation
echo -ne "$1" | nc -w 2 192.168.178.111 80
Next, we have to enable the bash script. This is done by entering in to the terminal the following:
chmod +x /config/send_payload.sh
Then we need the shell command (also located in /config):
shell_command:
# Note: No quotes at all
tcp_esp8266: bash /config/send_payload.sh {{ payload }}
Finally, we have the following code for the ESP8266:
#include <ESP8266WiFi.h>
// Server-Object for client communication
WiFiServer server(80);
// WLAN access
const char* ssid = "WLAN_SSID";
const char* password = "TheQueenIsDead";
char inputBuffer[10];
uint8_t counter = 0;
void setup()
{
// ON-Board LED for status indication
pinMode(D0, OUTPUT);
// Init UART for debugging
Serial.begin(115200);
// New line to remove rubbish to the top
Serial.println("");
// Start WLAN connection
Serial.println("Connection to WLAN");
WiFi.begin(ssid, password);
// Wait until connection is finish
while (WiFi.status() != WL_CONNECTED)
{
digitalWrite(D0, HIGH);
delay(100);
digitalWrite(D0, LOW);
delay(100);
Serial.print(".");
}
// Display WLAN data
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Start server
server.begin();
// Switch on LED (inverse logic)
digitalWrite(D0, LOW);
}
void loop()
{
// Listen for incoming clients
WiFiClient client = server.available();
if (client)
{
if (client.connected())
{
// Wait for complete data package (3 bytes)
while (client.available() < 3);
// Read received data
uint8_t receivedData[10];
uint8_t index = 0;
while (client.available())
{
receivedData[index] = client.read();
// Display received data
Serial.println(receivedData[index]);
index++;
}
// Switch LED regarding to the received data
if (receivedData[0] == 1)
{
// LED on (inverse logic!)
digitalWrite(D0, LOW);
}
else
{
// LED off
digitalWrite(D0, HIGH);
}
Serial.println("");
// Send response (state of LED and dummy data)
uint8_t dataOut[] = {!digitalRead(D0), 0x00, 0x00};
client.write(dataOut, 3);
}
// close the connection:
client.stop();
}
}
Maybe someone find this helpfull ![:slight_smile: :slight_smile:](https://community.home-assistant.io/images/emoji/twitter/slight_smile.png?v=12)