Go Down

Topic: [solved]RF433MHZ doesn't work (Read 272 times) previous topic - next topic

kinda

Mar 31, 2020, 04:09 pm Last Edit: Apr 01, 2020, 07:38 pm by kinda
Hi!!
I am using RF433MHZ module,but it didn't work.
I used the library RadioHead and I connected the Transmitter with uno and the Receiver with Mega.
This is the code of Transmitter:
Code: [Select]

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

RH_ASK driver;

void setup()
{
    Serial.begin(9600);    // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    const char *msg = "Hello World!";
    driver.send((uint8_t *)msg, strlen(msg));
    driver.waitPacketSent();
    delay(1000);
}

This is the code of Receiver:
Code: [Select]

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

RH_ASK driver;

void setup()
{
    Serial.begin(9600);  // Debugging only
    if (!driver.init())
         Serial.println("init failed");
}

void loop()
{
    uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking
    {
      int i;
      // Message with a good checksum received, dump it.
      Serial.print("Message: ");
      Serial.println((char*)buf);        
    }
}

The message "Hello World!" didn't appear on the serial monitor in the receiver.
I will uploaded two pictures in the attachments to show you how I connected the transmitter and receiver with uno and mega.
Please help me.Thank you.

pylon

#1
Mar 31, 2020, 05:09 pm Last Edit: Mar 31, 2020, 05:11 pm by pylon
Code: [Select]
#include <SPI.h> // Not actualy used but needed to compile


You don't need to include this header to compile.

You send the string without the terminating null byte:
Code: [Select]
    const char *msg = "Hello World!";
    driver.send((uint8_t *)msg, strlen(msg));


You're able to receive messages of exactly that size:
Code: [Select]
    uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking


So that code:
Code: [Select]
      Serial.println((char*)buf);         
      Serial.println((char*)buf);         


will produce unpredictable results.

Did you check your hardware? A HIGH on the send pin should produce a HIGH on the reception pin and vice-versa.

kinda

Code: [Select]
#include <SPI.h> // Not actualy used but needed to compile


You don't need to include this header to compile.

You send the string without the terminating null byte:
Code: [Select]
    const char *msg = "Hello World!";
    driver.send((uint8_t *)msg, strlen(msg));


You're able to receive messages of exactly that size:
Code: [Select]
    uint8_t buf[12];
    uint8_t buflen = sizeof(buf);
    if (driver.recv(buf, &buflen)) // Non-blocking


So that code:
Code: [Select]
      Serial.println((char*)buf);         
      Serial.println((char*)buf);         


will produce unpredictable results.

Did you check your hardware? A HIGH on the send pin should produce a HIGH on the reception pin and vice-versa.
Yes,I checked the hardware,and the code works with another people that use two uno boards.
I think that the code is correct.

jremington

#3
Apr 01, 2020, 12:57 am Last Edit: Apr 01, 2020, 01:08 am by jremington
No, the code is not correct.

buf[] is declared too small on the receiver side and is not zero terminated on receive.  You must make sure that the received message is zero terminated, as pointed out by pylon, or the result of printing will be unpredictable.

Correct the code as follows:
Code: [Select]
   
uint8_t buf[20];  //must be larger than any expected message
...
if (driver.recv(buf, &buflen)) // Non-blocking
    {
      buf[buflen]=0;  //make sure it is zero terminated.
      // Message with a good checksum received, dump it.
      Serial.print("Message: ");
      Serial.println((char*)buf);         
    }


But first, check your setup by replacing both the transmitter and receiver with a wire, and connect the grounds of the two boards.  The TX pin on the transmitter board is connected by a wire to the RX pin on the receiver board. If the code is correct, that will work to transmit data.

You need antennas on the two RF modules. 17 cm of straight wire is fine.

kinda

#4
Apr 01, 2020, 11:31 am Last Edit: Apr 01, 2020, 11:37 am by kinda
The TX pin on the transmitter board is connected by a wire to the RX pin on the receiver board. If the code is correct, that will work to transmit data.
I didn't understand what do you mean with this sentences.
Could you see the pictures in the attachments?
I connected the data pin of transmitter to pin 12 in arduino uno board,and the data pin of receiver to pin 11 in arduino mega board.Are there any problems in this case??
Thank you very much

jremington

#5
Apr 01, 2020, 05:09 pm Last Edit: Apr 01, 2020, 05:10 pm by jremington
To test the code without the RF modules:

1. remove the modules.

2. Connect a wire from pin 12 of the transmitter Arduino to pin 11 of the receiver Arduino.

3. Connect a wire from GND of the transmitter Arduino to GND of the receiver Arduino.

4. Test the code.


kinda

To test the code without the RF modules:

1. remove the modules.

2. Connect a wire from pin 12 of the transmitter Arduino to pin 11 of the receiver Arduino.

3. Connect a wire from GND of the transmitter Arduino to GND of the receiver Arduino.

4. Test the code.


The code works.Thank you very much.

Go Up