Two way communications


Two way communications open up a whole range of possibilities:

Remote control : You can send commands to your RFu-328 for it to do something, like trigger a relay to switch something on/off (e.g. light switch, or a servo of a radio controlled car).

Request/Reply : Send a command to the RFu-328 to provide you with information, like a ping to see if the sensor is still alive, or a request for information, like the value of a sensor (e.g. temperature or battery level). 

Acknowledgement: You can receive an acknowledgement from the sensor to make sure it receives your command. This allows you to have more intelligent communications between your devices and do something if communications are not received. For example if you want to remote control a servo you can have the RFu-328 send an acknowledgement back to say it has received a command, and also send back a response one the servo has been activated successfully. 

You will need:
Construction

All the construction work is explained in the tutorials listed below. 

 - Build the circuit shown in the Blink tutorial
 - We have assumed you know how to connect your Arduino development environment and can download sketches (code) from the Arduino IDE to the RFu-328. If not then follow the getting started section and also read the Blink tutorial.

Software - Remote control an LED

Using the following code you can control an LED attached to the RFu-328 by typing 1 and 0 into a terminal emulator. Once the LED is either on or off the RFu-328 sends a response back to the Raspberry Pi indicating that the the LED is either on or off.

Open up your Arduino IDE environment and create a new sketch (click File, New). Copy and paste the code below and save. Then upload the sketch to the RFu-328.

//
// Turn on/off the LED on pin 13 by command received from the radio
//  0 turns the LED off
//  1 turns the LED on
//  any other character sent has no effect
//
byte msg;  // the command buffer
void setup()
{
  pinMode(13, OUTPUT);   // initialize pin 13 as digital output (LED)
  pinMode(8, OUTPUT);    // initialize pin 8 to control the radio
  digitalWrite(8, HIGH); // select the radio
  Serial.begin(115200);    // start the serial port at 115200 baud 
  Serial.print("STARTED");
}
void loop()
{
  if (Serial.available()>=1) // character received
  {
    msg = (char)Serial.read();
    if (msg == '0')  // turn LED off
    {
      digitalWrite(13, LOW);
      Serial.print("  LED OFF  ");
    }
    else if (msg == '1')  // turn LED on
    {
      digitalWrite(13, HIGH);
      Serial.print("  LED ON   ");
    }  
  }
}



































You will be using the terminal emulator on your Raspberry Pi to send messages to the RFu-328. Set up the terminal emulator on your Raspberry Pi by logging in to your Raspberry Pi using the root user and navigate to the /usr/bin directory:

cd /usr/bin

Now load a terminal emulator called Miniterm. This emulator will display anything that comes from the serial port and it will send anything that is typed in to the serial port. 

python miniterm.py /dev/ttyAMA0

You should see a screen like this:


IMPORTANT!
If you are using a FTDI USB cable to update your RFu-328 make sure it is completely unplugged from the RFu-328 so that the radio gets access to the serial port.

Type 1 and 0 into the terminal and the LED should go on when you press 1 and off when you press 0. You should also see the response back from the RFu-328 that is displayed in the terminal emulator as shown below.









Comments