Getting weather data from openweathermap

Hi,

I'm trying to grab the temperature from the OpenWeatherMap API. I went through the Weather API tutorial and checked out the webhook documentation, but I'm struggling to get the temperature value.

My plan is to show this temperature on an OLED display.

Here's the code I'm using.

#include <Adafruit_SSD1306_RK.h>

//OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void myHandler(const char *event, const char *data);


float weatherVal = 0;
const char *EVENT_NAME = "hook-response/GetWeatherData/0";

void openWeatherDisplay()
{
  Particle.publish("GetWeatherData");
  
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,5);  
  display.print("open: ");
  display.print(weatherVal);
  display.print("c");
  display.display();
  
  delay(2500);
}

void setup() {
    
  String subscriptionName = String::format("%s/%s/", System.deviceID().c_str(), EVENT_NAME);
  Particle.subscribe(subscriptionName, myHandler, MY_DEVICES);


}

void loop() {

    openWeatherDisplay();
    
}

void myHandler(const char *event, const char *data) {
      
    JSONValue outerObj = JSONValue::parseCopy(data);
    JSONObjectIterator iter(outerObj);
    while(iter.next()) {
        if (iter.name() == "temp") {
             weatherVal = iter.value().toDouble();

        }
    }

}

Here is what I see in the Events

Here is the custom template created from the Weather API tutorial

It looks like the webhook is correct. Is anything at all displaying on the SSD1306? It looks like you're misising a call to

display.begin(SSD1306_SWITCHCAPVCC, 0x3D); 

This is typically done from setup().

Beyond that you should use USB Serial Debugging to log when you publish an event and receive a subscribed event.

1 Like

My bad, I forgot to add the display.begin().

Here's the updated code.

#include <Adafruit_SSD1306_RK.h>

//OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void myHandler(const char *event, const char *data);


float weatherVal = 0;
const char *EVENT_NAME = "hook-response/GetWeatherData/0";

void openWeatherDisplay()
{
  Particle.publish("GetWeatherData");
  
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,5);  
  display.print("open: ");
  display.print(weatherVal);
  display.print("c");
  display.display();
  
  delay(2500);
}

void setup() {
    
  String subscriptionName = String::format("%s/%s/", System.deviceID().c_str(), EVENT_NAME);
  Particle.subscribe(subscriptionName, myHandler, MY_DEVICES);
  
   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
  }
  display.clearDisplay();


}

void loop() {
    display.clearDisplay();
    display.display();

    openWeatherDisplay();
    
}

void myHandler(const char *event, const char *data) {
      
    JSONValue outerObj = JSONValue::parseCopy(data);
    JSONObjectIterator iter(outerObj);
    while(iter.next()) {
        if (iter.name() == "temp") {
             weatherVal = iter.value().toDouble();

        }
    }

}

The OLED display currently shows a static value of 0.00 and doesn't update with data from the webhook. I'll attempt to diagnose the issue by using USB Serial Debugging.

Hi @Instance

Welcome to the Particle Community!!

Just a question from my side - It seems the data is publishing to Particle Cloud correctly right? (temp: 10.37)

From your code it seems GetWeatherData is the value you are posting to Particle Cloud successfully. I am by no means an expert in Displays, but it stands to reason then that when you 'print' to the screen, you should print this same data.

Or you can do something like below:

float weatherVal = GetWeatherData;

You should then be able to print weatherVal to your screen:

 display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,5);  
  display.print("open: ");
  display.print(weatherVal);
  display.print("c");
  display.display();

At the moment you are setting float weatherVal = 0; but I am missing whether you are actually storing the value obtained by GetWeatherData in this variable you created.

Sorry if I missed the obvious :smile:

Regards, Friedl.