Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| efinf:blc2016:bu1:projekt4:projekt [2017/03/16 14:35] – [Code] noel_ottiger | efinf:blc2016:bu1:projekt4:projekt [2017/03/17 08:01] (current) – [Arduino Code] noel_ottiger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{backlinks> | ||
| + | ====== Stromsteuerung mit Arduino und Relay ===== | ||
| + | ==== Idee und Fernziel ==== | ||
| + | Bis anhin waren die externen (Backup-)Festplatten 24h pro Tag an den Server angebunden obwohl sie nur wenige Minuten pro Tag genutzt wurden. Durch automatisches ein- und ausschalten sollte der Stromverbrauch verringert werden. | ||
| + | |||
| + | Die Stromversorgung wird von einer Arduino/ | ||
| + | ==== Aktueller Stand ==== | ||
| + | Nach Ende der Woche was der Prototyp mehr oder weniger Funktionsfähig. Die Stromsteuerung und Kommunikation zwischen Server und Arduino bzw. Relay funktioniert einigermassen Zuverlässig, | ||
| + | |||
| + | Da das Relay Netzspannung regelt wurde es in ein 3D gedrucktes Gehäuse verpackt, um Kontakt zu vermeiden. | ||
| + | |||
| + | ===== Bericht ===== | ||
| + | ==== Material ==== | ||
| + | * Solid State Relay (z.B. [[https:// | ||
| + | * Arduino Uno | ||
| + | * 3 LEDs zum Debuggen | ||
| + | * 3 220Ω Widerstände für LEDS | ||
| + | * Div. Kabel | ||
| + | ==== Aufbau ==== | ||
| + | Schema mit Relay (gestrichelte Linien sind Netzspannung [230V]) | ||
| + | |||
| + | {{ : | ||
| + | ==== Arduino Code ==== | ||
| + | {{ : | ||
| + | |||
| + | <hidden Code> | ||
| + | <code c++> | ||
| + | void setup() { | ||
| + | // Serial dialect | ||
| + | Serial.begin(115200); | ||
| + | |||
| + | // Status LEDs | ||
| + | pinMode(3, OUTPUT); | ||
| + | pinMode(5, OUTPUT); | ||
| + | pinMode(6, OUTPUT); | ||
| + | pinMode(10, OUTPUT); | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | // Check for input | ||
| + | check_input(); | ||
| + | } | ||
| + | |||
| + | void check_input() { | ||
| + | if (Serial.available() > 0) { | ||
| + | if (Serial.read() == ' | ||
| + | // Device unmounted | ||
| + | // Cut power | ||
| + | if (cut_power() == 1) { | ||
| + | // Successful | ||
| + | light_pin(3); | ||
| + | delay(1000); | ||
| + | dark_pin(3); | ||
| + | } else { | ||
| + | error(); | ||
| + | } | ||
| + | } else { | ||
| + | // Wanting device | ||
| + | // Give power | ||
| + | if (give_power() == 1) { | ||
| + | // Successful | ||
| + | light_pin(6); | ||
| + | delay(1000); | ||
| + | dark_pin(6); | ||
| + | } else { | ||
| + | error(); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | // Execution delay | ||
| + | delay(100); | ||
| + | } | ||
| + | |||
| + | int cut_power() { | ||
| + | // Deactivate Relay => 0V | ||
| + | digitalWrite(10, | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | int give_power() { | ||
| + | // Activate Relay => 5V | ||
| + | digitalWrite(10, | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | void light_pin(int pin) { | ||
| + | // Turn pin on | ||
| + | digitalWrite(pin, | ||
| + | } | ||
| + | |||
| + | void dark_pin(int pin) { | ||
| + | // Turn pin off | ||
| + | digitalWrite(pin, | ||
| + | } | ||
| + | |||
| + | void error() { | ||
| + | // Unknown command received | ||
| + | light_pin(5); | ||
| + | delay(1000); | ||
| + | dark_pin(5); | ||
| + | } | ||
| + | |||
| + | // 42 | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | ==== Python Code (Server-Side) ==== | ||
| + | |||
| + | Verwendete Libraries | ||
| + | * Pyudev - Serien Schnittstellenüberwachung (erkennt, wenn Festplatte hochgefahren ist und per USB " | ||
| + | * Python Serial - Kommunikation mit Arduino (Standart Library) | ||
| + | * Python Glib - Laufzeit Library für Loops (Standart Library) | ||
| + | |||
| + | |||
| + | {{ : | ||
| + | |||
| + | <hidden Code> | ||
| + | <code python> | ||
| + | # Imports | ||
| + | import serial, time, glib, os | ||
| + | from pyudev import Context, Monitor | ||
| + | from pyudev.glib import MonitorObserver | ||
| + | |||
| + | # Variables | ||
| + | dialect = 115200 | ||
| + | timeout = 5 | ||
| + | |||
| + | # Functions | ||
| + | def send_bit(bit): | ||
| + | # Send bit to ardunio | ||
| + | arduino = serial.Serial('/ | ||
| + | time.sleep(2) | ||
| + | arduino.write(bit) | ||
| + | |||
| + | def mount(dev_id): | ||
| + | os.system(" | ||
| + | return True | ||
| + | |||
| + | def unmount(dev_id): | ||
| + | os.system(" | ||
| + | return True | ||
| + | |||
| + | # Userinput | ||
| + | do = raw_input(" | ||
| + | |||
| + | if do == ' | ||
| + | send_bit(' | ||
| + | action = ' | ||
| + | elif do == ' | ||
| + | send_bit(' | ||
| + | action = ' | ||
| + | |||
| + | print " | ||
| + | print " | ||
| + | time.sleep(timeout) | ||
| + | |||
| + | def device_event(observer, | ||
| + | if device.action == ' | ||
| + | print " | ||
| + | print " | ||
| + | mount(device.get(' | ||
| + | exit() | ||
| + | if device.action == ' | ||
| + | unmount(device.get(' | ||
| + | print " | ||
| + | exit() | ||
| + | |||
| + | context = Context() | ||
| + | monitor = Monitor.from_netlink(context) | ||
| + | |||
| + | monitor.filter_by(subsystem=' | ||
| + | observer = MonitorObserver(monitor) | ||
| + | |||
| + | observer.connect(' | ||
| + | monitor.start() | ||
| + | |||
| + | glib.MainLoop().run() | ||
| + | </ | ||
| + | Da das Einbinden unter Windows und Linux unterschiedlich geregelt wird, wird die Einbindung in externen Scripten geregelt. Auf Linux mit mount.sh, unter Windows wäre das wohl ein Batch Script. | ||
| + | </ | ||
| + | ==== Fazit ==== | ||
| + | * Vieles über die Verwendungszwecke von Arduinos gelernt | ||
| + | * Grundlagen von Elektronik mit Relays | ||
| + | * Bei Problemen Austausch mit Mitschülern | ||
| + | ==== Fotos und Film ==== | ||
| + | [[https:// | ||
| + | |||
| + | Modell in Fusion 360 | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Modell des Gehäuses in Cura (3D Slicer) | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Drucker bei der Arbeit | ||
| + | |||
| + | {{: | ||
| + | |||