I read that lot of people are having problem with TCP Client lib in Spark Core and with this same problem related too but I can’t find a clear solution for this.
I used Thinker Android App to communicate with my Spark Core and when it receives a command by cloud my Spark Core should send a message via TCP to a remote machine and after it receives the data, this machine send back the same data. I developed a TCP Server in C# that receives a message and returns the same data received to the source.
Spark Core is able to send the correct data to the remote machine but when it will get the data, it doesn’t receive connections anymore (from Thinker App), begins blinks green and it reconnect to the cloud. In the middle of this process, I’m not able to send commands to the board and after that the board become unstable and I don’t receive the data.
Here is my code:
int tinkerDigitalWrite(String command);
TCPClient client;
IPAddress ip = { 192, 168, 25, 13};
volatile bool isToWrite = false;
void setup()
{
Spark.function("digitalwrite", tinkerDigitalWrite);
// Make sure your Serial Terminal app is closed before powering your Core
Serial.begin(9600);
//Now open your Serial Terminal, and hit any key to continue!
while(!Serial.available()) SPARK_WLAN_Loop();
client.connect(ip, 3001);
}
void loop()
{
if (isToWrite)
{
byte message[3];
message[0] = 11;
message[1] = 12;
message[2] = 13;
client.write(message, 3);
Serial.print("Message received: ");
while (client.available()) {
char c = client.read();
Serial.print(c);
Serial.print(" ");
}
Serial.println();
isToWrite = false;
}
}
int tinkerDigitalWrite(String command){
bool value = 0;
int pinNumber = command.charAt(1) - '0';
if (pinNumber< 0 || pinNumber >7) return -1;
if(command.substring(3,7) == "HIGH") value = 1;
else if(command.substring(3,6) == "LOW") value = 0;
else return -2;
if(command.startsWith("D")){
isToWrite = true;
return 1;}
else if(command.startsWith("A")){
pinMode(pinNumber+10, OUTPUT);
digitalWrite(pinNumber+10, value);
return 1;}
else return -3;}
I wrote a TCP Client in C# and it works fine with my server.
Someone can help me with this two-way connection ?
Thanks