Wednesday 19 April 2017

X10 CM19A Hard Resetter Kludge



I have an X10 CM19A USB Transceiver. This lets me send and receive X10 RF signals.. Though I am slowly phasing out my X10 stuff, I have a bunch of stuff I still use that's RF (only 1 powerline device which is next on the list to be replaced). My switches and motion detectors will probably stick around for another few years...

I stripped the antenna and added on a wire running to my duct work, this improved the range significantly!

This device works pretty well, but every so often it stops sending/receiving data from the PC and there's pretty much no way to tell when this happens... This problem persists through a system reboot and is only resolved with an unplug and replug. The issue seems to crop up after a few days to weeks of perfectly fine usage.

I tried to get Windows to turn off the device in software to no avail..

My solution was to automate powering the device down and resetting it...

I got a 2 port relay board and wired it to 5v, GND and 2xGPIO ports on my Raspi.

I also got a USB extension cable and carefully cut it open (It helps if you get a high quality cable, the wires aren't so thin).  I pulled the Red +5v wire out of the twisted set and cut it, then wrapped the rest of the wires in foil tape.  I then added 2 extension wires to either end and put the extension into the NC side of the relay board.

Keeping the inputs high normally keeps the relay off thus leaving the CM19a on, and a scheduled cron job in line with my system reboot time sets the GPIO pin low for 10 seconds to reset the device, then brings it back to high.

IR2MQTT



I wanted a good solution for using my Harmony IR remote with my automation PC for controlling lights and other functions but there isn't a great low cost option since the actual PC is in the basement and the receiver is in my living room...

I have a USB extender and a USB receiver and a remote that only works with the 10 button remote it came with, it also acts as a HID device and Windows won't let me see which keyboard sent the keystrokes or capture them without being in the foreground...

Enjoying my Wemos D1 clone so much, I ordered 4 more :P   I also found out how to properly address the pins, you use D# instead of just an int with a standard Arduino.

I also got this IR receiver kit for less than $2.

Connecting it with the included wires was simple:
+ to 5v, - to GND and S to D4

The following sketch will broadcast the codes as numeric string to the MQTT topic IR2MQTT
You need to set your server IP in the code, then power it up, and it will broadcast a WiFi network for initial setup.

You just need to build a receiver to find codes you care about and then preform an action.
Just subscribe to the topic and press the button on your remote a few times until you see a common code...

/*
 *  IR 2 MQTT with $2 IR module kit
 *
 */

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
#include <IRremoteESP8266.h>

const char* ssid = "...";   // Not used using WiFiManager
const char* password = "...";  // Not used using WiFiManager



int RECV_PIN = D4; //an IR detector connected to D4

IRrecv irrecv(RECV_PIN);

decode_results results;



//const char* mqtt_server = "broker.mqtt-dashboard.com";    // Public Broker
const char* mqtt_server = "YOUR LOCAL SERVER IP HERE";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

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);
   WiFiManager wifiManager;
   wifiManager.autoConnect("AutoConnectAP");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());  
  irrecv.enableIRIn(); // Start the receiver

}

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();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      //client.publish("outTopic", "hello world");
      // ... and resubscribe
      client.subscribe("inTopic");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {

  Serial.begin(115200);

  // Bring up MQTT
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);




}

void loop() {
  // MQTT
 if (!client.connected()) {
    reconnect();
  }
  client.loop();



  // IR

    if (irrecv.decode(&results)) {
    //Serial.println(results.value, HEX);
    unsigned long buffer;
    buffer = results.value;
    String numberString = String(buffer);
    char charBuf[50];
    numberString.toCharArray(charBuf, 50);
 
    Serial.println(buffer);
 
    client.publish("IR2MQTT",  charBuf);
    irrecv.resume(); // Receive the next value
  }

  }

Monday 10 April 2017

Prototype Switch

This is a prototype switch I have started working on...

It was based on this guys work: http://www.echotwek.com/wp/2014/03/20/arduino-controlled-light-switch-v2

I had a Leviton Decora switch P/N 5601 from Home Depot lying around and I was going to try this mod, when I popped the rocker off with a flat blade screwdriver I noticed there were rivet holes drilled right through from behind the rocker all the way out the back cover and grounded.

I took 2 pieces of DuPont connector wire I had for my Arudino projects and hot glued one to the top half and one to the bottom half of the back side of the rocker plate.  I did not cut, or modify the switch in any way other than putting the wire through and adding some glue to the plastic paddle.

The final plan would be to replace the wire with something non conductive like fishing line and then make two tiny holes in  the back end of an electrical box and hot glue the servo to the outside back of the box.  This way the whole thing would be non-conductive should it break loose, and the low voltage servo and micro controller would not be in the same box as the high voltage switch... This also alleviates the problem of trying to cram a servo into a tiny electrical box.

I'm not 100% sure this is to code, but since all the modifications are non conductive and the LV stuff is outside the box it should be relatively safe.

I DO NOT RECOMMEND ANYONE TRY THIS AS IT IS PROBABLY AGAINST SOME KIND OF CODE AND TAKE NO RESPONSIBILITY IF YOUR HOUSE BURNS DOWN OR YOUR DIE!!!!

Here's a video of it in action:
https://www.youtube.com/watch?v=BXbQ4DRkqv0