Pages: [1]   Go Down
Author Topic: Read Temperature/Humidity  (Read 769 times)
0 Members and 1 Guest are viewing this topic.
Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  {"Conversion from string "24.90 Celsius      32.0% Humidity"to type 'Integer' is not valid."}

The code of Visual Studio is:
Code:

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
            SReaderT1 = SerialPort1.ReadLine()
SReaderT1 is a String.
Code:
        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?
Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
TemperatureHumidity = SReaderT1
  smiley-grin i know it sound silly,but the thing is i want to read both Temperature and Humidity

Code:
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 smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
        ListBoxTemperature1.Items.Add(SReaderT1)  ''Add the String to the ListBox''
Logged

Madrid,Spain
Offline Offline
Newbie
*
Karma: 4
Posts: 41
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Paul smiley-grin: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.
Logged

Pages: [1]   Go Up
Jump to: