Hello Particle Community!
I've picked up my Photon about a day ago and have been writing an application to monitor the weather with an RGB LED by receiving sky conditions across the internet to a remote server, however I seem to be stuck on a socket specific issue here that has been keeping me up for the past few hours.
The Issue:
When the Photon attempts to connect through a void function, the server does get the response:
[98.77.7*.] has connected
[98.77.7*.] server is gathering JSON data
[98.77.7*.] assuming ZIP Code is 349xx
[98.77.7*.] using location code: 151xx_PC
[98.77.7*.] has been sent sky condition: Mostly clear
[98.77.7*.] has been disconnected.
However, using "Particle.publish", in the else conditional on being connected returns to the dashboard that the particle has not connected to the server. (Screenshot of logs: https://i.pitter.us/UCosNc0TZMGPX.png)
Code:
bool silenceLogger = false;
//TCPClient Variables
TCPClient client;
String response;
//Main Code
String getWeatherData() {
if (!silenceLogger){
Particle.publish("Task", "Gathering Weather Data");
}
//clear the response
response = "";
client.connect("5.135.194.50", 8744);
if (client.connected()) {
client.println("condition!");
while (client.available()){
response += String(client.read());
}
client.stop();
//Check the response
if (response == "" || response == "error"){
silenceLogger = true;
return "error";
} else {
Particle.publish("Weather Data", response);
return response;
}
} else {
Particle.publish("Error", "Failed to connect to socket server.");
return "error";
}
}
Can someone please offer advice on how to solve this issue or suggest code?
Thanks!
EDIT:
Thought I would add this: if you would like to attempt to test the issue with my code, you can use my socket server which will respond with the weather data of your location using geolocation after sending the string "condition!", at your own consent.
(5.135.194.50, 8744)