Thursday, September 19, 2013

Tutorial: Arduino + Kit rf 433Mhz (Part4: Multiple variables)

In the final part of this tutorial we will see how to send multiple variables using virtual wire. Let's start then.

There are a lot of forms to send multiple variables trough virtual wire. However,if we want to send different types of data or just send some int's, we can do in the same way, with this code.

Transmitter

With Virtual Wire we can only send an string. So if we want to send other types of data, we need to convert this data into a string.

Fortunately, there are a function to do this. Here is the following  line of code that we have to add.

// Function sprintf
 sprintf(Stringtosend, "%d,%d,%d,%d,", Variable1, Variable2, Variable3, Variable4);
The funcion sprintf converts differents types of data into a string. The funciĆ³n works like this:
sprintf(Stringtosend, specifier of the type of data, data);

specifierOutputExample
d or iSigned decimal integer392
uUnsigned decimal integer7235
oUnsigned octal610
xUnsigned hexadecimal integer7fa
XUnsigned hexadecimal integer (uppercase)7FA
fDecimal floating point, lowercase392.65
FDecimal floating point, uppercase392.65
eScientific notation (mantissa/exponent), lowercase3.9265e+2
EScientific notation (mantissa/exponent), uppercase3.9265E+2
gUse the shortest representation: %e or %f392.65
GUse the shortest representation: %E or %F392.65
aHexadecimal floating point, lowercase-0xc.90fep-2
AHexadecimal floating point, uppercase-0XC.90FEP-2
cCharactera
sString of characterssample
pPointer addressb8000000
nNothing printed.
The corresponding argument must be a pointer to a signed int.
The number of characters written so far is stored in the pointed location.
%% followed by another % character will write a single % to the stream.%

In the table we see the different specifiers  for all the types of data. Using this funcion we can also send an integer and a floating point

sprintf(Stringtosend, "%d,%f,", integer, floatingpoint);

For more information you can see the C++ reference http://www.cplusplus.com/reference/cstdio/printf/

Transmitter Example 


/*.............................................................
Sending Multiple Variables Using VirtualWire. Transmitter
Author: Rodrigo Mompo Redoli
For controlrobotics.rodrigomompo.com
..............................................................*/
#include <VirtualWire.h>

int Sensor1Pin = A1;// The pins were sensor are attached
int Sensor2Pin = A2;
int Sensor3Pin = A3;
int Sensor4Pin = A4; 
int ledPin = 13;
int Sensor1Data;// The variable were the data from each sensor
int Sensor2Data;// will be stored 
int Sensor3Data;
int Sensor4Data;
char Sensor1CharMsg[21];// The string that we are going to send trought rf 

void setup() {

 // LED 
 pinMode(ledPin,OUTPUT);
 
 // Sensor(s)
 pinMode(Sensor1Pin,INPUT);
 pinMode(Sensor2Pin,INPUT);
 pinMode(Sensor3Pin,INPUT);
 pinMode(Sensor4Pin,INPUT);
 
 // VirtualWire setup
 vw_setup(2000); // Bits per sec
 vw_set_tx_pin(12);// Set the Tx pin. Default is 12

}

void loop() {
  
  // Read and store Sensor Data
  Sensor1Data = analogRead(Sensor1Pin);
  Sensor2Data = analogRead(Sensor2Pin);
  Sensor3Data = analogRead(Sensor3Pin);
  Sensor4Data = analogRead(Sensor4Pin);
  
  sprintf(Sensor1CharMsg, "%d,%d,%d,%d,", Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);
 
 // Turn on a light to show transmitting
 vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
 vw_wait_tx(); // Wait until the whole message is gone
 // Turn off a light after transmission
 delay(40);

}

Receiver

Now we must be able to send an string and receive it. However ,with this string you can't do anything unless you discompose it and storage on differents variables.For this new challenge we are going to use another function.

 sscanf(StringReceived, "%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&SeensorData3,&SensorData4);

This function is the opposite of sprinft, and let´s you to storage the information on differents variables. Compile the  receiver code and add some cool things that use this data.

At the end of the code you should erase all the previous data from the string where the new string is storage, so you must call this function

 memset( StringReceived, 0, sizeof( StringReceived));// This line is for reset the StringReceived

Receiver Example

/*.............................................................
Sending Multiple Variables Using VirtualWire. Receiver
Author: Rodrigo Mompo Redoli
For controlrobotics.rodrigomompo.com
..............................................................*/
#include <VirtualWire.h>
 
// Sensors 
int Sensor1Data;
int Sensor2Data;
int SensorData3;
int SensorData4;
 
char StringReceived[22]; 
 
 
 
void setup() {
    // VirtualWire 
    // Initialise the IO and ISR
    // Required for DR3100
 
    // Bits per sec
    vw_setup(2000);
    vw_set_rx_pin(11);  
     
    // Start the receiver PLL running
    vw_rx_start();       
 
} // END void setup
 
void loop(){
    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;
     
//Taking the data from the control base
    if (vw_get_message(buf, &buflen)) 
    {
 int i;
        // Message with a good checksum received, dump it. 
        for (i = 0; i < buflen; i++)
 {            
          // Fill Sensor1CharMsg Char array with corresponding 
          // chars from buffer.   
          StringReceived[i] = char(buf[i]);
 }
 
      sscanf(StringReceived, "%d,%d,%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&SensorData3,&SensorData4); // Converts a string to an array
         
        // Turn off light to and await next message 
        
    }
 
 memset( StringReceived, 0, sizeof( StringReceived));// This line is for reset the StringReceived
}
Please leave some comments, and send your ideas for new posts in this blog. Also, If you have a project you can publish it on my blog. Subscribe and add me to your Google plus circles.