rfx_echo_server_v1-ino

/*--------------------------------------------------------------
  Program:      echo_server_v1

  Description:  Simple RFX demo server for Arduino or similar accepts 
                incoming TCP/IP port connections from a telnet/
                terminal program such as PuTTY, and will echo back
                each line of text input it receives after receiving a 
                newline char.

                This version is easiest to understand for those new to RFX, 
                as it sticks pretty much to standard Arduino library function 
                syntax, but it has room for some improvements, which will be 
                shown next in echo_server_v2
 
  Hardware:     Uno-class Arduino/X:duino (or better) with nRF24L01+ 
                module, and RFX gateway (Raspberry Pi with RFX 
                daughterboard and SD Card).
                  
  Software:     Developed using RFX r2 libs and Arduino 1.0.5 software
                (Should be compatible with Arduino 1.0 +)
  
  Date:         July 2013
 
  Author:       Mark Pendrith, demo sketch for Arduino using 
                RFX. http://embeddedcoolness.com (July, 2013)

--------------------------------------------------------------*/

#include <Arduino.h>

#define BOARD_ID 1 // choose a value between 1-255
#define ID_STRING "echo_server_v1" // choose a string to help identify
#define BOARD_TYPE_UNO // select board type for RFX
#define SPI_DEMI_SHIELD // select a SPI pin configuration fro RFX
#define HW_SPI 1 // select hardware SPI or software SPI (bit banging)

#include <Rfx_r2.h>
#include <SPI.h>

Rfx rfx; // create instance of Rfx object for wireless IO

String echo_str; // stores the echo string request
  
void setup()
{
  Serial.begin(115200); // for diagnostics to uart serial if connected

  rfx.begin_id(BOARD_ID,ID_STRING); // set up wireless link to RFX gateway 
}

void loop()
{
  if (rfx.port_connected()) { // check for new incoming port connection
    Serial.println("new connection!"); // diagnostic message to uart serial
    rfx.println("Hello world! I'm an RFX echo server. Type something...\n");
    while (rfx.port_connected()) { // serve this client while port connected
      if (rfx.available()) { // client data available to read
        char c = rfx.read(); // read 1 byte (character) from client
        echo_str += c;  // save the echo string 1 char at a time
        if (c == '\n') { // echo string to client only after newline received
          rfx.print("echo: ");
          rfx.println(echo_str); // print echo string to TCP/IP port connection
          echo_str = ""; // reset string
        }
      }
    }
    Serial.println("port connection closed"); // diagnostic message to uart serial
  }
}