diff --git a/octoprint_enclosure/__init__.py b/octoprint_enclosure/__init__.py index 4a1445b..aa32c99 100644 --- a/octoprint_enclosure/__init__.py +++ b/octoprint_enclosure/__init__.py @@ -1,37 +1,82 @@ # coding=utf-8 from __future__ import absolute_import -import octoprint.plugin -import flask +from octoprint.events import eventManager, Events from octoprint.util import RepeatedTimer -import os from subprocess import Popen, PIPE +import octoprint.plugin +import RPi.GPIO as GPIO +import flask +import sched +import time +import os + +class EnclosureGPIO(): + def __init__(self, pinNumber, label, activeLow, enable, autoShutDown,isOutput,timeDelay): + self.pinNumber = pinNumber + self.label = label + self.activeLow = activeLow + self.enable = enable + self.autoShutDown = autoShutDown + self.isOutput = isOutput + self.timeDelay = timeDelay + + def configureGPIO(self): + if self.isOutput: + if self.activeLow: #if is active low, we start disabelling it by making it high! + GPIO.setup(self.pinNumber, GPIO.OUT, initial=GPIO.HIGH) + else: + GPIO.setup(self.pinNumber, GPIO.OUT, initial=GPIO.LOW) + else: + GPIO.setup(self.pinNumber, GPIO.IN, pull_up_down=GPIO.PUD_UP) + + def write(self,active): + if self.activeLow: + active = not active + + GPIO.output(self.pinNumber, active) + class EnclosurePlugin(octoprint.plugin.StartupPlugin, octoprint.plugin.TemplatePlugin, - octoprint.plugin.SettingsPlugin, - octoprint.plugin.AssetPlugin, - octoprint.plugin.BlueprintPlugin, + octoprint.plugin.SettingsPlugin, + octoprint.plugin.AssetPlugin, + octoprint.plugin.BlueprintPlugin, octoprint.plugin.EventHandlerPlugin): - enclosureSetTemperature=0.0 enclosureCurrentTemperature=0.0 enclosureCurrentHumidity=0.0 - + scheduler = sched.scheduler(time.time, time.sleep) def startGPIO(self): - self.configureGPIO(self._settings.get_int(["heaterPin"])) - self.configureGPIO(self._settings.get_int(["io1"])) - self.configureGPIO(self._settings.get_int(["io2"])) - self.configureGPIO(self._settings.get_int(["io3"])) - self.configureGPIO(self._settings.get_int(["io4"])) - - def configureGPIO(self, pin): - os.system("gpio -g mode "+str(pin)+" out") - os.system("gpio -g write "+str(pin)+" 1") - + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + + self.io1 = EnclosureGPIO(self._settings.get_int(["io1Pin"]),self._settings.get(["io1Label"]),self._settings.get(["io1ActiveLow"]), + self._settings.get(["io1Enable"]),self._settings.get(["io1AutoShutDown"]),True,self._settings.get(["io1TimeDelay"])) + + self.io2 = EnclosureGPIO(self._settings.get_int(["io2Pin"]),self._settings.get(["io2Label"]),self._settings.get(["io2ActiveLow"]), + self._settings.get(["io2Enable"]),self._settings.get(["io2AutoShutDown"]),True,self._settings.get(["io2TimeDelay"])) + + self.io3 = EnclosureGPIO(self._settings.get_int(["io3Pin"]),self._settings.get(["io3Label"]),self._settings.get(["io3ActiveLow"]), + self._settings.get(["io3Enable"]),self._settings.get(["io3AutoShutDown"]),True,self._settings.get(["io3TimeDelay"])) + + self.io4 = EnclosureGPIO(self._settings.get_int(["io4Pin"]),self._settings.get(["io4Label"]),self._settings.get(["io4ActiveLow"]), + self._settings.get(["io4Enable"]),self._settings.get(["io4AutoShutDown"]),True,self._settings.get(["io4TimeDelay"])) + + self.heater = EnclosureGPIO(self._settings.get_int(["heaterPin"]),"heater",self._settings.get(["heaterActiveLow"]), self._settings.get(["heaterEnable"]),True,True,0) + + self.filamentSensor = EnclosureGPIO(self._settings.get_int(["filamentSensorPin"]),"filamentSensor",False,self._settings.get(["filamentSensorEnable"]),False,False,0) + + self.io1.configureGPIO() + self.io2.configureGPIO() + self.io3.configureGPIO() + self.io4.configureGPIO() + self.heater.configureGPIO() + self.filamentSensor.configureGPIO() + def startTimer(self): - self._checkTempTimer = RepeatedTimer(10, self.checkEnclosureTemp, None, None, True) + self._checkTempTimer = RepeatedTimer(15, self.checkEnclosureTemp, None, None, True) self._checkTempTimer.start() def toFloat(self, value): @@ -44,7 +89,7 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, def checkEnclosureTemp(self): if self._settings.get(["dhtModel"]) == 1820 or self._settings.get(["dhtModel"]) == '1820': - stdout = Popen("sudo "+self._settings.get(["getTempScript"])+" "+str(self._settings.get(["dhtModel"])), shell=True, stdout=PIPE).stdout + stdout = Popen("sudo "+self._settings.get(["getTempScript"])+" "+str(self._settings.get(["dhtModel"])), shell=True, stdout=PIPE).stdout else: stdout = Popen("sudo "+self._settings.get(["getTempScript"])+" "+str(self._settings.get(["dhtModel"]))+" "+str(self._settings.get(["dhtPin"])), shell=True, stdout=PIPE).stdout sTemp = stdout.read() @@ -63,24 +108,39 @@ class EnclosurePlugin(octoprint.plugin.StartupPlugin, if sHum.find("Failed") != -1: self._logger.info("Failed to read Humidity") else: - self._logger.info(sHum) + # self._logger.info(sHum) self.enclosureCurrentHumidity = self.toFloat(sHum) self._plugin_manager.send_plugin_message(self._identifier, dict(enclosuretemp=self.enclosureCurrentTemperature,enclosureHumidity=self.enclosureCurrentHumidity)) self.heaterHandler() def heaterHandler(self): - command="" - if self.enclosureCurrentTemperature +
+ +
@@ -31,13 +36,13 @@ Available options are: 11 for DHT11; 22 for DHT22; 2302 for AM2302; 1820 for DS1 -
+
-
+
@@ -61,7 +66,21 @@ Available options are: 11 for DHT11; 22 for DHT22; 2302 for AM2302; 1820 for DS1
- + +
+
+ +
+
+ +
+ +
+
@@ -82,7 +101,21 @@ Available options are: 11 for DHT11; 22 for DHT22; 2302 for AM2302; 1820 for DS1
- + +
+
+ +
+
+ +
+ +
+
@@ -103,7 +136,21 @@ Available options are: 11 for DHT11; 22 for DHT22; 2302 for AM2302; 1820 for DS1
- + +
+
+ +
+
+ +
+ +
+
@@ -124,7 +171,39 @@ Available options are: 11 for DHT11; 22 for DHT22; 2302 for AM2302; 1820 for DS1
- + +
+
+ +
+
+ +
+ +
+
+ + +

{{ _('Filament Sensor') }}

+
+
+
+ +
+
+
+ +
+ +
+
+
\ No newline at end of file diff --git a/octoprint_enclosure/templates/enclosure_tab.jinja2 b/octoprint_enclosure/templates/enclosure_tab.jinja2 index d4e9b41..559ff7f 100644 --- a/octoprint_enclosure/templates/enclosure_tab.jinja2 +++ b/octoprint_enclosure/templates/enclosure_tab.jinja2 @@ -6,10 +6,10 @@ {{ _('Target') }} - {{ _('Enclosure') }} - - -
+ {{ _('Enclosure') }} + + +
°C
@@ -23,8 +23,8 @@
-
- +
+ {{ _('Humidity') }} diff --git a/setup.py b/setup.py index 68427e5..36fdef7 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ plugin_url = "https://github.com/vitormhenirque/OctoPrint-Enclosure" plugin_license = "AGPLv3" # Any additional requirements besides OctoPrint should be listed here -plugin_requires = [] +plugin_requires = ["RPi.GPIO>=0.6"] ### -------------------------------------------------------------------------------------------------------------------- ### More advanced options that you usually shouldn't have to touch follow after this point