def initialize_sensors(): print("\n--- Sensors initializing...") try: #For a raspberry pi, for example, to set up pins 4 and 5, you would add #GPIO.setmode(GPIO.BCM) #GPIO.setup(4, GPIO.IN) #GPIO.setup(5, GPIO.IN) print("--- Waiting 10 seconds for sensors to warm up...") time.sleep(10) # Activate the Phidget global ch ch = TemperatureSensor() # Assign event handlers ch.setOnAttachHandler(PhidgetAttached) ch.setOnDetachHandler(PhidgetDetached) ch.setOnErrorHandler(ErrorEvent) # Wait for the sensor to be attached print("--- Waiting for the Phidget Object to be attached...") ch.openWaitForAttachment(5000) print("--- Sensors initialized!") # Sync the time on this device to an internet time server try: print('\n--- Syncing time...') os.system('sudo service ntpd stop') time.sleep(1) os.system('sudo ntpd -gq') time.sleep(1) os.system('sudo service ntpd start') print('--- Success! Time is ' + str(datetime.datetime.now())) except: print('Error syncing time!') except Exception as ex: # Log any error, if it occurs print(str(datetime.datetime.now()) + " Error when initializing sensors: " + str(ex))
global tempslist3 tempslist3 = np.append(tempslist3, temperature) global start3 #if it has been 5s, calc an average and start again if time.time() >= start3 + 5: AvgTemp(3, tempslist3, start3) tempslist3 = np.asarray([]) start3 += 5 try: ch0.setOnAttachHandler(TemperatureSensorAttached) ch0.setOnDetachHandler(TemperatureSensorDetached) ch0.setOnErrorHandler(ErrorEvent) ch1.setOnAttachHandler(TemperatureSensorAttached) ch1.setOnDetachHandler(TemperatureSensorDetached) ch1.setOnErrorHandler(ErrorEvent) ch2.setOnAttachHandler(TemperatureSensorAttached) ch2.setOnDetachHandler(TemperatureSensorDetached) ch2.setOnErrorHandler(ErrorEvent) ch3.setOnAttachHandler(TemperatureSensorAttached) ch3.setOnDetachHandler(TemperatureSensorDetached) ch3.setOnErrorHandler(ErrorEvent) start0 = time.time()
def main(): try: """ * Allocate a new Phidget Channel object """ try: ch = TemperatureSensor() except PhidgetException as e: sys.stderr.write("Runtime Error -> Creating TemperatureSensor: \n\t") DisplayError(e) raise except RuntimeError as e: sys.stderr.write("Runtime Error -> Creating TemperatureSensor: \n\t" + e) raise """ * Set matching parameters to specify which channel to open """ #You may remove this line and hard-code the addressing parameters to fit your application channelInfo = AskForDeviceParameters(ch) ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber) ch.setHubPort(channelInfo.hubPort) ch.setIsHubPortDevice(channelInfo.isHubPortDevice) ch.setChannel(channelInfo.channel) if(channelInfo.netInfo.isRemote): ch.setIsRemote(channelInfo.netInfo.isRemote) if(channelInfo.netInfo.serverDiscovery): try: Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE) except PhidgetException as e: PrintEnableServerDiscoveryErrorMessage(e) raise EndProgramSignal("Program Terminated: EnableServerDiscovery Failed") else: Net.addServer("Server", channelInfo.netInfo.hostname, channelInfo.netInfo.port, channelInfo.netInfo.password, 0) """ * Add event handlers before calling open so that no events are missed. """ print("\n--------------------------------------") print("\nSetting OnAttachHandler...") ch.setOnAttachHandler(onAttachHandler) print("Setting OnDetachHandler...") ch.setOnDetachHandler(onDetachHandler) print("Setting OnErrorHandler...") ch.setOnErrorHandler(onErrorHandler) #This call may be harmlessly removed PrintEventDescriptions() print("\nSetting OnTemperatureChangeHandler...") ch.setOnTemperatureChangeHandler(onTemperatureChangeHandler) """ * Open the channel with a timeout """ print("\nOpening and Waiting for Attachment...") try: ch.openWaitForAttachment(5000) except PhidgetException as e: PrintOpenErrorMessage(e, ch) raise EndProgramSignal("Program Terminated: Open Failed") print("Sampling data for 10 seconds...") print("You can do stuff with your Phidgets here and/or in the event handlers.") time.sleep(10) """ * Perform clean up and exit """ #clear the TemperatureChange event handler ch.setOnTemperatureChangeHandler(None) print("\nDone Sampling...") print("Cleaning up...") ch.close() print("\nExiting...") return 0 except PhidgetException as e: sys.stderr.write("\nExiting with error(s)...") DisplayError(e) traceback.print_exc() print("Cleaning up...") ch.setOnTemperatureChangeHandler(None) ch.close() return 1 except EndProgramSignal as e: print(e) print("Cleaning up...") ch.setOnTemperatureChangeHandler(None) ch.close() return 1 finally: print("Press ENTER to end program.") readin = sys.stdin.readline()