Go Down

Topic: Weather data (Read 303 times) previous topic - next topic

Blerkk

Apr 19, 2019, 09:06 pm Last Edit: Apr 19, 2019, 09:08 pm by Blerkk
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.
Code: [Select]

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);
  }
}
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

vaj4088

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?

Blerkk

#2
Apr 20, 2019, 11:21 am Last Edit: Apr 20, 2019, 11:23 am by Blerkk
Here is my whole code but the rest of the code is just my clock.
https://pastebin.com/vfP7X8fR

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.
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

PaulRB

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.

Blerkk

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.
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

lesept

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.
A force d'essayer on finit par réussir... Donc, plus ça rate, plus on a de chances que ça marche (proverbe Sharduinok).

PaulRB

Try moving this line
Code: [Select]
StaticJsonBuffer<5000> jsonBuffer;
outside of loop() so it's a global.

Blerkk

I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

wildbill

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

Quote
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.

Blerkk

I am not using any delay since it would cause problems with my clock.
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

wildbill

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.

Blerkk

Now I put a 10 minute wait until the next request, I hope it's gonna work.
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

Blerkk

Well, I am still unable to connect even with a 10 minute waiting time beetween 2 requests.
I am not a programming expert and I do everything as a school project so don't be mean please. It ruins my morale.

PaulRB

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?

PaulRB

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

Go Up