Hello there, who can help me with my problem regarding on saving the serial data from DHT11 in arduino into MySQL database using VB.NET. In my case, the data are displayed to a textbox and saved to mysql but the problem is only the first read saved repeatedly. For example my data read using DHT11 sensor is
Temp: 30C RH:80%
Temp: 28C RH:87%
Temp: 27*C RH:87%
In VB.NET, only the first line is saved to database repeatedly. Here's my sample code:
Imports System.IO
Imports MySql.Data.MySqlClient
Public Class Form1
Public ds As New DataSet
Public strSQL As String
Public cmd As New MySqlCommand
Public dr As MySqlDataReader
Public table As New DataTable
Dim DataQ As Queue(Of String) = New Queue(Of String)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If SerialPort1.IsOpen() Then
MessageBox.Show("Disconnect before closing")
e.Cancel = True
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
For Each s In System.IO.Ports.SerialPort.GetPortNames()
lstPorts.Items.Add(s)
Next s
Catch ex As Exception
End Try
End Sub
Private Sub btndisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndisconnect.Click
SerialPort1.Close()
Timer1.Stop()
btndisconnect.Enabled = False
timer_for_systemTime.Stop()
MsgBox("All data are saved to a database.")
End Sub
Private Sub btnconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconnect.Click
timeref.Start()
btnStop.Enabled = True
btnStart.Enabled = False
If lstPorts.SelectedIndex = -1 Then
MsgBox("Please select a port")
Exit Sub
Else
SerialPort1.RtsEnable = True 'this is for the line communication of our serial port(important)
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 7
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = lstPorts.SelectedItem.ToString
SerialPort1.Open()
Timer1.Start()
btndisconnect.Enabled = True
btnconnect.Enabled = False
timer_for_systemTime.Start()
timer_for_startTime.Start()
Me.Close()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim Query As String
Dim Query2 As String
Dim Query3 As String
SyncLock DataQ
While DataQ.Count > 0
txtRealtime.Text &= DataQ.Dequeue
End While
End SyncLock
'START HERE\\\\\\\\\\\\\\\\\
Dim lv As ListViewItem = lvdata.Items.Add(txtRealtime.Text.Substring(0, 2))
lv.SubItems.Add(txtRealtime.Text.Substring(2, 2))
lv.SubItems.Add(lblsystemdate.Text)
lv.SubItems.Add(lblSystemTime.Text)
'END HERE\\\\\\\\\\\\\\\\\\\
Query = "INSERT INTO data2(Temperature, Humidity, Date, Time) VALUES ('" + txtRealtime.Text.Substring(0, 2) + "', '" + txtRealtime.Text.Substring(2, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
Query2 = "INSERT INTO temperature(dataRead, DateRead, TimeRead) VALUES ('" + txtRealtime.Text.Substring(0, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
Query3 = "INSERT INTO humidity(rhread, DateRead, TimeRead) VALUES ('" + txtRealtime.Text.Substring(2, 2) + "', '" + lblsystemdate.Text + "', '" + lblSystemTime.Text + "')"
MyCon.Open()
Dim sql As MySqlCommand = New MySqlCommand(Query, MyCon)
sql.ExecuteNonQuery()
Dim sql2 As MySqlCommand = New MySqlCommand(Query2, MyCon)
sql2.ExecuteNonQuery()
Dim sql3 As MySqlCommand = New MySqlCommand(Query3, MyCon)
sql3.ExecuteNonQuery()
MyCon.Close()
End Sub
End Class