Add support for TMP102 temperature sensor #49

Merged
skyhisi merged 2 commits from master into master 2017-07-11 21:40:35 +01:00
3 changed files with 76 additions and 4 deletions

View File

@@ -186,6 +186,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin,
hum = 0
elif temp_reader['sensorType'] == "si7021":
temp, hum = self.readSI7021Temp(temp_reader['sensorAddress'])
elif temp_reader['sensorType'] == "tmp102":
temp = self.readTmp102Temp(temp_reader['sensorAddress'])
hum = 0
else:
self._logger.info("sensorType no match")
temp = 0
@@ -296,6 +299,24 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin,
f.close()
return lines
def readTmp102Temp(self, address):
try:
script = os.path.dirname(os.path.realpath(__file__)) + "/tmp102.py"
args = ["python", script, str(address)]
if self._settings.get(["debug"]) == True and not self.disable_temeprature_log:
self._logger.info("Temperature TMP102 cmd: %s", " ".join(args))
proc = Popen(args, stdout=PIPE)
stdout, _ = proc.communicate()
if self._settings.get(["debug"]) == True and not self.disable_temeprature_log:
self._logger.info("TMP102 result: %s", stdout)
return self.toFloat(stdout.strip())
except Exception as ex:
template = "An exception of type {0} occurred on readTmp102Temp. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
self._logger.warn(message)
return 0
def handleTemperatureControl(self):
for control in self.temperature_control:
if control['isEnabled'] == True:
@@ -342,7 +363,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin,
def clearGPIO(self):
try:
for control in self.temperature_control:
GPIO.cleanup(self.toInt(control['gpioPin']))
if control['isEnabled']:
GPIO.cleanup(self.toInt(control['gpioPin']))
for rpi_output in self.rpi_outputs:
if self.toInt(rpi_output['gpioPin']) not in self.previous_rpi_outputs:
GPIO.cleanup(self.toInt(rpi_output['gpioPin']))
@@ -370,7 +392,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin,
def configureGPIO(self):
try:
for control in self.temperature_control:
GPIO.setup(self.toInt(control['gpioPin']), GPIO.OUT, initial=GPIO.HIGH if control['activeLow'] else GPIO.LOW)
if control['isEnabled']:
GPIO.setup(self.toInt(control['gpioPin']), GPIO.OUT, initial=GPIO.HIGH if control['activeLow'] else GPIO.LOW)
for rpi_output in self.rpi_outputs:
pin = self.toInt(rpi_output['gpioPin'])
if rpi_output['outputType'] == 'regular':

View File

@@ -19,6 +19,7 @@
<option value="18b20">DS18B20</option>
<option value="si7021">SI7021</option>
<option value="bme280">BME280</option>
<option value="tmp102">TMP102</option>
</select>
<span class="help-inline"> <span class="label label-important">Attention</span> You need to install and configure the necessary
libraries for the temeprature sensor, check the documentation on <a href=" https://github.com/vitormhenrique/OctoPrint-Enclosure">github</a> page</span>
@@ -33,7 +34,7 @@
</div>
</div>
<!-- /ko -->
<!-- ko if: ($data.sensorType() == "si7021") || ($data.sensorType() == "bme280") -->
<!-- ko if: ($data.sensorType() == "si7021") || ($data.sensorType() == "bme280") || ($data.sensorType() == "tmp102") -->
<div class="control-group">
<label class="control-label" for="settings-enclosure-dhtPin">{{ _('Sensor Pin') }}</label>
<div class="controls">
@@ -49,7 +50,7 @@
</div>
</div>
<!-- /ko -->
<!-- ko ifnot: ($data.sensorType() == "18b20") || ($data.sensorType() == "si7021") || ($data.sensorType() == "bme280") -->
<!-- ko ifnot: ($data.sensorType() == "18b20") || ($data.sensorType() == "si7021") || ($data.sensorType() == "bme280") || ($data.sensorType() == "tmp102") -->
<div class="control-group">
<label class="control-label" for="settings-enclosure-dhtPin">{{ _('Sensor Pin') }}</label>
<div class="controls">

View File

@@ -0,0 +1,48 @@
import ctypes
import struct
import sys
import smbus
def main():
# Get bus address if provided or use default address
address = 0x48
if len(sys.argv) >= 2:
address = int(sys.argv[1], 0)
if not 0x48 <= address <= 0x4b:
raise ValueError("Invalid address value")
# Connect to I2C bus (use 0 on original Raspberry Pi, 1 on later models)
bus = smbus.SMBus(1)
# Set pointer to the temperature register
bus.write_byte(address, 0)
# Read two byte value
temp = bus.read_word_data(address, 0)
# Disconnect from bus
bus.close()
# Byte swap
temp = struct.unpack(">H", struct.pack("<H", temp))[0]
# Shift
temp = temp >> 4
# Convert to 2 byte twos compliment negative if negative
if ((temp & 0x800) != 0):
temp |= 0xF800
# Convert into a signed number
temp = ctypes.c_short(temp).value
# Divide by 16 to get value in celsius
temp /= 16.0
print temp
if __name__ == "__main__":
main()