NodeMCU MQTT

Takk for svar Mikkel

Jeg fant denne koden for MQTT, hvordan kan jeg modifisere denne til å styre 4 rele og 1 input?
Jeg vil styre on/off og lese status på rele, på input vil jeg bare lese status

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "YourNetworkName";
const char* password = "YourNetworkPassword";
const char* mqttServer = "m11.cloudmqtt.com";
const int mqttPort = 12948;
const char* mqttUser = "YourMqttUser";
const char* mqttPassword = "YourMqttUserPassword";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {

Serial.begin(115200);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");

client.setServer(mqttServer, mqttPort);
client.setCallback(callback);

while (!client.connected()) {
Serial.println("Connecting to MQTT...");

if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {

Serial.println("connected");

} else {

Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);

}
}

client.publish("esp/test", "Hello from ESP8266");
client.subscribe("esp/test");

}

void callback(char* topic, byte* payload, unsigned int length) {

Serial.print("Message arrived in topic: ");
Serial.println(topic);

Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload*);*

  • }*

  • Serial.println();*

  • Serial.println("-----------------------");*

}

void loop() {

  • client.loop();*
    }