1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| # http://picamera.readthedocs.io/en/release-1.0/index.html
#*********************************************************
#* CARTE de SAUVEGARGE 26/04/2019 *
#*********************************************************
#!usr/bin/sudo python3 pour le demarage auto
# ATTENTION pour demarrer : python3 simple_cam.py
# ctrl + c pour quitter
# ps -ef | grep simple_cam.py pour avoir son numero et le killer
# sudo kill -9 son numero
# sudo chmod 777 rc.local pour avoir les droit
# sudo python3 /home/pi/henri/simple_cam.py dans RC.local
# apt-mark showauto | grep gpio recherche de paquets installé automatiquement
# apt-mark showmanual |grep pio recherche de paquets installé manuelement
# FING applic portable S3 pour détecter les IP utilisé
# liste des HSB : lsusb
# /opt/vc/bin/vcgencmd measure_temp //Temperarure interne du RPI
# history -c Clear History
# which vcgencmd ou n'importe quoi
# sudo find / -name NOM DU FICHIER
# sudo apt rem
import io
import picamera
import itertools
import datetime as dt
import time
import logging
import socketserver
from threading import Condition
from http import server
from picamera import Color
# gestion Temperature du RPI
import threading
import time
from subprocess import getoutput
# Librairies de lecture des roues codeuses etde l'horloge
import RPi.GPIO as GPIO # Importation des librairies qui gerent les ports
import time # Importation de la librairie temps
# Librairies des calculs des heures
from datetime import timedelta
from datetime import datetime
# **************** Fonctions du GPIO ******************************
GPIO.setmode(GPIO.BCM) # BCM : Numero des GPIO / BOARD : Numero du connecteur
# GPIO.setwarnIN,(False)
# **********
GPIO.setup(4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # DOWN , pull_up_down=GPIO.PUD_UP
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# **********
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(19, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# **********
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# **********
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# **************** Appele de Fonctions du GPIO ******************************
tempo1 = 0 # global variable
def roue(): # https://pythonbasics.org/multiple-return/
global tempo1
status4 = GPIO.input(4)
unitee = status4
status17= GPIO.input(17)
unitee = unitee + (status17 * 2)
status27 = GPIO.input(27)
unitee = unitee + (status27 * 4)
status22 = GPIO.input(22)
unitee = unitee + (status22 * 8)
status5 = GPIO.input(5)
dizaine = status5
status6= GPIO.input(6)
dizaine = dizaine + (status6 * 2)
status13 = GPIO.input(13)
dizaine = dizaine + (status13 * 4)
status19 = GPIO.input(19)
dizaine = dizaine + (status19 * 8)
status26 = GPIO.input(26)
centaine = status26
status21= GPIO.input(21)
centaine = centaine + (status21 * 2)
status20 = GPIO.input(20)
centaine = centaine + (status20 * 4)
status16 = GPIO.input(16)
centaine = centaine + (status16 * 8)
millier = 0
status12 = GPIO.input(12)
millier = status12
status25= GPIO.input(25)
millier = millier + (status25 * 2)
status24 = GPIO.input(24)
millier = millier + (status24 * 4)
status23 = GPIO.input(23)
millier = millier + (status23 * 8)
tempo = str(millier) + str(centaine) + str(dizaine) + str(unitee)
if (tempo1 != tempo):
tempo1 = tempo
boulet = 1
else:
boulet = 0
# print("___Boulet ____ : ", boulet)
return (unitee,dizaine,centaine,millier,boulet)
# ********************** Fonction Horloge ********************
t4 = 0
# while True:
def horloge():
global tempo
global t4
global t3
global t2
global t1
# t3 = datetime.now() # pour le singer
unitee,dizaine,centaine,millier,boulet = roue() # roue(unitee,dizaine,centaine,millier)
time.sleep(1)
tempo = str(millier) + str(centaine) + str(dizaine) + str(unitee)
print("Boulet =", boulet) # Flag pour distinguer si il y à un changement sur les roues codeuses
if (boulet == 1):
now = datetime.now()
t1 = timedelta(hours = int(now.strftime("%H")),minutes = int(now.strftime("%M")), seconds = int(now.strftime("%S")))
t2 = timedelta(hours = (millier * 10 + centaine),minutes = (dizaine *10 + unitee) , seconds = 0)
t3 = t1 + t2
print("t1 =", t1) # heure courante
print("t2 =", t2) # heure roue codeuse
print("t3 =", t3) # heure de Fin
else:
now = datetime.now()
t1 = timedelta(hours = int(now.strftime("%H")),minutes = int(now.strftime("%M")), seconds = int(now.strftime("%S")))
t4 = t3 - t1 # t4 = t3 - t1
print("t1_B =", t1) # heure courante
# print("t2_B =", t2) # heure roue codeuse
print("t3_B =", t3) # heure de Fin
print("Temps restant(t4_B) =",t4) #Temps restant
print("type of t4 =", type(t4))
# return(t4) # inutile
# ********************* Page serveur Camera ************************
PAGE="""
<html>
<head>
<title>picamera MJPEG streaming demo</title>
</head>
<body>
<center>
<h1>PiCamera MJPEG Streaming Demo de HENRI</h1>
<h1>CARTE de SAUVEGARGE 26/04/2019</h1>
<img src="stream.mjpg" width="640" height="480" />
</center>
</body>
</html>
"""
# gestion Temperature
chaleur= 0
def rien():
while True:
global chaleur
time.sleep(1) # 5 0.3
chaleur = float(getoutput('/opt/vc/bin/vcgencmd measure_temp')[5:-2])
horloge() # Appele de la fonction Horloge
# print( chaleur )
print("Horloge T4",t4)
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
self.buffer.truncate()
with self.condition:
# print("de visu : " + t4)
self.frame = self.buffer.getvalue()
self.condition.notify_all()
# camera.annotate_text = 'Date & Heure\n' + dt.datetime.now().strftime('%d-%m-%Y %H:%M:%S\n') + 'cal' # OK
camera.annotate_text = dt.datetime.now().strftime('%d-%B-%Y %H:%M:%S\n') + ("Temps restant :" + str(t4))+ ('\n Temperature RPI %0.1f ' % chaleur)
# camera.annotate_text = 'Chaleur : ' + float(chaleur)
self.buffer.seek(0)
return self.buffer.write(buf)
class StreamingHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.send_response(301)
self.send_header('Location', '/index.html')
self.end_headers()
elif self.path == '/index.html':
content = PAGE.encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', len(content))
self.end_headers()
self.wfile.write(content)
# threading.Thread(target=horloge().start) # Démmare le Thread de l'Horloge
threading.Thread(target=rien().start) # Démmare le Thread de temperature
elif self.path == '/stream.mjpg':
self.send_response(200)
self.send_header('Age', 0)
self.send_header('Cache-Control', 'no-cache, private')
self.send_header('Pragma', 'no-cache')
self.send_header('Content-Type', 'multipart/x-mixed-replace; boundary=FRAME')
self.end_headers()
try:
while True:
with output.condition:
output.condition.wait()
frame = output.frame
self.wfile.write(b'--FRAME\r\n')
self.send_header('Content-Type', 'image/jpeg')
self.send_header('Content-Length', len(frame))
self.end_headers()
self.wfile.write(frame)
self.wfile.write(b'\r\n')
except Exception as e:
logging.warning(
'Removed streaming client %s: %s',
self.client_address, str(e))
else:
self.send_error(404)
self.end_headers()
class StreamingServer(socketserver.ThreadingMixIn, server.HTTPServer):
allow_reuse_address = True
daemon_threads = True
with picamera.PiCamera(resolution='640x480', framerate=24) as camera:
camera.rotation='180' # pour donner une oriantation de seulement 90° à l'image : 0/90/180/270
# camera.hflip=False # Retournement à 180 droite/gauche
# camera.vflip=False # Retournement à 180 haut/bas
# camera.start_preview()
camera.annotate_text_size = 15
camera.annotate_foreground = Color('black')
camera.annotate_background = Color('white')
# camera.annotate_text = dt.datetime.now().strftime('%d-%m-%Y %H:%M:%S') #"Visu"
output = StreamingOutput()
camera.start_recording(output, format='mjpeg')
try:
address = ('', 8000)
server = StreamingServer(address, StreamingHandler)
server.serve_forever()
# gestion Temperature
# threading.Thread(target=rien().start)
# print(chaleur)
finally:
camera.stop_recording()
GPIO.cleanup |
Partager