Merge pull request #313 from Columbo818/master

Add AM2320.py
This commit was merged in pull request #313.
This commit is contained in:
Vitor de Miranda Henrique
2020-04-20 09:24:13 -05:00
committed by GitHub
4 changed files with 109 additions and 3 deletions

View File

@@ -0,0 +1,82 @@
import smbus
import time
try:
import struct
except ImportError:
import ustruct as struct
class AM2320Exception(Exception):
""" Base class for exception """
class AM2320DeviceNotFound(AM2320Exception, ValueError):
""" Device not found """
class AM2320ReadError(AM2320Exception, RuntimeError):
""" Read error or CRC mismatch """
def _crc16(data):
crc = 0xFFFF
for byte in data:
crc ^= byte
for _ in range(8):
if crc & 0x0001:
crc >>= 1
crc ^= 0xA001
else:
crc >>= 1
return crc
sensor = smbus.SMBus(1)
def getTemp(bus):
for _ in range(3):
try:
bus.write_byte(0x5C, 0x00)
except:
pass
bus.write_i2c_block_data(0x5C, 0x03, [0x02, 2])
time.sleep(0.001)
result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6))
if result[0] != 0x3 or result[1] != 2:
raise AM2320ReadError("Command does not match returned data")
temp = struct.unpack(">H", result[2:-2])[0]
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
crc2 = _crc16(result[0:-2])
if crc1 != crc2:
raise AM2320ReadError("CRC Mismatch")
if temp >= 32768:
temp = 32768 - temp
return (temp / 10.0)
def getHumi(bus):
for _ in range(3):
try:
bus.write_byte(0x5C, 0x00)
except:
pass
bus.write_i2c_block_data(0x5C, 0x03, [0x00, 2])
time.sleep(0.001)
result = bytearray(bus.read_i2c_block_data(0x5C, 0x00, 6))
if result[0] != 0x3 or result[1] != 2:
raise AM2320ReadError("Command does not match returned data")
humi = struct.unpack(">H", result[2:-2])[0]
crc1 = struct.unpack("<H", bytes(result[-2:]))[0]
crc2 = _crc16(result[0:-2])
if crc1 != crc2:
raise AM2320ReadError("CRC Mismatch")
return (humi / 10.0)
def main():
try:
temperature = getTemp(sensor)
humidity = getHumi(sensor)
print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity))
except:
print('-1 | -1')
if __name__ == "__main__":
main()

View File

@@ -797,6 +797,8 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP
hum = 0
elif sensor['temp_sensor_type'] == "bme280":
temp, hum = self.read_bme280_temp(sensor['temp_sensor_address'])
elif sensor['temp_sensor_type'] == "am2320":
temp, hum = self.read_am2320_temp() # sensor has fixed address
elif sensor['temp_sensor_type'] == "si7021":
temp, hum = self.read_si7021_temp(sensor['temp_sensor_address'])
elif sensor['temp_sensor_type'] == "tmp102":
@@ -910,6 +912,27 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP
self.log_error(ex)
return (0, 0)
def read_am2320_temp(self):
try:
script = os.path.dirname(os.path.realpath(__file__)) + "/AM2320.py "
if self._settings.get(["use_sudo"]):
sudo_str = "sudo "
else:
sudo_str = ""
cmd = sudo_str + "python " + script # sensor has fixed address 0x5C
if self._settings.get(["debug_temperature_log"]) is True:
self._logger.debug("Temperature AM2320 cmd: %s", cmd)
stdout = (Popen(cmd, shell=True, stdout=PIPE).stdout).read()
if self._settings.get(["debug_temperature_log"]) is True:
self._logger.debug("AM2320 result: %s", stdout)
temp, hum = stdout.split("|")
return (self.to_float(temp.strip()), self.to_float(hum.strip()))
except Exception as ex:
self._logger.info(
"Failed to execute python scripts, try disabling use SUDO on advanced section of the plugin.")
self.log_error(ex)
return (0, 0)
def read_si7021_temp(self, address):
try:
script = os.path.dirname(os.path.realpath(__file__)) + "/SI7021.py "

View File

@@ -56,7 +56,7 @@ $(function () {
self.notifications = ko.observableArray([]);
self.humidityCapableSensor = function(sensor){
if (['11', '22', '2302', 'bme280', 'si7021'].indexOf(sensor) >= 0){
if (['11', '22', '2302', 'bme280', 'am2320', 'si7021'].indexOf(sensor) >= 0){
return true;
}
return false;

View File

@@ -536,6 +536,7 @@
<option value="18b20">DS18B20</option>
<option value="si7021">SI7021</option>
<option value="bme280">BME280</option>
<option value="am2320">AM2320</option>
<option value="tmp102">TMP102</option>
<option value="max31855">MAX31855</option>
<option value="mcp9808">MCP9808</option>
@@ -563,7 +564,7 @@
</div>
</div>
<!-- /ko -->
<!-- ko if: ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "tmp102") || ($data.temp_sensor_type() == "mcp9808") -->
<!-- ko if: ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "am2320") || ($data.temp_sensor_type() == "tmp102") || ($data.temp_sensor_type() == "mcp9808") -->
<div class="control-group">
<label class="control-label" for="settings-enclosure-dhtPin">{{ _('Sensor Pin') }}</label>
<div class="controls">
@@ -605,7 +606,7 @@
</div>
<!-- /ko -->
<!-- ko ifnot: ($data.temp_sensor_type() == "18b20") || ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "tmp102") || ($data.temp_sensor_type() == "max31855") || ($data.temp_sensor_type() == "mcp9808") -->
<!-- ko ifnot: ($data.temp_sensor_type() == "18b20") || ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "am2320") || ($data.temp_sensor_type() == "tmp102") || ($data.temp_sensor_type() == "max31855") || ($data.temp_sensor_type() == "mcp9808") -->
<div class="control-group">
<label class="control-label" for="settings-enclosure-dhtPin">{{ _('Sensor Pin') }}</label>
<div class="controls">