-1

我想通过c#程序连接到从esp32打开的wifi网络,查看从esp32发送的数据。在通过串行端口使用 arduino 之前,我正在做这个项目,但我需要使用本地 wifi 网络来完成,我找不到连接到 wifi 网络的必要代码。你能帮助我吗。

我使用 BMP180 传感器做了一个小程序。我通过 Arduino 和 C# 程序传输数据来显示 C# 中的数据。在这个程序中,我想通过使用 ESP32 传输数据并连接到 wifi 网络(图中显示的是我使用 arduino 通过串行端口制作的程序。

public partial class Form1 : Form
{
    int line = 1;
    int column = 1;
    int lineNumber = 1;   
    public Form1()
    {
        InitializeComponent();
    }
    private string data;
    private void Form1_Load(object sender, EventArgs e)
    {
        string[] ports = SerialPort.GetPortNames();
        foreach (string port in ports)
        {
            comboBox1.Items.Add(port);
        }
        chart1.ChartAreas[0].AxisY.Minimum = 0;   
        chart1.ChartAreas[0].AxisY.Maximum = 1000;
        chart1.ChartAreas[0].AxisY.Interval = 100;
        chart1.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
        chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
        chart2.ChartAreas[0].AxisY.Minimum = 0;
        chart2.ChartAreas[0].AxisY.Maximum = 100;
        chart2.ChartAreas[0].AxisY.Interval = 10;
        chart2.ChartAreas[0].AxisX.MajorGrid.LineWidth = 0;
        chart2.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);                
    }
    private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        data = serialPort1.ReadLine(); 
        this.Invoke(new EventHandler(displaydata)); 
    }
    private void displaydata(object sender, EventArgs e)
    {
        DateTime myDateValue = DateTime.Now; 
        textBox3.Text = myDateValue.ToString();
        string[] value = data.Split(',');
        textBox1.Text = value[0];
        textBox2.Text = value[1];
        string pressure = Convert.ToString(value[0]);
        string temperature = Convert.ToString(value[1]);
        this.chart1.Series[0].Points.AddXY(myDateValue.ToString("HH:mm:ss"), pressure);        //zaman ve basınç değerini eksenlere ata
        this.chart2.Series[0].Points.AddXY(myDateValue.ToString("HH:mm:ss"), temperature);        //zaman ve sıcaklık değerini eksenlere ata   
        line = dataGridView1.Rows.Add();
        dataGridView1.Rows[line].Cells[0].Value = lineNumber;
        dataGridView1.Rows[line].Cells[1].Value = pressure;
        dataGridView1.Rows[line].Cells[2].Value = temperature;
        dataGridView1.Rows[line].Cells[3].Value = myDateValue.ToShortDateString();
        dataGridView1.Rows[line].Cells[4].Value = myDateValue.ToLongTimeString();
        line++;
        lineNumber++;
    }
    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            serialPort1.PortName = comboBox1.Text;
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            button1.Enabled = false;
            button2.Enabled=true;
            label1.Text = "Connected.";
            label1.ForeColor = Color.Green;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, ("Error:"));
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            serialPort1.Close();
            button1.Enabled = true;
            button2.Enabled = false;
            label1.Text = "Disconnected";
            label1.ForeColor = Color.Red;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, ("Error:"));
        }
    }

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (serialPort1.IsOpen) serialPort1.Close();
    }
    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Interval = 1000;
    }
    private void button3_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel.Application uyg = new Microsoft.Office.Interop.Excel.Application();
        uyg.Visible = true;
        Microsoft.Office.Interop.Excel.Workbook kitap = uyg.Workbooks.Add(System.Reflection.Missing.Value);
        Microsoft.Office.Interop.Excel.Worksheet sheet1 = (Microsoft.Office.Interop.Excel.Worksheet)kitap.Sheets[1];
        for (int i = 0; i < dataGridView1.Columns.Count; i++)
        {
            Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[1, i + 1];
            myRange.Value2 = dataGridView1.Columns[i].HeaderText;
        }

        for (int i = 0; i < dataGridView1.Columns.Count; i++)
        {
            for (int j = 0; j < dataGridView1.Rows.Count; j++)
            {
                Microsoft.Office.Interop.Excel.Range myRange = (Microsoft.Office.Interop.Excel.Range)sheet1.Cells[j + 2, i + 1];
                myRange.Value2 = dataGridView1[i, j].Value;
            }
        }


    }
}

}

4

1 回答 1

0

我认为最好的选择是使用 TCP/IP 套接字连接。

对于 ESP32,有一些库,例如: https ://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-guides/lwip.html

并来自 C#: https ://docs.microsoft.com/en-us/dotnet/framework/network-programming/using-tcp-services

于 2021-07-27T10:16:19.523 回答