I want to connect to the wifi network opened from esp32 via c# program and view the data sent from esp32. I was doing this project before using arduino via serialport, but I need to do it with local wifi network and I couldn't find the necessary codes to connect to wifi network. Can you help me.
I made a small program using the BMP180 sensor. I show data in C# by transferring data via Arduino and C# program. In this program, I want to transfer data by using ESP32 and connecting to the wifi network (The ones shown in the picture are a program I made over serialport using 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;
}
}
}
}
}