Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PIL import Image
- from PIL import ImageFont
- from PIL import ImageDraw
- from sense_hat import SenseHat
- import threading
- import datetime
- import picamera
- import socket
- import time
- import os
- import urllib3
- import sys
- #sys.exit()
- http = urllib3.PoolManager()
- HOST = "weather.parginli.ch"
- PORT = 5000
- CHUNK_SIZE = 8192
- bufDataPlan = 4
- currDataPlan = 4
- brightness = 2
- menu = False
- numImagesToday = 0
- # 0: 3x/d (8:00, 12:00, 16:00)
- # 1: every 2 hours (7:00-19:00)
- # 2: every hour (6:00-21:00)
- # 3: every 30 minutes (6:00-21:00)
- # 4: (with 150MB/month) every 5 minutes (6:00-21:00)
- # 4: (for testing) every 30 seconds (anytime)
- imageIntervals = [14400, 7200, 3600, 1800, 300, 30]
- imageSizes = [100, 40, 20, 10, 25, 10]
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- ##camera = picamera.PiCamera()
- ##camera.resolution = (1080, 720)
- sense = SenseHat()
- lasttime = int(round(time.time() * 1000)) - 60000
- r = [255, 0, 0]
- g = [0, 255, 0]
- o = [255, 127, 0]
- w = [0, 0, 0]
- v = [255, 255, 255]
- wifi = [
- w,w,w,w,w,w,w,r,
- w,w,w,w,w,w,r,w,
- w,v,v,v,v,r,v,w,
- v,w,w,w,r,w,w,v,
- w,w,v,r,v,v,w,w,
- w,v,r,w,w,w,v,w,
- w,r,w,v,v,w,w,w,
- r,w,w,v,v,w,w,w
- ]
- cross = [
- r,w,w,w,w,w,w,r,
- w,r,w,w,w,w,r,w,
- w,w,r,w,w,r,w,w,
- w,w,w,r,r,w,w,w,
- w,w,w,r,r,w,w,w,
- w,w,r,w,w,r,w,w,
- w,r,w,w,w,w,r,w,
- r,w,w,w,w,w,w,r
- ]
- upgrade = [
- w,w,w,o,w,w,w,w,
- w,w,o,o,o,w,w,w,
- w,o,o,w,o,o,w,w,
- o,o,w,o,w,o,o,w,
- o,w,o,o,o,w,o,w,
- w,o,o,w,o,o,w,w,
- o,o,w,w,w,o,o,w,
- o,w,w,w,w,w,o,w
- ]
- tick = [
- w,w,w,w,w,w,w,g,
- w,w,w,w,w,w,g,g,
- w,w,w,w,w,g,g,g,
- g,w,w,w,g,g,g,w,
- g,g,w,g,g,g,w,w,
- g,g,g,g,g,w,w,w,
- w,g,g,g,w,w,w,w,
- w,w,g,w,w,w,w,w
- ]
- def checkVer():
- version = open("version.txt", "r")
- currVersion = version.read()
- try:
- newVersion = http.request("GET", "http://www.parginli.ch/weatherfiles/version.txt").data.decode()
- if (currVersion not in newVersion):
- file = open("client.py", "w")
- file.write(http.request("GET", "http://www.parginli.ch/weatherfiles/client.py").data.decode())
- file.close()
- version.close()
- version = open("version.txt", "w")
- version.write(newVersion)
- version.close()
- sense.set_pixels(upgrade)
- os.system('sudo reboot')
- else:
- sense.set_pixels(tick)
- version.close()
- except:
- sense.set_pixels(wifi)
- sys.exit()
- def incMode(event):
- global menu
- if event.action == "pressed":
- menu = True
- global bufDataPlan
- bufDataPlan += 1
- if bufDataPlan > 5:
- bufDataPlan = 0
- sense.show_letter(str(bufDataPlan))
- def decMode(event):
- global menu
- if event.action == "pressed":
- menu = True
- global bufDataPlan
- bufDataPlan -= 1
- if bufDataPlan < 0:
- bufDataPlan = 5
- sense.show_letter(str(bufDataPlan))
- def lowLightOn(event):
- if event.action == "pressed":
- global brightness
- brightness += 1
- if brightness > 2:
- brightness = 2
- if (brightness == 1):
- sense.low_light = True
- else:
- sense.low_light = False
- def lowLightOff(event):
- if event.action == "pressed":
- global brightness
- brightness -= 1
- if brightness <= 0:
- brightness = 0
- sense.clear()
- if (brightness == 1):
- sense.low_light = True
- else:
- sense.low_light = False
- def click(event):
- global currDataPlan
- global bufDataPlan
- global menu
- if event.action == "pressed" and event.direction == "middle":
- currDataPlan = bufDataPlan
- menu = False
- def takePhoto():
- global numImagesToday
- global lasttime
- global brightness
- global menu
- if (brightness != 0 and menu == False):
- number_pixels = int((time.time() * 1000 - lasttime) / (imageIntervals[currDataPlan] * 1000) * 64)
- sense.clear(89, 242, 24)
- for i in range(0, number_pixels):
- if (i < 64):
- sense.set_pixel(int(i % 8), int(i / 8), 119, 171, 255)
- if (datetime.datetime.now().hour < 2 and numImagesToday != 0):
- numImagesToday = 0
- checkVer()
- thishour = datetime.datetime.now().hour
- if (lasttime + imageIntervals[currDataPlan] * 1000 < int(round(time.time() * 1000))):
- if (currDataPlan == 0):
- return thishour >= 8 and thishour <= 16 and numImagesToday < 3
- elif (currDataPlan == 1):
- return thishour >= 7 and thishour <= 19 and numImagesToday < 6
- elif (currDataPlan == 2):
- return thishour >= 6 and thishour <= 21 and numImagesToday < 15
- elif (currDataPlan == 3):
- return thishour >= 6 and thishour <= 21 and numImagesToday < 30
- elif (currDataPlan == 4):
- return thishour >= 6 and thishour <= 21 and numImagesToday < 180
- elif (currDataPlan == 5):
- return True
- def imageloop():
- while True:
- if takePhoto():
- #try:
- global numImagesToday
- global lasttime
- numImagesToday += 1
- lasttime = int(round(time.time() * 1000))
- print(lasttime)
- s.sendto("imagecoming".encode("utf-8"), (HOST, PORT))
- if (brightness != 0 and menu == False):
- sense.clear(119, 171, 255)
- camera.capture("image.jpg")
- imageCompression = 1
- print("resizing to " + str(imageSizes[currDataPlan]) + "KB")
- img = Image.open("image.jpg")
- while(os.path.getsize("image.jpg") > imageSizes[currDataPlan] * 1000):
- img = img.resize((int(img.size[0] * imageCompression), int(img.size[1] * imageCompression)), Image.ANTIALIAS)
- imageCompression -= 0.005
- img.save("image.jpg", quality=int(10 * imageCompression))
- draw = ImageDraw.Draw(img)
- font = ImageFont.truetype("Font.ttf", int(img.size[1] * 0.05))
- draw.text((1, int(img.size[1] * 0.965)), str(datetime.datetime.now().strftime("%d.%m.%Y %H:%M")), (255, 255, 255), font=font)
- img.save("resized.jpg")
- f = open("resized.jpg", "rb")
- chunk = f.read(CHUNK_SIZE)
- max = os.path.getsize("resized.jpg")
- index = 0
- while chunk:
- if (brightness != 0 and menu == False):
- number_pixels = (float(index * CHUNK_SIZE) / max) * 64
- sense.clear(119, 171, 255)
- for i in range(0, int(number_pixels)):
- sense.set_pixel(int(i % 8), int(i / 8), 89, 242, 24)
- s.sendto(chunk, (HOST, PORT))
- chunk = f.read(CHUNK_SIZE)
- index += 1
- time.sleep(1)
- f.close()
- if (brightness != 0 and menu == False):
- sense.clear(89, 242, 24)
- time.sleep(1)
- print("sent.")
- s.sendto(("isend," + str(index)).encode("utf-8"), (HOST, PORT))
- time.sleep(2)
- #except:
- #print("error!")
- else:
- time.sleep(5)
- sense.stick.direction_left = decMode
- sense.stick.direction_right = incMode
- sense.stick.direction_up = lowLightOn
- sense.stick.direction_down = lowLightOff
- sense.stick.direction_any = click
- sense.clear(255, 252, 119)
- time.sleep(5)
- checkVer()
- time.sleep(2)
- imageloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement