Go Down

Topic: 4 DHT22 divided string displayed on VB.Net (Read 1 time) previous topic - next topic

juycce

Apr 25, 2016, 08:46 pm Last Edit: Apr 25, 2016, 08:51 pm by juycce
Hi everybody, i need a expert advice for mi lifeproject. I am joining all kind of sensors and relays and about 8 arduinos conected to a pc running on VB.net. in order to make a important installation in my house. So..
Right now i am having dificulties displaying the data of 4 DHT22 sensors. Basicly what i want to do is read Temp and Humidity in 8 labels (on VB.net). There is a lot of data so i am going to start from the bottom, step by step.

I want to send the data like <TempSensorA0021><TempSensorB0019><HumSensA0050><HumSensB0046> (only 2 sensors for now..)

- can i send that amount of data with arduino?
- is this the best way to do it? or should i use a buffer (ex: char buf[128];) but i don't know how to separate the data recived on PC
- the VB app will have timers so it wont block itself (i learned it hard way) and every 10 min will ask arduino to send the data.

here is the code i was trying to use, the problem is i don't know how to add the
Code: [Select]
tempC,1 in my code to see the temp like 21,5 ºC... right now i can only see
Code: [Select]
<T0018><T0019> , and its fluctuating.

Code: [Select]


#include <DHT22.h>

DHT22 dht22(7);
unsigned int oldTempVal = 0;
unsigned int newTempVal = 0;

char numberString[10];

void setup()
{
  Serial.begin(9600);
}
void loop()
{
   dht22.readData();
 
  float tempC = dht22.getTemperatureC();
 
  newTempVal = dht22.getTemperatureC();
 
   if (newTempVal != oldTempVal)
    {
       oldTempVal = newTempVal;
       formatNumber( newTempVal, 4);
       Serial.print("<T");                 // i am having <T0018><T0019>
       Serial.print(numberString);
       Serial.print(">");
    }

  delay(1000);
}
void formatNumber( unsigned int number, byte digits)
{
    // formats a number in to a string and copies it to the global char array numberString
    // pads the start of the string with '0' characters
    // number = the integer to convert to a string
    // digits = the number of digits to use.
 
    char tempString[10] = "\0";
    strcpy(numberString, tempString);
 
    // convert an integer into a acsii string
    itoa (number, tempString, 10);
 
    // create a string of '0' characters to pad the number   
    byte numZeros = digits - strlen(tempString) ;
    if (numZeros > 0)
    {
       for (int i=1; i <= numZeros; i++)    { strcat(numberString,"0");  }
    }
   
    strcat(numberString,tempString);
 
}



I have asked my issue here in Spain withowt any response, and i can't find any example o web. Kind of tricky.
I am a begginer on programing, trying to learn...but a expert on electronic. I apreciate every ideea of you guys. Thank you all.

PaulS

Quote
- can i send that amount of data with arduino?
Yes, although sending less data is better. <TA21> <HB56> conveys the same amount of information (temperature or humidity, sensor ID, and value) but in much less space.

Quote
- is this the best way to do it?
It's as good as any, and better than some.

Quote
but i don't know how to separate the data recived on PC
You'll need to learn.

On the Arduino side, it really isn't necessary to pad the numbers to 4 digits. Once you strip the type and ID, 0021 and 21 are going to convert to the same value if you use atoi(). (Which you aren't from a VB app, but whatever you do use will convert the strings to the same value.)

The art of getting good answers lies in asking good questions.

juycce

Thank you PaulS for answering so fast..jeje yeah... ill learn :) .. can u please tell what to modify to see insted of 21 --> 21.2 . The DHT is quite precise so i wanted to take advantege of those 0.5 (half degree Celsius unit).
Thank you again Paul, i hope you and Arduino team will help me with it. I don't wanna disturb so much...i understand that.

PaulS

Quote
can u please tell what to modify to see insted of 21 --> 21.2 .
Code: [Select]
       formatNumber( newTempVal, 4);
calls a function that expects an int. The accuracy that you bought that sensor for just went out the window.

Code: [Select]
   dtostrf(newTempVal, 4, 1, numberString);
will create a string in numberString with 4 or more characters, including a decimal point and one digit after the decimal point, from the value in newTempVal.

By the way, an exact comparison between floating point values is not going to work well. You want to send a new value if the difference between two values is small, not whenever the values are not identical.
The art of getting good answers lies in asking good questions.

juycce

#4
Apr 28, 2016, 11:58 pm Last Edit: Apr 29, 2016, 12:50 am by juycce
Good evening Paul, thank you for your answer, i have added what you recomand, into the code and i got:
Code: [Select]
<TA21.0> <HA51.0>  <TA22.0> <HA57.0>
.. i mean the last decimal is allways on 0 , (for example change from 21.0º to 22º C)i don't understand why.

On the other side, i am considering what you told me, and perhaps it won't be necesary send the Temp/Hum every time gets changed. If i will get a reading between 1 to 10min it will be fine.
Then i tried to eliminate the  oldTempValue and newTempValue, but i don't know what shuld i write on if statement. (i want to write <TA21.2> <HA51.7> of every sensor on serial port )
Code: [Select]




#include <DHT22.h>

DHT22 dht22A(7);         // DHT is D7
 
unsigned int Temp_Val_A = 0;    // integer of Temperature Sensor A
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

char numberString[10];  //  allows for 9 digits

void setup()
{
  Serial.begin(9600);
}
void loop()
{
   dht22A.readData();
 
  float tempCA = dht22A.getTemperatureC();
  float humA = dht22A.getHumidity();
 
  Temp_Val_A = dht22.getTemperatureC();
  Hum_Val_A = dht22.getHumidity();
 
   if (Temp_Val_A != ???)  
    {
       dtostrf(Temp_Val_A, 4, 1, numberString);
       Serial.print("<TA");              // ex.: <Temperature of sensor A>
       Serial.print(numberString);
       Serial.print(">");
    }
    
   if (Hum_Val_A != ???)
    {
       dtostrf(Hum_Val_A, 4, 1, numberString);
       Serial.print("<HA");              //  <Humidity of sensor A>
       Serial.print(numberString);
       Serial.print(">");
    }

  delay(1000);
}
void formatNumber( unsigned int number, byte digits)
{
     char tempString[10] = "\0";
    strcpy(numberString, tempString);
 
    // convert an integer into a acsii string
    itoa (number, tempString, 10);
 
    // create a string of '0' characters to pad the number  
    byte numZeros = digits - strlen(tempString) ;
    if (numZeros > 0)
    {
       for (int i=1; i <= numZeros; i++)    { strcat(numberString,"0");  }
    }
  
    strcat(numberString,tempString);
 
}




Bad part is that millis are killing me with a complex code (for me :) )...
Thank you very much.

PaulS

Code: [Select]
       dtostrf(Temp_Val_A, 4, 1, numberString);
Would you like to hazard a guess at what d(ouble)tostr(ing)f(ormatted) stands for? Would you car to hazard a guess as to what type of variable it is meant to process?

Code: [Select]
unsigned int Temp_Val_A = 0;    // integer of Temperature Sensor A
Nope, that wasn't it.
The art of getting good answers lies in asking good questions.

juycce

#6
Apr 29, 2016, 01:34 am Last Edit: Apr 29, 2016, 01:41 am by juycce
:)) omg Paul i am laughin alone... no reay.. I did told you before i am a newby, when i have to program a simple task to arduino it's kind of easy, but when i have to deal with more than one sensor it gets verry complicate. I have done the relay part, iluminacion, lock door (house proj)...i only have left Temp/Humidity and automatic blinds. Pleaaaase help me with the DHT22, i didnt studyed informatics at school, i wish i did, be kind with me. Thank you  

PaulS

The dtostrf() function converts a Double To a STRing, Formatted. The key is that is converts a float/double to a string. Not an int. Call that function with TempCA or HumA, to get values like "21.5".
The art of getting good answers lies in asking good questions.

juycce

#8
Apr 30, 2016, 01:56 am Last Edit: Apr 30, 2016, 02:02 am by juycce
Sorry for disturbing all the time, but i don't get to work, i can't convert the double float to string.
I tried the example //  inString += (char)inChar; //
and then anotherone, but it can not compile.

int A;
int B;
float x;

A = int(x);
B = int((B - x) * 100);

Code: [Select]


#include <DHT22.h>

DHT22 dht22A(7);         // DHT is D7
 
unsigned int Temp_Val_A = 0;    
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

char numberString[10];  //  allows for 9 digits

void setup()
{
  Serial.begin(9600);
}
void loop()
{
   dht22A.readData();
 
  float tempCA = dht22A.getTemperatureC();  
  float humA = dht22A.getHumidity();
 
  Temp_Val_A = dht22A.getTemperatureC();   // read value and save it to variable
  Hum_Val_A = dht22A.getHumidity();
 
   if (Temp_Val_A != tempCA)
    {
       dtostrf(Temp_Val_A, 4, 1, numberString);
       Serial.print("<TA");              // ex.: <Temperature of sensor A>
       Serial.print(numberString);
       Serial.print(">");
    }
    
   if (Hum_Val_A != humA)
    {
       dtostrf(Hum_Val_A, 4, 1, numberString);
       Serial.print("<HA");              //  <Humidity of sensor A>
       Serial.print(numberString);
       Serial.print(">");
    }

  delay(1000);
}
void formatNumber( unsigned int number, byte digits)
{
     char tempString[10] = "\0";
    strcpy(numberString, tempString);
 
    // convert an integer into a acsii string
    itoa (number, tempString, 10);
 
    // create a string of '0' characters to pad the number  
    byte numZeros = digits - strlen(tempString) ;
    if (numZeros > 0)
    {
       for (int i=1; i <= numZeros; i++)    { strcat(numberString,"0");  }
    }
  
    strcat(numberString,tempString);
 
}





I am getting frustrated about :( :(,can you please help me with the code..
Thank you

PaulS

Quote
I am getting frustrated
So am I. It is pointless to call dtostf() with an int. Call it with the damned float!
The art of getting good answers lies in asking good questions.

juycce

i just tried
Code: [Select]
float dtostf(); and doesn't work, looks like i will never understand where to put it in the code. I have no clue how to call dtostf with float... thank you for your time anyway. :(

PaulS

Quote
I have no clue how to call dtostf with float... thank you for your time anyway.
Code: [Select]
float pi = 3.14159;
char piStg[10];
dtostrf(pi, 4, 1, piStg);
The art of getting good answers lies in asking good questions.

juycce

#12
May 02, 2016, 11:16 pm Last Edit: May 02, 2016, 11:32 pm by juycce
thank you very much for your answer ,but i also tried what you answer (thank you again for that), thats why i need to someone expain me where the error is. If you look at the code i have:

Code: [Select]

  float pi = 3.14159
  char numberString[10];
  dtostrf(Temp_Val_A, 4, 1, numberString);
 


I am posting the actual code i am using, better expained. I am shure more than one will need it.

Code: [Select]

/*
* DHT Sensor send data to VB.Net
* The Temp/Humidity send data (for ex. temperature) with the format < 21.0 >
* so that the VB aplicacion can separate it and dispay it in a label.
* Data wire is plugged into port 7 on the Arduino
* Connect a 4.7K resistor between VCC and the data pin (strong pullup)
*/



#include <DHT22.h>

DHT22 dht22A(7);         // DHT is D7
 
unsigned int Temp_Val_A = 0;    // integer of Temperature Sensor A
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

char numberString[10];  // used to hold an ascii representation of a number
                        // [10] allows for 9 digits but in this example
void setup()
{
  Serial.begin(9600);
}
void loop()             // read the pins
{
   dht22A.readData();
 
  float tempCA = dht22A.getTemperatureC();  
  float humA = dht22A.getHumidity();
 
  Temp_Val_A = dht22A.getTemperatureC();   // read value and save it to variable
  Hum_Val_A = dht22A.getHumidity();
 
   if (Temp_Val_A != tempCA)
    {
       dtostrf(Temp_Val_A, 4, 1, numberString);
       Serial.print("<TA");              // // print ex.: <21.0>      theese are ºC
       Serial.print(numberString);
       Serial.print(">");
    }
    
   if (Hum_Val_A != humA)
    {
       dtostrf(Hum_Val_A, 4, 1, numberString);
       Serial.print("<HA");              
       Serial.print(numberString);
       Serial.print(">");
    }

  delay(1000);
}
void formatNumber( unsigned int number, byte digits)
{
   // formats a number in to a string and copies it to the global char array numberString
   // pads the start of the string with '0' characters
   // number = the integer to convert to a string
   // digits = the number of digits to use.

     char tempString[10] = "\0";
    strcpy(numberString, tempString);
 
    // convert an integer into a acsii string
    itoa (number, tempString, 10);
 
    // create a string of '0' characters to pad the number  
    byte numZeros = digits - strlen(tempString) ;
    if (numZeros > 0)
    {
       for (int i=1; i <= numZeros; i++)    { strcat(numberString,"0");  }
    }
  
    strcat(numberString,tempString);
 
}






The problem is that second decimal ( 0.1 to 0.9 )  are not displayed. Thank you

PaulS

Code: [Select]
unsigned int Temp_Val_A = 0;    // integer of Temperature Sensor A
unsigned int Hum_Val_A = 0;     // integer of Humidity Sensor A

I'm going to say this just once more. DELETE THIS CRAP. The temperature and humidity are NOT integer values, or we wouldn't be having thins discussion.

When you delete these two lines, and change all the references to them to the float values, then things will work!

The art of getting good answers lies in asking good questions.

Go Up