Advertisement
Guest User

SeveurBottle

a guest
Jul 20th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.18 KB | None | 0 0
  1. #coding=utf8
  2.  
  3. import serial
  4. import os
  5. import threading
  6.  
  7. from bottle import Bottle
  8.  
  9. import subprocess
  10. from bottle import route, request, response, redirect, template, run
  11.  
  12. #os.system('gpio mode 0 out') #On
  13. #os.system("gpio write 0 1")
  14. os.system('gpio mode 0 in')
  15. os.system('gpio mode 7 in')
  16.  
  17.  
  18.  
  19. app = Bottle()
  20.  
  21.  
  22. #-------------------------
  23.  
  24. # On va lancer un thread qui lit la temperature a cote du serveur web
  25.  
  26. #senseurs = {}
  27.  
  28.  
  29. '''def connect_arduino():
  30.    for path in ('/dev/ttyACM0',
  31.                 '/dev/ttyACM1',
  32.                 '/dev/tty.usbserial-A8008KAi'):
  33.        try:
  34.            return serial.Serial(path, 9600)
  35.        except serial.serialutil.SerialException:
  36.            pass # Only accept that exception class
  37.    raise serial.serialutil.SerialException(
  38.        "could not open any predefined port")
  39.  
  40. global arduino
  41. arduino = connect_arduino()
  42.  
  43. def lance_thread(arduino):
  44.    print('lance_thread')
  45.    return
  46.    while True:
  47.        temperature = arduino.readline().strip()
  48.        senseurs['temperature'] = temperature
  49.        print('temperature', temperature)
  50.  
  51.  
  52. lecture_serial = threading.Thread(target=lance_thread, args=tuple(arduino))
  53. lecture_serial.start()
  54. '''
  55.  
  56. #@app.route('/api/temp')
  57. #def temp():
  58. #    return senseurs['temperature']
  59.  
  60. #------------------------
  61.  
  62. @app.route('/api/temp')
  63. def temp():
  64. #    sensor = ser.readline().strip()
  65.     sensor = "Arduino débranché !!!";
  66.     return sensor
  67.  
  68. @app.route("/")
  69. def index():
  70.     redirect("/hello")
  71.  
  72. @app.route('/hello')
  73. def hello():  
  74.     return template('template.html')
  75.  
  76.  
  77. @app.route('/api/relay1/on')
  78. def relay():
  79.     os.system("gpio mode 0 out")    
  80.     return "OK"
  81.  
  82. @app.route('/api/relay1/off')
  83. def relay():
  84.     os.system("gpio mode 0 in")    
  85.     return "OK"
  86.  
  87. @app.route('/api/relay2/on')
  88. def relay():
  89.     os.system("gpio mode 7 out")
  90.     return "OK"
  91.  
  92. @app.route('/api/relay2/off')
  93. def relay():
  94.     os.system("gpio mode 7 in")
  95.     return "OK"
  96.  
  97. @app.route('/api/relay/status')
  98. def relay_status():
  99. #    status = subprocess.check_output(["gpio", "read", "7"])
  100.  
  101.     data2 = subprocess.check_output(["gpio", "read", "7"])
  102.     coin = data2
  103.     if(coin == b'1\n'):
  104.         status = 'on'
  105.     elif(coin == b'0\n'):
  106.         status = 'off'
  107.     return status
  108.  
  109.  
  110. @app.route('/api/relay/status1')
  111. def relay_status1():
  112.  
  113.     data2 = subprocess.check_output(["gpio", "read", "0"])
  114.     coin = data2
  115.     if(coin == b'1\n'):
  116.         status = 'on'
  117.     elif(coin == b'0\n'):
  118.         status = 'off'
  119.     return status
  120.  
  121. @app.route('/api/relay/status2')
  122. def relay_status2():
  123.    
  124.     etat = open("etat.txt", "r")
  125.     status2 = etat.read()
  126.     return status2
  127.  
  128.  
  129. @app.route('/api/temp2')
  130. def temp():  
  131.     sensor2 = subprocess.check_output(["/opt/vc/bin/vcgencmd", "measure_temp"])
  132.     sensor2 = sensor2.decode(encoding='UTF-8')
  133.     sensor2 = sensor2.split("temp=")[1].split("'C")[0]
  134.     sensor2 = sensor2.split("temp=")[0].split("'C")[0]
  135.     return sensor2
  136.  
  137.  
  138. @app.route('/api/rideaux/ouvrir')
  139. def ouvrir():
  140.     os.system("python open.py")
  141.     return "OK"
  142.  
  143. @app.route('/api/rideaux/fermer')
  144. def fermer():
  145.     os.system("python close.py")
  146.     return "OK"
  147.  
  148. @app.route('/api/rideaux/stop')
  149. def fermer():
  150.     os.system("python stop.py")
  151.     return "OK"
  152. @app.route('/script.js')
  153. def index():
  154.     # rb pour lire en mode 'binaire' (nécessaire pour les images etc)
  155.     return open('script.js', 'rb').read()
  156.  
  157. @app.route('/jquery-2.1.0.min.js')
  158. def index():
  159.     # rb pour lire en mode 'binaire' (nécessaire pour les images etc)
  160.     return open('jquery-2.1.0.min.js', 'rb').read()
  161.  
  162. @app.route('/bootstrap.min.js')
  163. def index():
  164.     # rb pour lire en mode 'binaire' (nécessaire pour les images etc)
  165.     return open('bootstrap.min.js', 'rb').read()
  166.  
  167.  
  168. @app.route('/bootstrap-theme.min.css')
  169. def index():
  170.     # rb pour lire en mode 'binaire' (nécessaire pour les images etc)
  171.     return open('bootstrap-theme.min.css', 'rb').read()
  172.  
  173. @app.route('/bootstrap.min.css')
  174. def index():
  175.     # rb pour lire en mode 'binaire' (nécessaire pour les images etc)
  176.     return open('bootstrap.min.css', 'rb').read()
  177.  
  178. app.run(host='', port=8088, reloader=True, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement