ASK RF receiving Hex instead of text

Hello All. I am attempting to transmit switch depression data as text. The goal is to have the receiver compare the received text (string?) to the receivers text (string?) variables and act on a match.
The receiver is receiving correct data but it is in hex. The sketches are based on Radiohead's Transmit & Receive example sketches. Although the example codes have been modified to include code for other components, I have not changed any code that does the transmitting or receiving so as not mess with known good code. At least I think I have not messed it up somehow. I cannot detect any differences between my sketches and Radiohead's that would have my sketch act differently (displaying hex). Perhaps you guys can help. - Scotty

My Transmitter

#include <RH_ASK.h>
#include <SPI.h> //library defaults to using pin 12
#include <ezButton.h>
#define byte uint8_t
byte i = 0;
byte c = 0;
ezButton red_plus_button(5);
ezButton red_minus_button(7);
ezButton blue_plus_button(4);
ezButton blue_minus_button(6);
RH_ASK rf_transmitter;

void setup()
{
  rf_transmitter.init();
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  red_plus_button.loop();
  red_minus_button.loop();
  blue_plus_button.loop();
  blue_minus_button.loop();
  if (blue_plus_button.isPressed()) {
    char *msg1 = "BLUE PLUS";
    rf_transmitter.send((uint8_t *)msg1, strlen(msg1));
    Serial.println("                                            blue plus");
    Serial.println(msg1);
  }
  else if (red_plus_button.isPressed()) {
    
    char *msg2 = "RED PLUS";
    rf_transmitter.send((uint8_t *)msg2, strlen(msg2));
    Serial.println("                                            red plus");
    Serial.println(msg2);
  }
  else if (blue_minus_button.isPressed()) {
    char *msg3 = "BLUE MINUS";
    rf_transmitter.send((uint8_t *)msg3, strlen(msg3));
    Serial.println("                                            blue minus");
    Serial.println(msg3);
  }
  else if (red_minus_button.isPressed()) {
    char *msg4 = "RED MINUS";
    rf_transmitter.send((uint8_t *)msg4, strlen(msg4));
    Serial.println("                                            red minus");
    Serial.println(msg4);
  }
rf_transmitter.waitPacketSent();
  delay(250);

 // Serial.print("       *          ");
  //Serial.println(msg);
  //Serial.println(*msg);
  //Serial.println(strlen(msg));
}

My Receiver

#include <RH_ASK.h>
#include <SPI.h> //library defaults to using pin 11
#define byte uint8_t
byte led_pin = 3;
byte buzzer_pin = 6;
RH_ASK rf_receiver;

void setup()
{
  // Initialize ASK Object
  //rf_driver.init();
  rf_receiver.init();
  // Setup Serial Monitor
  Serial.begin(9600);
  pinMode(led_pin, OUTPUT);
  pinMode(buzzer_pin, OUTPUT);
  digitalWrite(led_pin, HIGH);
  digitalWrite(buzzer_pin, HIGH);
  delay(1000);
  digitalWrite(led_pin, LOW);
  digitalWrite(buzzer_pin, LOW);
}

void loop()
{
  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
  uint8_t buflen = sizeof(buf);
  if (rf_receiver.recv(buf, &buflen))
  {
    int i;
    rf_receiver.printBuffer("GOT: ", buf, buflen);
  }
}

Radiohead Transmit

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module

#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";

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

Radiohead Receive

showing as hex
// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module

#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[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
	int i;

	// Message with a good checksum received, dump it.
	driver.printBuffer("Got:", buf, buflen);
    }
}

Below is receiver serial monitor data

GOT:
52 45 44 20 4D 49 4E 55 53
GOT:
42 4C 55 45 20 4D 49 4E 55 53
GOT:
52 45 44 20 50 4C 55 53
GOT:
42 4C 55 45 20 50 4C 55 53
GOT:
42 4C 55 45 20 4D 49 4E 55 53
GOT:
52 45 44 20 4D 49 4E 55 53
GOT:
52 45 44 20 50 4C 55 53
GOT:
52 45 44 20 50 4C 55 53
GOT:
42 4C 55 45 20 4D 49 4E 55 53

is the received data being displayed in hex?

see ASCII table

So it's ASCII rather than hex?

In that case I've scouring the internet using the wrong key words.

Bytes received from a serial port are just that; bytes. A serial connection doesn't carry information about what the bytes represent, your code has to do that. If you want the received bytes to be interpreted as text then store them in a char array and possibly explicitly cast them as char.

Your buffer is currently uint8_t, so it's not going to get printed as text.

You can wrap you receive buffer is a SafeString and then treat it like a String

showing as hex
// ask_receiver.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to receive messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) receiver with an Rx-B1 module

#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
#include <SafeString.h>
// install SafeString from library manager,  tutorial at https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html
RH_ASK driver;

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

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

  if (driver.recv(buf, &buflen)) // Non-blocking
  {
    int i;
    // wrap the buffer in a SafeString
    createSafeStringFromCharPtr(sfBuffer, buf); // or cSFP(sfBuffer, buf); for short
    // now use String type compares and prints etc
    Serial.print("Got:"); Serial.print(sfBuffer);
    if (sfBuffer == "Hello") {
      // got Hello do something
    }
  }
}

This transmits ASCII characters, but does not send the required terminating zero byte of an ASCII C-string.

    char *msg3 = "BLUE MINUS";
    rf_transmitter.send((uint8_t *)msg3, strlen(msg3));

To print out that message on the receiving end, you should add back that terminating zero, then print it as a character array:

    if (driver.recv(buf, &buflen)) // Non-blocking
    {
	// Message with a good checksum received, terminate and print it
       buf[buflen]=0;
       Serial.println( (char *) buf);
    }
[code]
buf[buflen]=0;

necessary but not safe as the driver can return
buflen == RH_ASK_MAX_MESSAGE_LEN and so
buf[buflen]=0 will overflow the input buffer (welcome to the world of c-string programming errors)

You need

  uint8_t buf[RH_ASK_MAX_MESSAGE_LEN+1];  // for the null
  uint8_t buflen = RH_ASK_MAX_MESSAGE_LEN;

You need

No, but thanks for the suggestion.

If this were my project, I would of course check whether buflen==RH_ASK_MAX_MESSAGE_LEN and terminate the message with buf[RH_ASK_MAX_MESSAGE_LEN-1]=0;

The premature termination of the character string should act to remind the user of their mistake.

terminate the message with buf[RH_ASK_MAX_MESSAGE_LEN-1]=0;

Then you would loose the last byte of data :frowning:
buflen==RH_ASK_MAX_MESSAGE_LEN is a valid length

Then you would loose the last byte of data :frowning:

Yes, of course. Read the last line of reply #7 to see why I think that is a good idea.

For my work, which is mostly with remote sensors, it makes no sense to send text so the issue does not come up.

For my work, which is mostly with remote sensors, it makes no sense to send text so the issue does not come up.

Interesting, I wondered why Paul Stoffregen did not just add a terminating '\0' in the driver code.
My radio work is all in text.
https://www.forward.com.au/pfod/GarageDoorRemote/LoRa_GarageDoorRemote/index.html

RadioHead was written by and is maintained by Michael McCauley. Why do you ask about Paul Stoffregen?

My mistake

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