at first I am noob .. but i am trying ...
i need help with code .. i need to control 8ch relay with MQTT from homebridge and problem is:
1. I cant make more than one relay
2. I had find some code but not working
3. I cannot find any good tutorial for MQTT for more then one output with status
4. if i post to topic device have no response allso it dont post the status
ps: pinout doesent matter.. its only test version
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "ZTE-CJYFDJ";
const char* password = "*********";
const char* mqtt_server = "192.168.1.15";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
//pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
//Serial.begin(9600);
pinMode(D0, OUTPUT);
digitalWrite(D0, HIGH);
pinMode(D2, OUTPUT);
digitalWrite(D2, HIGH);
pinMode(D4, OUTPUT);
digitalWrite(D4, HIGH);
pinMode(D5, OUTPUT);
digitalWrite(D5, HIGH);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
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* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
//1 ( pin D0 )
if (strcmp(topic,"house/balkon/ledky/1/set") == 0) {
if (payload[0] == '0'){
digitalWrite(D0, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("house/balkon/ledky/1/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(D0, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("house/balkon/ledky/1/state","1");
}
}
//2 ( pin 2 )
if (strcmp(topic,"house/balkon/ledky/2/set")==0) {
if (payload[0] == '0'){
digitalWrite(D2, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("house/balkon/ledky/2/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(D2, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("house/balkon/ledky/2/state","1");
}
}
//3 ( pin 4 )
if (strcmp(topic,"house/balkon/ledky/3/set")==0) {
if (payload[0] == '0'){
digitalWrite(D4, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("house/balkon/ledky/3/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(D4, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("house/balkon/ledky/3/state","1");
}
}
//4 ( pin 5 )
if (strcmp(topic,"house/balkon/ledky/4/set")==0) {
if (payload[0] == '0'){
digitalWrite(D5, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("house/balkon/ledky/4/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(D5, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("house/balkon/ledky/4/state","1");
}
}
//5 ( pin 6 )
if (strcmp(topic,"house/balkon/ledky/5/set")==0) {
if (payload[0] == '0'){
digitalWrite(D6, HIGH);
Serial.print("Turning Light ON");
delay(100);
client.publish("house/balkon/ledky/5/state","0");
}
else if (payload[0] == '1')
{
digitalWrite(D6, LOW);
Serial.print("Turning Light OFF");
delay(100);
client.publish("house/balkon/ledky/5/state","1");
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} 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();
long now = millis();
}