How How 433MHz RF Tx-Rx Modules Work & Interface with Arduino

Both Tx and Rx Files produce the same comments:

  1. 'The 'File' is either 'Binary' or uses an un-supported 'Text Coding'

  2. and occasionally - 'Compilation Error: unterminated comment'

Thank you for any help

Do you think there’s more we could use to help you ?

Very little help possible based on that description.

No details of the 'RF Modules' you are using.

No detail of the Arduino you are using.

No indication of what code or language you are using.

Thank you for you reply. I am Using Arduino R3. This project came through Pinterest and 'Last Minute Engineers.com'. It uses RadioHead Library.

and

Thank you for any help.

It would be better if you insert the code as text using code tags rather than images.
After that please clarify what is your question.

And where are the files that you talked about:

Both Tx and Rx Files produce the same comments:

'The 'File' is either 'Binary' or uses an un-supported 'Text Coding'

and occasionally - 'Compilation Error: unterminated comment'

Thank you for any help

//Arduino 433Mhz Rx Code
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
    // Setup Serial Monitor
    Serial.begin(9600);
}
void loop()
{
    // Set buffer to size of expected message
    uint8_t buf[11];
    uint8_t buflen = sizeof(buf);
    // Check if received packet is correct size
    if (rf_driver.recv(buf, &buflen))
  
      // Message received with valid checksum
      Serial.print("Message Received: ");
      Serial.println((char*)buf);         
    }
}
// Arduino 433MHz Tx Code
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library 
#include <SPI.h> 
 
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
 
void setup()
{
    // Initialize ASK Object
    rf_driver.init();
}
 
void loop()
{
    const char *msg = "Hello World";
    rf_driver.send((uint8_t *)msg, strlen(msg));
    rf_driver.waitPacketSent();
    delay(1000);
}

I hope tht the above is sufficeint.

Are the files a part of the library? Please provide a link to it

I am afraid no.
What is your question?

When I try to 'Verify' these two skektches, prior to uploading them, the error messages shown above are indicated.

if I Verify the first sketch of post 6 I get

C:\Users\xx\AppData\Local\Temp\.arduinoIDE-unsaved20231128-10704-1tdih7f.rlul\sketch_dec28a\sketch_dec28a.ino:27:1: error: expected declaration before '}' token
 }

looks like you may have corrupt code files - where did you get the code from?
what operating system and version of the Arduino IDE are you using?
what 433MHz radio boards are you using?
have you tried Radiohead example code, e.g. File>Examples>Radiohead>ask>ask_receiver

@panda-henderson
According to the compiler's messages, these files are not sourcecode.

But the problem is not with the files, the problem is with you. There are already 10 messages of completely meaningless correspondence - and you are not even able to clearly state the problem. What kind of files are these, where did you get them from, what are their filenames??? Why did you decide that they need to be compiled?

If you want help, answer questions clearly and completely, with details. And if you are too lazy to write an extra word, then why did you even come to the forum?

I am sory I have inconvenienced you. I am trying to do my best, I am new to Arduino and am not sure whether I am following the right procedures some of the time time. You are probbaly right, perhaps, I should not be on the Forum. So I will leave the Forum and come back when I have a better understanding of the protocols. Thank you for your help anyway.

You may have copied and pasted off a web site or other source and inadvertently picked up some characters that are not flying, or

Try using the code you posted here. Maybe in the act of posting you fixed something. Use the little button that appears in the code window to copy the code; paste it into a barn new blank sketch.

I'd try it myself but I am in transit looking through the ti y window.

a7

Thank you for your input. I will look again at from where I copied the files and see whether there is a problem there. The Arduino board I use is upto date. The 433MHz Rx and Tx boards are those shown in the 'Last Minute Engineers.com Example of how to use these small radios with Arduino'. I have included RadioHead's Library. I have not got as far as actually buying the boards, but just trying to very the sketches
image

before you buy modules which maybe unsuitable for your project it would be worth while for you to describe the project requirements

are you looking at 433MHz transmitter and receiver
it may be worth looking at

  1. hc-12-long-range-wireless-communication-module 433.4 MHz to 473.0 MHz which can transmit and receive
  2. LoRa 433MHz long range radio

the above require a host microcontroller - have you selected one ?
if not consider a microcontroller with onboard radio, e.g. ESP32 with onboard WiFi, Bluetooth Classic and BLE

but first tell us what the project is??

I have already built a successful HC-12 project. I am now looking at other possibilities and am told that ESP32 is a good option. As I am sruggling with the project from 'Last Minute Engineers' - 'Insight into how 433MHz R.F. Tx-Rx Modules Work and Interface with Arduino', this could prove to be a better option. I used two-Arduinos and two-HC-12 boards for the first project. Thank you for your help an dpointing me in a different direction.

a simple test I ran using 433MHz transmitter and receiver transmitting a string
ESP32 acting as host for a 433MHz transmitter

// ESP32 433MHz transmitter test 1 (Tools>Board set to NodeMCU-32S)

#include <RH_ASK.h>  // Include RadioHead Amplitude Shift Keying Library
#include <SPI.h>     // Include dependant SPI Library

//RH_ASK(uint16_t speed = 2000, uint8_t rxPin = 11, uint8_t txPin = 12, uint8_t pttPin = 10, bool pttInverted = false);
RH_ASK rf_driver(2000, 21, 22);  // ESP32 Create Amplitude Shift Keying Object

void setup() {
  Serial.begin(115200);
  delay(4000);
  if (RH_PLATFORM == RH_PLATFORM_ESP32)
    Serial.println("RH_PLATFORM_ESP32");
  Serial.println("ESP32 433MHz transmitter");
  if (!rf_driver.init()) {
    Serial.println("init failed");
    while (1) delay(10000);
  }
  Serial.println("Transmitter: rf_driver initialised");
}

// transmit packet every 5 seconds
void loop() {
  Serial.println("Transmitting packet");
  const char *msg = "Hello World";
  rf_driver.send((uint8_t *)msg, strlen(msg) + 1);
  rf_driver.waitPacketSent();
  delay(5000);
}

ESP8266 acting as host for a 433MHz receiver

// ESP8266 433MHz receiver test 1  (Tools>Board set to Generic ESP8266 module)

#include <RH_ASK.h>  // Include RadioHead Amplitude Shift Keying Library
#include <SPI.h>     // Include dependant SPI Library

RH_ASK rf_driver(2000, 2, 4);  // ESP8266 Create Amplitude Shift Keying Object

void setup() {
  Serial.begin(115200);
  delay(4000);
  Serial.println("ESP8266 433MHz receiver");
  if (RH_PLATFORM == RH_PLATFORM_ESP8266)
    Serial.println("RH_PLATFORM_ESP8266");
  delay(5000);
  Serial.println("Receiver: rf_driver initialising");
  if (!rf_driver.init()) {
    Serial.println("init failed");
    while (1) delay(1000);
  }
  Serial.println("Receiver: rf_driver initialised");
}

void loop() {
  uint8_t buf[20]={0};  // Set buffer to size of expected message
  uint8_t buflen = sizeof(buf);
  // Check if received packet is correct size
  if (rf_driver.recv(buf, &buflen)) {
    // Message received with valid checksum
    Serial.print("Message Received: ");
    Serial.println((char*)buf);
  }
}

esp32 serial monitor output

RH_PLATFORM_ESP32
ESP32 433MHz transmitter
Transmitter: rf_driver initialised
Transmitting packet
Transmitting packet
Transmitting packet
Transmitting packet

ESP8266 serial monitor output

ESP8266 433MHz receiver
RH_PLATFORM_ESP8266
Receiver: rf_driver initialising
Receiver: rf_driver initialised
Message Received: Hello World
Message Received: Hello World
Message Received: Hello World
Message Received: Hello World

although the above is for a ESP32 and ESP8266 should work on any microcontroller by setting up the correct pins for

RH_ASK(uint16_t speed = 2000, uint8_t rxPin = 11, uint8_t txPin = 12, uint8_t pttPin = 10, bool pttInverted = false);

the above code only transmits a simple 'hello world' string - I seem to remember having an example transmitting a complext structure

Thank you for taking the time to put together these sketches. I took yesterday off, as it was New Year's Eve. I will keep these aside until I have the ESP32.

Thank you for your time and a Happy New Year.

transmit and receive a structure - I was using ESP32 but any suitable microcontroller should work

// ESP32 433mHz transmit a structure - from File>Examples>RadioHead>ask

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

//RH_ASK driver;
RH_ASK driver(2000, 4, 5, 0);  // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85),
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS

#define nodeID 2  //  <<< set up required node ID

// test structure
struct __attribute__((packed)) Struct1 {
  byte StructureID;  // identifies the structure type
  byte NodeID;       // ID of transmitting node
  int16_t seq;       // sequence number
  int16_t x;
  float y;
  char z[10];
  byte crc;           // CRC check
};

Struct1 struct1 = { 1, nodeID, 0, 1, 4.5, "hello", 0 };

void setup() {
  Serial.begin(115200);  // Debugging only
  delay(2000);
  Serial.println("\n433mHz transmit Structure");
  if (!driver.init()) {
    Serial.println("init failed");
    while(1) delay(1000);
  }
  Serial.println("init OK");
}

void loop() {
  Serial.print("Transmitting ");
  struct1.seq++;  // increment packet sequence number
  struct1.x += 1; // and test data
  struct1.y += 1;
  struct1.z[0] += 1;
  struct1.crc=CRC((byte *) &struct1);   // calculate CRC
  // transmit the structure
  driver.send((uint8_t *)&struct1, sizeof(struct1));
  driver.waitPacketSent();
   Serial.print(" StructureID ");
  Serial.print(struct1.StructureID);
  Serial.print(" Node ");
  Serial.print(nodeID);
  Serial.print(" seq number ");
  Serial.print(struct1.seq);
  Serial.print(" x = ");
  Serial.print(struct1.x);
  Serial.print(" y = ");
  Serial.print(struct1.y);
  Serial.print(" z = ");
  Serial.print(struct1.z);
  Serial.print(" crc = 0x");
  Serial.println(struct1.crc, HEX);
  delay(2000);
}

// simple checksum - replace with CRC8
byte CRC(byte * s) {
  byte crc=0;
  for(int i=0;i<sizeof(Struct1)-1;i++) crc+=s[i];
  return crc;  
}

receiver

// ESP32 433mHz receive structure - from File>Examples>RadioHead>ask

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

//RH_ASK driver;
RH_ASK driver(2000, 4, 5, 0);  // ESP8266 or ESP32: do not use pin 11 or 2
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85),
// RH_ASK driver(2000, PD14, PD13, 0); STM32F4 Discovery: see tx and rx on Orange and Red LEDS

struct __attribute__((packed)) Struct1 {
  byte StructureID;  // identifies the structure type
  byte NodeID;       // ID of transmitting node
  int16_t seq;       // sequence number
  int16_t x;
  float y;
  char z[10];
  byte crc;
} struct1;

void setup() {
  Serial.begin(115200);  // Debugging only
  delay(2000);
  Serial.println("433mHz receive Structure");
  if (!driver.init()) {
    Serial.println("init failed");
    while (1) delay(1000);
  }
  Serial.println("init OK");
}

void loop() {
  static int16_t node1seq = 0, node2seq = 0, seqErrors = 0, crcErrors;
  uint8_t buf[100];  // Set buffer to size of expected message
  // receive  packet - check is correct size
  uint8_t len = 100;
  if (driver.recv((uint8_t *)&struct1, &len)) {
    if (len != sizeof(Struct1)) {
      Serial.print("Error - incorrect packet size received!! length ");
      Serial.println(len);
      return;
    }
    // Message received with valid checksum  ??
    Serial.print("Received ");
    Serial.print(len);
    Serial.print("bytes StructID ");
    Serial.print(struct1.StructureID);
    Serial.print(" Node ");
    Serial.print(struct1.NodeID);
    Serial.print(" seq ");
    Serial.print(struct1.seq);
    Serial.print(" x = ");
    Serial.print(struct1.x);
    Serial.print(" y = ");
    Serial.print(struct1.y);
    Serial.print(" z = ");
    Serial.print(struct1.z);
    Serial.print(" crc = 0x");
    Serial.print(struct1.crc, HEX);
    if (struct1.NodeID == 1) {
      if (struct1.seq != node1seq) {
        Serial.print("\n  seq number error expected ");
        Serial.print(node1seq);
        node1seq = struct1.seq;
        ++seqErrors;
      }
      node1seq++;
    }
    if (struct1.NodeID == 2) {
      if (struct1.seq != node2seq) {
        Serial.print("\n  seq number error expected ");
        Serial.print(node2seq);
        ++seqErrors;
        node2seq = struct1.seq;
      }
      node2seq++;
    }
    Serial.print(" seq errors ");
    Serial.print(seqErrors);
    byte crc = CRC((byte *)&struct1);
    //crc++;                        // uncommend to display crc error message
    if (crc != struct1.crc) {
      Serial.print("\n  crc error calculated 0x");
      Serial.print(crc, HEX);
      crcErrors++;
    }
    Serial.print(" crc errors ");
    Serial.println(crcErrors);
  }
}

// simple checksum - replace with CRC8
byte CRC(byte *s) {
  byte crc = 0;
  for (int i = 0; i < sizeof(Struct1) - 1; i++) crc += s[i];
  return crc;
}

serial output

433mHz transmit Structure
init OK
Transmitting  StructureID 1 Node 2 seq number 1 x = 2 y = 5.50 z = iello crc = 0xB
Transmitting  StructureID 1 Node 2 seq number 2 x = 3 y = 6.50 z = jello crc = 0x2E
Transmitting  StructureID 1 Node 2 seq number 3 x = 4 y = 7.50 z = kello crc = 0x51
Transmitting  StructureID 1 Node 2 seq number 4 x = 5 y = 8.50 z = lello crc = 0x6D
Transmitting  StructureID 1 Node 2 seq number 5 x = 6 y = 9.50 z = mello crc = 0x80
Transmitting  StructureID 1 Node 2 seq number 6 x = 7 y = 10.50 z = nello crc = 0x93
Transmitting  StructureID 1 Node 2 seq number 7 x = 8 y = 11.50 z = oello crc = 0xA6
Transmitting  StructureID 1 Node 2 seq number 8 x = 9 y = 12.50 z = pello crc = 0xB9
Transmitting  StructureID 1 Node 2 seq number 9 x = 10 y = 13.50 z = qello crc = 0xCC
Transmitting  StructureID 1 Node 2 seq number 10 x = 11 y = 14.50 z = rello crc = 0xDF
Transmitting  StructureID 1 Node 2 seq number 11 x = 12 y = 15.50 z = sello crc = 0xF2
Transmitting  StructureID 1 Node 2 seq number 12 x = 13 y = 16.50 z = tello crc = 0x1
Transmitting  StructureID 1 Node 2 seq number 13 x = 14 y = 17.50 z = uello crc = 0xC
Transmitting  StructureID 1 Node 2 seq number 14 x = 15 y = 18.50 z = vello crc = 0x17
Transmitting  StructureID 1 Node 2 seq number 15 x = 16 y = 19.50 z = wello crc = 0x22
Transmitting  StructureID 1 Node 2 seq number 16 x = 17 y = 20.50 z = xello crc = 0x2D
Transmitting  StructureID 1 Node 2 seq number 17 x = 18 y = 21.50 z = yello crc = 0x38
Transmitting  StructureID 1 Node 2 seq number 18 x = 19 y = 22.50 z = zello crc = 0x43



433mHz receive Structure
init OK
Received 21bytes StructID 1 Node 2 seq 4 x = 5 y = 8.50 z = lello crc = 0x6D
  seq number error expected 0 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 5 x = 6 y = 9.50 z = mello crc = 0x80 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 6 x = 7 y = 10.50 z = nello crc = 0x93 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 7 x = 8 y = 11.50 z = oello crc = 0xA6 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 8 x = 9 y = 12.50 z = pello crc = 0xB9 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 9 x = 10 y = 13.50 z = qello crc = 0xCC seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 10 x = 11 y = 14.50 z = rello crc = 0xDF seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 11 x = 12 y = 15.50 z = sello crc = 0xF2 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 12 x = 13 y = 16.50 z = tello crc = 0x1 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 13 x = 14 y = 17.50 z = uello crc = 0xC seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 14 x = 15 y = 18.50 z = vello crc = 0x17 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 15 x = 16 y = 19.50 z = wello crc = 0x22 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 16 x = 17 y = 20.50 z = xello crc = 0x2D seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 17 x = 18 y = 21.50 z = yello crc = 0x38 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 18 x = 19 y = 22.50 z = zello crc = 0x43 seq errors 1 crc errors 0
Received 21bytes StructID 1 Node 2 seq 19 x = 20 y = 23.50 z = {ello crc = 0x4E seq errors 1 crc errors 0

the receiver displays a sequence error message then the sequence number synchronise

The last post seems to have resolved the problem. Thank you for your help; the solution was more complex than I first realised

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.