Arduino 433 mhz rf Sending numbers instead of strings

I am learning about using the very cheap $2 433 Mhz wireless transmit & Receive eBay boards.
I followed the instructions and got it working (hello and other strings).
My intended use is to transmit water pressure numbers from the arduino at the pipe to the arduino at my display. But every example i see only sends strings. It looks like if i want to send numbers, i must convert them to strings for transmission, and convert them back to numbers so i can use some logic on them.

Am i missing something, or does the RadioHead library only send strings ?

The RadioHead.h file has some information on sending other than text. Look in the file at line 950.

You can send any sequence of bytes. Post the code you would like to modify and we can suggest changes.

Or, since this question comes up often, search the forum for solutions.

I think that it's time that you understand a little more in-depth how it works :wink:

Looking at the loop() in the ask_transmitter example

void loop()
{
    const char *msg = "hello";

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

There is a send() method; you can find that in C:\Users\yourUserName\Documents\Arduino\libraries\RadioHead\RH_ASK.cpp and the first line is important.

bool RH_ASK::send(const uint8_t* data, uint8_t len)

and 'states' that the send method expects a pointer to an uint8_t (aka byte) variable and a length.

The line can also be found (in a slightly different form) in C:\Users\yourUserName\Documents\Arduino\libraries\RadioHead\RH_ASK.h (that's the file that you include at the top of the sketch).

So if you now go back to the example code, you will see (uint8_t *)msg ; this is a so-called cast that tells the compiler to treat the char array (msg) as a byte array.

So now you know that you actually need to send bytes and not characters etc. and how that's achieved.

To send an int value, you can cast the address of the variable (the pointer) to an pointer to a byte. An int occupies a number of bytes; you can find that with the sizeof operator.

That would look like

int waterPressure = 1234;
drive.send((uint8_t*)&waterPressure, sizeof(waterPressure));

Because waterPressure is not an array, you need the & in front of it to get the address of the variable

For now, I'll leave the receiver side to you.

Note:
You did not tell us which Arduinos are involved. Both might be Unos or one can be an Uno and the other one a Due or whatever which can make a difference.

1 Like

I struck gold with you three people. Special thanks to groundFungus and sterretje. I sat there smiling like an idiot while I was reading the RH library source, at 950.

Now maybe I can blunder my way to finishing my project.

I had never read a library source code before. I had always assumed it would be, to me, just the same as machine language. But a well commented library is actually very interesting. Thanks Mike McCauley.

BTW, if you want the receiver to have much longer range, and/or work at 3.3v, get an RXB12 receiver on ebay - they're only like $1 shipped, and are way the hell better than the cheap green receivers that everyone uses.

I read what I could find, which wasn't much. But out of curiosity I ordered a couple.
thanks for the suggestion.

I got it working (sending and receiving numbers), but it is clear I don't understand a part of the code that i copied. Below is the receiver sketch. As I say, it works fine except that if I try to comment out any of the Serial.println commands, or the xyz variable, it stops working. It no longer recognizes changes I make to the transmit data. I don't need them, or want them, but can't figure out how to get rid of them.

// ask_receiver.pde  11/16/19
// -*- 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
// Tested on Arduino Mega, Duemilanova, Uno, Due, Teensy, ESP-12

#include <Arduino.h>
#include <Wire.h>
#include <U8g2lib.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);

#include <RH_ASK.h>
#ifdef RH_HAVE_HARDWARE_SPI
#include <SPI.h>
#endif

//RH_ASK driver;
RH_ASK driver(1000, 2, 4, 0); // Arduino
// RH_ASK driver(2000, 3, 4, 0); // ATTiny, RX on D3 (pin 2 on attiny85) TX on D4 (pin 3 on attiny85), 

void setup()
{
 
#ifdef RH_HAVE_SERIAL
    Serial.begin(9600);	  // Debugging only
#endif
    if (!driver.init())
#ifdef RH_HAVE_SERIAL
         Serial.println("init failed");
#else
	;
#endif

 display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (use I2C scanner to find addresses for I2C devices)
  display.clearDisplay();
}

void loop()
{
    
     uint16_t data;
     uint16_t xyz = data;
     uint8_t datalen = sizeof(data);
      if (   driver.recv((uint8_t*)&data, &datalen)
       && datalen == sizeof(data))
   {
   
     // Have the data, so do something with it
     //uint16_t xyz = data;
      }   // this } was not included in the example i copied, so i just stuck it in
      
     Serial.println(xyz);  // If I comment out this line,or remove xyz, it fails
     Serial.println(data);
     Serial.println("");   // If I comment out this line, it fails
     Serial.println("");Serial.println("");  // If I comment out this line, it fails
     delay(100);

     display.setTextSize(3);
     display.setTextColor(WHITE);
     display.setCursor(32,25);
     //display.println("Hello,gorld");
     display.println(data);
     display.display();
     delay(100);
     display.clearDisplay();
   
  
}

From line 950 on of the library, I read and pasted to get things working.

It may be that I goofed things up right off the bat with the {} right after the 'have the data, so do something with it'. that only came with an opening {, and i started guessing where to put the closing bracket, and what to put inside it.

Would someone have a look ?

It will be interesting to see what your sender sketch looks like.

I did two things. I started a new sketch from scratch, and that helped. and because i was getting inconsistent results (and arduino was warning me that the memory was 92% used), i bought a MEGA and put the sketch on it.
Now it works very well.