Advice The Home Improvement/Automation Thread

I see.

You've mistaken my use of "HA". I mean "home automation", not "home assistant".
I was referring to this post: jesus christ, no, I'm not switching off openhab. I like my shit to work from week to week rather than getting ratfucked by walled garden bullshit.

That seemed like you were referencing just Home Assistant being broken. Home Assistant works just as well as OpenHab. ;)
 
I was referring to this post: jesus christ, no, I'm not switching off openhab. I like my shit to work from week to week rather than getting ratfucked by walled garden bullshit.

That seemed like you were referencing just Home Assistant being broken. Home Assistant works just as well as OpenHab. ;)
yeah, but now they're both broken because MyQ relies on cloud garbage :p
 
Ran up against a Z-wave canopy module that I can't figure out, so I decided to whip up a NodeMCU/MQTT thinger instead.

Wrote a pub/sub bit for it that subscribes to a command topic, and then reports to a status topic. The old dumb switch is on a pullup GPIO pin, and on state change will send an update to the command topic. The light itself will be on a relay, but it's just an output GPIO line. I haven't debugged it yet, I'm not sure if stuff published to a topic gets sent back for the callback, but I guess I'll find out when I do the wiring for an LED before I wire up the 110V relay and the 110VAC to 3.3VDC power adapter.

I've got a shitload of space in the canopy, so lucky me on that front.
 
  • Gravy
Reactions: fly
It turns out the callback happens regardless of the source of the update with Mosquitto. So now all I have to do is build the hardware side of it and I'm good to go.

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

#define wifi_ssid "Fisting4Jesus"
#define wifi_password "TiddySprinkles"
#define mqtt_server "192.168.0.2"
#define client_name "diningLights"
#define command_topic "command"
#define status_topic "status"

#define RELAYPIN 14
#define SWITCHPIN 12

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(wifi_ssid);
  WiFi.begin(wifi_ssid, wifi_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 connect() {
  char tName[40];
  sprintf(tName, "%s/%s", client_name, command_topic);
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    //if (client.connect(client_name, mqtt_user, mqtt_password)) {
    if (client.connect(client_name)) {
      Serial.println("connected");
      client.subscribe(tName);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  char tVal[40];
  char tName[40];
  memset(tVal, '\0', 40);
  memset(tName, '\0', 40);
  for (unsigned int i = 0; i < length; i++) {
    tVal[i] = (char) payload[i];
  }
  sprintf(tName, "%s/%s", client_name, status_topic);
  if (strcmp(tVal, "ON") == 0) {
    digitalWrite(RELAYPIN, HIGH);
    client.publish(tName, "ON");
  }
  if (strcmp(tVal, "OFF") == 0) {
    digitalWrite(RELAYPIN, LOW);
    client.publish(tName, "OFF");
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  pinMode(SWITCHPIN, INPUT_PULLUP);
  pinMode(RELAYPIN, OUTPUT);
}

// HIGH = "off", LOW = "on"
long switchState = HIGH;
long lastMsg = 0;
bool cmd = false;
bool stat = false;

void loop() {
  char tName[40];
  if (!client.connected()) {
    connect();
  }
  client.loop();
  long tmpSwitch = digitalRead(SWITCHPIN);
  if (tmpSwitch != switchState) {
    char tStat[5];
    if (tmpSwitch == LOW) {
      sprintf(tStat, "ON");
    } else {
      sprintf(tStat, "OFF");
    }
    switchState = tmpSwitch;
    Serial.println("setting command topic");
    sprintf(tName, "%s/%s", client_name, command_topic);
    client.publish(tName, tStat);
  }
}
 
Code:
#define wifi_ssid "Fisting4Jesus"

🤣 🤣 🤣
Thug Life Dog GIF by Macattack