add DHT20 support #470
43
octoprint_enclosure/DHT20.py
Normal file
43
octoprint_enclosure/DHT20.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import time
|
||||
import smbus
|
||||
import sys
|
||||
|
||||
class DHT20Error(Exception):
|
||||
""" Bast class for exception """
|
||||
|
||||
if len(sys.argv) == 2 or len(sys.argv) == 3:
|
||||
address = int(sys.argv[1],16)
|
||||
if len(sys.argv) == 3:
|
||||
busNum = int(sys.argv[2],16)
|
||||
else:
|
||||
busNum = 1
|
||||
else:
|
||||
print('-1 | -1')
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
sensor = smbus.SMBus(busNum)
|
||||
|
||||
data = sensor.read_i2c_block_data(address,0x71,1)
|
||||
if(data[0] | 0x08) == 0:
|
||||
raise DHT20Error('Initialization error')
|
||||
|
||||
|
||||
def getValue(bus):
|
||||
bus.write_i2c_block_data(address,0xac,[0x33,0x00])
|
||||
data = bus.read_i2c_block_data(address,0x71,7)
|
||||
Traw = ((data[3] & 0xf) << 16) + (data[4] << 8) + data[5]
|
||||
Hraw = ((data[3] & 0xf0) << 4) + (data[1] << 12) + (data[2] << 4)
|
||||
temp = 200*float(Traw)/2**20 - 50
|
||||
humi = 100*float(Hraw)/2**20
|
||||
return temp,humi
|
||||
|
||||
def main():
|
||||
try:
|
||||
temperature,humidity = getValue(sensor)
|
||||
print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity))
|
||||
except:
|
||||
print('-1 | -1')
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1002,6 +1002,9 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP
|
||||
if sensor['temp_sensor_type'] in ["11", "22", "2302"]:
|
||||
temp, hum = self.read_dht_temp(sensor['temp_sensor_type'], sensor['gpio_pin'])
|
||||
airquality = 0
|
||||
elif sensor['temp_sensor_type'] == "20":
|
||||
temp, hum = self.read_dht20_temp(sensor['temp_sensor_address'], sensor['temp_sensor_i2cbus'])
|
||||
airquality = 0
|
||||
elif sensor['temp_sensor_type'] == "18b20":
|
||||
temp = self.read_18b20_temp(sensor['ds18b20_serial'])
|
||||
hum = 0
|
||||
@@ -1155,6 +1158,27 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP
|
||||
self.log_error(ex)
|
||||
return (0, 0)
|
||||
|
||||
def read_dht20_temp(self, address, i2cbus):
|
||||
try:
|
||||
script = os.path.dirname(os.path.realpath(__file__)) + "/DHT20.py "
|
||||
if self._settings.get(["use_sudo"]):
|
||||
sudo_str = "sudo "
|
||||
else:
|
||||
sudo_str = ""
|
||||
cmd = sudo_str + "python " + script + str(address) + " " + str(i2cbus)
|
||||
if self._settings.get(["debug_temperature_log"]) is True:
|
||||
self._logger.debug("Temperature DHT20 cmd: %s", cmd)
|
||||
stdout = (Popen(cmd, shell=True, stdout=PIPE).stdout).read()
|
||||
if self._settings.get(["debug_temperature_log"]) is True:
|
||||
self._logger.debug("DHT20 result: %s", stdout)
|
||||
temp, hum = stdout.decode("utf-8").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_bme280_temp(self, address):
|
||||
try:
|
||||
script = os.path.dirname(os.path.realpath(__file__)) + "/BME280.py"
|
||||
|
||||
@@ -56,7 +56,7 @@ $(function () {
|
||||
self.notifications = ko.observableArray([]);
|
||||
|
||||
self.humidityCapableSensor = function(sensor){
|
||||
if (['11', '22', '2302', 'bme280', 'bme680', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){
|
||||
if (['11', '20', '22', '2302', 'bme280', 'bme680', 'am2320', 'aht10' , 'si7021', 'hum_raw_i2c', 'temp_raw_i2c'].indexOf(sensor) >= 0){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -604,6 +604,7 @@
|
||||
<select data-bind="value: temp_sensor_type">
|
||||
<option value="">Select Sensor</option>
|
||||
<option value="11">DHT11</option>
|
||||
<option value="20">DHT20</option>
|
||||
<option value="22">DHT22</option>
|
||||
<option value="2302">AM2302</option>
|
||||
<option value="18b20">DS18B20</option>
|
||||
@@ -681,7 +682,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<!-- /ko -->
|
||||
<!-- ko if: ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "am2320") || ($data.temp_sensor_type() == "aht10") || ($data.temp_sensor_type() == "tmp102") || ($data.temp_sensor_type() == "mcp9808") -->
|
||||
<!-- ko if: ($data.temp_sensor_type() == "si7021") || ($data.temp_sensor_type() == "20") || ($data.temp_sensor_type() == "bme280") || ($data.temp_sensor_type() == "am2320") || ($data.temp_sensor_type() == "aht10") || ($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">
|
||||
|
||||
Reference in New Issue
Block a user