Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for shelly wi-fi witch #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# OctoLight
A simple plugin that adds a button to the navigation bar for toggleing a GPIO pin on the Raspberry Pi.
A simple plugin that adds a button to the navigation bar for toggleing a GPIO pin on the Raspberry Pi or send a HTML GET request to shelly wi-fi switch

![WebUI interface](img/screenshoot.png)

Expand All @@ -22,6 +22,9 @@ Curently, you can configure two settings:
- `Inverted output`: If true, the output will be inverted
- Usage: if you have a light, that is turned off when voltage is applied to the pin (wired in negative logic), you should turn on this option, so the light isn't on when you reboot your Raspberry Pi.

- `Use Shelly Wi-fi switch to turn on/off lights instead of GPIO`
- `Shelly IP address`: type in local ip address like 192.168.178.71

## API
Base API URL : `GET http://YOUR_OCTOPRINT_SERVER/api/plugin/octolight?action=ACTION_NAME`

Expand Down
Binary file modified img/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 24 additions & 10 deletions octoprint_octolight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import RPi.GPIO as GPIO

import requests

GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)

Expand Down Expand Up @@ -51,14 +53,19 @@ def on_after_startup(self):
self._settings.get(["light_pin"]),
self._settings.get(["inverted_output"])
))
self._logger.info("Shelly IP address: {}, use shelly instead of GPIO: {}".format(
self._settings.get(["shelly_ip"]),
self._settings.get(["use_shelly"])
))
self._logger.info("--------------------------------------------")

# Setting the default state of pin
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)
if bool(self._settings.get(["inverted_output"])):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
else:
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)

if not self._settings.get(["use_shelly"]):
# Setting the default state of pin
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)
if bool(self._settings.get(["inverted_output"])):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
else:
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)

#Because light is set to ff on startup we don't need to retrieve the current state
"""
Expand All @@ -77,15 +84,22 @@ def on_after_startup(self):

def light_toggle(self):
# Sets the GPIO every time, if user changed it in the settings.
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)
if not self._settings.get(["use_shelly"]):
GPIO.setup(int(self._settings.get(["light_pin"])), GPIO.OUT)

self.light_state = not self.light_state

# Sets the light state depending on the inverted output setting (XOR)
if self.light_state ^ self._settings.get(["inverted_output"]):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
if not self._settings.get(["use_shelly"]):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.HIGH)
else:
r = requests.get('http://' + self._settings.get(["shelly_ip"]) + '/relay/0?turn=on')
else:
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)
if not self._settings.get(["use_shelly"]):
GPIO.output(int(self._settings.get(["light_pin"])), GPIO.LOW)
else:
r = requests.get('http://' + self._settings.get(["shelly_ip"]) + '/relay/0?turn=off')

self._logger.info("Got request. Light state: {}".format(
self.light_state
Expand Down
18 changes: 16 additions & 2 deletions octoprint_octolight/templates/octolight_settings.jinja2
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
<form class="form-horizontal">
<h3>OctoLight settings</h3>
<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.octolight.use_shelly" checked>
{{ _('Use Shelly Wi-fi switch to turn on/off lights instead of GPIO') }}
</label>
</div>
<div class="control-group">
<label class="control-label">{{ _('Light PIN') }}</label>
<div class="controls">
<input id="light_pin-input" type="number" min="1" max="40" class="input-small" data-bind="value: settings.plugins.octolight.light_pin">
</div>

<div class="controls">
<label class="checkbox">
<input type="checkbox" data-bind="checked: settings.plugins.octolight.inverted_output">
{{ _('Inverted output') }}
</label>
</div>
</div>
</form>

<div class="control-group2">
<label class="control-label">{{ _('Shelly IP address') }}</label>
<div class="controls">
<input id="light_pin-input" type="text" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" data-bind="value: settings.plugins.octolight.shelly_ip">

</div>
</div>

</form>