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;
}