Fabian Peter Hammerle před 11 roky
revize
e5c501cd3a
4 změnil soubory, kde provedl 131 přidání a 0 odebrání
  1. 56 0
      Arduino/Arduino.ino
  2. 4 0
      ardvindo-command
  3. 4 0
      ardvindo-server-start
  4. 67 0
      ardvindo-server.py

+ 56 - 0
Arduino/Arduino.ino

@@ -0,0 +1,56 @@
+#include <RCSwitch.h>
+#include <Streaming.h>
+
+RCSwitch rcswitch = RCSwitch();
+
+void setup() 
+{
+  Serial.begin(9600);
+  //timeout for Serial.find(), 
+  Serial.setTimeout(100);
+  
+  rcswitch.enableTransmit(4);
+}
+
+void loop() 
+{
+  if(Serial.available() > 0) 
+  {
+    // - 'i' info for identification
+    // - 'e' enable / switch on
+    // - 'd' disable / switch off
+    char command = Serial.read();
+    
+    if(command == 'i')
+    {
+      Serial.println("ardvindo");
+    }
+    else if(command == 'e' || command == 'd')
+    {
+      //wait for space and family
+      while(Serial.available() < 2)
+      {
+        delay(1);
+      }
+      
+      Serial.read(); //space
+      char family = Serial.read();
+      int group = Serial.parseInt();
+      int device = Serial.parseInt();
+
+      if(command == 'e')
+      {
+        rcswitch.switchOn(family, group, device);
+      }
+      else
+      {
+        rcswitch.switchOff(family, group, device);
+      }
+      
+      Serial << command << " " << family << " " << group << " " << device << "\n";
+    }
+    
+    // strip all further chars until end of line
+    Serial.find("\n");
+  }
+}

+ 4 - 0
ardvindo-command

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+echo -n "$@" >/dev/udp/localhost/61291
+

+ 4 - 0
ardvindo-server-start

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+/usr/bin/screen -dmS ardvindo-server /usr/local/bin/ardvindo-server.py
+

+ 67 - 0
ardvindo-server.py

@@ -0,0 +1,67 @@
+#!/usr/bin/python3
+
+import serial
+import serial.tools.list_ports
+import time
+import datetime
+import socket 
+import socketserver
+
+def log(msg):
+    print("[" + str(datetime.datetime.now()) + "] " + msg)
+
+class Ardvindo:
+
+    def __init__(self, comPort):
+        self.comPort = comPort
+        self.comConnection = None
+        self.connect()
+
+    def connected(self):
+        return self.comConnection != None and self.ping()
+
+    def reconnect(self):
+        while True:
+            if self.comConnection != None:
+                self.comConnection.close()
+            self.comConnection = serial.Serial(self.comPort, 9600, timeout = 0.5)
+            # establishing an serial connection automatically resets the arduino
+            # wait for arduino to restart
+            time.sleep(4)
+            if self._readline() == "ardvindo started":
+                log("Connection established!")
+                break
+            else:
+                log("Failed to connect!")
+
+    def connect(self):
+        if not self.connected():
+            self.reconnect()   
+
+    def ping(self):
+        return self.sendCommand("i") == "ardvindo"
+
+    def _readline(self):
+        line = self.comConnection.readline().decode().strip()
+        log("Received '" + line + "' !")
+        return line
+
+    def sendCommand(self, cmd):
+        log("Sending command '" + cmd + "' ...")
+        self.comConnection.write((cmd + "\n").encode())
+        return self._readline()
+
+ardvindo = Ardvindo(serial.tools.list_ports.comports()[0][0])
+
+class ServerRequestHandler(socketserver.BaseRequestHandler):
+    
+    def handle(self):
+        data = self.request[0].decode().strip()
+        ardvindo.sendCommand(data)
+
+server = socketserver.UDPServer(('', 61291), ServerRequestHandler)
+log("Server is started!")
+
+log("Waiting for commands... ")
+server.serve_forever()
+