Advertisement
yvonnezoe

pilldispense3.py

Mar 16th, 2014
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.02 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import cherrypy
  4.  
  5. class Control:
  6.    
  7.     @cherrypy.expose
  8.     def index(self):
  9.         return '''<!DOCTYPE html>
  10.         <html>
  11.             <head>
  12.                 <title>RaspiCare</title>
  13.  
  14.                 <meta name="viewport" content="width=device-width, initial-scale=1">
  15.  
  16.                 <link rel="stylesheet" href="static/jquery.mobile-1.4.0.min.css">
  17.                 <link rel="stylesheet" href="static/style.css">
  18.                 <script src="static/jquery-1.10.2.min.js"></script>
  19.                 <script src="static/jquery.mobile-1.4.0.min.js"></script>
  20.                
  21.                 <script>
  22.                     $(document).ready(function(){
  23.                         $('#dbmgmt').click(function(){
  24.                             window.location.href='static/dbmgmt.php';
  25.                         });
  26.                        
  27.                         $('#left_button').click(function(){
  28.                             $.post('/turnCamera', {direction:"left"}).done(function (reply) {
  29.                                 $('#camerapos').empty().append(reply);
  30.                                 alert("left button clicked");});
  31.                            
  32.                         });
  33.                         $('#right_button').click(function(){
  34.                             $.post('/turnCamera', {direction:"right"}).done(function (reply) {
  35.                                 $('#camerapos').empty().append(reply);
  36.                                 alert("right button clicked");});
  37.                         });
  38.                         $('#dispense').click(function(){
  39.                             $.post('/turnCamera', {direction:"dispense"}).done(function (reply) {
  40.                                 $('#camerapos').empty().append(reply);
  41.                                 alert("dispense button clicked");});
  42.                         });
  43.                         $("#light_1").change(function () {$.post('/switch',{key_pressed:"power_"+$(this).val()})});
  44.                         $('#photocell').click(function(){
  45.                             $.post('/photocell').done(function (reply) {
  46.                                 $('#photocellvalue').empty().append(reply);});
  47.                         });
  48.                         $('#FSR').click(function(){
  49.                             $.post('/FSR').done(function (reply) {
  50.                                 $('#photocellvalue').empty().append(reply);});
  51.                         });
  52.                     });
  53.                 </script>
  54.             </head>
  55.             <body style="overflow: scroll;overflow-x:hidden;">
  56.                            <div data-role="page" data-theme="a" id="page1">
  57.  
  58.                 <div data-role="header">
  59.                     <img src="static/raspicare_logo.png">
  60.                    
  61.                 </div><!-- /header -->
  62.  
  63.                 <div role="main" class="ui-content">
  64.                     <p>Welcome to the Homepage of RaspiCare. RaspiCare is a smart system that facilitates the daily routine of hospitals.</p>
  65.  
  66.                   <div id="control_button">
  67.                     <img src="static/button_left.png" id="left_button" height="50" width="50">
  68.                     <img src="static/button_right.png" id="right_button" height="50" width="50">
  69.                     <p id="camerapos"> test </p>
  70.                   </div>
  71.  
  72.                     <div class="ui-grid-b">
  73.                         <div class="ui-block-a" ><div class="ui-bar ui-bar-a" style="height:30px">Light Control</div>
  74.                             <form>
  75.                                 <label for="light_1">Light 1</label>
  76.                                 <select name="light_1" id="light_1" data-role="slider" data-track-theme="b" data-theme="b">
  77.                                     <option value="off">Off</option>
  78.                                     <option value="on">On</option>
  79.                                 </select>
  80.                                 <label for="light_2">Light 2</label>
  81.                                 <select name="light_2" id="light_2" data-role="slider" data-track-theme="b" data-theme="b">
  82.                                     <option value="off">Off</option>
  83.                                     <option value="on">On</option>
  84.                                 </select>
  85.                                 <label for="light_3">Light 3</label>
  86.                                 <select name="light_3" id="light_3" data-role="slider" data-track-theme="b" data-theme="b">
  87.                                     <option value="off">Off</option>
  88.                                     <option value="on">On</option>
  89.                                 </select>
  90.                             </form>
  91.                         </div>
  92.                         <div class="ui-block-b"><div class="ui-bar ui-bar-a" style="height:30px">Sensor</div>
  93.                                                    <a id="photocell"; href="#">Sensor Controlled Lighting</a>
  94.                                                    <p id="photocellvalue"></p>
  95.                                                    <a id="FSR"; href="#">FSR Controlled Lighting</a>
  96.                         </div>
  97.                         <div class="ui-block-c"><div class="ui-bar ui-bar-a" style="height:30px">Pill Dispenser</div>
  98.                             <div class="ui-input-btn ui-btn ui-btn-b">
  99.                                 Database Management
  100.                                 <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dbmgmt">
  101.                             </div>
  102.                             <div class="ui-input-btn ui-btn ui-btn-b">
  103.                                 Dispense Pill
  104.                                 <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dispense">
  105.                             </div>
  106.                         </div>
  107.                     </div><!-- /grid-b -->
  108.                    
  109.                 </div><!-- /content -->
  110.                
  111.                 <div data-role="footer">
  112.                     <p align="center"><small>NTU EEE 2013-2014</small></p>
  113.                 </div><!-- /footer -->
  114.             </div><!-- /page -->
  115.  
  116.             </body>
  117.         </html>
  118. '''
  119.     @cherrypy.expose
  120.     def turnCamera (self, **data):
  121.  
  122.         import pigpio
  123.         import time
  124.  
  125.         # import serial
  126.         # def servo_control(command):
  127.         # serialport= serial.Serial ("/dev/ttyACM0", 9600, timeout=0.5)
  128.         # serialport.write(command)
  129.         # return serialport.readlines(1)
  130.  
  131.         servos=[4,7] #GPIO number
  132.         pigpio.start()
  133.         key = data['direction']
  134. ##        if key=="left":
  135. ##                servostatus = "left"
  136. ##                print servostatus
  137. ##                m=1500
  138. ##                
  139. ##        elif key=="right":
  140. ##                m=1000
  141.  
  142.         #to dispense pill
  143.         if key=="dispense":
  144.             m=900
  145.             pigpio.set_servo_pulsewidth(servos[0], m)
  146.             servostat= "Position of servo: %s"%m
  147.             print servostat
  148.             time.sleep(1)
  149.             m=1500
  150.             pigpio.set_servo_pulsewidth(servos[0], m)
  151.             servostat= "Position of servo: %s"%m
  152.             print servostat
  153.             time.sleep(1)
  154.  
  155.  
  156.         pigpio.stop()
  157.  
  158.         return servostat
  159.  
  160.     @cherrypy.expose
  161.     def switch(self, **data):
  162.         import RPi.GPIO as GPIO #Import GPIO library
  163.         import time #Import 'time' library so that we can use 'sleep()'
  164.  
  165.         # Use physical board pin numbers
  166.         GPIO.setmode(GPIO.BOARD)
  167.         # Set up header pin 11 (GPIO17) as an output
  168.         print "Setup Pin 11"
  169.         GPIO.setup(11, GPIO.OUT)
  170.  
  171.         key = data['key_pressed']
  172.         if key=="power_on":
  173.             print "switch on light"
  174.             GPIO.output(11, True) #switch on
  175.             time.sleep(1)
  176.         elif key=="power_off":
  177.             print "switch off light"
  178.             GPIO.output(11, False) #switch off
  179.             time.sleep(1)
  180.            
  181.     @cherrypy.expose
  182.     def photocell(self):
  183.  
  184.         import RPi.GPIO as GPIO, time, os
  185.  
  186.         DEBUG = 1
  187.         GPIO.setmode(GPIO.BOARD)
  188.         GPIO.setup(11, GPIO.OUT)
  189.          
  190.         def RCtime (RCpin):
  191.             reading = 0
  192.             GPIO.setup(RCpin, GPIO.OUT)
  193.             GPIO.output(RCpin, GPIO.LOW)
  194.             time.sleep(1)
  195.          
  196.             GPIO.setup(RCpin, GPIO.IN)
  197.             # This takes about 1 millisecond per loop cycle
  198.             while (GPIO.input(RCpin) == GPIO.LOW):
  199.                 reading += 1
  200.             return reading
  201.  
  202.         while True:
  203.             sensorvalue= RCtime(12)
  204.  
  205.             if sensorvalue > 1000:
  206.                 print "switch on light"
  207.                 GPIO.output(11, True) #switch on
  208.                 lightstate ="On"
  209.                 time.sleep(1)
  210.             else:
  211.                 print "switch off light"
  212.                 GPIO.output(11, False) #switch off
  213.                 lightstate ="Off"
  214.                 time.sleep(1)
  215.  
  216.            
  217.             print sensorvalue # Read RC timing using BCM pin #18, physical pin 12
  218.  
  219.         return lightstate
  220.  
  221.     @cherrypy.expose
  222.     def FSR(self):
  223.  
  224.         import RPi.GPIO as GPIO, time, os
  225.  
  226.         DEBUG = 1
  227.         GPIO.setmode(GPIO.BOARD)
  228.         GPIO.setup(11, GPIO.OUT)
  229.          
  230.         def Force (RCpin):
  231.             reading = 0
  232.             GPIO.setup(RCpin, GPIO.OUT)
  233.             GPIO.output(RCpin, GPIO.LOW)
  234.             time.sleep(1)
  235.          
  236.             GPIO.setup(RCpin, GPIO.IN)
  237.             # This takes about 1 millisecond per loop cycle
  238.             while (GPIO.input(RCpin) == GPIO.LOW):
  239.                 reading += 1
  240.             return reading
  241.  
  242.         while True:
  243.             sensorvalue= Force(13)
  244.            
  245.             if sensorvalue < 100:
  246.                 print "switch on light"
  247.                 GPIO.output(11, True) #switch on
  248.                 lightstate ="On"
  249.                 time.sleep(1)
  250.             else:
  251.                 print "switch off light"
  252.                 GPIO.output(11, False) #switch off
  253.                 lightstate ="Off"
  254.                 time.sleep(1)
  255.                
  256.             print sensorvalue # Read RC timing using BCM pin #18, physical pin 12
  257.  
  258.         return lightstate
  259.    
  260.     #shutdown server
  261.     @cherrypy.expose
  262.     def shutdown(self):  
  263.         cherrypy.engine.exit()
  264.        
  265. import os.path
  266. tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
  267.  
  268. if __name__ == '__main__':
  269.     # CherryPy always starts with app.root when trying to map request URIs
  270.     # to objects, so we need to mount a request handler root. A request
  271.     # to '/' will be mapped to Comfort().index().
  272.     cherrypy.quickstart(Control(), config=tutconf)
  273. else:
  274.     # This branch is for the test suite; you can ignore it.
  275.     cherrypy.tree.mount(Control(), config=tutconf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement