From 988c2e778dd1c6b21d33d099bcd856b4354577f1 Mon Sep 17 00:00:00 2001 From: NilsGrunwald Date: Sat, 6 Mar 2021 13:39:13 +0100 Subject: [PATCH] Initial check-in --- App/GetState.bat | 3 + App/MasterOff.bat | 3 + App/MasterOn.bat | 3 + App/USBCaseControl.py | 33 +++++++++++ Firmware/RelayBoardOnMicro.ino | 100 +++++++++++++++++++++++++++++++++ readme.md | 10 ++++ 6 files changed, 152 insertions(+) create mode 100644 App/GetState.bat create mode 100644 App/MasterOff.bat create mode 100644 App/MasterOn.bat create mode 100644 App/USBCaseControl.py create mode 100644 Firmware/RelayBoardOnMicro.ino create mode 100644 readme.md diff --git a/App/GetState.bat b/App/GetState.bat new file mode 100644 index 0000000..8ed3394 --- /dev/null +++ b/App/GetState.bat @@ -0,0 +1,3 @@ +@echo off +.\USBCaseControl.py -c "GETSTATE" +pause \ No newline at end of file diff --git a/App/MasterOff.bat b/App/MasterOff.bat new file mode 100644 index 0000000..7bbee69 --- /dev/null +++ b/App/MasterOff.bat @@ -0,0 +1,3 @@ +@echo off +.\USBCaseControl.py -c "MASTER=OFF" +pause \ No newline at end of file diff --git a/App/MasterOn.bat b/App/MasterOn.bat new file mode 100644 index 0000000..91d76e8 --- /dev/null +++ b/App/MasterOn.bat @@ -0,0 +1,3 @@ +@echo off +.\USBCaseControl.py -c "MASTER=ON" +pause \ No newline at end of file diff --git a/App/USBCaseControl.py b/App/USBCaseControl.py new file mode 100644 index 0000000..9c08fe9 --- /dev/null +++ b/App/USBCaseControl.py @@ -0,0 +1,33 @@ +import serial.tools.list_ports +import sys, getopt +import time + + +def main(argv): + cmd = 'GETSTATE' + print("USBCaseControl starting up...") + try: + opts, args = getopt.getopt(argv,"hc:",["cmd="]) + except getopt.GetoptError as err: + print(err) + sys.exit(2) + for opt, arg in opts: + if opt == '-h': + print("USBCaseControl.py --cmd ") + sys.exit() + elif opt in ("-c", "--cmd"): + cmd = arg + ports = serial.tools.list_ports.comports() + for port, desc, hwid in sorted(ports): + if "Arduino Leonardo" in desc: + print("{}: {} [{}]".format(port, desc, hwid)) + finalport=port + ser = serial.Serial(finalport, 9600, timeout=0,parity=serial.PARITY_EVEN, rtscts=1) + print("Calling command: {} on Port {}".format(cmd,port)) + ser.write(cmd.encode('utf_8')) + time.sleep(2) + out = ser.read(100) + print("Command return: {}".format(out.decode(encoding="utf_8", errors="ignore"))) + +if __name__ == "__main__": + main(sys.argv[1:]) \ No newline at end of file diff --git a/Firmware/RelayBoardOnMicro.ino b/Firmware/RelayBoardOnMicro.ino new file mode 100644 index 0000000..0ed5cef --- /dev/null +++ b/Firmware/RelayBoardOnMicro.ino @@ -0,0 +1,100 @@ +/* + Serial Call and Response in ASCII + Language: Wiring/Arduino + + This program sends an ASCII A (byte of value 65) on startup and repeats that + until it gets some data in. Then it waits for a byte in the serial port, and + sends three ASCII-encoded, comma-separated sensor values, truncated by a + linefeed and carriage return, whenever it gets a byte in. + + The circuit: + - potentiometers attached to analog inputs 0 and 1 + - pushbutton attached to digital I/O 2 + + created 26 Sep 2005 + by Tom Igoe + modified 24 Apr 2012 + by Tom Igoe and Scott Fitzgerald + Thanks to Greg Shakar and Scott Fitzgerald for the improvements + + This example code is in the public domain. + + http://www.arduino.cc/en/Tutorial/SerialCallResponseASCII +*/ + +#define MASTER 6 +#define CIRCUIT1 7 +#define CIRCUIT2 8 +#define CIRCUIT3 9 + +String inString; +int MasterState = 0; +int Circuit1State = 0; +int Circuit2State = 0; +int Circuit3State = 0; + +void setup() { + // start serial port at 9600 bps and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + + pinMode(MASTER, OUTPUT); + pinMode(CIRCUIT1, OUTPUT); + pinMode(CIRCUIT2, OUTPUT); + pinMode(CIRCUIT3, OUTPUT); + + digitalWrite(MASTER, HIGH); + digitalWrite(CIRCUIT1, HIGH); + digitalWrite(CIRCUIT2, HIGH); + digitalWrite(CIRCUIT3, HIGH); +} + +int setState(String cmd, String ref,int pin,int state,int store){ + if (cmd.indexOf(ref) >= 0){ + digitalWrite(pin,state); + Serial.print("Pin "); + Serial.print(pin); + Serial.print(" was set to: "); + Serial.println(state); + return !state; + } + return store; +} + +void loop() { + // if we get a valid byte, read analog ins: + + if (Serial.available() > 0) { + // get incoming byte: + inString = Serial.readString(); + Serial.print("Command received: "); + Serial.println(inString); + + MasterState = setState(inString,"MASTER=ON",MASTER,LOW,MasterState); + MasterState = setState(inString,"MASTER=OFF",MASTER,HIGH,MasterState); + + Circuit1State = setState(inString,"CIRCUIT1=ON",CIRCUIT1,LOW,Circuit1State); + Circuit1State = setState(inString,"CIRCUIT1=OFF",CIRCUIT1,HIGH,Circuit1State); + + Circuit2State = setState(inString,"CIRCUIT2=ON",CIRCUIT2,LOW,Circuit2State); + Circuit2State = setState(inString,"CIRCUIT2=OFF",CIRCUIT2,HIGH,Circuit2State); + + Circuit3State = setState(inString,"CIRCUIT3=ON",CIRCUIT3,LOW,Circuit3State); + Circuit3State = setState(inString,"CIRCUIT3=OFF",CIRCUIT3,HIGH,Circuit3State); + + if (inString.indexOf("GETSTATE") >= 0){ + Serial.print("MASTER="); + Serial.println(MasterState); + Serial.print("CIRCUIT1="); + Serial.println(Circuit1State); + Serial.print("CIRCUIT2="); + Serial.println(Circuit2State); + Serial.print("CIRCUIT3="); + Serial.println(Circuit3State); + } + } + +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..f749e79 --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ +# USB Base Control + +This is a small project to enable the switch of several relays on a 4 relay board connected to an Arduino Board which is connected to a PC via USB cable. +So switch the different relays and the master power a python script is provided aswell. + +## App +Contains the pyhton script and a couple of batch files to be used under windows. + +## Firmware +Contains the Arduino IDE sketch to switch the relays based on TTY over USB cable connection. \ No newline at end of file