Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from picamera import PiCamera
- import logging
- import ntplib
- from time import ctime
- import time
- from datetime import datetime
- import socket,datetime
- import RPi.GPIO as GPIO
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- def my_callback(channel):
- print "falling edge detected on 24"
- # Create a datagram socket
- tempSensorSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM);
- # Socket is not in connected state yet...sendto() can be used
- # Send temperature to the server
- now = str(time.clock_gettime(time.CLOCK_REALTIME)+2)
- then = str(time.clock_gettime(time.CLOCK_REALTIME)+2)
- print(now)
- tempSensorSocket.sendto(then.encode(), serverAddress);
- # Read UDP server's response datagram
- response = tempSensorSocket.recv(1024);
- print(response);
- while now < then:
- now = str(time.clock_gettime(time.CLOCK_REALTIME)+1)
- print("Client takes picture at: ",now)
- frame=(time.strftime("_%y-%b-%d-%H:%M:%S"))
- camera.capture("/home/pi/Desktop/"+"client" + frame + ".jpg")
- GPIO.add_event_detect(24, GPIO.FALLING, callback=my_callback, bouncetime=300)
- camera = PiCamera()
- camera.start_preview()
- # Get temperature
- #def getTemp():
- # temp = random.uniform(60.0, 62.0);
- # return temp;
- # A tuple with server ip and port
- serverAddress = ("192.168.1.13", 7070);
- class NtpException(Exception):
- pass
- class keyboardInterrupt(Exception):
- pass
- def SyncClockToNtp():
- """Syncs the hardware clock to an NTP server."""
- server = 'europe.pool.ntp.org'
- retries = 2
- RETRY_DELAY=5
- logging.info('Reading time from NTP server %s.', server)
- attempts = 0
- client = ntplib.NTPClient()
- response = None
- while True:
- try:
- response = client.request(server, version=3)
- except (ntplib.NTPException, socket.gaierror) as e:
- logging.error('NTP client request error: %s', str(e))
- if response or attempts >= retries:
- break
- logging.info(
- 'Unable to contact NTP server %s to sync machine clock. This '
- 'machine may not have an IP address yet; waiting %d seconds and '
- 'trying again. Repeated failure may indicate network or driver '
- 'problems.', server, RETRY_DELAY)
- time.sleep(RETRY_DELAY)
- attempts += 1
- if not response:
- raise NtpException('No response from NTP server.')
- now = time.clock_gettime(time.CLOCK_REALTIME)
- print("old time:",ctime(now))
- #print("Offset",response.offset)
- print("tx_time",ctime(response.tx_time))
- #print(response.tx_time)
- time.clock_settime(time.CLOCK_REALTIME, (response.tx_time))
- now = time.clock_gettime(time.CLOCK_REALTIME)
- print("Updated time:",ctime(now))
- if __name__=='__main__':
- try:
- SyncClockToNtp()
- except NtpException:
- print("No response from NTP server. Please check your internet connection")
- try:
- while True:
- time.sleep(5)
- except keyboardInterrupt:
- print("Exiting Nicely")
- camera.stop_preview()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement