Skip to content

Tcpclient break when sending the json #83

Description

@JacksonLv

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);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions