Will hang up after 2 hours WirelessShield+Wifly+WiflyHQ

Hi Folks,

I’m having a bit of trouble with a setup using Uno + Arduino Wireless Shield + Wifly RN-XV and a temperature sensor, and would be very grateful for some guidance on this.

The goal was to have the Uno read a DS18B20 One Wire Temperature sensor, and PUT the data to a COSM/Patchube datastream. My current setup does work, and will reliably send temperature readings accepted by COSM. But only for a couple of hours at a time, then after that, nothing happens.

Some info about my current setup:

  1. Pins RX0 and TX1 are bent back on the (official) Arduino wireless shield so as to not give contact with the Uno.

  2. Switch on wireless shield is always in “USB” mode, not “MICRO” mode.

  3. Pin 8 on the shield is jumped to pin RX0 on the shield, and pin 9 on the shield is jumped to pin TX1 on the shield for serial communication with the RN-XV.

  4. Center pin on the temp sensor is jumped to pin 2 on the shield. Sensor has 10k ohm pull-up resistor, is fed 5v from wireless shield.

  5. RN-XV firmware was upgraded about 24 hours ago to most recent version.

  6. Using WiflyHQ library.

  7. Wifly RN-XV is authenticated to the home Wifi router with no issues, did not need to designate a static IP for this application. Therefore the sketch does not call for signing into and out of the network for each PUT.

Symptoms:

After about two hours of PUTing data to COSM, Uno ceases to send data to COSM. The green LED on the RN-XV indicate that the module is still connected to the home network, and is awaiting instructions. The light is in a “slow blink” indicating it is not in CMD mode. Router shows Wifly is indeed connected to the network. But the Wifly no longer blinks its orange LED at intervals (no orange blinks at all), which leads me to believe that the Wifly is working perfectly, but the Uno is no longer sending the PUT request to the Wifly module. COSM debugger shows no API requests.

Here’s my code, which is the boilerplate COSM sketch, with added functionality from WiflyHQ library and Bildr’s one wire temp sensor library:

#include <WiFlyHQ.h>
#include <SoftwareSerial.h>
#include <OneWire.h> 

SoftwareSerial wifiSerial(9,8);

WiFly wifly;

#define APIKEY         "API KEY" // your cosm api key
#define FEEDID         92154 // your feed ID
#define USERAGENT      "DroneJournalismWX1" // user agent is the project name

const char mySSID[] = "SSID";
const char myPassword[] = "PASSPHRASE";
const char server[] = "api.cosm.com";
unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 10*1000; //delay between updates to Cosm.com
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2

void terminal();
OneWire ds(DS18S20_Pin);  // on digital pin 2

void setup() {

  Serial.begin(9600);
  Serial.println("Starting");
  Serial.print("Free memory: ");
  Serial.println(wifly.getFreeMemory(),DEC);
  
  if (!wifly.isAssociated()){
    Serial.println("Not associated");
  }
  else {
  Serial.println("Associated");
  }
  
  wifiSerial.begin(9600);
  if (!wifly.begin(&wifiSerial, &Serial)) {
       Serial.println("Failed to start wifly");
       terminal();
    }
}

void loop() {
  
  // gets temperature from sensor and converts to F
  float temperature = getTemp();
  temperature = ((temperature*9)/5) + 32;
  int sensorReading = temperature;
  delay(1000);

  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(millis() - lastConnectionTime > postingInterval) {
    Serial.println("attempting to send data...");
    sendData(sensorReading);
  }

  // store the state of the connection for next time through
  // the loop:
  lastConnected = wifly.isConnected();
}

// this method makes a HTTP connection to the server:
void sendData(int thisData) {
  // if there's a successful connection:
  if (wifly.open(server, 80)) {
    Serial.println("connecting...");
    // send the HTTP PUT request:
    wifly.print("PUT /v2/feeds/");
    wifly.print(FEEDID);
    wifly.println(".csv HTTP/1.1");
    wifly.println("Host: api.cosm.com");
    wifly.print("X-ApiKey: ");
    wifly.println(APIKEY);
    wifly.print("User-Agent: ");
    wifly.println(USERAGENT);
    wifly.print("Content-Length: ");
    Serial.println("Data posted.");

    // calculate the length of the sensor reading in bytes:
    // 8 bytes for "sensor1," + number of digits of the data:
    int thisLength = 12 + getLength(thisData);
    wifly.println(thisLength);

    // last pieces of the HTTP PUT request:
    wifly.println("Content-Type: text/csv");
    wifly.println("Connection: close");
    wifly.println();

    // here's the actual content of the PUT request:
    wifly.print("temperature,");
    wifly.println(thisData);
  
  } 
  else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    wifly.close();
  }
   // note the time that the connection was made or attempted:
  lastConnectionTime = millis();
}


// This method calculates the number of digits in the
// sensor reading.  Since each digit of the ASCII decimal
// representation is a byte, the number of digits equals
// the number of bytes:

int getLength(int someValue) {
  // there's at least one byte:
  int digits = 1;
  // continually divide the value by ten, 
  // adding one to the digit count for each
  // time you divide, until you're at 0:
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  // return the number of digits:
  return digits;
}

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


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

//returns the temperature from one DS18S20 in DEG Celsius
float getTemp(){
  byte data[12];
  byte addr[8];
  if ( !ds.search(addr)) {
      //no more sensors on chain, reset search
      ds.reset_search();
      return -1000;
  }
  if ( OneWire::crc8( addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return -1000;
  }
  if ( addr[0] != 0x10 && addr[0] != 0x28) {
      Serial.print("Device is not recognized");
      return -1000;
  }
  ds.reset();
  ds.select(addr);
  ds.write(0x44,1); // start conversion, with parasite power on at the end
  byte present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE); // Read Scratchpad
  for (int i = 0; i < 9; i++) { // we need 9 bytes
    data[i] = ds.read();
  }
  ds.reset_search();
  byte MSB = data[1];
  byte LSB = data[0];
  float tempRead = ((MSB << 8) | LSB); //using two's compliment
  float TemperatureSum = tempRead / 16;
  return TemperatureSum;
}

Thanks for your help, and happy new year!