miércoles, 15 de abril de 2015

How to use 315Mhz RF transmitter and receiver modules with arduino

http://www.buildcircuit.com/how-to-use-rf-module-with-arduino/


Hay que descargar la librería que utilizan en el ejemplo: http://www.pjrc.com/teensy/td_libs_VirtualWire.html

Pongo los códigos fuente:

emisorhttps://gist.githubusercontent.com/buildcircuit/5907275/raw/gistfile1.ino
/*
SimpleSend
This sketch transmits a short text message using the VirtualWire library
connect the Transmitter data pin to Arduino pin 12
*/
#include <VirtualWire.h>
void setup()
{
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
}
void loop()
{
send("Hello there");
delay(1000);
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}

receptor: https://gist.githubusercontent.com/buildcircuit/5907280/raw/gistfile1.ino
/*
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
void setup()
{
Serial.begin(9600);
Serial.println("Device is ready");
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
}
void loop()
{
if (vw_get_message(message, &messageLength)) // Non-blocking
{
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++)
{
Serial.write(message[i]);
}
Serial.println();
}
}

No hay comentarios:

Publicar un comentario