This repository was archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Example - managing fan of Raspberry Pi enclosure #55
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psd1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Examples/Microsoft.PowerShell.IoT.Fan/Microsoft.PowerShell.IoT.Fan.psm1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)] | ||
anmenaga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ValidateNotNullOrEmpty()] | ||
[int] $Pin | ||
) | ||
|
||
Set-GpioPin -Id $Pin -Value Low | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
 | ||
 | ||
|
||
## 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: | ||
 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.