2 Replies - 1092 Views - Last Post: 22 January 2014 - 09:11 AM Rate Topic: -----

#1 ronzarevele  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-January 14

Saving Serial Read Data from sensor in Arduino to MySQL using VB.NET

Posted 22 January 2014 - 01:55 AM

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: 30*C RH:80%
Temp: 28*C 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



Is This A Good Question/Topic? 0
  • +

Replies To: Saving Serial Read Data from sensor in Arduino to MySQL using VB.NET

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,296
  • Joined: 12-June 08

Re: Saving Serial Read Data from sensor in Arduino to MySQL using VB.NET

Posted 22 January 2014 - 07:58 AM

I would highly recommend putting a breakpoint on line 73 and seeing what your substrings are doing with your textbox data.. Though I am not overly clear on what you are actually doing.. is this a multiline textbox?
Was This Post Helpful? 0
  • +
  • -

#3 ronzarevele  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 08-January 14

Re: Saving Serial Read Data from sensor in Arduino to MySQL using VB.NET

Posted 22 January 2014 - 09:11 AM

Yes sir. I set the textbox property to multiline..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1