From ea393e7ee1f7a27207e1ba10fd2e763f110130da Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Tue, 26 Oct 2021 21:37:55 +0200 Subject: [PATCH 1/7] use RPi.bme280 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f2ccc04..13655f7 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ plugin_url = "https://github.com/vitormhenrique/OctoPrint-Enclosure" plugin_license = "AGPLv3" # Any additional requirements besides OctoPrint should be listed here -plugin_requires = ["RPi.GPIO>=0.6.5","requests>=2.7","smbus2>=0.3.0","gpiozero==1.6.2"] +plugin_requires = ["RPi.GPIO>=0.6.5", "requests>=2.7", "smbus2>=0.3.0", "gpiozero==1.6.2", "RPi.bme280"] additional_setup_parameters = {} From f06e1e6d57f75d7e61e05278c079bef4ea4a306c Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Tue, 26 Oct 2021 21:40:51 +0200 Subject: [PATCH 2/7] use RPi.bme280 --- octoprint_enclosure/BME280.py | 157 ++++------------------------------ 1 file changed, 16 insertions(+), 141 deletions(-) diff --git a/octoprint_enclosure/BME280.py b/octoprint_enclosure/BME280.py index fa5f7c0..e6710ed 100644 --- a/octoprint_enclosure/BME280.py +++ b/octoprint_enclosure/BME280.py @@ -1,151 +1,26 @@ -import smbus import sys -import time -from ctypes import c_short -from ctypes import c_byte -from ctypes import c_ubyte +import smbus2 +import bme280 + +# Rev 2 Pi, Pi 2 & Pi 3 uses bus 1 +# Rev 1 Pi uses bus 0 +port = 1 +bus = smbus2.SMBus(port) if len(sys.argv) == 2: - DEVICE = int(sys.argv[1],16) + DEVICE = int(sys.argv[1], 16) else: print('-1 | -1') sys.exit(1) -bus = smbus.SMBus(1) # Rev 2 Pi, Pi 2 & Pi 3 uses bus 1 - # Rev 1 Pi uses bus 0 - -def getShort(data, index): - # return two bytes from data as a signed 16-bit value - return c_short((data[index+1] << 8) + data[index]).value - -def getUShort(data, index): - # return two bytes from data as an unsigned 16-bit value - return (data[index+1] << 8) + data[index] - -def getChar(data,index): - # return one byte from data as a signed char - result = data[index] - if result > 127: - result -= 256 - return result - -def getUChar(data,index): - # return one byte from data as an unsigned char - result = data[index] & 0xFF - return result - -def readBME280ID(addr=DEVICE): - # Chip ID Register Address - REG_ID = 0xD0 - (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2) - return (chip_id, chip_version) - -def readBME280All(addr=DEVICE): - # Register Addresses - REG_DATA = 0xF7 - REG_CONTROL = 0xF4 - REG_CONFIG = 0xF5 - - REG_CONTROL_HUM = 0xF2 - REG_HUM_MSB = 0xFD - REG_HUM_LSB = 0xFE - - # Oversample setting - page 27 - OVERSAMPLE_TEMP = 2 - OVERSAMPLE_PRES = 2 - MODE = 1 - - # Oversample setting for humidity register - page 26 - OVERSAMPLE_HUM = 2 - bus.write_byte_data(addr, REG_CONTROL_HUM, OVERSAMPLE_HUM) - - control = OVERSAMPLE_TEMP<<5 | OVERSAMPLE_PRES<<2 | MODE - bus.write_byte_data(addr, REG_CONTROL, control) - - # Read blocks of calibration data from EEPROM - # See Page 22 data sheet - cal1 = bus.read_i2c_block_data(addr, 0x88, 24) - cal2 = bus.read_i2c_block_data(addr, 0xA1, 1) - cal3 = bus.read_i2c_block_data(addr, 0xE1, 7) - - # Convert byte data to word values - dig_T1 = getUShort(cal1, 0) - dig_T2 = getShort(cal1, 2) - dig_T3 = getShort(cal1, 4) - - dig_P1 = getUShort(cal1, 6) - dig_P2 = getShort(cal1, 8) - dig_P3 = getShort(cal1, 10) - dig_P4 = getShort(cal1, 12) - dig_P5 = getShort(cal1, 14) - dig_P6 = getShort(cal1, 16) - dig_P7 = getShort(cal1, 18) - dig_P8 = getShort(cal1, 20) - dig_P9 = getShort(cal1, 22) - - dig_H1 = getUChar(cal2, 0) - dig_H2 = getShort(cal3, 0) - dig_H3 = getUChar(cal3, 2) - - dig_H4 = getChar(cal3, 3) - dig_H4 = (dig_H4 << 24) >> 20 - dig_H4 = dig_H4 | (getChar(cal3, 4) & 0x0F) - - dig_H5 = getChar(cal3, 5) - dig_H5 = (dig_H5 << 24) >> 20 - dig_H5 = dig_H5 | (getUChar(cal3, 4) >> 4 & 0x0F) - - dig_H6 = getChar(cal3, 6) - - # Wait in ms (Datasheet Appendix B: Measurement time and current calculation) - wait_time = 1.25 + (2.3 * OVERSAMPLE_TEMP) + ((2.3 * OVERSAMPLE_PRES) + 0.575) + ((2.3 * OVERSAMPLE_HUM)+0.575) - time.sleep(wait_time/1000) # Wait the required time - - # Read temperature/pressure/humidity - data = bus.read_i2c_block_data(addr, REG_DATA, 8) - pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4) - temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4) - hum_raw = (data[6] << 8) | data[7] - - #Refine temperature - var1 = ((((temp_raw>>3)-(dig_T1<<1)))*(dig_T2)) >> 11 - var2 = (((((temp_raw>>4) - (dig_T1)) * ((temp_raw>>4) - (dig_T1))) >> 12) * (dig_T3)) >> 14 - t_fine = var1+var2 - temperature = float(((t_fine * 5) + 128) >> 8); - - # Refine pressure and adjust for temperature - var1 = t_fine / 2.0 - 64000.0 - var2 = var1 * var1 * dig_P6 / 32768.0 - var2 = var2 + var1 * dig_P5 * 2.0 - var2 = var2 / 4.0 + dig_P4 * 65536.0 - var1 = (dig_P3 * var1 * var1 / 524288.0 + dig_P2 * var1) / 524288.0 - var1 = (1.0 + var1 / 32768.0) * dig_P1 - if var1 == 0: - pressure=0 - else: - pressure = 1048576.0 - pres_raw - pressure = ((pressure - var2 / 4096.0) * 6250.0) / var1 - var1 = dig_P9 * pressure * pressure / 2147483648.0 - var2 = pressure * dig_P8 / 32768.0 - pressure = pressure + (var1 + var2 + dig_P7) / 16.0 - - # Refine humidity - humidity = t_fine - 76800.0 - humidity = (hum_raw - (dig_H4 * 64.0 + dig_H5 / 16384.0 * humidity)) * (dig_H2 / 65536.0 * (1.0 + dig_H6 / 67108864.0 * humidity * (1.0 + dig_H3 / 67108864.0 * humidity))) - humidity = humidity * (1.0 - dig_H1 * humidity / 524288.0) - if humidity > 100: - humidity = 100 - elif humidity < 0: - humidity = 0 - - return temperature/100.0,pressure/100.0,humidity - def main(): try: - temperature,pressure,humidity = readBME280All() - print('{0:0.1f} | {1:0.1f}'.format(temperature, humidity)) - except: - print('-1 | -1') + calibration_params = bme280.load_calibration_params(bus, DEVICE) -if __name__=="__main__": - main() + # the sample method will take a single reading and return a + # compensated_reading object + data = bme280.sample(bus, DEVICE, calibration_params) + + print('{0:0.1f} | {1:0.1f}'.format(data.temperature, data.humidity)) + except Exception: + print('-1 | -1') From 5798c8e313f6af15b615ef350b55e4e7306c931c Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Tue, 26 Oct 2021 21:43:17 +0200 Subject: [PATCH 3/7] use sys.executable --- octoprint_enclosure/__init__.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 86c1077..63e3b5d 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1120,20 +1120,23 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplateP def read_bme280_temp(self, address): try: - script = os.path.dirname(os.path.realpath(__file__)) + "/BME280.py " + script = os.path.dirname(os.path.realpath(__file__)) + "/BME280.py" + cmd = [sys.executable, script, str(address)] if self._settings.get(["use_sudo"]): - sudo_str = "sudo " - else: - sudo_str = "" - cmd = sudo_str + "python " + script + str(address) + cmd.insert(0, "sudo") if self._settings.get(["debug_temperature_log"]) is True: self._logger.debug("Temperature BME280 cmd: %s", cmd) - stdout = (Popen(cmd, shell=True, stdout=PIPE).stdout).read() - if self._settings.get(["debug_temperature_log"]) is True: - self._logger.debug("BME280 result: %s", stdout) - temp, hum = stdout.decode("utf-8").split("|") - + stdout = Popen(cmd, stdout=PIPE, stderr=PIPE, universal_newlines=True) + output, errors = stdout.communicate() + + if self._settings.get(["debug_temperature_log"]) is True: + if len(errors) > 0: + self._logger.error("BME280 error: %s", errors) + else: + self._logger.debug("BME280 result: %s", output) + + temp, hum = output.split("|") return (self.to_float(temp.strip()), self.to_float(hum.strip())) except Exception as ex: self._logger.info( From 8d1e339086f520a5e3b3f4a5975094ab60fbcee3 Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Wed, 27 Oct 2021 02:03:53 +0200 Subject: [PATCH 4/7] move bus declaration --- octoprint_enclosure/BME280.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/octoprint_enclosure/BME280.py b/octoprint_enclosure/BME280.py index e6710ed..d38f16a 100644 --- a/octoprint_enclosure/BME280.py +++ b/octoprint_enclosure/BME280.py @@ -2,17 +2,16 @@ import sys import smbus2 import bme280 -# Rev 2 Pi, Pi 2 & Pi 3 uses bus 1 -# Rev 1 Pi uses bus 0 -port = 1 -bus = smbus2.SMBus(port) - if len(sys.argv) == 2: DEVICE = int(sys.argv[1], 16) else: print('-1 | -1') sys.exit(1) +# Rev 2 Pi, Pi 2 & Pi 3 & Pi 4 use bus 1 +# Rev 1 Pi uses bus 0 +bus = smbus2.SMBus(1) + def main(): try: calibration_params = bme280.load_calibration_params(bus, DEVICE) From c32b20832c961f52cea7e97f3da1b687a46529e1 Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Wed, 27 Oct 2021 02:12:18 +0200 Subject: [PATCH 5/7] doc update --- octoprint_enclosure/BME280.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/octoprint_enclosure/BME280.py b/octoprint_enclosure/BME280.py index d38f16a..3fd387a 100644 --- a/octoprint_enclosure/BME280.py +++ b/octoprint_enclosure/BME280.py @@ -15,11 +15,8 @@ bus = smbus2.SMBus(1) def main(): try: calibration_params = bme280.load_calibration_params(bus, DEVICE) - - # the sample method will take a single reading and return a - # compensated_reading object data = bme280.sample(bus, DEVICE, calibration_params) print('{0:0.1f} | {1:0.1f}'.format(data.temperature, data.humidity)) - except Exception: + except: print('-1 | -1') From 20312381d55ebcd4e8697256e4894526e4062584 Mon Sep 17 00:00:00 2001 From: Thijs Triemstra Date: Wed, 27 Oct 2021 02:15:32 +0200 Subject: [PATCH 6/7] restore __main__ --- octoprint_enclosure/BME280.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/octoprint_enclosure/BME280.py b/octoprint_enclosure/BME280.py index 3fd387a..c5c93dc 100644 --- a/octoprint_enclosure/BME280.py +++ b/octoprint_enclosure/BME280.py @@ -20,3 +20,6 @@ def main(): print('{0:0.1f} | {1:0.1f}'.format(data.temperature, data.humidity)) except: print('-1 | -1') + +if __name__== "__main__": + main() From 4ca82c1036936b4ada93b0845123973b4cb27a02 Mon Sep 17 00:00:00 2001 From: David Ross Smith <5095074+DragRedSim@users.noreply.github.com> Date: Mon, 1 Nov 2021 18:36:05 +1100 Subject: [PATCH 7/7] Changed README.md to new Adafruit_DHT library The library used has changed, but the instructions were never updated to explain how to install the new library. This corrects that oversight. --- README.md | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index f6f4270..825bd8e 100644 --- a/README.md +++ b/README.md @@ -45,32 +45,23 @@ To control the enclosure temperature or get temperature triggered events, you ne #### DHT11, DHT22 and AM2302 sensors -Wire the sensor following the wiring diagram on the pictures on thingiverse, you can use any GPIO pin. +Wire the sensor following the wiring diagram on the pictures on thingiverse; you can use any GPIO pin. For DHT11 and DHT22 sensors, don't forget to connect a 4.7K - 10K resistor from the data pin to VCC. Also, be aware that DHT sensors some times can not work reliably on linux, this is a limitation of reading DHT sensors from Linux--there's no guarantee the program will be given enough priority and time by the Linux kernel to reliably read the sensor. Another common issue is the power supply. you need a constant and good 3.3V, sometimes a underpowered raspberry pi will not have a solid 3.3V power supply, so you could try powering the sensor with 5V and using a level shifter on the read pin. -You need to install Adafruit library to use the temperature sensor on raspberry pi. +You need to install the Adafruit library to use the temperature sensor on Raspberry Pi. Recent versions of this plugin have moved to supporting the CircuitPython-based library on Python3 installed below. Open a raspberry pi terminal and type: ``` -cd ~ -git clone https://github.com/adafruit/Adafruit_Python_DHT.git -cd Adafruit_Python_DHT +pip3 install adafruit-circuitpython-dht sudo apt-get update -sudo apt-get install build-essential python-dev python-openssl -sudo python setup.py install +sudo apt-get install libgpiod2 -y ``` -Note: All libraries need to be installed on raspberry pi system python not octoprint virtual environment. +This will install all requirements of the library. -You can test the library by using: - -``` -cd examples -sudo ./AdafruitDHT.py 2302 4 -``` -Note that the first argument is the temperature sensor (11, 22, or 2302), and the second argument is the GPIO that the sensor was connected. +You can test the library by using the sample code from https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging/python-setup. #### DS18B20 sensor