Looping Issue with Thingspeak

Basically this is what is happening

I can either send data to thingspeak but the program will not loop.
Or I can get the program to loop but not send the data to Thingspeak.

In order to have the program send data I have to remove these lines of code

 client.print("Connection: close\n");
    Serial.println("Connection Close");
if (client.connect(thingSpeakAddress, 80))

However when these lines are removed the program will not loop and will hang up when stating at the top of void loop.

I have a feeling that I am just missing something super simple here but have been unable to find it thus far. Any help with this would be awesome

Also sorry for the disastrous state of the code at the moment. First time coding in over 4 years so still fairly rusty.

#include <OneWire.h>   //one wire DO sesnor
#include <DallasTemperature.h>
#include "RTClib.h"
#include <Wire.h> // Helps with i2c
#include <ccspi.h>
#include <string.h>
#include "utility/debug.h"
#include <Adafruit_CC3000.h>
#include <ccspi.h>

#define DS18B20 2 //the pin you connect the ds18b20 to
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
                                       
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);


#define WLAN_SSID       "Dreddit"        // cannot be longer than 32 characters!
#define WLAN_PASS       "loxg1493"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

const int chipSelect = 10;


char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "2EBZ76PBJWYEWN91";              //enter your channel's Write API Key
const int updateThingSpeakInterval = 20 * 1000;  // 20 second interval at which to update ThingSpeak

long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

uint32_t ip;  

void setup() {
  
Serial.begin(115200);
sensors.begin();
Wire.begin();
pinMode(10, OUTPUT); //OUTPUT FOR DATA LOGGER
cc3000.begin();

  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
  
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
Serial.println(sensors.getTempCByIndex(0));

Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

}
void loop() {

sensors.requestTemperatures(); // Send the command to get temperatures
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);
String temp = String(sensors.getTempCByIndex(0), DEC);

if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  else {
    Serial.println("Client not available");
  }
  
if (!client.connected() && lastConnected)
  {
    Serial.println("...disconnected");
    Serial.println();
    client.stop();
  }
  else
  {
    Serial.println("client not disconnected");
  }

Serial.println(millis() - lastConnectionTime > updateThingSpeakInterval);

 if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
  {
    Serial.println("Updating Thingspeak");
    updateThingSpeak("field1="+temp);
  }
  
else {
  Serial.println("Thingspeak not updated");
}
  

delay(60000 * 5); // 5 min delay between checks


}
void updateThingSpeak(String tsData) {
  

Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  //if (client.connect(thingSpeakAddress, 80)) {   
     Serial.println("connected to TCP");
    client.print("POST /update HTTP/1.1\n");
     Serial.println("POST Update");
    client.print("Host: api.thingspeak.com\n");
     Serial.println("host");
      client.print("Connection: close\n");
    Serial.println("Connection Close");
     client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
     Serial.println("APIkey");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
     Serial.println("Content Type");
    client.print("Content-Length: ");
     Serial.println("content Lentgth");
    client.print(tsData.length());
     Serial.println("ts.data length");
    client.print("\n\n");
     Serial.println("\n\n");
    client.print(tsData);
     Serial.println("connected to TCP"); 
   

    
    lastConnectionTime = millis();
    
    if (client.connected())
    {
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
      
      failedCounter = 0;
    }
    else
    {
      failedCounter++;
  
      Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");   
      Serial.println();
    }
  //}
  //  else
 // {
   // failedCounter++;
    
   // Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");   
   // Serial.println();
    
    lastConnectionTime = millis(); 
 // }
    
  
}

Using IDE v1.6.8

Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

On every pass through loop()? Why?

So a quick update with this. Removed some of the clutter as a lot of the code in the above piece was not really doing anything and was mainly there for testing / pulled from examples I found online.

This code will export data to thingspeak but does not loop properly

#include <OneWire.h>   //one wire DO sesnor
#include <DallasTemperature.h>
#include "RTClib.h"
#include <Wire.h> // Helps with i2c
#include <ccspi.h>
#include <string.h>
#include "utility/debug.h"
#include <Adafruit_CC3000.h>
#include <ccspi.h>

#define DS18B20 2 //the pin you connect the ds18b20 to
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
                                       
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);


#define WLAN_SSID       "Dreddit"        // cannot be longer than 32 characters!
#define WLAN_PASS       "loxg1493"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

const int chipSelect = 10;


char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "2EBZ76PBJWYEWN91";              //enter your channel's Write API Key
//const int updateThingSpeakInterval = 20 * 1000;  // 20 second interval at which to update ThingSpeak

long lastConnectionTime = 0;
boolean lastConnected = false;

uint32_t ip;  



void setup() {
  
Serial.begin(115200);
sensors.begin();
Wire.begin();
pinMode(10, OUTPUT); //OUTPUT FOR DATA LOGGER
cc3000.begin();

  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
  
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
Serial.println(sensors.getTempCByIndex(0));

Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  /* Display the IP address DNS, Gateway, etc. */  
 
}
void loop() {


sensors.requestTemperatures(); // Send the command to get temperatures

String temp = String(sensors.getTempCByIndex(0), DEC);
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  Serial.println(temp);
   //client.connect(thingSpeakAddress, 80); 
   Serial.println("Client Connect");
  updateThingSpeak("field1=" + temp); 
  Serial.println(sensors.getTempCByIndex(0));

delay(2000);

 
}
void updateThingSpeak(String tsData) {
  
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  Serial.println("connected to TCP");

  
    client.connect(thingSpeakAddress, 80); 
     Serial.println("thingspeakaddress");
    client.print("POST /update HTTP/1.1\n");
     Serial.println("POST Update");
    client.print("Host: api.thingspeak.com\n");
     Serial.println("host");
    //client.print("Connection: close\n");
    // Serial.println("Connection Close");
    client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
     Serial.println("APIkey");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
     Serial.println("Content Type");
    client.print("Content-Length: ");
     Serial.println("content Lentgth");
    client.print(tsData.length());
     Serial.println("ts.data length");
    client.print("\n\n");
     Serial.println("\n\n");
    client.print(tsData);
     Serial.println("connected to TCP");
    client.connected();
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
    
  
}
Attempting to connect to Dreddit
Connected!
22.44
Request DHCP
22.4375000000
Client Connect
connected to TCP
thingspeakaddress
POST Update
host
APIkey
Content Type
content Lentgth
ts.data length



connected to TCP
Connecting to ThingSpeak...

22.44

If i do this to the code it will not send data to thingspeak but will loop. The only difference being this line of code " client.connect(thingSpeakAddress, 80); " is removed.

#include <OneWire.h>   //one wire DO sesnor
#include <DallasTemperature.h>
#include "RTClib.h"
#include <Wire.h> // Helps with i2c
#include <ccspi.h>
#include <string.h>
#include "utility/debug.h"
#include <Adafruit_CC3000.h>
#include <ccspi.h>

#define DS18B20 2 //the pin you connect the ds18b20 to
#define ADAFRUIT_CC3000_IRQ   3  // MUST be an interrupt pin!
// These can be any two pins
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10
// Use hardware SPI for the remaining pins
// On an UNO, SCK = 13, MISO = 12, and MOSI = 11
Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,
                                         SPI_CLOCK_DIVIDER); // you can change this clock speed but DI
                                       
OneWire ourWire(DS18B20);
DallasTemperature sensors(&ourWire);


#define WLAN_SSID       "Dreddit"        // cannot be longer than 32 characters!
#define WLAN_PASS       "loxg1493"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY   WLAN_SEC_WPA2

const int chipSelect = 10;


char thingSpeakAddress[] = "api.thingspeak.com";
String APIKey = "2EBZ76PBJWYEWN91";              //enter your channel's Write API Key
//const int updateThingSpeakInterval = 20 * 1000;  // 20 second interval at which to update ThingSpeak

long lastConnectionTime = 0;
boolean lastConnected = false;

uint32_t ip;  



void setup() {
  
Serial.begin(115200);
sensors.begin();
Wire.begin();
pinMode(10, OUTPUT); //OUTPUT FOR DATA LOGGER
cc3000.begin();

  char *ssid = WLAN_SSID;             /* Max 32 chars */
  Serial.print(F("\nAttempting to connect to ")); Serial.println(ssid);
  
if (!cc3000.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
    Serial.println(F("Failed!"));
    while(1);
  }
   
  Serial.println(F("Connected!"));
Serial.println(sensors.getTempCByIndex(0));

Serial.println(F("Request DHCP"));
  while (!cc3000.checkDHCP())
  {
    delay(100); // ToDo: Insert a DHCP timeout!
  }  

  /* Display the IP address DNS, Gateway, etc. */  
 
}
void loop() {


sensors.requestTemperatures(); // Send the command to get temperatures

String temp = String(sensors.getTempCByIndex(0), DEC);
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  Serial.println(temp);
   //client.connect(thingSpeakAddress, 80); 
   Serial.println("Client Connect");
  updateThingSpeak("field1=" + temp); 
  Serial.println(sensors.getTempCByIndex(0));

delay(2000);

 
}
void updateThingSpeak(String tsData) {
  
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80);

  Serial.println("connected to TCP");

  
  //  client.connect(thingSpeakAddress, 80); 
     Serial.println("thingspeakaddress");
    client.print("POST /update HTTP/1.1\n");
     Serial.println("POST Update");
    client.print("Host: api.thingspeak.com\n");
     Serial.println("host");
    client.print("Connection: close\n");
     Serial.println("Connection Close");
    client.print("X-THINGSPEAKAPIKEY: " + APIKey + "\n");
     Serial.println("APIkey");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
     Serial.println("Content Type");
    client.print("Content-Length: ");
     Serial.println("content Lentgth");
    client.print(tsData.length());
     Serial.println("ts.data length");
    client.print("\n\n");
     Serial.println("\n\n");
    client.print(tsData);
     Serial.println("connected to TCP");
    client.connected();
      Serial.println("Connecting to ThingSpeak...");
      Serial.println();
    
  
}
Attempting to connect to Dreddit
Connected!
22.62
Request DHCP
22.7500000000
Client Connect
connected to TCP
thingspeakaddress
POST Update
host
Connection Close
APIkey
Content Type
content Lentgth
ts.data length



connected to TCP
Connecting to ThingSpeak...

22.75
22.7500000000
Client Connect
connected to TCP
thingspeakaddress
POST Update
host
Connection Close
APIkey
Content Type
content Lentgth
ts.data length



connected to TCP
Connecting to ThingSpeak...

22.75
22.8125000000
Client Connect
connected to TCP
thingspeakaddress
POST Update
host
Connection Close
APIkey
Content Type
content Lentgth
ts.data length



connected to TCP
Connecting to ThingSpeak...

Also as for the Adafruit_CC3000_Client client = cc3000.connectTCP(ip, 80); line of code. For some reason if I put this in with the other declarations before VOID Setup the program will not even start.

For some reason if I put this in with the other declarations before VOID Setup the program will not even start.

You need to separate the declaration of client from the valuation of client, I would think. Post a link to the library.

This is the webpage that I pulled the libraries from