Advertisement
musman2015

serve.py

Mar 6th, 2021
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 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. # Define the IP address and the Port Number
  9. ip      = "127.0.0.1";
  10. port    = 7070;
  11.  
  12. camera = PiCamera()
  13. camera.start_preview()
  14. class NtpException(Exception):
  15.     pass
  16. def SyncClockToNtp():
  17.   """Syncs the hardware clock to an NTP server."""
  18.   server = 'europe.pool.ntp.org'
  19.   retries = 2
  20.   RETRY_DELAY=5
  21.   logging.info('Reading time from NTP server %s.', server)
  22.  
  23.   attempts = 0
  24.   client = ntplib.NTPClient()
  25.   response = None
  26.  
  27.   while True:
  28.     try:
  29.       response = client.request(server, version=3)
  30.     except (ntplib.NTPException, socket.gaierror) as e:
  31.       logging.error('NTP client request error: %s', str(e))
  32.     if response or attempts >= retries:
  33.       break
  34.     logging.info(
  35.         'Unable to contact NTP server %s to sync machine clock.  This '
  36.         'machine may not have an IP address yet; waiting %d seconds and '
  37.         'trying again. Repeated failure may indicate network or driver '
  38.         'problems.', server, RETRY_DELAY)
  39.     time.sleep(RETRY_DELAY)
  40.     attempts += 1
  41.  
  42.   if not response:
  43.     raise NtpException('No response from NTP server.')
  44.  
  45.   now = time.clock_gettime(time.CLOCK_REALTIME)
  46.   print("old time:",ctime(now))
  47.  
  48.   #print("Offset",response.offset)
  49.   print("tx_time",ctime(response.tx_time))
  50.   #print(response.tx_time)
  51.   time.clock_settime(time.CLOCK_REALTIME, (response.tx_time))
  52.   now = time.clock_gettime(time.CLOCK_REALTIME)
  53.   print("Updated time:",ctime(now))
  54.  
  55.  
  56. if __name__=='__main__':
  57.     try:
  58.         SyncClockToNtp()
  59.     except NtpException:
  60.         print("No response from NTP server. Please check your internet connection")
  61.     listeningAddress = (ip, port)
  62.     print("Listning on ",listeningAddress)
  63.     # Create a datagram based server socket that uses IPv4 addressing scheme
  64.     datagramSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  65.     datagramSocket.bind(listeningAddress)
  66.     try:
  67.         while(True):
  68.             tempVal, sourceAddress = datagramSocket.recvfrom(128)
  69.             print("Time decided by %s is %s"%(sourceAddress, tempVal.decode()))
  70.             response = "Received at: %s"%datetime.datetime.now()
  71.             #print("Response: ",response)
  72.             now = str(time.clock_gettime(time.CLOCK_REALTIME)+1)
  73.             print(now)
  74.             datagramSocket.sendto(response.encode(), sourceAddress)
  75.             while now < tempVal.decode():
  76.                 now = str(time.clock_gettime(time.CLOCK_REALTIME)+1)
  77.             print("Server takes picture at: ", now)
  78.             camera.capture('/home/pi/Desktop/image%s.jpg' % ctime(datetime.datetime.now()))
  79.     except keyboardInterrupt:
  80.         print("Exiting Nicely")
  81.         camera.stop_preview()
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement