4 DHT22 divided string displayed on VB.Net

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 (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]:wink: 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 tempC,1 in my code to see the temp like 21,5 ºC... right now i can only see <T0018><T0019> , and its fluctuating.

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

  • can i send that amount of data with arduino?

Yes, although sending less data is better. conveys the same amount of information (temperature or humidity, sensor ID, and value) but in much less space.

  • is this the best way to do it?

It's as good as any, and better than some.

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

Thank you PaulS for answering so fast..jeje yeah... ill learn :slight_smile: .. 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.

can u please tell what to modify to see insted of 21 --> 21.2 .

       formatNumber( newTempVal, 4);

calls a function that expects an int. The accuracy that you bought that sensor for just went out the window.

   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.

Good evening Paul, thank you for your answer, i have added what you recomand, into the code and i got:

<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 )

#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 :slight_smile: )...
Thank you very much.

       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?

unsigned int Temp_Val_A = 0;    // integer of Temperature Sensor A

Nope, that wasn't it.

:)) 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

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

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);

#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 :frowning: :(,can you please help me with the code..
Thank you

I am getting frustrated

So am I. It is pointless to call dtostf() with an int. Call it with the damned float!

i just tried 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. :frowning:

I have no clue how to call dtostf with float... thank you for your time anyway.

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

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:

  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.

/*
* 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

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!