Get data from OpenWeatherMap API without webpage to Photon

photon
Tags: #<Tag:0x00007f1ca648b0d8>

#1

Hi,

I’m building a IoT keyholder, the keyholder is supposed to get data from the OpenWeatherMap API, from the data it gets, the keyholder will shine different colors on the keys. For example when it is sunny the light will shine green on the bike key and red on the car key. Now I’m doing this with a webpage which gets the data from OpenWeatherMap and does a POST to my particle with a value.

But now I want to get rid of the webpage and get the values directly from the cloud. Is there anyway to do this?

Any help would be appreciated.

This is the code I’m using now

//HTML & JS webpage code
<!DOCTYPE HTML>
<html>
<script src="js/jquery-3.3.1.js"></script>

<body>

    <div id="weather">
        <p id="city-name">Genk</p>
        <ul>
            <li>
                <p id="temp"></p>
            </li>
            <li>°C</li>
        </ul>
        <p id="desc1"></p>
    </div>


    <script type="text/javascript">
        window.setInterval(function() {
            $(document).ready(function() {
                var cityName = $("#city-name").text();
                getWeather(cityName);
            });

            function getWeather(city) {
                $.get("https://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=metric&appid=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", function(data) {
                    console.log(data);

                    //Display weather data
                    $("#desc1").html(data.weather[0].description);
                    $("#temp").html(data.main.temp);

                    // POST VALUE TO PARTICLE
                    var weatherID = data.weather[0].id;
                    if (weatherID > 199 && weatherID < 299) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "storm"
                        });
                    }

                    if (weatherID > 299 && weatherID < 399) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "rain"
                        });
                    }

                    if (weatherID > 499 && weatherID < 599) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "rain"
                        });
                    }

                    if (weatherID > 599 && weatherID < 699) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "freezing"
                        });
                    }

                    if (weatherID > 699 && weatherID < 799) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "mist"
                        });
                    }

                    if (weatherID > 799) {
                        $.post("https://api.particle.io/v1/devices/xxxxxxxxxxxxxxxxxxxxxxxxx/Weather?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", {
                            value: "dry"
                        });
                    }
                });
            }
        }, 1000);

    </script>
</body>
</html>


This is my particle code


//#include "application.h"
#include "neopixel/neopixel.h"

SYSTEM_MODE(AUTOMATIC);

#define PIXEL_COUNT_A 4 //number of pixels in strip
#define PIXEL_PIN_A D0 // pin
#define PIXEL_TYPE_A WS2812B

Adafruit_NeoPixel stripA = Adafruit_NeoPixel(PIXEL_COUNT_A, PIXEL_PIN_A, PIXEL_TYPE_A);

void setup() {
    
  stripA.begin();
  stripA.show(); 
  
  Particle.function("Weather", colorOne); // Expose the color function
}

void loop() {
}

int colorOne(String command){
    // 0 & 3 car
    // 1 & 2 bike

    if(command == "dry"){
        stripA.setPixelColor(1, 0, 255, 0);
        stripA.setPixelColor(2, 0, 255, 0);
        
        stripA.setPixelColor(0, 255 ,0, 0);
        stripA.setPixelColor(3, 255, 0, 0);
        
        stripA.show();
        return 1;
    }else if(command == "Lrain"){
        stripA.setPixelColor(1, 227, 200, 0);
        stripA.setPixelColor(2, 227, 200, 0);
        
        stripA.setPixelColor(0, 255, 127, 0);
        stripA.setPixelColor(3, 255, 127, 0);
        
        stripA.show();
      
        return 1;
    }else if(command == "rain"){
        stripA.setPixelColor(1, 227, 250, 0);
        stripA.setPixelColor(2, 227, 250, 0);
        
        stripA.setPixelColor(0, 255, 127, 0);
        stripA.setPixelColor(3, 255, 127, 0);
        
        stripA.show();
        return 1;
    }else if(command == "mist"){
        stripA.setPixelColor(1, 164, 196, 0);
        stripA.setPixelColor(2, 164, 196, 0);
        
        stripA.setPixelColor(0, 255, 127, 0);
        stripA.setPixelColor(3, 255, 127, 0);
        
        stripA.show();
         return 1;
    }else if(command == "freezing"){
        stripA.setPixelColor(1, 144, 196, 0);
        stripA.setPixelColor(2, 144, 196, 0);
        
        stripA.setPixelColor(0, 235, 127, 0);
        stripA.setPixelColor(3, 235, 127, 0);
        
        stripA.show();
         return 1;
    }else if(command == "storm"){
        stripA.setPixelColor(1, 255, 127, 0);
        stripA.setPixelColor(2, 255, 127, 0);
        
        stripA.setPixelColor(0, 0, 255, 0);
        stripA.setPixelColor(3, 0, 255, 0);
        
        stripA.show();
         return 1;
    }
    else return -1;
}


#2

Webhooks is the keyword here and the search feature of this forum should help too
https://community.particle.io/search?q=openweather


#3

Hi @ScruffR,

I’ve created a webhook for the weather but And I’m getting response.

This is the response

HTTP/1.1 200 OK
Server: openresty
Date: Sun, 24 Mar 2019 16:34:54 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 440
Connection: keep-alive
X-Cache-Key: /data/2.5/weather?coreid=XXXXXXXXXXXXXXXXX&data=10&event=CurrentWeather&published_at=2019-03-24T16%3A34%3A54.387Z&q=diepenbeek&units=metric
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST

{"coord":{"lon":5.42,"lat":50.91},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":11.23,"pressure":1025,"humidity":37,"temp_min":10,"temp_max":12.78},"visibility":10000,"wind":{"speed":3.6,"deg":300},"clouds":{"all":0},"dt":1553445151,"sys":{"type":1,"id":1525,"message":0.0059,"country":"BE","sunrise":1553405578,"sunset":1553450180},"id":2799413,"name":"Diepenbeek","cod":200}
	      

I want to trigger my leds on weather - main but I’ve no idea on how to do that… Can you help me with this?

Thanks!


#4

What do you see in console.particle.io/events after you triggered that webhook?
Can you post your webhook definition?
What is your current code for the device?
Have you looked at the docs/tutorials about webhooks?
e.g. https://docs.particle.io/tutorials/device-cloud/webhooks/#getting-the-response


#5

Hi ScruffR

This is what I’m seeing in the events. I just need the weather-main

This is my webhook definition

{
    "event": "CurrentWeather",
    "deviceID": "XXXXXXXXXXXXXXXXXXXX",
    "responseTopic": "",
    "url": "https://api.openweathermap.org/data/2.5/weather?q=Diepenbeek&units=metric&appid=609fd5eb18ba6e03f9baa3160d1ffcef",
    "requestType": "GET",
    "noDefaults": false,
    "rejectUnauthorized": true
}

This is the code I’m using, just the code provided by the webhook to see what data I’m getting.

void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/CurrentWeather", myHandler, MY_DEVICES);
}

void myHandler(const char *event, const char *data) {
  // Handle the integration response
}


void loop() {
  // Get some data
  String data = String(10);
  // Trigger the integration
  Particle.publish("CurrentWeather", data, PRIVATE);
 
  // Wait 60 seconds
  delay(60000);
}
     

And yes I’ve looked at the docs but not seen info on this…

Thanks


#6

Since the server response is rather big but you are only interested in a part of it, you want to set a custom response template returning {{weather.main}} and in your myHandler() function you need to catch the data value (e.g. Serial.println(data)).

See here too
https://docs.particle.io/reference/device-cloud/webhooks/#receiving-complex-data