Program:      rfx_websrv_AJAX_switch

  Description:  Arduino web server shows the state of a switch
				on a web page using AJAX via RFX. The state of the
				switch must be read by clicking a button on
				the web page - for demonstrating AJAX.
  Hardware:     Arduino Uno (or equivalent) and RFX. Should work 
				with other Arduinos/X:duinos, perhaps w/ minor 
  Software:     Developed using RFX and Arduino 1.0.5 software
				Should be compatible with Arduino 1.0 +
  References:   - WebServer example by David A. Mellis and 
				  modified by Tom Igoe
				- Ethernet library documentation:
				- Learning PHP, MySQL & JavaScript by
				  Robin Nixon, O'Reilly publishers

  Date:         15 January 2013
  Author:       W.A. Smith, http://startingelectronics.com

  Modified by:  Mark Pendrith, to demonstrate AJAX for Arduino via RFX.
				Original ethernet shield code commented out to show what
				needed to be changed to convert ethernet shield code to
				RFX. http://embeddedcoolness.com (July, 2013)


#ifdef ARDUINO
#if (ARDUINO >= 100)
#include <Arduino.h>
#include <WProgram.h>

#include "Rfx_config.h"
#include <Rfx_r1.h>
#include <SPI.h>
//#include <Ethernet.h>

// MAC address from Ethernet shield sticker under board
//byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress ip(10, 0, 0, 20); // IP address, may need to change depending on network
//EthernetServer server(80);  // create a server at port 80

Rfx rfx;

String HTTP_req;      // stores the HTTP request
#define LED 13        // LED for switching is attached to Arduino pin 13

static uint8_t  led_state = 0;

void setup()
  //Ethernet.begin(mac, ip);  // initialize Ethernet device
  //server.begin();           // start to listen for clients
  Serial.begin(115200);       // for diagnostics
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);


void loop()
  //EthernetClient client = server.available();  // try to get client

  //if (client) {  // got client?
  if (rfx.joined()) { // joined to RFX gateway?
    boolean currentLineIsBlank = true;
    //while (client.connected()) {
    while (rfx.port_connected()) {
      //if (client.available()) {   // client data available to read
      if (rfx.available()) {   // client data available to read
        //char c = client.read(); // read 1 byte (character) from client
        char c = rfx.read(); // read 1 byte (character) from client
        HTTP_req += c;  // save the HTTP request 1 char at a time
        // last line of client request is blank and ends with \n
        // respond to client only after last line received
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          rfx.fpln("HTTP/1.1 200 OK");
          rfx.fpln("Content-Type: text/html");
          rfx.fpln("Connection: keep-alive");
          rfx.out.prln(); // need to print blank line after header
          // AJAX request for switch state
          if (HTTP_req.indexOf("ajax_switch") > -1) {
            // read switch state and send appropriate paragraph text
          else {  // HTTP request for web page
            // send web page - contains JavaScript with AJAX calls
            rfx.fpln("<!DOCTYPE html>");
            rfx.fpln("<title>RFX AJAX Demo</title>");
            rfx.fpln("function GetSwitchState() {");
            rfx.fpln("nocache = \"&nocache=\" + Math.random() * 1000000;");
            rfx.fpln("var request = new XMLHttpRequest();");
            rfx.fpln("request.onreadystatechange = function() {");
            rfx.fpln("if (this.readyState == 4) {");
            rfx.fpln("if (this.status == 200) {");
            rfx.fpln("if (this.responseText != null) {");
            rfx.fpln("document.getElementById(\"switch_txt\").innerHTML = this.responseText;");
            rfx.fpln("request.open(\"GET\", \"ajax_switch\" + nocache, true);");
            //rfx.fpln("request.open(\"GET\", \"ajax_switch\", true);");
            rfx.fpln("<h1>RFX Switch Status via AJAX</h1>");
            rfx.fpln("<p id=\"switch_txt\">Switch state: Not requested...</p>");
            rfx.fpln("<button type=\"button\" onclick=\"GetSwitchState()\">Get Switch State</button>");
          delay(1); // give the web browser time to receive the data
          rfx.port_close();  // close the connection (better placed here rather than outside the loop)
          // display received HTTP request on serial port
          HTTP_req = "";            // finished with request, empty string
        // every line of text received from the client ends with \r\n
        if (c == '\n') {
          // last character on line of received text
          // starting new line with next character read
          currentLineIsBlank = true;
        else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
      } // end if (client.available())
    } // end while (client.connected())
    //delay(1);      // give the web browser time to receive the data
    //client.stop(); // close the connection
  } // end if (client)

// send the state of the switch to the web browser
void GetSwitchState(Rfx *r)
  static bool flipflop = false;
  //if (digitalRead(3)) {
  if ((flipflop = !flipflop)) {
    r->fpln("Switch state: ON");
    digitalWrite(LED,HIGH); // switch on the led
  else {
    r->fpln("Switch state: OFF");
    digitalWrite(LED,LOW); // switch off the led