0

I used an HC-SR04 sensor and connected it to an arduino board (BOARD1) with a 433 mhz transmitter and the other board (BOARD2) is connected to the 433 mhz receiver but I don't know how to see the sensor value readings from the (BOARD1) and seen in the serial monitor.

HERE IS THE CODE OF THE TRANSMITTER

#include <RH_ASK.h>
#include <SPI.h> // Not actually used but needed to compile


RH_ASK driver;
int echo=11;
int trig=9; int sensorval;
void setup()
{

    Serial.begin(9600);   // Debugging only

   driver.init();

}

void loop()
{
    digitalWrite(trig,LOW);
   delayMicroseconds(10);
   digitalWrite(trig,HIGH);
   delayMicroseconds(10);
   digitalWrite(trig,LOW);
   delayMicroseconds(10);
   sensorval=pulseIn(echo,HIGH);
     const char *msg = sensorval;

    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(200);
}

HERE IS THE CODE OF THE RECEIVER

#include <RH_ASK.h>

#include <SPI.h> // Not actually used but needed to compile


RH_ASK driver;
const int sensorval;
void setup()
{
    Serial.begin(9600);   // Debugging only
    driver.init();

}

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

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
    Serial.print("message Received: ");
 Serial.println((char*)buf);
    }
}

1 Answer 1

0

First: A short excourse about c-strings

In C a string is an array of characters. Such an array is defined with

char msg[10];

10 being the size of the array. Every valid c-string needs to be terminated with the null character \0. This character is not printable, thus you don't see it normally. But the standard string functions (like printf(), strlen(), etc) use the null character to find the end of the string. So when handling strings as char arrays, you absolutely always need to terminate the string with the null character. The standard functions are incrementing through the array, until they find the null character. If it is missing, they don't stop at the end of the array.

Needing the null character also means, that the above char array can only hold 9 characters (since the 10th is the null character).

The standard functions for creating a string (like printf() and its siblings) already terminate the string correctly with the null character.


You are obviously trying to send an ASCII encoded string. But sensorval is not a string, it is of type int (a number). In C/C++ one does not simply implicitly convert a number to a string (please insert the image of boromir from Lord of the Rings here in your head). You actually have to say the compiler how to convert the number to a string. Also sensorval cannot itself hold that string (as it is an integer number). For that you need a char array (often called c-string) for saving all the characters of your string.

So first we need to define the char array to hold our string:

char msg[10];

This creates a char array with 10 elements, each one can hold a character of your string. So you have space for 9 characters. If you need more, you can increase that number.

Now we need to build a string in msg, that represents the value of sensorval. For that we can use sprintf(), which lets us format a string out of data and save it in a char array.

sprintf(msg, "%d", sensorval);

The first parameter is the char array to put the string in. The second parameter is called format-string. That is a string literal, that defines the format. %d represents an integer value; it is a placeholder for the actual data and describes, how it should be formatted (you can read about the different format options in the C++ documentation if you search for printf()). The following parameters (here only one) are the data values for the placeholders in the format string. Let's take an example: Assume sensorval is 1000. Then msg will be:

1000\0

The \0 being the null character.

So in your code you would replace

const char *msg = sensorval;

with

char msg[10];
sprintf(msg, "%d", sensorval);

What you have done in your code:

This line

const char *msg = sensorval;

will NOT create a string. You are defining msg as a pointer to a constant character (a pointer is a variable, that holds the address another variable in the memory, use google terms like "C pointers" for more info). And then you set the pointer to sensorval, which of course is not a valid pointer (valid meaning pointing to something reasonable here). It just points somewhere in the memory.

The example, that you got it from uses:

const char *msg = "hello";

This works, because the compiler takes all string literals (the strings enclosed by the double quotes) and puts them fixed into the memory, somewhat like a variable without a name. So here the compiler instructs the microcontroller so save the string "hello" in the memory. With the above line we define a pointer to the place in the memory, where this string is saved.

So this works good for string literals, but not, if you first need to actually build the string.

5
  • i'm sorry i am new to this so i don't quite understand what to do. From what I have read on your answer, should I replace the const char *msg=sensorval; to ``` char msg[10]; sprintf(msg, "%d", sensorval);``` or before the void setup, I will put a ``` string sensorval; ```? Apr 26, 2021 at 12:49
  • @ThomasRaymondSantos I overhauled my answer to explain in more depth, what is happening here and what to do.
    – chrisl
    Apr 26, 2021 at 13:44
  • okay i'll try to understand, thank you so much for the help! Apr 26, 2021 at 14:03
  • Otherwise it might be good to do some tutorials about c-strings. I'm sure there are many online
    – chrisl
    Apr 26, 2021 at 14:05
  • thank you @chrisl appreciate it! Apr 26, 2021 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.