Go Down

Topic: Connecting to other websites whilst connected to IOT cloud (Read 695 times) previous topic - next topic

homegeek

Hi,

I seem to have discovered a way to break the connection to the IOT cloud (reliably....) and would appreciate a pointer on the right way to do it please.

My sketch starts up normally but when I call my isRainForecast function it (reliably) breaks the link to the cloud

Code: [Select]
bool isRainForecast() {
  //check whether rain is forecast from Open Weather Map
 
 
  WiFiClient webclient;
  String webServerResponse = "";

  //Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (webclient.connect(weatherServer, 80)) {
    //Serial.println("connected to server");
   
    // construct a HTTP request:
    webclient.print("GET /data/2.5/forecast?");
    webclient.print("id=" + weatherLocation);
    webclient.print("&appid=" + apiKeyOWM);
    webclient.print("&cnt=3");
    webclient.println("&units=metric");
    webclient.println("Host: api.openweathermap.org");
    webclient.println("Connection: close");
    webclient.println();
   
  } else {
      Serial.println("unable to connect");
  }
 
  delay(1000);
 
 
  while (webclient.connected()) {
    // grab the data coming back from the server
    webServerResponse = webclient.readStringUntil('\n');
  }
 
  if (webServerResponse.indexOf("rain") > -1) {  //does the forecast data contain the word rain?
      return true;
  } else {
      Serial.println("Rain not forecast.");
  }
 
}


I'm guessing my error is to start up another client to connect to the web, although I do use the existing WiFI connection as instantiated by the IOT code.

Is there an existing WiFiClient created I can use to make the connection to the open weather map server please?  Apologies if this is documented elsewhere, I've not been able to track it down and my skills in working through the code for IOT isn't good enough to pin it down.


Thanks


Go Up