sohotcall

reservoir-distance.py

Nov 25th, 2018
179
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import datetime
  3. import subprocess
  4. import sys
  5. import threading
  6. from rpi_ws281x import *
  7. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  8. import RPi.GPIO as GPIO
  9.  
  10. GPIO.setmode(GPIO.BCM)
  11.  
  12. #hostname = "107.174.13.179:81" #hiformance
  13. hostname = "93.188.163.1:81" #hostinger
  14.  
  15. LED_COUNT = 12
  16. LED_PIN = 18 #12
  17. LIGHT_PIN = 4
  18. TRIG1 = 17 #11
  19. ECHO1 = 27 #13
  20. TRIG2 = 23 #16
  21. ECHO2 = 24 #18
  22. FAST_PIN = 21 #40
  23. USBCAM_PIN = 13 #33
  24. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, 800000, 10, False, 255, 0)
  25. strip.begin()
  26.  
  27. GPIO.setup(FAST_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  28. GPIO.setup(USBCAM_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  29.  
  30.  
  31. fast   = not GPIO.input(FAST_PIN)   # active low
  32. usbcam = not GPIO.input(USBCAM_PIN) # active low
  33. sim    = 'sim' in sys.argv
  34.  
  35. def getSN():
  36.     cpuserial = "0000000000000000"
  37.     try:
  38.         f = open('/proc/cpuinfo','r')
  39.         for line in f:
  40.             if line[0:6]=='Serial':
  41.                 cpuserial = line[10:26]
  42.         f.close()
  43.     except:
  44.         cpuserial = "ERROR000000000"
  45.     return cpuserial
  46.  
  47. def ws2812b(count, r, g, b):
  48.     print("WS2812B Turning %d LEDs" % count)
  49.     for i in range(strip.numPixels()):
  50.         if i < count:
  51.             strip.setPixelColor(i, Color(r, g, b))
  52.         else:
  53.             strip.setPixelColor(i, Color(0, 0, 0))
  54.     strip.show()
  55.  
  56. ws2812b(LED_COUNT, 0, 0, 0)
  57.  
  58. GPIO.setup(TRIG1, GPIO.OUT)
  59. GPIO.setup(ECHO1, GPIO.IN)
  60. GPIO.setup(TRIG2, GPIO.OUT)
  61. GPIO.setup(ECHO2, GPIO.IN)
  62.  
  63. GPIO.setup(LIGHT_PIN, GPIO.OUT)
  64.  
  65. busy = False
  66. def getDistance(trig, echo): #2.5
  67.     global busy, fast, sim
  68.     while busy:
  69.         pass
  70.     busy = True
  71.     print("Measuring distance from (%d, %d)" % (trig, echo))
  72.     GPIO.output(trig, True)
  73.     time.sleep(0.00001)
  74.     GPIO.output(trig, False)
  75.     StartTime = time.time()
  76.     StopTime = time.time()
  77.     if sim:
  78.         time.sleep(1)
  79.         StopTime = time.time()
  80.     else:
  81.         while GPIO.input(echo) == 0 and time.time()-StopTime < 5:
  82.             StartTime = time.time()
  83.         while GPIO.input(echo) == 1 and time.time()-StartTime < 5:
  84.             StopTime = time.time()
  85.     TimeElapsed = StopTime - StartTime
  86.     distance = (TimeElapsed * 34300) / 2
  87.     print("Result: (cm)"+ str(distance))
  88.     time.sleep(2.5)
  89.     busy = False
  90.     return distance
  91.  
  92. def visualizeDistance(distance, r, g, b): #2.5
  93.     global busy, fast, sim
  94.     while busy:
  95.         pass
  96.     busy = True
  97.     print("Visualizing distance")
  98.     j = 0
  99.     for i in (0, 10, 15, 23, 34, 51, 76, 144, 171, 256, 384, 577):
  100.         if distance > i:
  101.             j = j + 1
  102.     for i in (1,2,3,4,5,6,7,8,9,10,11,12):
  103.         if j>=i:
  104.             ws2812b(i, r, g, b)
  105.             time.sleep(0.1)
  106.     time.sleep(0.1)
  107.     for i in (12,11,10,9,8,7,6,5,4,3,2,1):
  108.         if j>=i:
  109.             ws2812b(i, r, g, b)
  110.             time.sleep(0.1)
  111.     ws2812b(0,0,0,0)
  112.     busy = False
  113.  
  114. def capturePhoto():#5
  115.     global busy, fast, sim
  116.     while busy:
  117.         pass
  118.     busy = True
  119.     print("Capturing photo to /tmp/capture.jpg")
  120.     ws2812b(LED_COUNT, 255, 255, 255)
  121.     GPIO.output(LIGHT_PIN, True)
  122.     time.sleep(2.5)
  123.     if sim:
  124.         subprocess.call(['cp', '/home/pi/capture.jpg', '/tmp/capture.jpg'])
  125.     elif usbcam:
  126.         subprocess.call(['fswebcam', '-S', '10', '-r', '640x480',  '/tmp/capture.jpg'])
  127.     else:
  128.         subprocess.call(['raspistill', '-w', '640', '-h', '480', '-n', '-t', '100', '-q', '10', '-e', 'jpg', '-th', 'none', '-o',
  129.             '/tmp/capture.jpg'])
  130.     time.sleep(2.5)
  131.     ws2812b(0, 0, 0, 0)
  132.     GPIO.output(LIGHT_PIN, False)
  133.     print("Captured")
  134.     busy = False
  135.  
  136. class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
  137.     def do_GET(self):
  138.         self.send_response(200)
  139.         self.send_header('Access-Control-Allow-Origin','*')
  140.         if 'photo' in self.path:
  141.             self.send_header('Content-type', 'image/jpeg')
  142.             self.end_headers()
  143.             capturePhoto() #5
  144.             f = open('/tmp/capture.jpg', 'rb')
  145.             self.wfile.write(f.read())
  146.             f.close()
  147.         elif 'distance' in self.path:
  148.             self.send_header('Content-type', 'text/html')
  149.             self.end_headers()
  150.             distance1 = getDistance(TRIG1, ECHO1) #2.5
  151.             visualizeDistance(distance1, 255, 0, 0) #2.5
  152.             distance2 = getDistance(TRIG2, ECHO2) #2.5
  153.             visualizeDistance(distance2, 0, 255, 0) #2.5
  154.             self.wfile.write(b'Sensor1:' + bytes(str(distance1)) + ' Sensor2:' + bytes(str(distance2)))
  155.         elif 'ok' in self.path:
  156.             self.send_header('Content-type', 'text/html')
  157.             self.end_headers()
  158.             self.wfile.write(b'OK')
  159.         else:
  160.             self.send_header('Content-type', 'text/html')
  161.             self.end_headers()
  162.             self.wfile.write(b'<a href=?photo>Photo</a> <a href=?distance>Distance</a>')
  163. httpd = HTTPServer(('', 8123), SimpleHTTPRequestHandler)
  164. print("Server is running on 8123")
  165. #httpd.serve_forever()
  166. myThread = threading.Thread(target=httpd.serve_forever)
  167. myThread.daemon = True
  168. myThread.start()
  169. print("Move on")
  170.  
  171. if not fast:
  172.     time.sleep(60)
  173.  
  174. #$ raspistill -t 3000 -o test.jpg
  175. #$ raspistill -w 640 -h 480 -n -t 100 -q 10 -e jpg -th none -o vgasize10nothumb.jpg
  176. #$ curl -F "photo=@/tmp/capture.jpg" http://107.174.13.179:81/reservoir.php
  177. #datetime.datetime.now().isoformat() '2018-09-23T11:33:34.325805'
  178. curl = ['curl', '-F', 'reservoir=' + str(getSN())]
  179. ws2812b(0, 0, 0, 0)
  180. while True:
  181.     result = [0,0,0,0,0]
  182.     for _ in range(5 if not fast else 3): #5 times, each is 12 min
  183.         for i in range(5 if not fast else 3): #25s
  184.             distance = getDistance(TRIG1, ECHO1) # 2.5
  185.             visualizeDistance(distance, 255, 0, 0) # 2.5
  186.             result[i] = distance
  187.         result.sort()
  188.         curl = curl + ['-F', 'hcsr04_0[' + datetime.datetime.now().isoformat() + ']=' + str(result[2])]
  189.         for i in range(5 if not fast else 3): #25s
  190.             distance = getDistance(TRIG2, ECHO2) # 2.5
  191.             visualizeDistance(distance, 0, 255, 0) # 2.5
  192.             result[i] = distance
  193.         result.sort()
  194.         curl = curl + ['-F', 'hcsr04_1[' + datetime.datetime.now().isoformat() + ']=' + str(result[2])]
  195.         time.sleep(10)
  196.         if not fast:
  197.             time.sleep(12 * 60 - 60 - 10)
  198.     capturePhoto() # 5
  199.     curl = curl + ['-F', 'photodate='+datetime.datetime.now().isoformat()]
  200.     curl = curl + ['-F', 'photo=@/tmp/capture.jpg']
  201.     curl = curl + ["http://" + hostname + "/reservoir.php"]
  202.     print("Sending results to website")
  203.     print(str(curl))
  204.     if not sim:
  205.         subprocess.call(curl)
  206.     curl = ['curl', '-F', 'reservoir=' + str(getSN())]
RAW Paste Data