How to implement wireless communication between two arduino via two RF4432
Release time:2016-07-03
How to implement wirelessly communicate between 2 arduinos using 2 RF4432´s, I can communicate fine from PC to arduino but arduino to arduino is no go so far,
None of them mention arduino to arduino communication, but it kinda has to be possible right? I´ve connected 1(GND) to ground, 2(VCC) to 5+, 4(rx) to 1(tx) and 5(tx) to 0(rx) and as said works fine between my computer and arduino, use the RX code on my arduino and connect a RF4432 using the USB adapter board and a new serial port shows up in the arduino IDE, use the serial monitor and send 1/2 turns the LED on/off
Since I need to unplug the RF4432 from the serial ports to re-program the arduino using the IDE I kept unplugging the tx/rx lines and at some point got them backwards on the TX arduino... *facepalm* anyway corrected that and it now works, both using "raw" serial and EasyTransfer library, code bellow.
Image for reference, I´m using the regular LED on #13 though and tactile push button switch on #10, I use the same button setup as the button tutorial/example except on pin 10 (push = high)
I´m running stock configuration on both modules, I´m considering upping the baudrate but for now I´ve left it at it´s default speed of 9600, no other settings modified.
Wiring:
Ground -> RF4432 1
+5 -> RF4432 2
Arduino 1 (UART RX) -> RF4432 4 (TX)
Arduino 0 (UART TX) -> RF4432 5 (RX)
Wiring is identical on both ends except the button is of course redundant on RX end, and LED on TX end.
Here is my code for "raw" serial:
// TX
// Alternate sending 1 and 2 to make RX arduino blink
const int buttonPin = 10; // the number of the pushbutton pin
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
Serial.println("1");
}
else {
// turn LED off:
Serial.println("2");
}
delay(100);
}
// RX
int led = 13;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available()){
char input = Serial.read();
switch (input){
case ´1´:
digitalWrite(led, HIGH);
delay(100);
break;
case ´2´:
digitalWrite(led, LOW);
delay(100);
break;
}
}
else // blinks if there is no serial connection available
{
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
}
Here is my code for EasyTransfer:
// TX ET
#include
EasyTransfer ET;
struct SEND_DATA_STRUCTURE{
int buttonState;
};
SEND_DATA_STRUCTURE mydata;
const int buttonPin = 10; // the number of the pushbutton pin
void setup() {
Serial.begin(9600);
ET.begin(details(mydata), &Serial);
}
void loop() {
mydata.buttonState = digitalRead(buttonPin);
ET.sendData();
}
// RX ET
#include
EasyTransfer ET;
struct RECEIVE_DATA_STRUCTURE{
int buttonState;
};
RECEIVE_DATA_STRUCTURE mydata;
int led = 13;
//-- TIMING
long previousMillis = 0;
long interval = 500;
unsigned long currentMillis;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
ET.begin(details(mydata), &Serial);
}
void loop()
{
currentMillis = millis();
if(ET.receiveData())
{
// we got a data packet, can use yet another LED to visualize the data rate, set to HIGH here, flashes like an ethernet port does (yellow in my case)
previousMillis = currentMillis; // reset deadman timer
digitalWrite(led, mydata.buttonState); // LED matches TX´s button state, push the button and the LED ligths up, let go and it darkens
}
else
{
// no data packet this loop, loop might be going faster than baud rate, not an issue, set your packet visualizer LED to LOW here (yellow in my case)
}
if(currentMillis - previousMillis > interval) {
//-- No packet for 500ms, DEADMANSWITCH! (required for remote control etc, don´t want your robot to keep executing it´s last command forever if it looses signal, lets say full speed ahead)
// Flashing to indicate no signal, I use a red LED
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
else
{
// we got signal, can use a secondary LED to show that (green)
}
}
I actually use a red yellow and green LED on my robot which I use for status, green lights up when there is a signal, yellow flashes in time with packets, red lights up if the deadmanswitch triggers due to no packets for 500ms
I´m fairly new to arduino, my code might not be very neat but it works, feel free to use, tweak and improve it, hope it´s of use to you.
My unmodified RX code with 3 LEDs: http://pastebin.com/3ppKB7uf (TX is identical) I´m using a mega hence the 40 range LEDs (end connector)
Here is a quick and rough tutorial showing that linked RX ET code and the TX ET code above running on an Uno and Mega (disregard the robot, mega is a bit stuck to it, not really part of the example, only the LEDs and RF4432.