Saturday, August 24, 2013

Tutorial: Arduino + Kit rf 433Mhz(Part3: Software)

 If you are reading this post, you have a Rf link kit working with the example code of Virtualwire. If not you should read part 2  of this tutorial. Today we are going to go through Virtualwire understanding all the functions of this library.

Virtualwire Library

Virtualwire is a library made by Mike McCauley. The library is distributed under an Open Source License. You can use it even if you are making money but you have to distribute your code with Open Source License. However, it's time to view all the functions of the library.

Transmitter 

For the transmitter we have the following functions:
  • vw_set_tx_pin(pin); This function just sets the transmitter pin. The default pin is 12.
  • vw_setup(uint16_t speed); is used to set the speed of the communication.
  • vw_wait_tx(); is very usefull, you call it after sending the message and the arduino will wait until all the message has gone. Then the program would continue.
  • vw_send((uint8_t*) message, message len);  This function is used to send the message. To understand how it works we will decompose it. The function is vw_send(); inside it you have to put the message.So, you have to put (message, message len). It's very simple. Also, you have this part  (uint8_t*), you have to write it before the message, it's like the function (byte*). 
Note: The function strlen(), is used to obtain the size of the string, the easiest way of doing that.

// transmitter.pde
//
// Simple example of how to use VirtualWire to transmit messages
// Implements a simplex (one-way) transmitter with an TX-C1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// $Id: transmitter.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <VirtualWire.h>

void setup()
{
    Serial.begin(9600);   // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);  // Bits per sec
}

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

    digitalWrite(13, true); // Flash a light to show transmitting
    vw_send((uint8_t *)msg, strlen(msg));
    vw_wait_tx(); // Wait until the whole message is gone
    digitalWrite(13, false);
    delay(200);
}
Receiver 

The receiver have the following functions:
  • vw_set_tx_pin(pin); This function just set the receiver pin. The default pin it's 11.
  • vw_setup(uint16_t speed); is used to set the speed of the communication. It must be the transmitter speed.
  • vw_rx_start(); You must call it before receive any data.
  • vw_have_message(); Returns true if message has been received. This is similar to the "available" function of most other libraries.
  • vw_wait_rx();Wait for a message to be received. This will only return when a message has been received, otherwise it will wait forever, and the  program won`t continue. 
  • vw_wait_rx(timeout_ms); In this case the arduino will wait until the time waiting is more than the max time that you set.
  • vw_get_message(buf, &buflen) This function is used to store the message in a string. Buf is the name of the string and buflen is name of the variable that store the maximum size of the string.Then when you need data you take from buf.
  • vw_rx_stop() Disable the receiver process. 
// receiver.pde
//
// Simple example of how to use VirtualWire to receive messages
// Implements a simplex (one-way) receiver with an Rx-B1 module
//
// See VirtualWire.h for detailed API docs
// Author: Mike McCauley (mikem@open.com.au)
// Copyright (C) 2008 Mike McCauley
// $Id: receiver.pde,v 1.3 2009/03/30 00:07:24 mikem Exp $

#include <virtualwire.h>


void setup()
{
    Serial.begin(9600); // Debugging only
    Serial.println("setup");

    // Initialise the IO and ISR
    vw_set_ptt_inverted(true); // Required for DR3100
    vw_setup(2000);  // Bits per sec

    vw_rx_start();       // Start the receiver PLL running
}

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

    if (vw_get_message(buf, &amp;buflen)) // Non-blocking
    {
 int i;

        digitalWrite(13, true); // Flash a light to show received good message
 // Message with a good checksum received, dump it.
 Serial.print("Got: ");
 
 for (i = 0; i &lt; buflen; i++)
 {
     Serial.print(buf[i], HEX);
     Serial.print(" ");
 }
 Serial.println("");
        digitalWrite(13, false);
    }
}
Now what?
With this code you will be able to send string of data, but if you try to send a variable you will see that the Arduino IDE print some errors. In the next tutorial we will learn how to send multiple variables using Virtualwire.


1 comment: