changeset 5:4c44c80a6a72

move garage door opener from parport to arduino Ignore-this: c7d21f40841af0e64b26dd4991e441ae
author drewp@bigasterisk.com
date Sun, 14 Aug 2011 21:46:57 -0700
parents be855a111619
children 1444da5242d8
files service/garageArduino/garage/garage.pde service/garageArduino/garageArduino.py service/garageArduino/index.html
diffstat 3 files changed, 45 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/service/garageArduino/garage/garage.pde	Mon Aug 08 00:31:31 2011 -0700
+++ b/service/garageArduino/garage/garage.pde	Sun Aug 14 21:46:57 2011 -0700
@@ -8,6 +8,14 @@
   
   // the phototransistor on analog2 is jameco 2006414
 
+  pinMode(3, OUTPUT);
+  digitalWrite(3, LOW);
+  // this drives a relay for the garage door. There is a 
+  // LP filter on it so the garage doesn't open if there's 
+  // an arduino power-on glitch. It may be that atmel has 
+  // circuitry to prevent such glitches, so a pull-down
+  // resistor may be enough. I haven't checked carefully.
+
   Serial.begin(115200);
 }
 
@@ -61,6 +69,11 @@
       Serial.print("{\"threshold\":");
       Serial.print(threshold);
       Serial.print("}\n");
+    } else if (cmd == 0x04) {
+      digitalWrite(3, arg);
+      Serial.print("{\"garage\":");
+      Serial.print(arg ? "true" : "false");
+      Serial.print("}\n");     
     }
   }
 }
--- a/service/garageArduino/garageArduino.py	Mon Aug 08 00:31:31 2011 -0700
+++ b/service/garageArduino/garageArduino.py	Sun Aug 14 21:46:57 2011 -0700
@@ -5,19 +5,20 @@
 
 from __future__ import division
 
-import cyclone.web, json, traceback, os, sys, time
-from twisted.python import log
-from twisted.internet import reactor, task
+import cyclone.web, json, traceback, os, sys, time, logging
+from twisted.internet import reactor, task, defer
 from twisted.web.client import getPage
 sys.path.append("/my/proj/house/frontdoor")
 from loggingserial import LoggingSerial        
+sys.path.append("/my/proj/homeauto/lib")
+from cycloneerr import PrettyErrorHandler
+from logsetup import log
 sys.path.append("../../../room")
 from carbondata import CarbonClient
 sys.path.append("/my/site/magma")
 from stategraph import StateGraph      
 from rdflib import Namespace, RDF, Literal
-sys.path.append("/my/proj/homeauto/lib")
-from cycloneerr import PrettyErrorHandler
+
 
 ROOM = Namespace("http://projects.bigasterisk.com/room/")
 DEV = Namespace("http://projects.bigasterisk.com/device/")
@@ -46,6 +47,11 @@
         self.ser.write("\x60\x03"+chr(max(1 << 2, t) >> 2))
         return self.ser.readJson()['threshold']
 
+    def setGarage(self, level):
+        """set garage door opener pin"""
+        self.ser.write("\x60\x04"+chr(int(bool(level))))
+        return self.ser.readJson()['garage']
+
 
 class Index(PrettyErrorHandler, cyclone.web.RequestHandler):
     def get(self):
@@ -112,6 +118,19 @@
     def put(self):
         pass
 
+class GarageDoorOpen(PrettyErrorHandler, cyclone.web.RequestHandler):
+    def post(self):
+        self.set_header("Content-Type", "text/plain")
+        self.settings.arduino.setGarage(True)
+        self.write("pin high, waiting..\n")
+        self.flush()
+        d = defer.Deferred()
+        def finish():
+            self.settings.arduino.setGarage(False)            
+            self.write("pin low. Done")
+            d.callback(None)
+        reactor.callLater(1.5, finish) # this time depends on the LP circuit
+        return d
 
 class Application(cyclone.web.Application):
     def __init__(self, ard, poller):
@@ -122,6 +141,7 @@
             (r'/housePower', HousePower),
             (r'/housePower/raw', HousePowerRaw),
             (r'/housePower/threshold', HousePowerThreshold),
+            (r'/garageDoorOpen', GarageDoorOpen),
         ]
         settings = {"arduino" : ard, "poller" : poller}
         cyclone.web.Application.__init__(self, handlers, **settings)
@@ -220,7 +240,10 @@
         'boardName' : 'garage', # gets sent with updates
         }
 
-    #log.startLogging(sys.stdout)
+    #from twisted.python import log as twlog
+    #twlog.startLogging(sys.stdout)
+
+    log.setLevel(logging.DEBUG)
 
     ard = ArduinoGarage()
 
--- a/service/garageArduino/index.html	Mon Aug 08 00:31:31 2011 -0700
+++ b/service/garageArduino/index.html	Sun Aug 14 21:46:57 2011 -0700
@@ -19,6 +19,8 @@
 
     <p>Talking to an arduino uno on host <tt>slash</tt></p>
 
+    <p>Pulse pin 3 to trigger garage door opener <form method="post" action="garageDoorOpen"><input type="submit" value="Garage door opener"/></form></p>
+
     <p><a href="http://octopart.com/555-28027-parallax-708653">PIR sensor</a> (in <a href="http://octopart.com/1551ggy-hammond-15686">a box</a>) measuring front door motion: <span class="val" id="frontDoorMotion"/></p>
 
     <p><a href="http://www.jameco.com/webapp/wcs/stores/servlet/ProductDisplay?langId=-1&amp;productId=2006414&amp;catalogId=10001&amp;freeText=2006414&amp;app.products.maxperpage=15&amp;storeId=10001&amp;search_type=jamecoall&amp;ddkey=http:StoreCatalogDrillDownView">phototransistor</a> watching IR pulses on the power meter: last pulse was <span class="val" id="lastPulseAgo"/>; current power usage is <span class="val"><span id="currentWatts"/> watts</span> (assuming <span class="val" id="kwhPerBlink"/> kwh/blink)</p>
@@ -73,4 +75,4 @@
      // ]]>
     </script>
   </body>
-</html>
\ No newline at end of file
+</html>