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


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

調料

Raspberry Pi 3B+
樹莓派GPIO擴展板
3.5寸電容觸摸屏(GPIO接口)
土壤濕度傳感器(GPIO接口)
光照傳感器(GPIO接口)
由於作品已經交上去了 這里只能先放個以前的圖

這里展示的是土壤濕度傳感器,光照傳感器道理一樣

第一步 安裝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對應表可以在網上搜或者看下面的字符畫

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

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

pi@raspberrypi:~ $ 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+-+---+------+---------+-----+-----+

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


然后把傳感器從水里拿出來,再次readall

pi@raspberrypi:~ $ 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+-+---+------+---------+-----+-----+


可以看到BCM為6,接口號29發生了變化,在水中是0,不在水中是1.
OK 搞清楚了,接下來就好辦了

第三步 撰寫代碼

首先寫個UI,沒UI設計頭腦的我就簡單粗暴解決了

`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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM