Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import datetime
- import subprocess
- import sys
- import threading
- from rpi_ws281x import *
- from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BCM)
- #hostname = "107.174.13.179:81" #hiformance
- hostname = "93.188.163.1:81" #hostinger
- LED_COUNT = 12
- LED_PIN = 18 #12
- LIGHT_PIN = 4
- TRIG1 = 17 #11
- ECHO1 = 27 #13
- TRIG2 = 23 #16
- ECHO2 = 24 #18
- FAST_PIN = 21 #40
- USBCAM_PIN = 13 #33
- strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, 800000, 10, False, 255, 0)
- strip.begin()
- GPIO.setup(FAST_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- GPIO.setup(USBCAM_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- fast = not GPIO.input(FAST_PIN) # active low
- usbcam = not GPIO.input(USBCAM_PIN) # active low
- sim = 'sim' in sys.argv
- def getSN():
- cpuserial = "0000000000000000"
- try:
- f = open('/proc/cpuinfo','r')
- for line in f:
- if line[0:6]=='Serial':
- cpuserial = line[10:26]
- f.close()
- except:
- cpuserial = "ERROR000000000"
- return cpuserial
- def ws2812b(count, r, g, b):
- print("WS2812B Turning %d LEDs" % count)
- for i in range(strip.numPixels()):
- if i < count:
- strip.setPixelColor(i, Color(r, g, b))
- else:
- strip.setPixelColor(i, Color(0, 0, 0))
- strip.show()
- ws2812b(LED_COUNT, 0, 0, 0)
- GPIO.setup(TRIG1, GPIO.OUT)
- GPIO.setup(ECHO1, GPIO.IN)
- GPIO.setup(TRIG2, GPIO.OUT)
- GPIO.setup(ECHO2, GPIO.IN)
- GPIO.setup(LIGHT_PIN, GPIO.OUT)
- busy = False
- def getDistance(trig, echo): #2.5
- global busy, fast, sim
- while busy:
- pass
- busy = True
- print("Measuring distance from (%d, %d)" % (trig, echo))
- GPIO.output(trig, True)
- time.sleep(0.00001)
- GPIO.output(trig, False)
- StartTime = time.time()
- StopTime = time.time()
- if sim:
- time.sleep(1)
- StopTime = time.time()
- else:
- while GPIO.input(echo) == 0 and time.time()-StopTime < 5:
- StartTime = time.time()
- while GPIO.input(echo) == 1 and time.time()-StartTime < 5:
- StopTime = time.time()
- TimeElapsed = StopTime - StartTime
- distance = (TimeElapsed * 34300) / 2
- print("Result: (cm)"+ str(distance))
- time.sleep(2.5)
- busy = False
- return distance
- def visualizeDistance(distance, r, g, b): #2.5
- global busy, fast, sim
- while busy:
- pass
- busy = True
- print("Visualizing distance")
- j = 0
- for i in (0, 10, 15, 23, 34, 51, 76, 144, 171, 256, 384, 577):
- if distance > i:
- j = j + 1
- for i in (1,2,3,4,5,6,7,8,9,10,11,12):
- if j>=i:
- ws2812b(i, r, g, b)
- time.sleep(0.1)
- time.sleep(0.1)
- for i in (12,11,10,9,8,7,6,5,4,3,2,1):
- if j>=i:
- ws2812b(i, r, g, b)
- time.sleep(0.1)
- ws2812b(0,0,0,0)
- busy = False
- def capturePhoto():#5
- global busy, fast, sim
- while busy:
- pass
- busy = True
- print("Capturing photo to /tmp/capture.jpg")
- ws2812b(LED_COUNT, 255, 255, 255)
- GPIO.output(LIGHT_PIN, True)
- time.sleep(2.5)
- if sim:
- subprocess.call(['cp', '/home/pi/capture.jpg', '/tmp/capture.jpg'])
- elif usbcam:
- subprocess.call(['fswebcam', '-S', '10', '-r', '640x480', '/tmp/capture.jpg'])
- else:
- subprocess.call(['raspistill', '-w', '640', '-h', '480', '-n', '-t', '100', '-q', '10', '-e', 'jpg', '-th', 'none', '-o',
- '/tmp/capture.jpg'])
- time.sleep(2.5)
- ws2812b(0, 0, 0, 0)
- GPIO.output(LIGHT_PIN, False)
- print("Captured")
- busy = False
- class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- self.send_response(200)
- self.send_header('Access-Control-Allow-Origin','*')
- if 'photo' in self.path:
- self.send_header('Content-type', 'image/jpeg')
- self.end_headers()
- capturePhoto() #5
- f = open('/tmp/capture.jpg', 'rb')
- self.wfile.write(f.read())
- f.close()
- elif 'distance' in self.path:
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- distance1 = getDistance(TRIG1, ECHO1) #2.5
- visualizeDistance(distance1, 255, 0, 0) #2.5
- distance2 = getDistance(TRIG2, ECHO2) #2.5
- visualizeDistance(distance2, 0, 255, 0) #2.5
- self.wfile.write(b'Sensor1:' + bytes(str(distance1)) + ' Sensor2:' + bytes(str(distance2)))
- elif 'ok' in self.path:
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- self.wfile.write(b'OK')
- else:
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- self.wfile.write(b'<a href=?photo>Photo</a> <a href=?distance>Distance</a>')
- httpd = HTTPServer(('', 8123), SimpleHTTPRequestHandler)
- print("Server is running on 8123")
- #httpd.serve_forever()
- myThread = threading.Thread(target=httpd.serve_forever)
- myThread.daemon = True
- myThread.start()
- print("Move on")
- if not fast:
- time.sleep(60)
- #$ raspistill -t 3000 -o test.jpg
- #$ raspistill -w 640 -h 480 -n -t 100 -q 10 -e jpg -th none -o vgasize10nothumb.jpg
- #$ curl -F "photo=@/tmp/capture.jpg" http://107.174.13.179:81/reservoir.php
- #datetime.datetime.now().isoformat() '2018-09-23T11:33:34.325805'
- curl = ['curl', '-F', 'reservoir=' + str(getSN())]
- ws2812b(0, 0, 0, 0)
- while True:
- result = [0,0,0,0,0]
- for _ in range(5 if not fast else 3): #5 times, each is 12 min
- for i in range(5 if not fast else 3): #25s
- distance = getDistance(TRIG1, ECHO1) # 2.5
- visualizeDistance(distance, 255, 0, 0) # 2.5
- result[i] = distance
- result.sort()
- curl = curl + ['-F', 'hcsr04_0[' + datetime.datetime.now().isoformat() + ']=' + str(result[2])]
- for i in range(5 if not fast else 3): #25s
- distance = getDistance(TRIG2, ECHO2) # 2.5
- visualizeDistance(distance, 0, 255, 0) # 2.5
- result[i] = distance
- result.sort()
- curl = curl + ['-F', 'hcsr04_1[' + datetime.datetime.now().isoformat() + ']=' + str(result[2])]
- time.sleep(10)
- if not fast:
- time.sleep(12 * 60 - 60 - 10)
- capturePhoto() # 5
- curl = curl + ['-F', 'photodate='+datetime.datetime.now().isoformat()]
- curl = curl + ['-F', 'photo=@/tmp/capture.jpg']
- curl = curl + ["http://" + hostname + "/reservoir.php"]
- print("Sending results to website")
- print(str(curl))
- if not sim:
- subprocess.call(curl)
- curl = ['curl', '-F', 'reservoir=' + str(getSN())]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement