VirtualWire: I'dd like to use buf[i] within an IF-statement? PLEASE HELP ME OUT

I’ve made my second project. Used a 433Mhz RFlink with DHT11 to create an oudside thermometer
So far it’s working but…

I’dd like to let burn a LED with an if statement for the case that the temperature falls below 0 degrees C
so this is the part of my code (line 70-75) I think where I have to add some IF statements, but I was not really succesfull…

for (i = 0; i < buflen; i++)
{
lcd.write(buf*); ← this is the message I’ve received by 433 RFLink from DHT11*

  • }*
  • lcd.print((char)223);*
  • lcd.print(" C");*
    ==========================
    So when I’m getting a temp which is below 0 degrees Celsius I’dd like to let a LED burn (digitalWrite(2, HIGH):wink:
    and when the temp is higher than 0 degrees Celsius the LED should turn off (digitalWrite(2, LOW):wink:
    You can find my receiver code below:
    ```

  • Loek Pfundt for DHT11 and LCD 20x4 with rflink and clock DS1302
  • this is the receiver code for 433Mhz RF link kit

*/

//load libraries
#include <VirtualWire.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <virtuabotixRTC.h>

//Initialise the LCD
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

// Clock DS1302; VCC + / GND - / CLK 6 / DAT 5 / RST 8
virtuabotixRTC myRTC(6, 5, 8);         //installation clock DS1302

int i;                                 //and integer used to count

void setup()
{
//Define the LCD as 20 column by 4 rows
lcd.begin (20, 4);                 // LCD 20 tekens breed en 4 regels hoog

// here you write the actual date / time, afterwards place ‘//’ or delete to set time /date
// myRTC.setDS1302Time(00, 41, 20, 3, 19, 2, 2019);  //sec, min, uur, dow, date, mnd, year

//Define the receiver pin and rate
vw_set_rx_pin(12);                 // Sets pin D12 as the RX Pin
vw_setup(2000);                   // Bits per sec
vw_rx_start();                     // Start the receiver PLL running
}

void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;

if( vw_get_message(buf, &buflen) )
  {
    myRTC.updateTime();
    lcd.clear();                   // scherm wissen om ontvangen vervuiling weg te vegen
    lcd.setCursor(1, 0);
    lcd.print("");
    lcd.setCursor(18, 0);
    lcd.print("
");
    lcd.setCursor(3, 0);
    lcd.print(“St. Anton 2019”);
    lcd.setCursor(1,1);
    lcd.print(myRTC.dayofmonth);   // datumdag
    lcd.print("/");
    lcd.print(myRTC.month);        // datummaand
    lcd.print("/");
    lcd.print(myRTC.year);         // datumjaar
    lcd.print(" - “);
    lcd.print(myRTC.hours);        // heleuur
    lcd.print(”:");
    lcd.print(myRTC.minutes);      //minuten
    lcd.print(“u”);
    lcd.setCursor(1, 2);
    lcd.print(“Buitentemperatuur:”);
    lcd.setCursor(1 ,3);
    lcd.print(“");
    lcd.setCursor(16, 3);
    lcd.print("
”);
    lcd.setCursor(5, 3);  
   
   for (i = 0; i < buflen; i++)
     {
       lcd.write(buf[i]);          
     }
       lcd.print((char)223);       // maakt het karakter voor het ‘graden’ teken
       lcd.print(" C");
  }

}*
```

Please edit your post to add code tags ("</>" button on editor), as described in “How to use this forum”.

buf is a string (null terminated array of chars). C++ (the language of the Arduino code) provides some nice functions to deal with strings. The one you will be interested in for this project is atoi:
http://www.cplusplus.com/reference/cstdlib/atoi/
atoi converts a string representation of a number to a number. Once you have that number, it's easy to use in an if statement.

Please edit your post to replace the quote tags with code tags.

Hi Pert!

Thank you for reacting. Unfortunately I'm just working a few weeks with C++ and not understanding everything already.... So 'ATOI' looks like the solution, but at this moment it's to difficult for me yet. After hours of trying to make some good lines also on this issue I wasn't very successfull ...

Grtz Loek

thisExplains it quite clearly. (don't want to get in trouble with the moderators, you should do the thinking yourself they'd say..)

What, exactly, are the characters in the array "buf"? Please post (using code tags) the transmitter code that sends the data.

The function atoi() expects a zero terminated character array, or C-string and will often misbehave if that is not the case.

jremington:
The function atoi() expects a zero terminated character array, and will often misbehave if that is not the case.

That is a good point maybe before trying to extract the integer do buf[buflen]='\0';

Or send the '\0' --> make sure you send 1 extra char (the trailing null) when you send your data, this way when you receive the information in the buf buffer it will be a proper cString and you can print it to the LCD directly without the for loop, you could just do a

  lcd.print(buf); 
  lcd.print((char)223);       // maakt het karakter voor het 'graden' teken
  lcd.print("C");

and then if you want to know what's the associated numeric value, then use atoi() indeed (if it's an integer or you just want the integral part)