Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tcpclient break when sending the json #83

Open
JacksonLv opened this issue Mar 31, 2016 · 0 comments
Open

Tcpclient break when sending the json #83

JacksonLv opened this issue Mar 31, 2016 · 0 comments

Comments

@JacksonLv
Copy link

I use the library to generate a Json and use a Spark Photon to send the Json to a client as a TCP Server.As first ,it runs great.But after a few time,the client could not get the json any more.
Here is my code:

#define MAX_CLIENT_NUM   3
// Server Port
TCPServer server = TCPServer(8888);
TCPClient client[MAX_CLIENT_NUM];
aJsonStream serial_stream(&Serial);

void setup()
{
   char addr[16];

    Serial.begin(115200);
    delay(5000);

    WiFi.on();
    WiFi.setCredentials(AP, PIN, WPA2);
    WiFi.connect();

    IPAddress localIP = WiFi.localIP();

    while (localIP[0] == 0)
    {
        localIP = WiFi.localIP();
        Serial.println("waiting for an IP address");
        delay(1000);
    }

    sprintf(addr, "%u.%u.%u.%u", localIP[0], localIP[1], localIP[2], localIP[3]);

    Serial.println(addr);

    server.begin();
}
void loop()
{

    aJsonObject *root = aJson.createObject();
    aJson.addItemToObject(root, "cmd", aJson.createItem("G"));
    aJson.addItemToObject(root, "pin", aJson.createItem(1));
    aJson.addItemToObject(root, "pinmode", aJson.createItem(1));
    aJson.addItemToObject(root, "data", aJson.createItem(999));
    Serial.print("Json_tx_string:  ");
    aJson.print(root, &serial_stream);
    Serial.println(); 
    for(uint8_t i = 0;i<MAX_CLIENT_NUM;i++)
    {
        if (client[i].connected())
        {
            Serial.println("Connected by TCP client.");
            client[i].println(aJson.print(root));                      
        }
        else
        {
            client[i] = server.available();
        }
    }
    aJson.deleteItem(root);
    delay(200);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant