在樹莓派用C#+Winform實現感測器監測

楓葉1003 發表於 2021-09-20
樹莓派 C#

最近學校裡發了個任務,說要做一個科技節小發明,然後我就掏出我的樹莓派準備大幹一場。

調料

Raspberry Pi 3B+
樹莓派GPIO擴充套件板
3.5寸電容觸控式螢幕(GPIO介面)
土壤溼度感測器(GPIO介面)
光照感測器(GPIO介面)
由於作品已經交上去了 這裡只能先放個以前的圖
在樹莓派用C#+Winform實現感測器監測
這裡展示的是土壤溼度感測器,光照感測器道理一樣

第一步 安裝mono

樹莓派不能直接執行C#圖形化應用程式,去網上搜了一下解決方案,WPF框架是肯定沒戲,不過我看到了一個叫mono的專案,可以在Linux平臺執行Winform程式,還有這等好事?!趕緊整!

國際慣例哈,更新軟體源
sudo apt-get update
然後執行安裝命令
sudo apt-get install mono-complete -y
如果沒法安裝就檢查你的軟體源是否包含mono,我個人推薦清華大學的開源映象站
最後我們檢查是否安裝成功
mono
如果不報錯,就是安裝成功了

第二步 探索GPIO

環境搭建好了,然而我對GPIO介面所知甚少,還是上網查查
連線感測器,首先是微控制器和檢測頭的連線,母對母兩條杜邦線搞定
然後是樹莓派和微控制器,VCC接3.3V或者5V,GND接樹莓派GND,DO接任意GPIO空閒介面
樹莓派的GPIO對應表可以在網上搜或者看下面的字元畫
在樹莓派用C#+Winform實現感測器監測在樹莓派用C#+Winform實現感測器監測

一開始使用CSDN上某個大佬的GPIO類
https://blog.csdn.net/weixin_30878361/article/details/97233437
結果測試了好久都拋異常,如果哪位大佬知道如何呼叫可以在評論區告訴我,感謝

於是我果斷放棄了這個現成的類庫,然後去讀了下感測器的文件,發現文件裡有一個很有意思的命令
gpio readall
趕緊跑到樹莓派終端上,執行它!

[email protected]:~ $ gpio readall
+-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 1 | OUT | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 1 | OUT | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | ALT0 | 1 | 23 || 24 | 0 | OUT | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | OUT | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 0 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+

從這裡看可能不太直觀哈,放個圖

在樹莓派用C#+Winform實現感測器監測
然後把感測器從水裡拿出來,再次readall

[email protected]:~ $ gpio readall
+-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | ALT0 | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | ALT0 | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT5 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT5 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 1 | OUT | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | ALT0 | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | ALT0 | 0 | 21 || 22 | 1 | OUT | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | ALT0 | 1 | 23 || 24 | 0 | OUT | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | OUT | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 1 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 1 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+---Pi 3B+-+---+------+---------+-----+-----+

在樹莓派用C#+Winform實現感測器監測
可以看到BCM為6,介面號29發生了變化,在水中是0,不在水中是1.
OK 搞清楚了,接下來就好辦了

第三步 撰寫程式碼

首先寫個UI,沒UI設計頭腦的我就簡單粗暴解決了
在樹莓派用C#+Winform實現感測器監測
`namespace OpenEnvLite
{
partial class Form1
{
///


/// 必需的設計器變數。
///

private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的資源。
    /// </summary>
    /// <param name="disposing">如果應釋放託管資源,為 true;否則為 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗體設計器生成的程式碼

    /// <summary>
    /// 設計器支援所需的方法 - 不要修改
    /// 使用程式碼編輯器修改此方法的內容。
    /// </summary>
    private void InitializeComponent()
    {
        this.title = new System.Windows.Forms.Label();
        this.土壤溼度 = new System.Windows.Forms.Label();
        this.土壤溼度顯示 = new System.Windows.Forms.Label();
        this.亮度 = new System.Windows.Forms.Label();
        this.亮度顯示 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.label1 = new System.Windows.Forms.Label();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // title
        // 
        this.title.AutoSize = true;
        this.title.Font = new System.Drawing.Font("宋體", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.title.ForeColor = System.Drawing.Color.White;
        this.title.Location = new System.Drawing.Point(12, 36);
        this.title.Name = "title";
        this.title.Size = new System.Drawing.Size(187, 29);
        this.title.TabIndex = 1;
        this.title.Text = "土壤屬性探測";
        // 
        // 土壤溼度
        // 
        this.土壤溼度.AutoSize = true;
        this.土壤溼度.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.土壤溼度.ForeColor = System.Drawing.Color.White;
        this.土壤溼度.Location = new System.Drawing.Point(43, 89);
        this.土壤溼度.Name = "土壤溼度";
        this.土壤溼度.Size = new System.Drawing.Size(94, 21);
        this.土壤溼度.TabIndex = 6;
        this.土壤溼度.Text = "土壤溼度";
        // 
        // 土壤溼度顯示
        // 
        this.土壤溼度顯示.AutoSize = true;
        this.土壤溼度顯示.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.土壤溼度顯示.ForeColor = System.Drawing.Color.White;
        this.土壤溼度顯示.Location = new System.Drawing.Point(143, 89);
        this.土壤溼度顯示.Name = "土壤溼度顯示";
        this.土壤溼度顯示.Size = new System.Drawing.Size(73, 21);
        this.土壤溼度顯示.TabIndex = 7;
        this.土壤溼度顯示.Text = "檢測中";
        // 
        // 亮度
        // 
        this.亮度.AutoSize = true;
        this.亮度.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.亮度.ForeColor = System.Drawing.Color.White;
        this.亮度.Location = new System.Drawing.Point(63, 110);
        this.亮度.Name = "亮度";
        this.亮度.Size = new System.Drawing.Size(52, 21);
        this.亮度.TabIndex = 8;
        this.亮度.Text = "亮度";
        // 
        // 亮度顯示
        // 
        this.亮度顯示.AutoSize = true;
        this.亮度顯示.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.亮度顯示.ForeColor = System.Drawing.Color.White;
        this.亮度顯示.Location = new System.Drawing.Point(143, 110);
        this.亮度顯示.Name = "亮度顯示";
        this.亮度顯示.Size = new System.Drawing.Size(73, 21);
        this.亮度顯示.TabIndex = 9;
        this.亮度顯示.Text = "檢測中";
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label2.ForeColor = System.Drawing.Color.White;
        this.label2.Location = new System.Drawing.Point(27, 174);
        this.label2.Name = "label2";
        this.label2.Size = new System.Drawing.Size(189, 21);
        this.label2.TabIndex = 10;
        this.label2.Text = " 微型土壤探測系統";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Cursor = System.Windows.Forms.Cursors.Default;
        this.label1.Font = new System.Drawing.Font("宋體", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.label1.ForeColor = System.Drawing.Color.White;
        this.label1.Location = new System.Drawing.Point(27, 142);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(189, 21);
        this.label1.TabIndex = 10;
        this.label1.Text = "相互科技 版權所有";
        // 
        // button2
        // 
        this.button2.BackColor = System.Drawing.Color.Black;
        this.button2.Font = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
        this.button2.ForeColor = System.Drawing.Color.White;
        this.button2.Location = new System.Drawing.Point(191, 26);
        this.button2.Name = "button2";
        this.button2.Size = new System.Drawing.Size(74, 39);
        this.button2.TabIndex = 13;
        this.button2.Text = "重新整理";
        this.button2.UseVisualStyleBackColor = false;
        this.button2.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.Color.Black;
        this.ClientSize = new System.Drawing.Size(266, 235);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.亮度顯示);
        this.Controls.Add(this.亮度);
        this.Controls.Add(this.土壤溼度顯示);
        this.Controls.Add(this.土壤溼度);
        this.Controls.Add(this.title);
        this.ForeColor = System.Drawing.Color.White;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form1";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Form1";
        this.TopMost = true;
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Label title;
    private System.Windows.Forms.Label 土壤溼度;
    private System.Windows.Forms.Label 土壤溼度顯示;
    private System.Windows.Forms.Label 亮度;
    private System.Windows.Forms.Label 亮度顯示;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button2;
}

}這是設計器程式碼 然後是主要程式碼using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using System.Collections;

namespace OpenEnvLite
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    public static string Bash(string command)
    {
        var escapedArgs = command.Replace("\"", "\\\"");
        var process = new Process()
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "/bin/bash",
                Arguments = $"-c \"{escapedArgs}\"",
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true,
            }
        };
        process.Start();
        string result = process.StandardOutput.ReadToEnd();
        process.WaitForExit();
        process.Dispose();
        return result;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string SenseWater = Bash("gpio readall");
        if (SenseWater.Contains("|   5 |  21 | GPIO.21 |   IN | 0 | 29 |"))
        {
            土壤溼度顯示.Text = "溼度正常";
        }
        else
        {
            土壤溼度顯示.Text = "溼度過低";
        }
        string SenseLight = Bash("gpio readall");
        if (SenseLight.Contains("| 40 | 1 | IN   | GPIO.29 | 29  | 21  |"))
        {
            亮度顯示.Text = "亮度過低";
        }
        else
        {
            亮度顯示.Text = "亮度正常";
        }
    }
}

}
`

搞定!
如果要調節感測器的靈敏度可以用十字起子轉動微控制器上的電阻。
光敏電阻和溼度電阻道理是一樣的。
最後匯入exe檔案到樹莓派,使用mono執行
mono -run test.exe
test.exe換成自己程式的名字,執行後點選重新整理可以正常顯示
大功告成!

該專案在gayhub 啊不github開源:
https://github.com/fengye1003/OpenEnvLite