Open Kontrol Gateway with RN-XV stops posting after period of time

Hi, I've managed to get the RN-XV setup working ( I've read a lot of posts here and elsewhere on the challenges with the rn-xv). I’m using the http://openmicros.org/index.php/articles/254-openkontrol-gateway-document-index with the RN-XV and after about a few days, I lose the update to the website. I reconfigured the tx/rx lines to use the LH xbee socket having seen the code for the http://openmicros.org/index.php/articles/92-ciseco-product-documentation/openkontrol-gateway/166-openkontrol-gateway-example-code-llap-to-pachube-via-xv example. I also read that a lot of sites saying you needed hardware UART for it to work reliably. I take the tx/rx lines from the ftdi connector by bending the pins up from the PCB so that I can use them to program the OKG with jump wires, or monitor serial comms using the software serial port, or access the RN-XV comms to enter command mode on the rn-xv. My sketch is at https://github.com/PowerMeter/OKG/blob/master/OKG_RN-XV I tested the watchdog and it worked, so I’m using that to reset the atmega without understanding what the root cause is( never a good way to solve a problem ? ) RN-XV: I’ve updated firmware to 4.01 and set up SSID, and password and WLAN AUTH to be WAP2. I then save the config and reboot. The wlan join parameter is ‘1’ so it should rejoin the network automatically. Arduino IDE is V1.0 Any input on coding or points of failure you think I should be looking at would be appreciated. I’ve been working at this for 6 weeks. Thanks, Eamonn

I’ve just looked at both base units and found that the RN-XV was associated with the wifi channel. But this would point to an issue communicating with the site. I telnet’d to the rn-xv using windows shell, and all I could see was an attempt to open to the website(open powermeter.ie 80). Good news, but not a lot of debugging information for me - It’s an hours drive from my desk, so if anyone has any comments on my code which is based on the WiFlyHQ library (WiFlyHQ/httpclient_progmem.ino at master · harlequin-tech/WiFlyHQ · GitHub I’d really appreciate it.

/*  Credits:
  Based on the work of the openenergymonitor.org project
  Licence: GNU GPL V3  Authors: Trystan Lea and Glyn Hudson 
  https://github.com/openenergymonitor/Open-Kontrol-Gateway/blob/master/OKG_RN_XV_Wifi_RFM12B_emoncms/OKG_RN_XV_Wifi_RFM12B_emoncms.ino
  Based on Arduino DNS and DHCP-based Web client by David A. Mellis
  modified 12 April 2011by Tom Igoe, based on work by Adrian McEwen
  Uses Jeelabs.org JeeLib library for RFM12B 
*/  
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#undef PROGMEM 
#define PROGMEM __attribute__(( section(".progmem.data") )) 
#undef PSTR 
#define PSTR(s) (__extension__({static prog_char __c[] PROGMEM = (s); &__c[0];})) 
#include <WiFlyHQ.h>
#include <JeeLib.h>
#include <SoftwareSerial.h>
SoftwareSerial ftdiSerial(5,6);
//#define SERIALPRINT								// include print statement for commissioning - comment this line to exclude

// RFM12B Wireless Config

#define MYNODE 17            // node ID of OKG (Change in print statement below also!)
#define freq RF12_433MHZ     // frequency - must match RFM12B module and emonTx
#define group 220            // network group - must match emonTx (Change in print statement below also!)

// RN-XV wifi Config & MyPowerBase

WiFly wifly;

/* Change these to match your WiFi network */
const char mySSID[] = "SSID";
const char myPassword[] = "PASSWD";
char apiurl[] = "http://powermeter.ie/mypowerbase/api/post.json?apikey&json=";
const char site[] = "powermeter.ie";

void terminal();
void print_P(const prog_char *str);
void println_P(const prog_char *str);

// Open Kontrol Gateway Config

const int LEDpin=17;         //front status LED on OKG

// The PacketBuffer class is used to generate the json string that is send via ethernet - JeeLabs

class PacketBuffer : public Print {
public:
    PacketBuffer () : fill (0) {}
    const char* buffer() { return buf; }
    byte length() { return fill; }
    void reset()
    { 
      memset(buf,NULL,sizeof(buf));
      fill = 0; 
    }
    virtual size_t write (uint8_t ch)
        { if (fill < sizeof buf) buf[fill++] = ch; }
    byte fill;
    char buf[150];
    private:
};
PacketBuffer str;

int data_ready, rf_error;
unsigned long last_rf, reboot_timer;
int resetCounter, reboot_count = 0;

// SETUP

void setup() {
    char buf[32];
    ftdiSerial.begin(9600);
    #ifdef SERIALPRINT
      println_P(PSTR("Starting-ID 17:Net Grp 210"));
      print_P(PSTR("Free memory: "));
    ftdiSerial.println(wifly.getFreeMemory(),DEC);
    #endif
    pinMode(LEDpin, OUTPUT);
    digitalWrite(LEDpin,HIGH);
  
    rf12_set_cs(9);                                                 //RFM12B CS connnected to Dig 9 on OKG - change to 10 for JeeNode/emonTx ...
    rf12_initialize(MYNODE, freq,group);
    last_rf = millis()-40000;                                       // setting lastRF back 40s is useful as it forces the ethernet code to run straight away
 
    Serial.begin(9600);
    if (!wifly.begin(&Serial, &ftdiSerial)) {
            #ifdef SERIALPRINT
              println_P(PSTR("Failed to start wifly"));
            #endif
	wifly.reboot();
        terminal();
    }

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
	/* Setup the WiFly to connect to a wifi network */
	    #ifdef SERIALPRINT
               println_P(PSTR("Joining network"));
            #endif
	wifly.setSSID(mySSID);
	wifly.setPassphrase(myPassword);
	wifly.enableDHCP();

	if (wifly.join()) {
	        #ifdef SERIALPRINT
                   println_P(PSTR("Joined wifi network"));
                #endif
	} else {
	        #ifdef SERIALPRINT
                   println_P(PSTR("Failed to join wifi network"));
                #endif
	    terminal();
	}
    } else {
            #ifdef SERIALPRINT
               println_P(PSTR("Already joined network"));
            #endif
    }
    //terminal();

    #ifdef SERIALPRINT
      print_P(PSTR("MAC: "));
      ftdiSerial.println(wifly.getMAC(buf, sizeof(buf)));
      print_P(PSTR("IP: "));
      ftdiSerial.println(wifly.getIP(buf, sizeof(buf)));
      print_P(PSTR("Netmask: "));
      ftdiSerial.println(wifly.getNetmask(buf, sizeof(buf)));
      print_P(PSTR("Gateway: "));
      ftdiSerial.println(wifly.getGateway(buf, sizeof(buf)));
      print_P(PSTR("SSID: "));
      ftdiSerial.println(wifly.getSSID(buf, sizeof(buf)));
    #endif
      wifly.setDeviceID("Wifly-WebClient");
    #ifdef SERIALPRINT   
      print_P(PSTR("DeviceID: "));
      ftdiSerial.println(wifly.getDeviceID(buf, sizeof(buf)));
    #endif
    if (wifly.isConnected()) {
    #ifdef SERIALPRINT
        println_P(PSTR("Old connection active. Closing"));
      #endif
	wifly.close();
    }

    if (wifly.open(site, 80)) {
      #ifdef SERIALPRINT
        print_P(PSTR("Connected to "));
	ftdiSerial.println(site);
      #endif
    }
	/* Send the request */
	//wifly.println("GET / HTTP/1.0");
	//wifly.println();
    /*} else {
      #ifdef SERIALPRINT
        println_P(PSTR("Failed to connect"));
      #endif
    }*/
      wdt_enable(WDTO_8S);                //enable an 8's reset watchdog
}

void loop() {

  // 1) Receive date from emonTx via RFM12B
    wdt_reset();
   reboot_count = millis(); 
  if (rf12_recvDone()){      
      if (rf12_crc == 0 && (rf12_hdr & RF12_HDR_CTL) == 0)
      {
        digitalWrite(LEDpin,HIGH);
        int node_id = (rf12_hdr & 0x1F);
        byte n = rf12_len;
         
        str.reset();
        str.print("{&node=");  str.print(node_id);
        str.print("&csv=");
        for (byte i=0; i<n; i+=2)
        {
          int num = ((unsigned char)rf12_data[i+1] << 8 | (unsigned char)rf12_data[i]);
          if (i) str.print(",");
          str.print(num);
        }

        data_ready = 1; 
        last_rf = millis(); 
        //ftdiSerial.println("rf12_recvDone");
        rf_error=0;
      }
  }
  
  // 2) If no data is recieved from rf12 emoncms is updated every 30s with RFfail = 1 indicator for debugging

  if ((millis()-reboot_timer)>600000)
  {
    reboot_timer = millis();                                             // reset reboot timer
    str.reset();                                                         // reset json string
    str.print("{reboot:");      
    str.println(reboot_count);
    reboot_count++;                  
    data_ready = 1;                                                     // Ok, data is ready    
    delay(10000);
  }
  
  // 3) Post Data

  if (data_ready)
      
  {
     if (wifly.isConnected()) {
           #ifdef SERIALPRINT
             ftdiSerial.println("Old connection active. Closing....");
           #endif
	wifly.close();
     }

     if (wifly.open(site, 80)) {
        /* Send the request */
        str.print("}\0");
        #ifdef SERIALPRINT
          ftdiSerial.print("Connected to ");
	  ftdiSerial.println(site);
          ftdiSerial.print("Free memory: ");
          ftdiSerial.println(wifly.getFreeMemory(),DEC);
          ftdiSerial.print("Sent: "); ftdiSerial.println(str.buf);
	#endif
        resetCounter ++;
	wifly.print("GET "); wifly.print(apiurl); wifly.print(str.buf);
	wifly.println();
      }
      } else { ; }
        data_ready=0;
        digitalWrite(LEDpin,LOW);		  // turn off status LED to indicate succesful data receive and online posting
        delay (300);
        if (resetCounter > 10) {delay(10000);}

 // 4) Read Reply 

    if (wifly.available() > 0) {
	char ch = wifly.read();
	ftdiSerial.write(ch);
	if (ch == '\n') {
	    /* add a carriage return */ 
	    ftdiSerial.write('\r');
	   }
     }

    if (ftdiSerial.available() > 0) {
	wifly.write(ftdiSerial.read());
     } 

}//end loop


/* Connect the WiFly serial to the serial monitor. */
void terminal()
{
    while (1) {
	if (wifly.available() > 0) {
	    ftdiSerial.write(wifly.read());
	}


	if (ftdiSerial.available() > 0) {
	    wifly.write(ftdiSerial.read());
	}
    }
}

/* Print a string from program memory */
void print_P(const prog_char *str)
{
    char ch;
    while ((ch=pgm_read_byte(str++)) != 0) {
	ftdiSerial.write(ch);
    }
}

void println_P(const prog_char *str)
{
    print_P(str);
    ftdiSerial.println();
}