Damit ioBroker die Werte an den MQTT Server überträgt, musst du schon einen MQTT Client (Adapter) konfigurieren und über das Zahnrad rechts vom Wert (drauf klicken) konfigurieren.
Da in deinem Screenshot die Zahnrädchen noch so "blass" sind, scheinst Du das noch nicht getan zu haben.
Normalerweise sieht das so aus:
Wenn man das korrekt gemacht hat, kommen die geänderten Werte auch auf dem ESP8266 an:
Der Code zu diesem Beispiel:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <iostream>
#include "MQTT_Network.h"
// Redefine max package size of mqtt client
#ifdef MQTT_MAX_PACKET_SIZE
#undef MQTT_MAX_PACKET_SIZE
#endif
#define MQTT_MAX_PACKET_SIZE 1024
//////////////////////////////////////////////////////////////////////////////
/// Global constants
///
//////////////////////////////////////////////////////////////////////////////
constexpr unsigned int MAX_WLAN_CONNATTEMPTS {20};
constexpr unsigned int MAX_MQTT_CONNATTEMPTS {5};
constexpr unsigned int MQTT_CONNECT_DELAY_MS {5000};
const std::vector<const char *> topics {"iobroker/0_userdata/0/relais/1", "iobroker/0_userdata/0/relais/2",
"iobroker/0_userdata/0/relais/3", "iobroker/0_userdata/0/relais/4"};
//////////////////////////////////////////////////////////////////////////////
/// Global variables/objects
///
//////////////////////////////////////////////////////////////////////////////
WiFiClient espClient; // Non Secure Client. Fast but unsecure.
PubSubClient mqttClient(espClient); // MQTT Client connection object
bool wifiIsConnected {false};
//////////////////////////////////////////////////////////////////////////////
/// Function forward declaration
///
//////////////////////////////////////////////////////////////////////////////
bool wificonnect();
bool mqttConnect();
void subscribeTopics(PubSubClient &, const std::vector<const char *> &);
void subCallback(char *, uint8_t *, unsigned int);
//////////////////////////////////////////////////////////////////////////////
/// Main Program
///
//////////////////////////////////////////////////////////////////////////////
void setup() {
/* Initialize serial output for debug */
rst_info *rinfo = ESP.getResetInfoPtr(); // get the Reason for Reset
// Serial.setDebugOutput(true);
Serial.begin(74880);
std::cout << "\nResetInfo.reason = " << (*rinfo).reason << std::endl;
mqttClient.setServer(MQTT_BROKER, MQTT_UNSECURE_PORT);
mqttClient.setCallback(subCallback); // Set Callback funktion
wifiIsConnected = wificonnect();
if (wifiIsConnected == true) {
std::cout << "WLAN Connnection established\n";
} else {
std::cout << "WLAN Connnection error\n";
}
}
void loop() {
// Main loop. Attempt to re-connect to MQTT broker if connection drops, and service the mqttClient task.
if (wifiIsConnected == true && mqttClient.connected() == false) {
if (mqttConnect() == false) {
std::cout << "MQTT Connect failed\n";
delay(100);
}
}
mqttClient.loop();
}
//////////////////////////////////////////////////////////////////////////////
/// @brief Try to estabish a WiFi connection. Returns true (1) if successfull.
/// Otherwise, false (0) is returned if the maximum number of connection
/// attempts fails.
///
/// @return true
/// @return false
//////////////////////////////////////////////////////////////////////////////
bool wificonnect() {
unsigned int numOfConnAttempts = 0;
// Connect to local WiFi access point
// Static IP address configuration
IPAddress ip(STAT_IP_OCT_1, STAT_IP_OCT_2, STAT_IP_OCT_3, STAT_IP_HOST);
IPAddress subnet(STAT_NET_MASK_1, STAT_NET_MASK_1, STAT_NET_MASK_1, STAT_NET_MASK_2);
IPAddress gateway(STAT_IP_OCT_1, STAT_IP_OCT_2, STAT_IP_OCT_3, STAT_IP_GW);
IPAddress dns(STAT_IP_OCT_1, STAT_IP_OCT_2, STAT_IP_OCT_3, STAT_IP_GW);
// WiFi.persistent(false);
WiFi.mode(WIFI_STA); // WiFi mode station (connect to wifi router only)
WiFi.config(ip, dns, gateway, subnet);
WiFi.hostname(DEVICENAME); // DHCP Hostname (useful for finding device for static lease)
WiFi.begin(WLAN_SSID, WLAN_PWD);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
++numOfConnAttempts;
if (numOfConnAttempts == MAX_WLAN_CONNATTEMPTS) { return false; }
}
// When WiFi connection is complete, debug log connection info
char ipAddress[16];
IPAddress ipo = WiFi.localIP();
snprintf(ipAddress, 15, "%d.%d.%d.%d", ipo[0], ipo[1], ipo[2], ipo[3]);
std::cout << std::endl << "Connected, IP address: " << ipAddress << std::endl;
return true;
}
//////////////////////////////////////////////////////////////////////////////
/// @brief Connecting the mqtt Client
///
/// @return true Connect successful
/// @return false Connect failed
//////////////////////////////////////////////////////////////////////////////
bool mqttConnect() {
bool connected = true;
unsigned int act_attempt = 0;
while (!mqttClient.connected()) { // Attempt to connect
std::cout << "Attempting MQTT broker connection...";
delay(1);
if (mqttClient.connect(MQTT_CID, MQTT_USER, MQTT_PWD)) {
std::cout << "connected\n";
subscribeTopics(mqttClient, topics); // Once connected, resubscribe
} else {
std::cout << "Failed, rc = " << mqttClient.state() << ". Trying again in " << MQTT_CONNECT_DELAY_MS / 1000
<< " seconds...\n";
++act_attempt;
if (act_attempt == MAX_MQTT_CONNATTEMPTS) {
connected = false;
break;
}
delay(MQTT_CONNECT_DELAY_MS); // Wait for MQTT_CONNECT_DELAY_MS between retries
}
}
return connected;
}
//////////////////////////////////////////////////////////////////////////////
/// @brief If the connection to the MQTT server was successful,
/// the topics are subscribed to in this function.
///
/// @param client Handle to mqtt client
/// @param tp Vector with pointers to the topics
//////////////////////////////////////////////////////////////////////////////
void subscribeTopics(PubSubClient &client, const std::vector<const char *> &tp) {
std::cout << "Subscribing the following topics:\n";
for (auto i = tp.begin(); i != tp.end(); ++i) {
client.subscribe(*i);
std::cout << *i << std::endl;
}
}
//////////////////////////////////////////////////////////////////////////////
/// @brief MQTT Callback routine. You can react to changed topic values here
///
/// @param topic subscribet mqtt topic
/// @param payload mqtt data as char array
/// @param length lenght of payload char array
//////////////////////////////////////////////////////////////////////////////
void subCallback(char *topic, uint8_t *payload, unsigned int length) {
std::cout << "Received message [" << topic << "] ";
char msg[length + 1];
memcpy(msg, payload, length);
msg[length] = '\0';
// convert char array to a number
// In this example, the values received may only be "true" or "false". Any other value generates an error.
unsigned int value = (strcmp(msg, "true") == 0) ? 1U : (strcmp(msg, "false") == 0) ? 0U : 255U;
if (value > 1) {
std::cout << "Error: Value is neither true nor false!\n";
} else {
std::cout << value << std::endl;
}
}
Es wurde nur der reine Empfang der Daten und deren Anzeige implementiert, nicht das Schalten von Relais. Aber wenn die Werte erst mal da sind, ist das ja ne einfache Nummer....
So nebenbei ... was für einen MQTT Server verwendest du?