当前位置:网站首页>09 design of water quality monitoring system based on ZigBee
09 design of water quality monitoring system based on ZigBee
2022-07-20 06:54:00 【Steam steam】
1 Applicable scenario
The river 、 The swimming pool 、 The pool 、 Fish pond 、 domestic water 、 Water quality monitoring scenarios such as industrial water .
2 development environment
Lower computer development environment :IAR embedded Workbench
Upper computer development environment :Visual Studio 2022
3 Hardware components ( Consider adding PH Or conductivity sensor )
ZigBee Gateway nodes ( The chip model is CC2530 Single chip microcomputer )、ZigBee Collection node ( The chip model is CC2530 Single chip microcomputer )、DS18B20 Temperature sensor ( Waterproof type )、 Water level sensor 、 Turbidity sensor 、 Buzzer 、 Key module 、USB turn TTL-CH340 modular 、OLED Display screen 、USB Charging line
4 function
- Real-time monitoring : Real time monitoring of water temperature 、 The water level 、 turbidity , And display to OLED On the screen .
- Abnormal alarm : When the water temperature 、 The water level 、 Turbidity is not within the set threshold , Automatic audible and visual alarm reminder .
- Upper computer monitoring : The water temperature value can be checked in real time on the upper computer 、 Water level 、 Turbidity value , Set the water temperature value 、 Water level 、 Threshold of turbidity value .
- database : You can check the historical water temperature 、 The water level 、 The real-time value and parameters of turbidity are abnormal .
5 Physical drawing of lower computer
6 Upper computer
6 database
7 Upper computer related source code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ZigBeeApp
{
public partial class FormMain : Form
{
/*@start Serial Base*********************************************/
private SerialPort sp = new SerialPort();// Declare a serial port class
private bool isOpen = false;// Open the serial port flag bit
private bool usart_closing = false;// Whether the serial port is being shut down , perform Application.DoEvents, Prevent re-entry invoke
private bool usart_listening = false;// Monitor whether the serial port is being executed invoke The relevant operation
private List<byte> buffer = new List<byte>(4096);// Default assignment 1 Page memory , And always limit not to exceed
private long SendCount = 0;
private long ReceiveCount = 0;
// Serial port form initialization
private void SerialSettingInit()
{
// Maximum support to serial port 32, It can be increased according to your own needs
for (int i = 0; i < 32; i++)
{
cboxCOM.Items.Add("COM" + (i + 1).ToString());
}
cboxCOM.SelectedIndex = 0;
// List commonly used baud rates
cboxBaudRate.Items.Add("1200");
cboxBaudRate.Items.Add("2400");
cboxBaudRate.Items.Add("4800");
cboxBaudRate.Items.Add("9600");
cboxBaudRate.Items.Add("19200");
cboxBaudRate.Items.Add("38400");
cboxBaudRate.Items.Add("43000");
cboxBaudRate.Items.Add("56000");
cboxBaudRate.Items.Add("57600");
cboxBaudRate.Items.Add("115200");
cboxBaudRate.SelectedIndex = 9;
// List stop bits
cboxStopBits.Items.Add("0");
cboxStopBits.Items.Add("1");
cboxStopBits.Items.Add("1.5");
cboxStopBits.Items.Add("2");
cboxStopBits.SelectedIndex = 1;
// List data bits
cboxDataBits.Items.Add("8");
cboxDataBits.Items.Add("7");
cboxDataBits.Items.Add("6");
cboxDataBits.Items.Add("5");
cboxDataBits.SelectedIndex = 0;
// List parity bits
cboxParity.Items.Add(" nothing ");
cboxParity.Items.Add(" Odd check ");
cboxParity.Items.Add(" Even check ");
cboxParity.SelectedIndex = 0;
countClear();
}
// Detect available serial ports
private void buttonCheck_Click(object sender, EventArgs e)
{
bool comExistence = false;// There are available serial port flag bits
cboxCOM.Items.Clear(); // Clear all serial port names in the current serial port number
for (int i = 0; i < 32; i++)
{
try
{
SerialPort sp = new SerialPort("COM" + (i + 1).ToString());
sp.Open();
sp.Close();
cboxCOM.Items.Add("COM" + (i + 1).ToString());
comExistence = true;
}
catch (Exception)
{
continue;
}
}
if (comExistence)
{
cboxCOM.SelectedIndex = 0;// send ListBox According to the first 1 Added indexes
}
else
{
MessageBox.Show(" No available serial ports found !", " Error message ");
}
}
// Detect serial port settings
private bool CheckPortSetting()
{
if (cboxCOM.Text.Trim() == "") return false;
if (cboxBaudRate.Text.Trim() == "") return false;
if (cboxDataBits.Text.Trim() == "") return false;
if (cboxParity.Text.Trim() == "") return false;
if (cboxStopBits.Text.Trim() == "") return false;
return true;
}
// Open the serial port and press the key to respond
private void buttonOpen_Click(object sender, EventArgs e)
{
if (isOpen == false)
{
if (CheckPortSetting())
{
sp.PortName = cboxCOM.Text.Trim();// Set serial port name
sp.BaudRate = Convert.ToInt32(cboxBaudRate.Text.Trim());// Set the baud rate of the serial port
float f = Convert.ToSingle(cboxStopBits.Text.Trim());// Set stop bit
if (f == 0)
{
sp.StopBits = StopBits.None;
}
else if (f == 1.5)
{
sp.StopBits = StopBits.OnePointFive;
}
else if (f == 1)
{
sp.StopBits = StopBits.One;
}
else if (f == 2)
{
sp.StopBits = StopBits.Two;
}
else
{
sp.StopBits = StopBits.One;
}
sp.DataBits = Convert.ToInt16(cboxDataBits.Text.Trim());// set data bit
string s = cboxParity.Text.Trim(); // Set parity bit
if (s.CompareTo(" nothing ") == 0)
{
sp.Parity = Parity.None;
}
else if (s.CompareTo(" Odd check ") == 0)
{
sp.Parity = Parity.Odd;
}
else if (s.CompareTo(" Even check ") == 0)
{
sp.Parity = Parity.Even;
}
else
{
sp.Parity = Parity.None;
}
}
else
{
return;
}
// Set the timeout reading time
sp.ReadTimeout = -1;
sp.RtsEnable = true;
// Definition DataReceived event , An event is triggered when the serial port receives data
sp.DataReceived -= new SerialDataReceivedEventHandler(sp_DataReceived);
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
try// Open the serial port
{
sp.Open();
isOpen = true;
buttonOpen.Text = " Turn off the serial port ";
// After the serial port is opened, the relevant serial port setting button can no longer be used
cboxCOM.Enabled = false;
cboxBaudRate.Enabled = false;
cboxStopBits.Enabled = false;
cboxDataBits.Enabled = false;
cboxParity.Enabled = false;
buttonCheck.Enabled = false;
}
catch (Exception)
{
// After failed to open the serial port , The corresponding flag bit is cancelled
isOpen = false;
MessageBox.Show(" The serial port is invalid or occupied !", " Error message ");
}
}
else
{
try// Turn off the serial port
{
usart_closing = true;// tell Invoke The serial port is closing
while (usart_listening) Application.DoEvents();// Waiting to arrive Invoke After execution
sp.Close();
usart_closing = false;// The above code is designed to prevent locking when closing the serial port
isOpen = false;
buttonOpen.Text = " Open the serial port ";
// After the serial port is opened, the relevant serial port setting button can no longer be used
cboxCOM.Enabled = true;
cboxBaudRate.Enabled = true;
cboxStopBits.Enabled = true;
cboxDataBits.Enabled = true;
cboxParity.Enabled = true;
buttonCheck.Enabled = true;
countClear();
}
catch (Exception)
{
// After failed to open the serial port , The corresponding flag bit is cancelled
isOpen = false;
MessageBox.Show(" An error occurred while closing the serial port !", " Error message ");
}
}
}
// Serial port sends string
private void UartSend(String str)
{
if (isOpen == true)
{
try
{
sp.Write(str);
SendCount += Encoding.Default.GetBytes(str).Length;
refreshSendCount();
}
catch
{
PortErrorDeal();
}
}
else
{
MessageBox.Show(" The serial port is not open !", " Error message ");
}
}
// Send array
private void UartSend(byte[] buf, int len)
{
if (isOpen == true)
{
try
{
sp.Write(buf, 0, len);
SendCount += len;
refreshSendCount();
}
catch
{
PortErrorDeal();
}
}
else
{
MessageBox.Show(" The serial port is not open !", " Error message ");
}
}
// Send array
private void UartSend(byte[] buf)
{
UartSend(buf, buf.Length);
}
// Serial port receiving event
private void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (usart_closing)// If the serial port is shutting down , Ignore action , Go straight back to
{
return;
}
else
{
usart_listening = true; // perform Invoke Start ,ui You can't close the serial port
}
//this.Invoke Cross thread access ui Methods , It is also an example of this article
try
{
this.Invoke((EventHandler)(delegate
{
int Bytes_n = sp.BytesToRead; // Avoid thinking that the cause of cache inconsistency
Byte[] ReceivedDatabuf = new Byte[Bytes_n]; // Create receive byte array
sp.Read(ReceivedDatabuf, 0, Bytes_n); // Read the received data
ReceiveCount += Bytes_n;
buffer.AddRange(ReceivedDatabuf); //1. Cache data
refreshReceiveCount();
}));
}
catch
{
PortErrorDeal();
}
finally
{
usart_listening = false;// After execution Invoke 了 ,ui You can turn off the serial port
}
}
// Serial port error handling
private void PortErrorDeal()
{
MessageBox.Show(" Disconnect or unplug the serial port !", " Error message ");
try
{
sp.Close();
}
catch
{
}
isOpen = false;
buttonOpen.Text = " Open the serial port ";
// After the serial port is opened, the relevant serial port setting button can no longer be used
cboxCOM.Enabled = true;
buttonCheck.Enabled = true;
}
// Refresh the receive count
private void refreshReceiveCount()
{
labReiveCount.Text = " receive :" + ReceiveCount.ToString();
}
// Refresh send count
private void refreshSendCount()
{
labSendCount.Text = " send out :" + SendCount.ToString();
}
// The count value is cleared
private void countClear()
{
SendCount = 0;
ReceiveCount = 0;
refreshReceiveCount();
refreshSendCount();
}
// Send data changes
private void labSendCount_TextChanged(object sender, EventArgs e)
{
}
// Receive data changes
private void labReiveCount_TextChanged(object sender, EventArgs e)
{
if (ReceiveCount == 0)
{
buffer.Clear();
}
else
{
recevivedData();
}
}
}
边栏推荐
猜你喜欢
AcWing 379. 捉迷藏 题解 (最小路径不重复点覆盖)
Activiti工作流网关
注解开发管理第三方bean
sql编辑器里面的红叉代表什么意思(toad、waterdrop都遇到过…)
U++ using the SetTimer function
[leetcode daily question] - 109 Ordered linked list transformation binary search tree
P3166数三角形(容斥+gcd)
专访铃盛(RingCentral)何必苍:以不断创新的MVP赋能未来混合办公
DOM operation of JS -- event object
微信小程序-获取用户位置(经纬度+所在城市)
随机推荐
【自校正控制】递推最小二乘法
Live broadcast today | Apache pulsar meetup: vivo, Tencent cloud, bigo, Yunxing technology practice sharing
U++ 子系统
U++打印信息到屏幕和日志
微信小程序-获取用户位置(经纬度+所在城市)
D - Dire Wolf(区间dp)
U++ subsystem
Redis删除策略和淘汰策略
云主机内网通信ping不通问题处理过程
sql编辑器里面的红叉代表什么意思(toad、waterdrop都遇到过…)
Mysql database engine analysis
电赛猜题?我觉得没用,还不如做好这些!
Jedis
[Android开发学iOS系列] 语言篇: Swift vs Kotlin
【C语言刷LeetCode】443. 压缩字符串(M)
DOM operation of JS - Events
Iterators and generators (ES6)
shell查询prometheus数据
D. Rating Compression(思维 + 双指针)
为什么调试器会显示错误的函数