Advertisement
Guest User

Untitled

a guest
Mar 15th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 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('/sensor').done(function (reply) {
  46. $('#photocellvalue').empty().append(reply);});
  47. });
  48. });
  49. </script>
  50. </head>
  51. <body style="overflow: scroll;overflow-x:hidden;">
  52. <div data-role="page" data-theme="a" id="page1">
  53.  
  54. <div data-role="header">
  55. <img src="static/raspicare_logo.png">
  56.  
  57. </div><!-- /header -->
  58.  
  59. <div role="main" class="ui-content">
  60. <p>Welcome to the Homepage of RaspiCare. RaspiCare is a smart system that facilitates the daily routine of hospitals.</p>
  61.  
  62. <div id="control_button">
  63. <img src="static/button_left.png" id="left_button" height="50" width="50">
  64. <img src="static/button_right.png" id="right_button" height="50" width="50">
  65. <p id="camerapos"> test </p>
  66. </div>
  67.  
  68. <div class="ui-grid-b">
  69. <div class="ui-block-a" ><div class="ui-bar ui-bar-a" style="height:30px">Light Control</div>
  70. <form>
  71. <label for="light_1">Light 1</label>
  72. <select name="light_1" id="light_1" data-role="slider" data-track-theme="b" data-theme="b">
  73. <option value="off">Off</option>
  74. <option value="on">On</option>
  75. </select>
  76. <label for="light_2">Light 2</label>
  77. <select name="light_2" id="light_2" data-role="slider" data-track-theme="b" data-theme="b">
  78. <option value="off">Off</option>
  79. <option value="on">On</option>
  80. </select>
  81. <label for="light_3">Light 3</label>
  82. <select name="light_3" id="light_3" data-role="slider" data-track-theme="b" data-theme="b">
  83. <option value="off">Off</option>
  84. <option value="on">On</option>
  85. </select>
  86. </form>
  87. </div>
  88. <div class="ui-block-b"><div class="ui-bar ui-bar-a" style="height:30px">Sensor</div>
  89. <a id="photocell"; href="#">Sensor Controlled Lighting</a>
  90. <p id="photocellvalue"></p>
  91. </div>
  92. <div class="ui-block-c"><div class="ui-bar ui-bar-a" style="height:30px">Pill Dispenser</div>
  93. <div class="ui-input-btn ui-btn ui-btn-b">
  94. Database Management
  95. <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dbmgmt">
  96. </div>
  97. <div class="ui-input-btn ui-btn ui-btn-b">
  98. Dispense Pill
  99. <input data-enhanced="true" value="Enhanced - Theme swatch B" type="button" id="dispense">
  100. </div>
  101. </div>
  102. </div><!-- /grid-b -->
  103.  
  104. </div><!-- /content -->
  105.  
  106. <div data-role="footer">
  107. <p align="center"><small>NTU EEE 2013-2014</small></p>
  108. </div><!-- /footer -->
  109. </div><!-- /page -->
  110.  
  111. </body>
  112. </html>
  113. '''
  114. @cherrypy.expose
  115. def turnCamera (self, **data):
  116.  
  117. import pigpio
  118. import time
  119.  
  120. # import serial
  121. # def servo_control(command):
  122. # serialport= serial.Serial ("/dev/ttyACM0", 9600, timeout=0.5)
  123. # serialport.write(command)
  124. # return serialport.readlines(1)
  125.  
  126. servos=[4,7] #GPIO number
  127. pigpio.start()
  128. key = data['direction']
  129. ## if key=="left":
  130. ## servostatus = "left"
  131. ## print servostatus
  132. ## m=1500
  133. ##
  134. ## elif key=="right":
  135. ## m=1000
  136.  
  137. #to dispense pill
  138. if key=="dispense":
  139. m=900
  140. pigpio.set_servo_pulsewidth(servos[0], m)
  141. servostat= "Position of servo: %s"%m
  142. print servostat
  143. time.sleep(1)
  144. m=1500
  145. pigpio.set_servo_pulsewidth(servos[0], m)
  146. servostat= "Position of servo: %s"%m
  147. print servostat
  148. time.sleep(1)
  149.  
  150.  
  151. pigpio.stop()
  152.  
  153. return servostat
  154.  
  155. @cherrypy.expose
  156. def switch(self, **data):
  157. import RPi.GPIO as GPIO #Import GPIO library
  158. import time #Import 'time' library so that we can use 'sleep()'
  159.  
  160. # Use physical board pin numbers
  161. GPIO.setmode(GPIO.BOARD)
  162. # Set up header pin 11 (GPIO17) as an output
  163. print "Setup Pin 11"
  164. GPIO.setup(11, GPIO.OUT)
  165.  
  166. key = data['key_pressed']
  167. if key=="power_on":
  168. print "switch on light"
  169. GPIO.output(11, True) #switch on
  170. time.sleep(1)
  171. elif key=="power_off":
  172. print "switch off light"
  173. GPIO.output(11, False) #switch off
  174. time.sleep(1)
  175.  
  176. @cherrypy.expose
  177. def sensor(self):
  178.  
  179. import RPi.GPIO as GPIO, time, os
  180.  
  181. DEBUG = 1
  182. GPIO.setmode(GPIO.BOARD)
  183. GPIO.setup(11, GPIO.OUT)
  184.  
  185. def RCtime (RCpin):
  186. reading = 0
  187. GPIO.setup(RCpin, GPIO.OUT)
  188. GPIO.output(RCpin, GPIO.LOW)
  189. time.sleep(1)
  190.  
  191. GPIO.setup(RCpin, GPIO.IN)
  192. # This takes about 1 millisecond per loop cycle
  193. while (GPIO.input(RCpin) == GPIO.LOW):
  194. reading += 1
  195. return reading
  196.  
  197. while True:
  198. sensorvalue= RCtime(12)
  199.  
  200. if sensorvalue > 1000:
  201. print "switch on light"
  202. GPIO.output(11, True) #switch on
  203. lightstate ="On"
  204. time.sleep(1)
  205. else:
  206. print "switch off light"
  207. GPIO.output(11, False) #switch off
  208. lightstate ="Off"
  209. time.sleep(1)
  210.  
  211. print sensorvalue # Read RC timing using BCM pin #18, physical pin 12
  212.  
  213. return lightstate
  214.  
  215. #shutdown server
  216. @cherrypy.expose
  217. def shutdown(self):
  218. cherrypy.engine.exit()
  219.  
  220. import os.path
  221. tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
  222.  
  223. if __name__ == '__main__':
  224. # CherryPy always starts with app.root when trying to map request URIs
  225. # to objects, so we need to mount a request handler root. A request
  226. # to '/' will be mapped to Comfort().index().
  227. cherrypy.quickstart(Control(), config=tutconf)
  228. else:
  229. # This branch is for the test suite; you can ignore it.
  230. cherrypy.tree.mount(Control(), config=tutconf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement