Hello all, I’m a newbie when it comes to Home Assistant, I found it a couple of weeks ago and it looks like the perfect solution for my home that I’m currently renovating. With that being said I am having an issue with controlling some relays on an Arduino Mega with an Ethernet shield to home assistant using MQTT.
I have a DHT11 sensor connected and it is reporting temperature and humidity so the Mega is connected and talking but whenever I try to turn on one of the relays I get no response. I was wondering if someone here could look at the c code for the arduino and the yaml in Home Assistant and see where I went wrong? I’ve been at this a while making slow progress, but I’m almost ready to pack it in.
#include <SPI.h>
#include <Ethernet2.h>
#include <PubSubClient.h>
#include <DHT.h>
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
DHT dht(2, DHT11, 23); // DHT 22 on pin 9 - get power from pin 8 (cant use 10-13 as used for SPI by the ethernet card)
long previousMillis = 25000;
long interval = 30000;
// Update these with values suitable for your network.
const char* mqtt_server = "192.168.0.135";
const char* client_name = "zone1";
const int Zone1Pin = 25;
const int Zone2Pin = 27;
char const* switchTopic1 = "/boiler/zone1/";
char const* switchTopic2 = "/boiler/zone2/";
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0xA9, 0xCE }; //physical mac address
byte ip[] = { 192, 168, 0, 101 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80); //server port
String readString;
// Callback function header
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient client(mqtt_server, 1883, callback, ethClient);
void setup() {
pinMode(Zone1Pin, OUTPUT);
digitalWrite(Zone1Pin, HIGH);
pinMode(Zone2Pin, OUTPUT);
digitalWrite(Zone2Pin, HIGH);
Serial.begin(57600);
delay(100);
Ethernet.begin(mac, ip);
//client.connect("Zone1", mqtt_user, mqtt_password);
client.connect("zone1", "boiler", "123186");
reconnect();
// start the Ethernet connection and the server:
Serial.println("about to begin ethernet");
delay(1500); // Allow the hardware to sort itself out
// server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
if (!ethClient.connected()) {
Serial.println("about to retry ethernet");
ethClient.stop();
Ethernet.begin(mac, ip);
Serial.print("server is now at ");
Serial.println(Ethernet.localIP());
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String topicStr = topic;
Serial.println("Callback update.");
Serial.print("Topic: ");
Serial.println(topicStr); //debug if we got a topic
if (topicStr == "/boiler/zone1/")
{
if(payload[0] == '1'){
digitalWrite(Zone1Pin, LOW);
client.publish("/boiler/zone1/", "1");
}
else if (payload[0] == '0'){
digitalWrite(Zone1Pin, HIGH);
client.publish("/boiler/zone1/", "0");
}
}
if (topicStr == "/boiler/zone2/")
{
if(payload[0] == '1'){
digitalWrite(Zone2Pin, LOW);
client.publish("/boiler/zone2/", "1");
}
else if (payload[0] == '0'){
digitalWrite(Zone2Pin, HIGH);
client.publish("/boiler/zone2/", "0");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(client_name)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.subscribe(switchTopic1);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(2000);
}
}
}
void loop() {
if (!client.connected()) {
Serial.println(" not connected - will try agin - calling reconect");
reconnect();
}
client.loop();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
float h = dht.readHumidity();
float f = dht.readTemperature(false);
if (isnan(h) || isnan(f)) {
client.publish("boiler/supply/temperature", "DHT problem");
previousMillis = currentMillis; // add on and try again next loop
return;
}
previousMillis = currentMillis;
Serial.println(String(f));
client.publish("boiler/supply/temperature", String(f).c_str());
Serial.println(String(h));
client.publish("boiler/supply/humidity", String(h).c_str());
}
}
mqtt:
switch:
- name: "Zone1"
state_topic: "/boiler/zone1/"
command_topic: "/boiler/zone1/"
payload_on: "1"
payload_off: "0"
qos: 0
retain: true
unique_id: "zone1"
- name: "Zone2"
state_topic: "/boiler/zone2/"
command_topic: "/boiler/zone2/"
payload_on: "1"
payload_off: "0"
qos: 0
retain: true
unique_id: "zone2"
sensor:
- name: "Supply Temp"
state_topic: "boiler/supply/temperature"
unit_of_measurement: "F"
value_template: '{{ value | round(0) }}'
unique_id: "Supply Temp"
- name: "Supply Humidity"
state_topic: "boiler/supply/humidity"
unit_of_measurement: "%"
value_template: '{{ value | round(0) }}'
unique_id: "Supply Humidity"