Advertisement
musman2015

clie_v3.py

Mar 7th, 2021
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.03 KB | None | 0 0
  1. from picamera import PiCamera
  2. import logging
  3. import ntplib
  4. from time import ctime
  5. import time
  6. from datetime import datetime
  7. import socket,datetime
  8. import RPi.GPIO as GPIO  
  9. GPIO.setmode(GPIO.BCM)
  10.  
  11. GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  12. def my_callback(channel):  
  13.     print "falling edge detected on 24"
  14.     # Create a datagram socket
  15.     tempSensorSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
  16.     # Socket is not in connected state yet...sendto() can be used
  17.     # Send temperature to the server
  18.     now = str(time.clock_gettime(time.CLOCK_REALTIME)+2)
  19.     then = str(time.clock_gettime(time.CLOCK_REALTIME)+2)
  20.     print(now)
  21.     tempSensorSocket.sendto(then.encode(), serverAddress);
  22.     # Read UDP server's response datagram
  23.     response = tempSensorSocket.recv(1024);
  24.     print(response);
  25.     while now < then:
  26.         now = str(time.clock_gettime(time.CLOCK_REALTIME)+1)
  27.     print("Client takes picture at: ",now)
  28.     frame=(time.strftime("_%y-%b-%d-%H:%M:%S"))
  29.     camera.capture("/home/pi/Desktop/"+"client" + frame + ".jpg")
  30.  
  31. GPIO.add_event_detect(24, GPIO.FALLING, callback=my_callback, bouncetime=300)
  32. camera = PiCamera()
  33. camera.start_preview()
  34. # Get temperature
  35. #def getTemp():
  36. #    temp = random.uniform(60.0, 62.0);
  37. #    return temp;
  38. # A tuple with server ip and port
  39. serverAddress = ("192.168.1.13", 7070);
  40.  
  41. class NtpException(Exception):
  42.     pass
  43. class keyboardInterrupt(Exception):
  44.     pass
  45.  
  46. def SyncClockToNtp():
  47.   """Syncs the hardware clock to an NTP server."""
  48.   server = 'europe.pool.ntp.org'
  49.   retries = 2
  50.   RETRY_DELAY=5
  51.   logging.info('Reading time from NTP server %s.', server)
  52.  
  53.   attempts = 0
  54.   client = ntplib.NTPClient()
  55.   response = None
  56.  
  57.   while True:
  58.     try:
  59.       response = client.request(server, version=3)
  60.     except (ntplib.NTPException, socket.gaierror) as e:
  61.       logging.error('NTP client request error: %s', str(e))
  62.     if response or attempts >= retries:
  63.       break
  64.     logging.info(
  65.         'Unable to contact NTP server %s to sync machine clock.  This '
  66.         'machine may not have an IP address yet; waiting %d seconds and '
  67.         'trying again. Repeated failure may indicate network or driver '
  68.         'problems.', server, RETRY_DELAY)
  69.     time.sleep(RETRY_DELAY)
  70.     attempts += 1
  71.  
  72.   if not response:
  73.     raise NtpException('No response from NTP server.')
  74.  
  75.   now = time.clock_gettime(time.CLOCK_REALTIME)
  76.   print("old time:",ctime(now))
  77.  
  78.   #print("Offset",response.offset)
  79.   print("tx_time",ctime(response.tx_time))
  80.   #print(response.tx_time)
  81.   time.clock_settime(time.CLOCK_REALTIME, (response.tx_time))
  82.   now = time.clock_gettime(time.CLOCK_REALTIME)
  83.   print("Updated time:",ctime(now))
  84.  
  85.  
  86. if __name__=='__main__':
  87.     try:
  88.         SyncClockToNtp()
  89.     except NtpException:
  90.         print("No response from NTP server. Please check your internet connection")
  91.     try:
  92.         while True:
  93.            
  94.             time.sleep(5)
  95.     except keyboardInterrupt:
  96.         print("Exiting Nicely")
  97.         camera.stop_preview()
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement