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(;;);
}
}
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.
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.
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?
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!