NodeMCU+MQTT

Hi

Newbie on Arduino, and don't know where to begin.

Can anyone help me here, I have purchased this NodeMCU:
https://www.kjell.com/no/produkter/elektro-og-verktoy/arduino/utviklingskort/nodemcu-utviklingskort-p87949

I want to program it to run 4 relay(output) and 1 magnet switch (input)

My goal is to control the relays and read the state from MQTT

I used a Arduino plugin for my Homeseer(Home Automation), but that was unstable so I must try the MQTT.

Is there anyone her that could help me with a sketch or help me to find where to begin?

This is for controlling 4 valves for my garden sprinklers, all the automation will be done in HomeSeer

Br
Kim

Have you googled Arduino + MQTT? Lots of interesting stuff...

Hi Paul

Yes, I have.

As I am not familiar with the programming of the arduino/nodemcu I am not sure what I need to modify to get it to fit my needs.

I think I have been googling for 10 hours to find a solution.

Br
Kim

I have found this code, how can i modify it to use with 4 relays and 1 input?

I would like to control and read the outputs and read the input on MQTT

#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();*
    }

Hi Kimstory,

why don't you try this one: GitHub - AchD/Frog: A simple to use MQTT Client for Arduino

AchimD