I've got two arduino nano devices connected via RF 433 MHz. The modules are MX-05V and MX-FS-03V. I'm using VirtualWire for sending and receiving. The speed is set to 1000 bps.
One has got a pir sensor and sends "1:ON" when it detects movement and "1:OFF" when movement stops.
The other is just receiving these messages, but after a while (20 messages) the text is chopped off. I only receive "1". If I restart everything the same thing happens.
They are currently on my table (cca 20cm distance) and powered by my PC via USB.
What can I do? Is it because the modules are from ebay and probably not the best quality?
Receive part:
// recieve
int ledPin = 13;
int rcvPin = 13;
#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_set_rx_pin(rcvPin); // set pin for rx
vw_setup(1000); // Bits per sec
vw_rx_start(); // Start the receiver
digitalWrite(ledPin,HIGH);
}
void loop() {
// non-blocking receive
if (vw_get_message(message, &messageLength)) {
Serial.print("Received: ");
for (int i = 0; i < messageLength; i++) {
Serial.write(message[i]);
}
Serial.println();
digitalWrite(ledPin, LOW);
delay(500);
digitalWrite(ledPin,HIGH);
}
}
Transmit part:
/*
SimpleSend
This sketch transmits a short text message using the VirtualWire library
connect the Transmitter data pin to Arduino pin 12
pin 11 to high for power
gnd connected to gnd
*/
// RF
int ledPin = 13;
int trxPin = 12;
int pwrPin = 11;
// PIR
int inpPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
#include <VirtualWire.h>
void setup()
{
Serial.begin(9600);
Serial.println("Starting...");
// PIR
pinMode(inpPin, INPUT); // declare sensor as input
// RF
pinMode(pwrPin, OUTPUT); // Set digital pin 11 as OUTPUT pins
digitalWrite(pwrPin,HIGH);
// Initialize the IO and ISR
vw_set_tx_pin(trxPin);
vw_setup(1000); // Bits per sec
Serial.println("Device is ready to send.");
}
void loop() {
val = digitalRead(inpPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion on seonsor 1!");
send("1:ON");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended on seonsor 1");
send("1:OFF");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
void send (char *message)
{
vw_send((uint8_t *)message, strlen(message));
vw_wait_tx(); // Wait until the whole message is gone
}