diff --git a/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psd1 b/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psd1 new file mode 100644 index 0000000..b495f7e --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psd1 @@ -0,0 +1,30 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +@{ + GUID="0432ee36-7e87-4a21-814f-8feb17974647" + Author="Microsoft Corporation" + CompanyName="Microsoft Corporation" + Copyright="© Microsoft Corporation. All rights reserved." + Description='PowerShell module for controling a fan over GPIO.' + ModuleVersion="0.1.0" + FunctionsToExport = @('Enable-Fan','Disable-Fan') + CmdletsToExport = @() + AliasesToExport = @() + RootModule = 'Microsoft.PowerShell.IoT.Fan.psm1' + NestedModules=@('Microsoft.PowerShell.IoT') + HelpInfoURI = 'https://github.com/PowerShell/PowerShell-IoT' + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + PSData = @{ + # Tags applied to this module. These help with module discovery in online galleries. + Tags = 'IoT','RaspberryPi','Raspbian' + + # A URL to the license for this module. + LicenseUri = 'https://github.com/PowerShell/PowerShell-IoT/blob/master/LICENSE.txt' + + # A URL to the main website for this project. + ProjectUri = 'https://github.com/PowerShell/PowerShell-IoT' + } + } +} diff --git a/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psm1 b/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psm1 new file mode 100644 index 0000000..65c3a48 --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psm1 @@ -0,0 +1,25 @@ +function Enable-Fan +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true, Position=0)] + [ValidateNotNullOrEmpty()] + [int] $Pin + ) + + Set-GpioPin -Id $Pin -Value High +} + +function Disable-Fan +{ + [CmdletBinding()] + param + ( + [Parameter(Mandatory=$true, Position=0)] + [ValidateNotNullOrEmpty()] + [int] $Pin + ) + + Set-GpioPin -Id $Pin -Value Low +} diff --git a/Examples/Microsoft.PowerShell.IoT.Fan/README.md b/Examples/Microsoft.PowerShell.IoT.Fan/README.md new file mode 100644 index 0000000..783b685 --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.Fan/README.md @@ -0,0 +1,56 @@ +# Example module Microsoft.PowerShell.IoT.Fan + +This PowerShell module is for turning on/off a fan on Raspberry Pi 4 case enclosure. +This showcases GPIO functionality of [the Microsoft.PowerShell.IoT module](../../README.md). + +## Hardware setup + +[This Raspberry Pi 4 case enclosure](https://www.amazon.com/gp/product/B07XTRK8D4) comes with a 5V fan that can be connected to Raspberry Pi 5V and GND pins. +This fan is nice but a little noisy so we can use this example module to turn it off when the CPU temperature is relatively low. +An [IRLB8721 transistor](https://www.adafruit.com/product/355) can be used to switch power to the fan based on GPIO line of Raspberry Pi. + +## Wiring + +Insert IRLB8721 transistor into the break of the negative wire of the fan. +Connect transistor gate to GPIO 17 (BCM schema) on Raspberry Pi. + +![Mossfet1](https://user-images.githubusercontent.com/11860095/79925701-d6a85580-83ef-11ea-894d-93507507df5e.jpg) +![Mossfet2](https://user-images.githubusercontent.com/11860095/79925725-e6c03500-83ef-11ea-9abd-7dd39be69bd1.jpg) + +## Software setup + +### Install PowerShell Core on Raspberry Pi + +Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian). + +### Start Powershell and install modules + +```powershell +pwsh + +Install-Module -Name Microsoft.PowerShell.IoT + +git clone https://github.com/PowerShell/PowerShell-IoT.git +``` + +### Usage + +```powershell +# Start monitoring CPU temperature and turn on the fan when it reaches 71 degrees; turn fan off when CPU temperature drops below 55 degrees +pwsh ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 -Pin 17 -OnTemperature 71 -OffTemperature 55 -TemperatureScale Celsius +VERBOSE: 1:36:05 PM: CPU temperature = 71.575 C | 160.835 F +VERBOSE: Starting fan... +VERBOSE: 1:36:10 PM: CPU temperature = 70.601 C | 159.0818 F +VERBOSE: 1:36:16 PM: CPU temperature = 70.114 C | 158.2052 F +VERBOSE: 1:36:21 PM: CPU temperature = 68.653 C | 155.5754 F +#... +VERBOSE: 1:39:01 PM: CPU temperature = 55.504 C | 131.9072 F +VERBOSE: 1:39:06 PM: CPU temperature = 55.504 C | 131.9072 F +VERBOSE: 1:39:11 PM: CPU temperature = 54.043 C | 129.2774 F +VERBOSE: Stopping fan... +VERBOSE: 1:39:17 PM: CPU temperature = 55.991 C | 132.7838 F +#... +``` + +This produces following CPU temperature graph: +![CPU-Temp-graph](https://user-images.githubusercontent.com/11860095/79926138-fbe99380-83f0-11ea-8705-b1336fc7bd7d.png) \ No newline at end of file diff --git a/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 b/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 new file mode 100644 index 0000000..9aba64b --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.Fan/SmartFan.ps1 @@ -0,0 +1,40 @@ +param +( + [int] $Pin = 17, + [int] $OnTemperature = 70, + [int] $OffTemperature = 50, + [ValidateSet("Fahrenheit","Celsius")] + [string]$TemperatureScale = "Celsius", + [int] $PollPeriodSeconds = 5 +) + +Import-Module $PSScriptRoot/Microsoft.PowerShell.IoT.Fan.psd1 + +$OnTemperatureC = $OnTemperature +$OffTemperatureC = $OffTemperature +if ($TemperatureScale -eq "Fahrenheit") +{ + $OnTemperatureC = ($OnTemperature - 32) * 5 / 9 + $OffTemperatureC = ($OffTemperature - 32) * 5 / 9 +} + +while($true) +{ + $CpuTemperatureC = (Get-Content /sys/class/thermal/thermal_zone0/temp) / 1000 + $CpuTemperatureF = ($CpuTemperatureC * 9 / 5) + 32 + + (Get-Date).ToString() + ": CPU temperature = $CpuTemperatureC C | $CpuTemperatureF F" | Write-Verbose + + if ($CpuTemperatureC -gt $OnTemperatureC) + { + "Starting fan..." | Write-Verbose + Enable-Fan -Pin $Pin + } + elseif ($CpuTemperatureC -lt $OffTemperatureC) + { + "Stopping fan..." | Write-Verbose + Disable-Fan -Pin $Pin + } + + Start-Sleep -Seconds $PollPeriodSeconds +}