Example1-GetTemperature

examples/Example1-GetTemperature.py
  1#!/usr/bin/env python
  2#-----------------------------------------------------------------------------
  3# Example1-GetTemperature.py
  4#
  5# Simple Example for the Qwiic TMP102 Device
  6#------------------------------------------------------------------------
  7#
  8# Written by  SparkFun Electronics, May 2019
  9# 
 10# This python library supports the SparkFun Electroncis qwiic 
 11# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
 12# board computers. 
 13#
 14# More information on qwiic is at https://www.sparkfun.com/qwiic
 15#
 16# Do you like this library? Help support SparkFun. Buy a board!
 17#
 18#==================================================================================
 19# Copyright (c) 2019 SparkFun Electronics
 20#
 21# Permission is hereby granted, free of charge, to any person obtaining a copy 
 22# of this software and associated documentation files (the "Software"), to deal 
 23# in the Software without restriction, including without limitation the rights 
 24# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 25# copies of the Software, and to permit persons to whom the Software is 
 26# furnished to do so, subject to the following conditions:
 27#
 28# The above copyright notice and this permission notice shall be included in all 
 29# copies or substantial portions of the Software.
 30#
 31# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 32# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 33# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 34# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 35# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 36# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 37# SOFTWARE.
 38#==================================================================================
 39# Example 1
 40#
 41
 42from __future__ import print_function
 43import qwiic_tmp102
 44import time
 45import sys
 46
 47def runExample():
 48
 49	print("\nSparkFun Qwiic TMP102 Sensor Example 1\n")
 50	myTmpSensor = qwiic_tmp102.QwiicTmp102Sensor()
 51
 52	if myTmpSensor.is_connected == False:
 53		print("The Qwiic TMP102 Sensor device isn't connected to the system. Please check your connection", \
 54			file=sys.stderr)
 55		return
 56
 57	myTmpSensor.begin()
 58
 59	print("Initialized.")
 60
 61	# Initialize sensor0 settings
 62	# These settings are saved in the sensor, even if it loses power
 63  
 64	# set the number of consecutive faults before triggering alarm.
 65	# 0-3: 0:1 fault, 1:2 faults, 2:4 faults, 3:6 faults.
 66	myTmpSensor.set_fault(0)
 67  
 68	# set the polarity of the Alarm. (0:Active LOW, 1:Active HIGH).
 69	myTmpSensor.set_alert_polarity(1)	# Active HIGH
 70  
 71	# set the sensor in Comparator Mode (0) or Interrupt Mode (1).
 72	myTmpSensor.set_alert_mode(0)	# Comparator Mode.
 73  
 74	# set the Conversion Rate (how quickly the sensor gets a new reading)
 75	#0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz
 76	myTmpSensor.set_conversion_rate(2)
 77  
 78	#set Extended Mode.
 79	#0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C)
 80	myTmpSensor.set_extended_mode(0)
 81
 82	#set T_HIGH, the upper limit to trigger the alert on
 83	myTmpSensor.set_high_temp_f(85.0)  # set T_HIGH in F
 84	#myTmpSensor.set_high_temp_c(29.4) # set T_HIGH in C
 85  
 86	#set T_LOW, the lower limit to shut turn off the alert
 87	myTmpSensor.set_low_temp_f(84.0)	# set T_LOW in F
 88	#myTmpSensor.set_low_temp_c(26.67)	# set T_LOW in C
 89		
 90	while True:
 91		myTmpSensor.wakeup()
 92		
 93		temperature = myTmpSensor.read_temp_f()
 94		
 95		# Check for alert
 96		alertRegisterState = myTmpSensor.alert()		# read the Alert from register
 97		
 98		# Place sensor in sleep mode to save power.
 99		# Current consumption typically <0.5uA.
100		myTmpSensor.sleep()
101		
102		print("Temperature: ", temperature)
103		print("Alert Register: ", alertRegisterState)
104		time.sleep(2)
105	
106
107if __name__ == '__main__':
108	try:
109		runExample()
110	except (KeyboardInterrupt, SystemExit) as exErr:
111		print("\nEnding Example 1")
112		sys.exit(0)
113
114