Skip to content

Instantly share code, notes, and snippets.

@tdgunes
Last active March 28, 2019 22:02
Show Gist options
  • Save tdgunes/71192db38010b6ee6868867063f6335c to your computer and use it in GitHub Desktop.
Save tdgunes/71192db38010b6ee6868867063f6335c to your computer and use it in GitHub Desktop.
TRUST AC3-1000R Arduino Control
#include <NewRemoteTransmitter.h>
static unsigned long ADDRESS = 17503242; // TRUST AC3-1000R
static unsigned short DATA_PIN = 11; // Arduino 443Mhz transmitter's Data pin
static unsigned int PULSE_RATE = 250; // Pulse rate of the protocol
// Receives input switch_id: 0,1,2 state: 0,1 for (on-off)
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
unsigned int device = command.substring(0, 1).toInt();
boolean onoff = false;
if (command.substring(2, 3).toInt() == 0) {
onoff = false;
} else {
onoff = true;
}
NewRemoteTransmitter transmitter(ADDRESS, DATA_PIN, PULSE_RATE);
transmitter.sendUnit(device, onoff);
Serial.print(device);
Serial.print(" ");
Serial.print(onoff);
Serial.println(" Message sent!");
}
}
'use strict';
var exec = require('child_process').exec;
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-okan-light', 'OkanLight', OkanAccessory);
};
function OkanAccessory(log, config) {
let device = config.device;
var command = config['command'] || "python3 /home/pi/light.py " + device + " ";
var platform = this;
this.log = log;
this.name = config.name;
this.service = new Service[config.service || 'Outlet'](this.name);
this.service.getCharacteristic(Characteristic.On)
.on('set', function(value, callback) {
var switchOn = value ? true : false
platform.log(config.name, "switch -> " + switchOn);
var onoff = switchOn ? 1 : 0;
var newCommand = command + onoff;
platform.log(newCommand);
exec(
newCommand,
{
encoding: 'utf8',
timeout: 10000,
maxBuffer: 200*1024,
killSignal: 'SIGTERM',
cwd: null,
env: null
},
function (err, stdout, stderr) {
});
callback();
});
};
OkanAccessory.prototype.getServices = function() {
return [this.service];
};
#!/usr/bin/env python3
# from raspberry pi to arduino through serial
import argparse
import serial
parser = argparse.ArgumentParser()
parser.add_argument("device", type=int,
help="plug number is between 0-2.")
parser.add_argument("onoff", type=int,
help="on=1, off=0")
port = '/dev/ttyACM0'
rate = 9600
args = parser.parse_args()
device = args.device
onoff = args.onoff
s = serial.Serial(port, rate)
time.sleep(2) # waiting for arduino to wake up
# FIXME: hardware solution to fix: https://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection/
# s.flushInput()
print('Given args:', device, onoff)
s.write('{} {}\n'.format(device, onoff).encode('ascii'))
s.close()
{
"name": "homebridge-okan-light",
"version": "1.0.0",
"description": "Okan Light",
"author": "Taha Dogan Gunes",
"license": "MIT",
"repository": {},
"keywords": [
"homebridge-plugin",
"homebridge",
"garage",
"garagedoor",
"home"
],
"engines": {
"node": ">=6.4.0",
"homebridge": ">=0.3.0"
},
"dependencies": {},
"devDependencies": {},
"scripts": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment