Hi everyone,
i am running node red on a raspberry pi 4 and want to controll one or more relays on the dashboard.
Also i want send sensor values from my ESP32 to the dashboard.
Sending the sensor values works already fine, i can display them in a chart or gauge.
But i cannot controll the relay and i cant find the issue.
It seems like the broker is working, the message gets send out to the right topic.
I can see that in the debugger in node red.
But the ESP32 doesnt seem to correctly subscribe to the MQTT topic.
Here is the code, from randomnerdtutorials, just modified.
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "**********";
const char* password = "********";
// Add your MQTT Broker IP address, example:
//const char* mqtt_server = "192.168.1.144";
const char* mqtt_server = "192.168.0.219";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
int Fuellstand = 26;
unsigned long lastMillis = 0;
int relais_one = 27; //Testrelay
void setup() {
Serial.begin(115200);
// default settings
// (you can also pass in a Wire library object like &Wire2)
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(relais_eins,OUTPUT);
pinMode(Fuellstand,INPUT);
digitalWrite(relais_eins, HIGH);
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// Feel free to add more if statements to control more GPIOs with MQTT
if (String(topic) == "output") {
Serial.print("changing output to");
if (messageTemp == "on") {
Serial.println("on");
digitalWrite(relais_one, HIGH);
delay(200);
}
else if (messageTemp == "off") {
Serial.println("off");
digitalWrite(relais_one, LOW);
delay(200);
}
}
// If a message is received on the topic esp32/output, you check if the message is either "on" or "off".
// Changes the output state according to the message
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("espClient")) {
Serial.println("connected");
// Subscribe
client.subscribe("output");
client.subscribe("hello");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
client.loop();
}
// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
Fuellstand = digitalRead(26);
char fuellString[8];
dtostrf(Fuellstand, 1, 2, fuellString);
Serial.print("Fuellstand: ");
Serial.println(fuellString);
client.publish("hello", fuellString);
client.publish("chattest", "hi, this is a test message");
client.publish("output", "topic output publish test ");
}
}
the flow, only the relay part:
[
{
"id": "eb8f9c0d054be30c",
"type": "tab",
"label": "Flow 2",
"disabled": false,
"info": "",
"env": []
},
{
"id": "4ce6cd876fd5441f",
"type": "mqtt out",
"z": "eb8f9c0d054be30c",
"name": "",
"topic": "output",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "33a951cef9d47be6",
"x": 330,
"y": 120,
"wires": []
},
{
"id": "8f79b17bd2c2101c",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "output",
"payload": "on",
"payloadType": "str",
"x": 120,
"y": 120,
"wires": [
[
"4ce6cd876fd5441f"
]
]
},
{
"id": "974a7a8bb6db9bf9",
"type": "mqtt in",
"z": "eb8f9c0d054be30c",
"name": "",
"topic": "output",
"qos": "2",
"datatype": "auto-detect",
"broker": "33a951cef9d47be6",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 90,
"y": 280,
"wires": [
[
"d0dc7378c7bfb03b"
]
]
},
{
"id": "d0dc7378c7bfb03b",
"type": "debug",
"z": "eb8f9c0d054be30c",
"name": "debug 4",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 300,
"y": 280,
"wires": []
},
{
"id": "8dbcaf7b23fbb466",
"type": "mqtt out",
"z": "eb8f9c0d054be30c",
"name": "",
"topic": "output",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "33a951cef9d47be6",
"x": 330,
"y": 200,
"wires": []
},
{
"id": "1298d2bf330b5cb5",
"type": "inject",
"z": "eb8f9c0d054be30c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "output",
"payload": "off",
"payloadType": "str",
"x": 120,
"y": 200,
"wires": [
[
"8dbcaf7b23fbb466"
]
]
},
{
"id": "33a951cef9d47be6",
"type": "mqtt-broker",
"name": "",
"broker": "192.168.0.219",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "/hello",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
}
]
Thanks for help.