├── pisugar2.py └── readme.md /pisugar2.py: -------------------------------------------------------------------------------- 1 | # Gets status of Pisugar2 - requires installing the PiSugar-Power-Manager 2 | # curl http://cdn.pisugar.com/release/Pisugar-power-manager.sh | sudo bash 3 | # 4 | # based on https://github.com/evilsocket/pwnagotchi/blob/master/pwnagotchi/plugins/default/ups_lite.py 5 | # https://www.tindie.com/products/pisugar/pisugar2-battery-for-raspberry-pi-zero/ 6 | import logging 7 | 8 | from pwnagotchi.ui.components import LabeledValue 9 | from pwnagotchi.ui.view import BLACK 10 | import pwnagotchi.ui.fonts as fonts 11 | import pwnagotchi.plugins as plugins 12 | import pwnagotchi 13 | import time 14 | 15 | class PiSugar(plugins.Plugin): 16 | __author__ = "10230718+tisboyo@users.noreply.github.com" 17 | __version__ = "0.0.1" 18 | __license__ = "GPL3" 19 | __description__ = "A plugin that will add a voltage indicator for the PiSugar 2" 20 | 21 | def __init__(self): 22 | self.ps = None 23 | self.is_charging = False 24 | self.is_new_model = False 25 | 26 | def on_loaded(self): 27 | # Load here so it doesn't attempt to load if the plugin is not enabled 28 | from pisugar2 import PiSugar2 29 | 30 | self.ps = PiSugar2() 31 | logging.info("[pisugar2] plugin loaded.") 32 | 33 | if self.ps.get_battery_led_amount().value == 2: 34 | self.is_new_model = True 35 | else: 36 | self.is_new_model = False 37 | 38 | if self.options["sync_rtc_on_boot"]: 39 | self.ps.set_pi_from_rtc() 40 | 41 | def on_ui_setup(self, ui): 42 | ui.add_element( 43 | "bat", 44 | LabeledValue( 45 | color=BLACK, 46 | label="BAT", 47 | value="0%", 48 | position=(ui.width() / 2 + 15, 0), 49 | label_font=fonts.Bold, 50 | text_font=fonts.Medium, 51 | ), 52 | ) 53 | # display charging status 54 | if self.is_new_model: 55 | ui.add_element( 56 | "chg", 57 | LabeledValue( 58 | color=BLACK, 59 | label="", 60 | value="", 61 | position=(ui.width() / 2 - 12, 0), 62 | label_font=fonts.Bold, 63 | text_font=fonts.Bold, 64 | ), 65 | ) 66 | 67 | def on_unload(self, ui): 68 | with ui._lock: 69 | ui.remove_element("bat") 70 | ui.remove_element("chg") 71 | 72 | def on_ui_update(self, ui): 73 | capacity = int(self.ps.get_battery_percentage().value) 74 | 75 | # new model use battery_power_plugged & battery_allow_charging to detect real charging status 76 | if self.is_new_model: 77 | if self.ps.get_battery_power_plugged().value and self.ps.get_battery_allow_charging().value: 78 | ui.set("chg", "CHG") 79 | if not self.is_charging: 80 | ui.update(force=True, new_data={"status": "Power!! I can feel it!"}) 81 | self.is_charging = True 82 | else: 83 | ui.set("chg", "") 84 | self.is_charging = False 85 | 86 | ui.set("bat", str(capacity) + "%") 87 | 88 | if capacity <= self.options["shutdown"]: 89 | logging.info( 90 | f"[pisugar2] Empty battery (<= {self.options['shutdown']}): shuting down" 91 | ) 92 | ui.update(force=True, new_data={"status": "Battery exhausted, bye ..."}) 93 | time.sleep(3) 94 | pwnagotchi.shutdown() 95 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Plugin to use with Pwnagotchi and the PiSugar2. 2 | 3 | ## This plugin DOES require using the official [pisugar-power-manager-rs](https://github.com/PiSugar/pisugar-power-manager-rs) install. 4 | 5 | ## Install guide: 6 | 7 | ```bash 8 | # Go to the home directory 9 | cd ~ 10 | 11 | # Install PiSugar Power Manager 12 | curl http://cdn.pisugar.com/release/Pisugar-power-manager.sh | sudo bash 13 | 14 | # Download the plugin and support library 15 | git clone https://github.com/PiSugar/pisugar2py.git 16 | git clone https://github.com/PiSugar/pwnagotchi-pisugar2-plugin.git 17 | 18 | #Make the installed-plugins directory if it doesn't already exist 19 | sudo mkdir -p /usr/local/share/pwnagotchi/installed-plugins/ 20 | 21 | # This installs the pisugar2 package into your python library 22 | sudo ln -s ~/pisugar2py/ /usr/local/lib/python3.7/dist-packages/pisugar2 23 | 24 | # Installs the user-plugin 25 | sudo ln -s ~/pwnagotchi-pisugar2-plugin/pisugar2.py /usr/local/share/pwnagotchi/installed-plugins/pisugar2.py 26 | 27 | 28 | ``` 29 | 30 | 31 | In /etc/pwnagotchi/config.toml add: 32 | ```toml 33 | main.custom_plugins = "/usr/local/share/pwnagotchi/installed-plugins/" 34 | main.plugins.pisugar2.enabled = true 35 | main.plugins.pisugar2.shutdown = 5 36 | main.plugins.pisugar2.sync_rtc_on_boot = true 37 | ``` 38 | 39 | 40 | 41 | PiSugar2 web settings are accessible at http://10.0.0.2:8421/#/ 42 | 43 | The support library exposes all of the available commands from the pi-sugar in a python library for developers 44 | --------------------------------------------------------------------------------