Go Down

Topic: Read Temperature/Humidity (Read 856 times) previous topic - next topic

juycce

Hi there guys i am having a problem with code in Visual Studio...and i need an expert opinion

i have the arduino and DHT22 sensor,when opening the Form1 a Timer send "1" thru the serial port.When "1" is recived on arduino,it send the temperature and humidity to the VS aplicacion.The problem i have is that,since the data send by arduino is more than one character (with Serial Monitor my data looks exactly like this  "24.90 Celsius      32.0% Humidity" ) i belive the Form cannot interpretate the data and gives me this error
Code: [Select]
  {"Conversion from string "24.90 Celsius      32.0% Humidity"to type 'Integer' is not valid."}
The code of Visual Studio is:
Code: [Select]


Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Shared _continue As Boolean
    Shared _serialPort As SerialPort
    Dim SReaderT1 As String


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        SerialPort1.Close()
        SerialPort1.PortName = "com17" 'change com port to match your Arduino port
        SerialPort1.BaudRate = 9600
        SerialPort1.DataBits = 8
        SerialPort1.Parity = Parity.None
        SerialPort1.StopBits = StopBits.One
        SerialPort1.Handshake = Handshake.None
        SerialPort1.Encoding = System.Text.Encoding.Default
    End Sub


    Dim TemperatureHumidity As Integer



    Private Sub TimerTick_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerTick.Tick
        Try
        Finally
            SerialPort1.Open()
            SerialPort1.Write("1")
            SReaderT1 = SerialPort1.ReadLine()
            SerialPort1.Write("0")
            SerialPort1.Close()

        End Try

        TemperatureHumidity = SReaderT1

        ListBoxTemperature1.Items.Add(TemperatureHumidity)                             ''Add the Temp/Hum to ListBoxTemperature1''
        ListBoxTemperature1.TopIndex = ListBoxTemperature1.Items.Count - 1        ''Count -1 i use it just to keep the data on top''


    End Sub
End Class



i am stuck in here,is there other way to read the data?thank you all of you.

PaulS

Opening the serial port resets the Arduino. Closing the serial port resets the Arduino. Is there some reason for doing that every time you want the temperature and humidity?

Code: [Select]
            SReaderT1 = SerialPort1.ReadLine()
SReaderT1 is a String.
Code: [Select]
        TemperatureHumidity = SReaderT1
TemperatureHumidity is an Integer. You can't assign a String to an Integer.
Even if you could, which float value in the string do you want TemperatureHumidity to contain? The temperature OR the humidity.

This is REALLY a dumb name for a variable. As well as being the wrong type.

If you are just adding the serial data to the list box, why not just add the String?

juycce

Thank you for Answering PaulS,i have the Arduino Duemilanove and i know is not like Leonardo,so every time i open the SerialComuncicacion it reset itself.I don't raly care much if it resets,i was doing it,because i thought that this way the aplicacion would work smoothly(corect me if im rong),since my intencion is to hookup 4 DHT22 sensors and 1 LDR,maybe to much data to one serial port.
I want to make it realy simple,less code is better...

Code: [Select]
TemperatureHumidity = SReaderT1  :D i know it sound silly,but the thing is i want to read both Temperature and Humidity

Code: [Select]
If you are just adding the serial data to the list box, why not just add the String?  could you please give me an example how it should look like the code.Thank you again Paul,you allways answer me ;)

PaulS

Quote
i know it sound silly,but the thing is i want to read both Temperature and Humidity

And store them in the same variable at the same time? Does that make sense?

Quote
could you please give me an example how it should look like the code.

Code: [Select]
        ListBoxTemperature1.Items.Add(SReaderT1)  ''Add the String to the ListBox''

juycce

Thank you Paul :D:D.Works great.Store the 2 values in one variable was the key,i thought i need 2 variables..one for Temp and the other one for Hum.
I apreciate your help.

Go Up