Awesome, I am grateful for your feedback. I do not have the technical knowledge currently to understand this nearly as well as you do, so your help is greatly appreciated.
I'm willing to test this code out and post the results good or bad. I'll try to figure out if the main loop is stalling when the CC3000 freezes up or not.
I just need to know how I should integrate your code above with my current sketch so it should work correctly.
I just had to rest my Spark Core 2 times in the last 3 hours due to it freezing up from the Cyan Flash the first time, and then the green flash the second time.
I've been sending data to Xivley for about 2 days now just testing how everything is working now with my Cell phone being my WiFi Hotspot.
Here is my current code:
define FEED_ID "1234" //note: fake id here..
define XIVELY_API_KEY "1234" //note: fake key here
TCPClient client;
int reading = 0;
int ledD = D7;
int count = 0;
int total_temp = 0;
int temp_calc = 0;
unsigned long LastUpTime = 0;
unsigned long LastCloudCheck = 0;
char whichApp[64] = "READ TEMPERATURE with XIVELY";
// This routine runs only once upon reset
void setup()
{
//Register our Spark function here
Spark.variable("whichapp", &whichApp, STRING);
Spark.variable("reading", &reading, INT);
Spark.function("degres", tempCalculation);
Spark.function("volt", analogReading);
pinMode(A7, INPUT);
pinMode(ledD, OUTPUT);
ledStatus(5, 100); //Blink
}
void loop()
{
reading = analogRead(A7);
temp_calc = (reading*3.3/4095)*100 - 50;
if (millis()-LastUpTime>1000)
{
if (count <= 5) {
total_temp += temp_calc;
count++;
}
else {
xivelyTemp(total_temp/count); //Send the average of the last 5 readings
count = 0;
total_temp = 0;
}
LastUpTime = millis();
}
if (millis()-LastCloudCheck > 1000*60*5) { //check every 5 min to see if the connection still exists
if(!Spark.connected()) Spark.connect();
LastCloudCheck = millis();
}
}
void xivelyTemp(int temperature) {
ledStatus(5, 100);
//Serial.println("Connecting to server...");
if (client.connect("api.xively.com", 8081))
{
// Connection succesful, update datastreams
client.print("{");
client.print(" "method" : "put",");
client.print(" "resource" : "/feeds/");
client.print(FEED_ID);
client.print("",");
client.print(" "params" : {},");
client.print(" "headers" : {"X-ApiKey":"");
client.print(XIVELY_API_KEY);
client.print(""},");
client.print(" "body" :");
client.print(" {");
client.print(" "version" : "1.0.0",");
client.print(" "datastreams" : [");
client.print(" {");
client.print(" "id" : "bedroom_temp",");
client.print(" "current_value" : "");
client.print(temperature-8); //adjustment for some weird reason..
client.print(""");
client.print(" }");
client.print(" ]");
client.print(" },");
client.print(" "token" : "0x123abc"");
client.print("}");
client.println();
ledStatus(3, 1000);
}
else
{
// Connection failed
//Serial.println("connection failed");
ledStatus(3, 2000);
}
if (client.available())
{
// Read response
//char c = client.read();
//Serial.print(c);
}
if (!client.connected())
{
//Serial.println();
//Serial.println("disconnecting.");
client.stop();
}
client.flush();
client.stop();
}
void ledStatus(int x, int t)
{
for (int j = 0; j <= x-1; j++)
{
digitalWrite(ledD, HIGH);
delay(t);
digitalWrite(ledD, LOW);
delay(t);
}
}
int tempCalculation(String command) {
int tempCalc = (reading*3.3/4095)*100 - 50;
return tempCalc;
}
int analogReading(String command) {
return reading;
}