Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

problems with the simultaneous operation of two switches #147

Open
Golovkov opened this issue Feb 13, 2019 · 3 comments
Open

problems with the simultaneous operation of two switches #147

Golovkov opened this issue Feb 13, 2019 · 3 comments

Comments

@Golovkov
Copy link

Good day!
I have added two switches to homekit2mqtt.
Code:

{
  "switch": {
    "id": "switch",
    "name": "button/03",
    "category": 8,
    "services": [
      {
        "name": "button/03",
        "service": "Switch",
        "topic": {
          "setOn": "button/03",
          "statusOn": "button/03"
        },
        "json": {
          "statusOn": ""
        },
        "payload": {
          "onFalse": "OFF",
          "onTrue": "ON"
        },
        "config": {},
        "props": {}
      }
    ],
    "payload": {},
    "config": {},
    "topicIdentify": "button/03"
  },
  "button": {
    "id": "button",
    "name": "button/01",
    "category": 8,
    "services": [
      {
        "name": "button/01",
        "service": "Switch",
        "topic": {
          "setOn": "button/01",
          "statusOn": "button/01"
        },
        "json": {
          "statusOn": ""
        },
        "payload": {
          "onFalse": "OFF",
          "onTrue": "ON"
        },
        "config": {},
        "props": {}
      }

Both devices will be added to the house, but do not want to work simultaneously. More precisely, one of the two switches can work when you try to turn on the second, both stop working, and when you repeatedly press one of the switches, it starts working.

code of the switch itself

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

const char *ssid =  "ххх";    
const char *pass =  "123";    



int inPin = 0;         // the number of the input pin
int ledPin = 5;        // Control Led to see if everything is working.

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds, will quickly become a bigger number than can be stored in an int.
long Time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers



// Update these with values suitable for your network.
IPAddress server(10, 0, 1, 2);   

#define BUFFER_SIZE 100


void callback(const MQTT::Publish& pub) {
  pinMode(ledPin, OUTPUT);

  // check if the message in On or Off
  if (pub.payload_string() == "ON") {
    state = LOW;
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.println(pub.payload_string());
  } else {
    state = HIGH;
    // turn LED off:
    digitalWrite(ledPin, LOW);
    Serial.println(pub.payload_string());
  }
}

WiFiClient wclient;
PubSubClient client(wclient, server);

void setup() {
  // Setup console
  pinMode(inPin, INPUT);
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, pass);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }

  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      if (client.connect("arduinoClient")) {
        client.set_callback(callback);
        client.subscribe("button/01"); // change InTopic to your Topic description
      }
    }

    if (client.connected())
      client.loop();

    reading = digitalRead(inPin);

    // if the input just went from LOW and HIGH and we've waited long enough
    // to ignore any noise on the circuit, toggle the output pin and remember
    // the time
    if (reading == HIGH && previous == LOW && millis() - Time > debounce) {
      if (state == HIGH) {
        state = LOW;
        digitalWrite(ledPin, HIGH);
        client.publish("button/01", "ON");
      }
      else {
        state = HIGH;
        digitalWrite(ledPin, LOW);
        client.publish("button/01", "OFF");
      }

      Time = millis();
    }

    previous = reading;

  }

}

When you run homebridge with the parameter -v debug, it does not give any errors. Shows parameter changes

2019-02-13 17:00:07.060 <debug> < hap set button/01 On true
2019-02-13 17:00:07.061 <debug> > mqtt button/01 ON
2019-02-13 17:00:07.069 <debug> < mqtt button/01 ON ON
2019-02-13 17:00:07.070 <debug> > hap update button/01 On true
2019-02-13 17:00:09.969 <debug> < hap set button/01 On false
2019-02-13 17:00:09.969 <debug> > mqtt button/01 OFF
2019-02-13 17:00:09.977 <debug> < mqtt button/01 OFF OFF
2019-02-13 17:00:09.978 <debug> > hap update button/01 On false
2019-02-13 17:00:11.473 <debug> < hap set button/03 On true
2019-02-13 17:00:11.474 <debug> > mqtt button/03 ON
2019-02-13 17:00:11.481 <debug> < mqtt button/03 ON ON
2019-02-13 17:00:11.482 <debug> > hap update button/03 On true
2019-02-13 17:00:12.739 <debug> < hap set button/03 On false
2019-02-13 17:00:12.739 <debug> > mqtt button/03 OFF
2019-02-13 17:00:12.747 <debug> < mqtt button/03 OFF OFF
2019-02-13 17:00:12.748 <debug> > hap update button/03 On false

at the same time the first switch works (turns on and off) and the second switch changes its parameters only on the terminal screen.

when only one of the switches is added everything works

Please tell me how to solve this problem?

@DogyGamer
Copy link

Hi, is this problem still relevant for you?

@DogyGamer
Copy link

This code works for me. The code commented. Sorry for bad English
ForGithub.zip

@DogyGamer
Copy link

#include <SimpleDHT.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define call1_pin 2
#define call1_topic "1Topic"
#define call2_pin 0
#define call2_topic "2Topic"
const char *ssid =  "ssid";
const char *pass =  "pass";
IPAddress server(x, x, x, x);
WiFiClient wclient;
PubSubClient client(wclient, server);
//its configure functions
void call1() {
  client.subscribe(call1_topic);
  client.set_callback(call_callback);
  client.loop();
}

void call2() {
  client.subscribe(call2_topic);
  client.set_callback(call_callback);
  client.loop();
}
void call_callback(const MQTT::Publish& pub) {
  if (pub.topic() == call1_topic) {//if topic for 1 lamp
    pinMode(call1_pin, OUTPUT);
    Serial.println(pub.topic());
    if (pub.payload_string() == "yourPayloadhere") {
      digitalWrite(call1_pin, HIGH);
    } else {
      digitalWrite(call1_pin, LOW);
    }
  } else if (pub.topic() == call2_topic) {//if topic for 2 lamp
    pinMode(call2_pin, OUTPUT);
    if (pub.payload_string() == "yourPayloadhere") {
      digitalWrite(call2_pin, HIGH);
    } else {
      digitalWrite(call2_pin, LOW);
    }
  }
}
//pub.topic(); topic on which the data came
void setup() {
  // Setup console
  Serial.begin(115200);
  delay(10);
  Serial.println();
  Serial.println();
  pinMode(call1_pin, OUTPUT);
  digitalWrite(call1_pin, LOW);
  pinMode(call2_pin, OUTPUT);
  digitalWrite(call2_pin, LOW);
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, pass);

    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;
    Serial.println("WiFi connected");
  }
  if (WiFi.status() == WL_CONNECTED) {
    if (!client.connected()) {
      if (client.connect("arduinoClient")) {
      } else {
        Serial.println("No connection to mqtt");
      }
    } else {
      call1();//configure topic
      delay(10);
      call2();//configure topic
      delay(10);
    }
  }
}




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants