Weather data

Hi,
I am struggling with my weather data for a while now and I don’t know what am I doing wrong.
So basically I just want to get one temperature data but I am always losing connection to the server. My code was working for a while when I had my json buffer set to 10k from 5k but now it is not working either way. I’ve tried 5k, 10k, 15k, 20k, ect... but all of them are just seem not to work.

void idojarasDolgai()
{
  Serial.println("\nStarting connection to server...");
  if (client.connect(server, 80))
  {
    Serial.println("connected to server");
    // Make a HTTP request:
    client.print("GET /data/2.5/forecast?");
    client.print("q=" + location);
    client.print("&appid=" + apiKey);
    client.print("&cnt=3");
    client.println("&units=metric");
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();
  }
  else
  {
    Serial.println("unable to connect");
  }
 
  //json adatbazis
  //http://api.openweathermap.org/data/2.5/forecast?q=budapest,HU&cnt=3&appid=*my very secret api key*
 
  //delay(1000);
  String line = "";
 
  while (client.connected())
  {
    line = client.readStringUntil('\n');
 
    //create a json buffer where to store the json data
    StaticJsonBuffer<5000> jsonBuffer;
    JsonObject& root = jsonBuffer.parseObject(line);
    if (!root.success())
    {
      Serial.println("parseObject() failed");
      return;
    }
 
    //get the data from the json tree
    String nextWeather0 = root["list"][0]["main"]["temp"];
 
    nextWeather[0] = nextWeather0;
 
    int idojaras = nextWeather0.toInt();
    Serial.println(idojaras);
  }
}

Where is the rest of your program? Snippets are highly suspect.

What did you change between working and non-working? What wires could have changed or been broken?

Here is my whole code but the rest of the code is just my clock.

I have changed nothing. (exept the json buffer from 5000 to 10000, on 10k it worked for once, didn't give any errors, but after a restart it's not working again)I get 3 answears from the server and the others are just lost connection errors.

ps.: I am not using any wires that could mess up with my weather data. I only use my 7 segment display to monitor the clock and later the weather.

Most Arduino don't have 5 or 10K of ram memory to use as a json buffer. As you did not tell us even the most basic fact of what type of Arduino you are using, I assume that is your problem.

Well, I am using an mkr1000.
But as I said, it was working with 10k json buffer for once. So what should I do to make it work? I am also not sure that I need a 10k or even a 5k buffer for 1 temperature data.

ArduinoJson has an assistant that gives you the code you need to parse your data.
First type your URL in a browser window to get the requested data.
Then copy the entire data into the assistant and it gives you the optimised code. You just have to get rid of the lines you don't need.

Try moving this line

StaticJsonBuffer<5000> jsonBuffer;

outside of loop() so it's a global.

Still the same.

How often are you calling the API? Note this API tip from the provider:

Please, mind that Free and Startup accounts have limited service availability. If you do not receive a response from the API, please, wait at least for 10 min and then repeat your request. We also recommend you to store your previous request.

I am not using any delay since it would cause problems with my clock.

Blerkk:
I am not using any delay since it would cause problems with my clock.

Then if you really are sending requests as fast as you can I would assume that the provider is rejecting them. You may even find, especially if you are using a free account, that they eventually disable it completely.

You could use millis to keep track of time and restrict the frequency of your API calls to comply with the restriction. It also seems that there is no point calling more frequently as the data won't have changed.

You could use Wireshark or the like to sniff the packet traffic and see what you're getting back from the API but I expect it'll be something like 403.

Now I put a 10 minute wait until the next request, I hope it's gonna work.

Well, I am still unable to connect even with a 10 minute waiting time beetween 2 requests.

What if you comment out all the code that extracts the data from the JSON message. Does the server connection then work reliably every time?

Also try putting client.stop() at the end of the function.

Nope, I've commented out the json stuff and still unable to connect.

Nope, I've commented out the json stuff and still unable to connect.

Show the serial data that you do see.

Your GET request is wrong. A valid GET request starts with "GET " and ends with " HTTP/1.n" where n is either 0 (no Host statement needed) or 1 (a Host statement is required).


I've literally changed NOTHING, just started at night and it goes since then.