I have two new cores. Both are connected to spark cloud and are breathing cyan. I have flashed both cores with the same code. When I flash the core I can see the flashing purple led. When the code is running I can see the Serial lines being printed to terminal window.
One of the cores runs all the code and will send updates to ThingSpeak. The other core does not want to play. If fails on the line: client.connect(“api.thingspeak.com”,80);
Complete code here (like I say one of the cores is processing this just fine, so I’m sure the code and my ThingSpeak creds are all OK)…
#include "math.h"
// Thinkspeak channel information
String writeAPIKey = "MYAPIKEYVALUES2O2BH9J"; // redacted
String channelID = "12345"; // redacted
// TCP socket initialize
TCPClient client;
float x;
float y;
/*--------------------------------------------------------
Setup
--------------------------------------------------------*/
void setup()
{
Serial.begin(9600);
delay(10000);
Serial.println("===Starting===");
}
/*--------------------------------------------------------
Main loop
--------------------------------------------------------*/
void loop()
{
if(Spark.connected())
{
for (float z = 4.712; z < 10.995; z = z + .15) // "z" sets the sin wave to the first zero crossing 4.712 and ends it on the next 10.995.
{
x = sin(z) * 127.5 + 127.5; // Making the sin wave all positive numbers and setting it to a scale of 0-255 (makes it easy to PWM an LED)
y = 255 - (sin(z) * 127.5 + 127.5); // This inverts the sin wave so we have two streams.
// Must convert data to Strings, make sure you use capital "S" in Strings
ThingSpeakUpdate("field1="+String(x)+"&field2="+String(y));
// I put this delay in place so we don't flood Thingspeak but you should really use a timer, delays screw with the sparkcloud connection some times.
delay(15000);
}
}
}
/*------------------------------------------------
Sends sensor data to Thingspeak
Inputs: String, data to be entered for each field
Returns:
------------------------------------------------*/
void ThingSpeakUpdate(String tsData)
{
Serial.println("Date string: " + tsData);
Serial.println("...Connecting to Thingspeak");
// Connecting and sending data to Thingspeak
if(client.connect("api.thingspeak.com", 80))
{
Serial.println("...Connection succesful, updating datastreams");
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.println(tsData); //the ""ln" is important here.
// This delay is pivitol without it the TCP client will often close before the data is fully sent
delay(200);
Serial.println("Thingspeak update sent.");
}
else{
// Failed to connect to Thingspeak
Serial.println("Unable to connect to Thingspeak.");
}
if(!client.connected()){
client.stop();
}
client.flush();
client.stop();
}
Any ideas why this might happen?
Thanks,
M