# Project#3 - DateTime Automation # Information - In this pr0ject, we're going to use the Dattime module to automate the relay # The relay can be used to control a combination lock. In the project video, I'll show you how. # Datetime is a fantastic took for automation, and can be used for SO MANY THINGS. import time # The 'time' module is included with pycharm and allows for us to create delays. import serial # The 'serial' or 'pyserial' module needs to be imported. See the project video for more info. import datetime # We're going to use this module to read the current time and compare it to a set time. # Note that serial/pyserial is the module that allows for us to communicate with external microcontrollers. # Remember that you need to program your RPInoRPI with the code for project#3. # The first thing that we need to do is to talk to our RPInoRPI. try: # Try to establish communication with the RPInoRPI board. if successful, only the 'try:' will be executed MCU = serial.Serial(port='COM10', baudrate=115200, timeout=0.01) # See project video for more info on this line. time.sleep(1) # Wait one second except: # If communication with the RPInoRPI could not be established, then perform the following instead: print('System Locked - Cannot communicate with MCU. Close this program, then reconnect to MCU and restart program') while True: # Forever loop. Close the program and start again. Check connection to the RPInoRPI pass # Do nothing def write_read(x): # This function acts to send data (x) to the RPInoRPI. Nothing more. MCU.write(bytes(x, 'utf-8')) # Write the value stored in 'x' to the RPInoRPI time.sleep(0.2) # Wait 0.2 seconds/200ms # Main Code x = '5' # Set x to a fixed value of 5. Relay_ON_Time = '08:28:00' # This the time that the relay will turn on. Set it 5 minutes ahead to test it out! # Time : 9AM 18MIN 30MIN, or 9:30AM. # You can add a second or third fixed time here. See comments at the end of the code #Relay_ON_Time2 = See comments at the end of this program #Relay_ON_Time3 = See comments at the end of this program while True: # Run the following code until you close the program (loop forever) Current_Time = datetime.datetime.now() # Grab a date stamp and place the result in fail_time Current_Time = Current_Time.strftime("%H:%M:%S") # This is here to play with time.sleep(1) # Wait for 2 seconds then start over print("Current_Time: " + str(Current_Time)) # Concatenate and print string if Current_Time == Relay_ON_Time: # If the current time equals our ON time, then execute the following: print("Turn the relay on!") # Print this to the screen write_read(x) # Send over the RELAY ON command (5 Decimal / 53 ASCII) time.sleep(2) # Wait for 2 seconds # How you use this project is up to you. I have just created a way to automate a relay for a few seconds once a day # In the line: 'if Current_Time == Relay_ON_Time:' - IF you have more than one relay activation time, you can do this: # if Current_Time == Relay_ON_Time or Current_Time == Relay2_ON_Time2 or Current_Time == Relay_ON_Time3: # You don't have to limit yourself to one time to automate the relay. You also don't need to use the relay... # You can automate whatever you'd like on the RPInoPRI using this method. # Here is a great tutorial on datetime: https://www.programiz.com/python-programming/datetime