Thanks for your advice @Dave
Edit: Actually I did what you told me to start with a simple code, and I do see @digixx code in this thread:
and it worked like a charm for me, so I could see the "Hello" output on the client's serial output. Then I modified my server code to be like this:
// Thanks for @Scruff and @BDub
#include "application.h"
#include "Serial2/Serial2.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
TCPServer server = TCPServer(9999);
TCPClient client;
const int wakePin = D0; // use Serial2 RX pin as wake pin
char szReceive[64] = { '\0' };
int idx = 0;
uint32_t ms;
uint32_t msPublish;
uint32_t msLastSerial;
bool frameStart = false;
void setup()
{
server.begin();
Serial.begin(19200);
Serial2.begin(19200);
while(!Serial.available()) SPARK_WLAN_Loop();
msLastSerial = millis();
}
void loop()
{
int d = Serial.read();
switch (d) {
case 'W':
{
WiFi.connect();
delay(500);
break;
}
case 'C':
{
Spark.connect();
break;
}
} // end of switch
ms = millis();
Serial2.flush();
idx = 0;
while (idx < 38 && millis() - ms < 1000 )
{
if(Serial2.available())
{
msLastSerial = millis();
char c = Serial2.read();
Serial.write(c);
if (c == 'S')
{
frameStart = TRUE;
idx = 0;
} // end of if (c=='S')
if (frameStart)
{
szReceive[idx++] = c;
szReceive[idx] = '\0';
} // end of if (frameStart)
} // end of if(Serial2.available())
} // end of while
if (idx >= 38 && millis() - msPublish > 1000)
{
Serial.println(szReceive);
if (client.connected())
{
server.write(szReceive);
while (client.available())
{
Serial.print("a"); // ooh, the client has send some data
server.write(client.read()); // just send back whats received
}
} //end of if (client.connected())
else
{
// if no client is yet connected, check for a new connection
client = server.available();
} //end of else
if (Spark.connected())
{
Spark.publish("Carpet1",szReceive,PRIVATE);
} // end of if (Spark)
msPublish = millis();
idx = 0;
frameStart = FALSE;
} // end of if (idx)
if (millis() - msLastSerial > 5000) // stay awake 5 sec after last serial byte
Spark.sleep(wakePin, RISING);
} // end of void
And the client code to be like this:
// Thanks for @BDub
#include "application.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
void tcp_connect();
byte server[] = { 192, 168, 1, 104 }; // Server
TCPClient client;
void setup()
{
Serial.begin(19200);
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println("Please Enter Your Character Input:");
}
void loop()
{
if (client.connected())
{
char x = client.read();
Serial.print(x);
}
if (Serial.available()) {
int c = Serial.read();
switch (c) {
case 'W':
{
WiFi.connect();
delay(500);
Serial.println("The client is connected to the router");
break;
}
case 'T': tcp_connect(); break;
case 'C':
{
Spark.connect();
delay(500);
Serial.println("The client is connected to the Spark Cloud");
break;
}
} //end of switch
} //end of serial check
}
void tcp_connect() {
Serial.println("connecting...");
if (client.connect(server, 9999))
{
Serial.println("connected to Server 192.168.1.104");
Serial.println("The below data is sent from the client to the server");
}
else
{
Serial.println("connection failed");
}
}
For now, I can see the output on the Server serial side as show below:
but on the Client serial side it looked like this:
Please @Dave, @ScruffR, @BDub do you know why I'm seeing these spaces at the client serial side? Also, if I want to capture the output at the client serial, I'm seeing these weird characters with the actual data:
Does this weird character (or spaces) is because I'm doing Serial2.flush();? Or it is error in somewhere else in the firmware?
Thanks in advance.