Help needed with TCPClient

I am currently trying to get my photon to connect and send a message to a device on my network. The message I am trying to send is : “http://192.168.1.6/BAS.cgi?rc=1&sound=1&plmod$=ONE” I am using a modified version of the example code but the target device isn’t reacting to the message. If I type the message into my browser I am seeing correct responses. I am going to assume it has something to do with the host section. I actually do not need to get a response from the device as it really doesn’t respond it just triggers a sound when the message is sent. Any help would be greatly appreciated. Thank you in advance.

TCPClient client;

// EXAMPLE USAGE

TCPClient client;
byte server[] = {192, 168, 1, 6}; 
void setup()
{
  // Make sure your Serial Terminal app is closed before powering your device
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) Particle.process();

  Serial.println("connecting...");

  if (client.connect(server, 80))
  {
    Serial.println("connected");
    client.println("GET /BAS.cgi?rc=1&sound=1&plmod$=ONE HTTP/1.0");
    client.print("Host: ");
    client.print(server[0]);
    client.print(".");
    client.print(server[1]);
    client.print(".");
    client.print(server[2]);
    client.print(".");
    client.println(server[3]);

    client.println("Content-Length: 0");
    client.println();
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;);
  }
}

You might be running into this issue

Try this instead

IPAddress server(192, 168, 1, 6);
1 Like

Thank you for the suggestion, just tried it but no dice. It says it is able to connect to the device, but the device isn’t reacting to the message, I am concerned that the host might be incorrect but I am just grabbing at straws on this.

Just for the test, you could try HttpClient to make the request.
This ensures the request to be well formed.

SYSTEM_MODE(MANUAL)
SYSTEM_THREAD(ENABLED)

#include <HttpClient.h>


HttpClient http;

http_header_t headers[] = {
    //  { "Content-Type", "application/json" },
    //  { "Accept" , "application/json" },
    { "Accept" , "*/*"},
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};

http_request_t request;
http_response_t response;

void setup() {
    Particle.connect();
}

void loop() {
    static uint32_t ms = millis();
    
    if (millis() - ms < 10000) return;
    ms = millis();

    Serial.println();
    Serial.println("Application>\tStart of Loop.");
    // Request path and body can be set at runtime or at setup.
    request.ip = IPAddress(192, 168, 1, 30);
    request.port = 80;
    request.path = "/index.html";

    // Get request
    http.get(request, response, headers);
    Serial.print("Application>\tResponse status: ");
    Serial.println(response.status);

    Serial.print("Application>\tHTTP Response Body: ");
    Serial.println(response.body);
}
3 Likes

As always @ScruffR advice is good–you should try it!

It could be that your browser is doing lots of things to that request before it really sends it out to the device, so I would try the command-line HTTP tool curl which is available for Windows/Mac/Linux. Use the verbose option -v to see more of what is really happening to your request.

2 Likes

I’ve not tested this yet, but this would be an alternative implementation of what you did

TCPClient client;
IPAddress server( 192, 168, 1, 6 ); 

void setup()
{
}
 

void loop()
{
  static uint32_t ms = millis();
  
  if (millis() - ms < 10000) return;
  ms = millis();
  
  if (client.connect(server, 80))
  {
    char request[256];
    Serial.println("connected");
    
    snprintf(request, sizeof(request), 
            "GET /BAS.cgi?rc=1&sound=1&plmod$=ONE HTTP/1.0\r\n"
            "Host: %s\r\n"
            "Content-Length: 0\r\n\r\n"
            , (const char*)server.toString()
    );
    
    Serial.println(request);
    client.print(request);
    
    delay(500); 

    while(client.available())
    {
      char c = client.read();
      Serial.print(c);
    }

    client.stop();
  }
  else
    Serial.println("connection failed");
}
2 Likes

Thank you for all the suggestions. I just tried out the HttpClient Library and it is working, is there any reason against using that over the TCPClient?

You can use TCPClient as shown with my adapted code, but HttpClient seems more convenient.

1 Like

Ok, I just tested with your code and it works flawlessly, I think I will go with it because the library has more features I don’t need. Thank you so much for your help!

2 Likes