Arduino RF Data Transmission

1.7k Views Asked by At

I have 3 ultrasonic sensor connected to 1 Arduino Uno device. I want to send their data to another Arduino Uno with RF transmitter. I want to send the sensor's id number (1,2,3) and the data (0 or 1).

I want to transmit the data from printDistance method but the message that transmitter sends is char *msg. Do I have to send only char values?

#include <VirtualWire.h>  

#undef int
#undef abs
#undef double
#undef float
#undef round

//Sonar 1
int echoPin1 =2;
int initPin1 =3;
int distance1 =0;

//Sonar 2
int echoPin2 =6;
int initPin2 =7;
int distance2 =0;

//Sonar 3
//int echoPin3 =8;
//int initPin3 =9;
//int distance3 =0;



void setup() {

        // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for RF Link module
    vw_setup(2000);                 
    vw_set_tx_pin(8);                


  pinMode(initPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(initPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
 // pinMode(initPin3, OUTPUT);
 // pinMode(echoPin3, INPUT);
//  pinMode(initPin4, OUTPUT);
 // pinMode(echoPin4, INPUT);
  delay(2000);
  Serial.begin(9600);
  Serial.println(" ");
}

void loop() {

     const char *msg;    // this is your message to send

   vw_send((uint8_t *)msg, strlen(msg));
   vw_wait_tx();                                          // Wait for message to finish
   delay(200);



  distance1 = getDistance(initPin1, echoPin1);
  printDistance(1, distance2);
  delay(10);

  distance2 = getDistance(initPin2, echoPin2);
  printDistance(2, distance2);
  delay(10);

  //distance3 = getDistance(initPin3, echoPin3);
  //printDistance(3, distance3);
  //delay(10);

 // distance4 = getDistance(initPin4, echoPin4);s
 // printDistance(4, distance4);

  Serial.println(" ");  
  delay(5000);
//  Serial.println(" ");
}

int getDistance (int initPin, int echoPin){

 digitalWrite(initPin, HIGH);
 delayMicroseconds(10); 
 digitalWrite(initPin, LOW); 
 delayMicroseconds(5);
 unsigned long pulseTime = pulseIn(echoPin, HIGH); 
 int distance = pulseTime/58;
 return distance;

}

 void printDistance(int id, int dist){

     Serial.print('<');  

     Serial.print( id );
       Serial.print( '>' );

    if (dist >= 30 || dist <= 0 ){
      Serial.print("0");
    }else {
    Serial.print("1");
    }

     Serial.print('<');

     Serial.print( '/' );

     Serial.print( id );
       Serial.print( '>' );

  //   Serial.println(" ");


 }
2

There are 2 best solutions below

1
On

uint8_t vw_send(uint8_t* buf, uint8_t len) sends an array of bytes, with maximum length 77. In C, the type for a byte is called char. In AVR, this has an alias uint8_t as well.

So you send an array of bytes, and it is up to you to decide how the chars in the array are interpreted. For example, you could use sprintf() to write numbers as ascii encoded strings. Then on the receiving end, you would have to use atoi() to get the number back out of the string.

You could also choose to simply fill the array with the actual number values. With this option, you have to break up ints into separate bytes, and combine them back together on the receiving end. In your particular case, the data already fits into bytes, so you don't have to do that.

Beware that vw_send((uint8_t *)msg, strlen(msg)); won't work correctly with this second method. strlen() will count up to the first byte that holds a 0, effectively truncating the array. You would only use this call when using the sprintf() approach.

It looks like you are sending the data for all three sensors at the same time, and that the data for each is either 0 or 1. Why not send a 3-byte message of 0s and 1s?

uint8_t msg[3];

msg[0] = 0;
msg[1] = 1;
msg[2] = 0; // fill these in as you like

vw_send(msg, 3);
0
On

I couldn't add code to comment.

I changed the code as this;

void printDistance(int id, int dist){
     uint8_t msg[1];

if (dist >= 30 || dist <= 0 ){
   msg[0] = 0;
    vw_send(msg, 1);
     //vw_wait_tx(); 

}else {
  msg[0] = 1;
    vw_send(msg, 1);
     //vw_wait_tx(); 

}