Open Weather Maps using Multiple ID's

Hey all-
I’m still super new here, so apologies if this question is dumb…

Im trying to use the Open Weather Maps API to Grab temperature data from multiple cities that i will eventually use with an Arduino

As per the instructions, ive grouped together the id’s in the code.
(Example link: https://openweathermap.org/current#severalid

If i just request the data, (temp, city name, max, min, etc.), from one city using the id, no problem.

json = loadJSONObject("http://api.openweathermap.org/data/2.5/weather?id=2972049&units=imperial&APPID="+APIKEY);

But when i start grouping the city id’s together, i get a null pointer exception when i try to println in the console (ex .“println(temp);”

  json = loadJSONObject("http://api.openweathermap.org/data/2.5/group?id=2972049,659180&units=imperial&APPID="+APIKEY);

Im not entirely sure why it works for just one city to grab the data, but i cant seem to with multiple cities.

Any advice/guidance is greatly appreciated!

JSONObject json, main;

String APIKEY = "XXXXXXXXXXXXX";

String city, country;
int temp, tempMin, tempMax;
int humidity, pressure;

String unit = "f";

void setup() {
  background(0);
  size(500, 500);
}

int time_to_fetch;
int time_between_fetches = 10000; // Five seconds.


void draw() {
  if ( millis() > time_to_fetch ) {
    loadData();
  }
}



void loadData() {
  time_to_fetch = millis() + time_between_fetches;
  json = loadJSONObject("http://api.openweathermap.org/data/2.5/group?id=2972049,659180&units=imperial&APPID="+APIKEY);
  city = json.getString("name");
  main = json.getJSONObject("main");
  //country = json.getJSONObject("sys").getString("country");
  temp = main.getInt("temp");
  //tempMin = json.getJSONObject("main").getFloat("temp_min");
  //tempMax = json.getJSONObject("main").getFloat("temp_max");
  //pressure = json.getJSONObject("main").getInt("pressure");
  //humidity = json.getJSONObject("main").getInt("humidity");
  println(temp);
}

I hope you found a solution…

I am looking at your code and maybe you could help me. In the api response the first part related to weather is in an array. Do you know how to get that data?

Hi mute8!

So… I’m not really sure if i can help all that much, but I will post my sketch here.I am not very good at coding, but I eventually figured it out if i recall. The thing of it is,I havent touched this sketch, nor Processing in a long time so i have forgotten the why/how of much of the sketch. If you have any specific questions about the code, I will try to answer best i can. Best of luck!!!

import processing.serial.*;
Serial myPort;  // Create object from Serial class
JSONObject json, main1, main2, main3, main4, main5, main6, main7, main8, wind1, wind2, wind3, wind4, wind5, wind6, wind7, wind8 ;

String APIKEY = "XXXXXXXXXXXX";

int temp1, temp2, temp3, temp4, temp5, temp6, temp7, temp8, humidity1, humidity2, humidity3, humidity4, humidity5, humidity6, humidity7, humidity8, speed1, speed2, speed3, speed4, speed5, speed6, speed7, speed8;


int increment1 = 0;
int time_to_fetch;
int time_between_fetches = 6000; // Ten seconds.

void setup() {
  //String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  //myPort = new Serial(this, portName, 500000);
  delay(5000);
  size(1200, 1200);
  background(102);
  noStroke();
  fill(0, 102);
}

void draw() {
  if ( millis() > time_to_fetch ) {
    loadData();
  }

  //myPort.write(temp1 + ";" + humidity1 + ";" + speed1 +  ";" + temp2 + ";" + humidity2 + ";" + speed2 + ";" + temp3 + ";" + humidity3 + ";" + speed3 +  ";" + temp4 + ";" + humidity4 + ";" + speed4 +  ";" + temp5 + ";" + humidity5 + ";" + speed5 +  ";" + temp6 + ";" + humidity6 + ";" + speed6 +  ";" + temp7 + ";" + humidity7 + ";" + speed7 +  ";" + temp8 + ";" + humidity8 + ";" + speed8 +  ";" + increment1 + ";" + 'e' + ";");
  //myPort.write(temp2 + ";" + humidity2 + ";" + speed2 + ";" + 'e' + ";");
  //myPort.write(increment1 + ";");
  //myPort.write(humidity1 + ":");
  //myPort.write(speed1 + ":");
  //myPort.write('\r'); //send an ASCII character 10 (value)
  //myPort.write(Integer.toString(humidity1));
  //myPort.write(Integer.toString(speed1));
  //myPort.write('e');

  //myPort.clear();
}


void loadData() {

  increment1++;
  
  time_to_fetch = millis() + time_between_fetches;
  json = loadJSONObject("http://api.openweathermap.org/data/2.5/group?id=4957956,4497666,4938157,5088438,5805687,4969398,4487042,4951257&units=imperial&APPID="+APIKEY);

  JSONArray weatherArr = json.getJSONArray("list");
  //println(weatherArr);

  ///////////////////////////////////////////////////////////////////////

  JSONObject weatherObj1 = weatherArr.getJSONObject(0);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main1 = weatherObj1.getJSONObject("main");
  wind1 = weatherObj1.getJSONObject("wind");
  speed1 = wind1.getInt("speed");
  humidity1 = main1.getInt("humidity");
  temp1 = main1.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
  JSONObject weatherObj2 = weatherArr.getJSONObject(1);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main2 = weatherObj2.getJSONObject("main");
  wind2 = weatherObj2.getJSONObject("wind");
  speed2 = wind2.getInt("speed");
  humidity2 = main2.getInt("humidity");
  temp2 = main2.getInt("temp");

  ///////////////////////////////////////////////////////////////////////

  JSONObject weatherObj3 = weatherArr.getJSONObject(2);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main3 = weatherObj3.getJSONObject("main");
  wind3 = weatherObj3.getJSONObject("wind");
  speed3 = wind3.getInt("speed");
  humidity3 = main3.getInt("humidity");
  temp3 = main3.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
    JSONObject weatherObj4 = weatherArr.getJSONObject(3);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main4 = weatherObj4.getJSONObject("main");
  wind4 = weatherObj4.getJSONObject("wind");
  speed4 = wind4.getInt("speed");
  humidity4 = main4.getInt("humidity");
  temp4 = main4.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
    JSONObject weatherObj5 = weatherArr.getJSONObject(4);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main5 = weatherObj5.getJSONObject("main");
  wind5 = weatherObj5.getJSONObject("wind");
  speed5 = wind5.getInt("speed");
  humidity5 = main5.getInt("humidity");
  temp5 = main5.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
    JSONObject weatherObj6 = weatherArr.getJSONObject(5);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main6 = weatherObj6.getJSONObject("main");
  wind6 = weatherObj6.getJSONObject("wind");
  speed6 = wind6.getInt("speed");
  humidity6 = main6.getInt("humidity");
  temp6 = main6.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
    JSONObject weatherObj7 = weatherArr.getJSONObject(6);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main7 = weatherObj7.getJSONObject("main");
  wind7 = weatherObj7.getJSONObject("wind");
  speed7 = wind7.getInt("speed");
  humidity7 = main7.getInt("humidity");
  temp7 = main7.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
  JSONObject weatherObj8 = weatherArr.getJSONObject(7);
  //println("Weather Object:\n" + weatherObj, ENTER);

  main8 = weatherObj8.getJSONObject("main");
  wind8 = weatherObj8.getJSONObject("wind");
  speed8 = wind8.getInt("speed");
  humidity8 = main8.getInt("humidity");
  temp8 = main8.getInt("temp");

  ///////////////////////////////////////////////////////////////////////
  //println(temp1);
  //println(humidity1);
  //println(speed1);
  //println(temp2);
  //println(humidity2);
  //println(speed2);
  //println(temp3);
  //println(humidity3);
  //println(speed3);
  //println(increment1);
  drawData();

  //exit();
}

void drawData() {
  background(102);
  textSize(18);
  text("Temp1: " + nfc(temp1) + 
    "\nHumidity1: " + nfc(humidity1) +
    "\nSpeed1: " + speed1 +
    "\n "  +
    "\nTemp2: " + temp2 +
    "\nHumidity2: " + humidity2 +
    "\nSpeed2: " + speed2 +
    "\n "  +
    "\nTemp3: " + temp3 +
    "\nHumidity3: " + humidity3 +
    "\nSpeed3: " + speed3 + 
    "\n "  +
    
        "\nTemp4: " + temp4 +
    "\nHumidity4: " + humidity4 +
    "\nSpeed4: " + speed4 + 
    "\n "  +
    
        "\nTemp5: " + temp5 +
    "\nHumidity5: " + humidity5 +
    "\nSpeed5: " + speed5 + 
    "\n "  +
    
        "\nTemp6: " + temp6 +
    "\nHumidity6: " + humidity6 +
    "\nSpeed6: " + speed6 + 
    "\n "  +
    
        "\nTemp7: " + temp7 +
    "\nHumidity7: " + humidity7 +
    "\nSpeed7: " + speed7 + 
    "\n "  +
    
        "\nTemp8: " + temp8 +
    "\nHumidity8: " + humidity8 +
    "\nSpeed8: " + speed8 + 
    "\n "  +
    
    "\nIncrement: " + increment1, 10, 50);
}

void serialEvent(Serial p) {
  try {
    // get message till line break (ASCII > 13)
    String message = p.readStringUntil(13);
    // just if there is data
    if (message != null) {
      println("message received: "+trim(message));
    }
  }
  catch (Exception e) {
  }
}