![](/img/trans.png)
[英]How can i send data from ESP32 to C# VIsual Studio with WIFI
[英]How can I connect to the local wifi network opened via ESP32 via C# and transfer the data from the sensors?
我想通過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;
}
}
}
}
我認為最好的選擇是使用 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
你解決了這個問題嗎? 如果你這樣做了,你能否分享你的項目的源代碼,因為我需要這樣的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.