Created
October 28, 2015 20:28
-
-
Save danaabs/8928fc999462ed8c2ddf to your computer and use it in GitHub Desktop.
Weather to NeoPixel Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var serial; // variable to hold an instance of the serialport library | |
var portName = '/dev/cu.usbmodemfa131'; // fill in your serial port name here | |
var inData; // for incoming serial data | |
var inByte; | |
var ID; | |
var temperature; | |
var lastTime = 0; | |
function setup() { | |
createCanvas(400, 300); // make the canvas | |
serial = new p5.SerialPort(); // make a new instance of the serialport library | |
serial.clear(); | |
serial.on('data', serialEvent); // callback for when new data arrives | |
serial.on('error', serialError); // callback for errors | |
serial.open(portName); // open a serial port | |
} | |
function askForWeather() { | |
var urlinByte = 'api.openweathermap.org/data/2.5/weather?q=' + inByte + ' ,&APPID=b5d53a60ec77f3c387de7b6b70581971'; | |
var url = 'http://api.openweathermap.org/data/2.5/weather?q=' + inByte + ' ,&APPID=b5d53a60ec77f3c387de7b6b70581971'; | |
loadJSON(url, drawWeather); | |
} | |
function serialEvent() { | |
inByte = serial.readStringUntil("\r\n"); | |
if (inByte.length > 0) { | |
if (inByte !== 'hello') { | |
// read a byte from the serial port: | |
console.log(inByte); | |
//send stuff | |
// store it in a global variable: | |
inData = inByte; | |
} | |
serial.write(' '); // send | |
} | |
if (inByte.length>0 && inByte !== "undefined"){ | |
askForWeather(); | |
} | |
} | |
function drawWeather(weather) { | |
// Get the loaded JSON data | |
console.log(weather); // inspect the weather JSON | |
var weatherID = weather.weather[0].id; // get the weather.humidity out of the loaded JSON | |
console.log(weatherID); // inspect the description in the console | |
println("This is the weather: " + str(weatherID)); | |
ID = str(weatherID); | |
//List all possible weather conditions | |
if (ID == 200) { | |
ID = "Thunderstorm with light rain"; | |
} | |
if (ID == 201) { | |
ID = "Thunderstorm with rain"; | |
} | |
if (ID == 202) { | |
ID = "Thunderstorm with heavy rain"; | |
} | |
if (ID == 210) { | |
ID = "Light thunderstorm"; | |
} | |
if (ID == 211) { | |
ID = "Thunderstorm"; | |
} | |
if (ID == 212) { | |
ID = "Heavy thunderstorm"; | |
} | |
if (ID == 221) { | |
ID = "Ragged thunderstorm"; | |
} | |
if (ID == 230) { | |
ID = "Thunderstorm with light drizzle"; | |
} | |
if (ID == 231) { | |
ID = "Thunderstorm with drizzle"; | |
} | |
if (ID == 232) { | |
ID = "Thunderstorm with heavy drizzle"; | |
} | |
if (ID == 300) { | |
ID = "Light intensity drizzle"; | |
} | |
if (ID == 302) { | |
ID = "Heavy intensity drizzle"; | |
} | |
if (ID == 310) { | |
ID = "Light intensity drizzle rain"; | |
} | |
if (ID == 311) { | |
ID = "Drizzle rain"; | |
} | |
if (ID == 312) { | |
ID = "Heavy intensity drizzle rain"; | |
} | |
if (ID == 313) { | |
ID = "Shower rain and drizzle"; | |
} | |
if (ID == 314) { | |
ID = "Heavy shower rain and drizzle"; | |
} | |
if (ID == 312) { | |
ID = "Shower drizzle"; | |
} | |
if (ID == 500) { | |
ID = "Light rain"; | |
} | |
if (ID == 501) { | |
ID = "Moderate rain"; | |
} | |
if (ID == 502) { | |
ID = "Heavy intensity rain"; | |
} | |
if (ID == 503) { | |
ID = "Very heavy rain"; | |
} | |
if (ID == 504) { | |
ID = "Extreme rain"; | |
} | |
if (ID == 511) { | |
ID = "Freezing rain"; | |
} | |
if (ID == 520) { | |
ID = "Light intensity shower rain"; | |
} | |
if (ID == 521) { | |
ID = "Shower rain"; | |
} | |
if (ID == 522) { | |
ID = "Heavy intnesity shower rain"; | |
} | |
if (ID == 531) { | |
ID = "Ragged shower rain"; | |
} | |
if (ID == 600) { | |
ID = "Light snow"; | |
} | |
if (ID == 601) { | |
ID = "Snow"; | |
} | |
if (ID == 602) { | |
ID = "Heavy snow"; | |
} | |
if (ID == 611) { | |
ID = "Sleet"; | |
} | |
if (ID == 612) { | |
ID = "Shower sleet"; | |
} | |
if (ID == 615) { | |
ID = "Light rain and snow"; | |
} | |
if (ID == 616) { | |
ID = "Rain and snow"; | |
} | |
if (ID == 620) { | |
ID = "Light shower snow"; | |
} | |
if (ID == 621) { | |
ID = "Shower snow"; | |
} | |
if (ID == 622) { | |
ID = "Heavy shower snow"; | |
} | |
if (ID == 701) { | |
ID = "Mist"; | |
} | |
if (ID == 711) { | |
ID = "Smoke"; | |
} | |
if (ID == 721) { | |
ID = "Haze"; | |
} | |
if (ID == 731) { | |
ID = "Sand, dust whirls"; | |
} | |
if (ID == 741) { | |
ID = "Fog"; | |
} | |
if (ID == 751) { | |
ID = "Sand"; | |
} | |
if (ID == 761) { | |
ID = "Dust"; | |
} | |
if (ID == 762) { | |
ID = "Volcanic ash"; | |
} | |
if (ID == 771) { | |
ID = "Squalls"; | |
} | |
if (ID == 781) { | |
ID = "Tornado"; | |
} | |
if (ID == 800) { | |
ID = "Clear sky"; | |
} | |
if (ID == 801) { | |
ID = "Few clouds"; | |
} | |
if (ID == 802) { | |
ID = "Scattered clouds"; | |
} | |
if (ID == 803) { | |
ID = "Broken clouds"; | |
} | |
if (ID == 804) { | |
ID = "Overcast clouds"; | |
} | |
if (ID == 900) { | |
ID = "Tornado"; | |
} | |
if (ID == 901) { | |
ID = "Tropical storm"; | |
} | |
if (ID == 902) { | |
ID = "Hurricane"; | |
} | |
if (ID == 903) { | |
ID = "Extreme cold"; | |
} | |
if (ID == 904) { | |
ID = "Extreme heat"; | |
} | |
if (ID == 905) { | |
ID = "Extreme wind"; | |
} | |
if (ID == 906) { | |
ID = "Hail"; | |
} | |
if (ID == 951) { | |
ID = "Calm"; | |
} | |
if (ID == 952) { | |
ID = "Light breeze"; | |
} | |
if (ID == 953) { | |
ID = "Gentle breeze"; | |
} | |
if (ID == 954) { | |
ID = "Moderate breeze"; | |
} | |
if (ID == 956) { | |
ID = "Strong breeze"; | |
} | |
if (ID == 957) { | |
ID = "High wind (near gale)"; | |
} | |
if (ID == 958) { | |
ID = "Gale"; | |
} | |
if (ID == 959) { | |
ID = "Severe gale"; | |
} | |
if (ID == 960) { | |
ID = "Storm"; | |
} | |
if (ID == 961) { | |
ID = "Violent storm"; | |
} | |
if (ID == 962) { | |
ID = "Hurricane"; | |
} | |
// serial.clear(); | |
} | |
function draw() { | |
//Check Weather once every fifteen seconds | |
if (millis() - lastTime > 15000) { | |
lastTime = millis(); | |
console.log("Checked Weather"); | |
askForWeather(); | |
if (ID !== "undefined" && inByte !== "undefined") { | |
serial.write(ID + " in " + inByte + " "); | |
serial.clear(); | |
} | |
} | |
// black background, white text: | |
background(0); | |
fill(255); | |
// display the incoming serial data as a string: | |
text("incoming value: " + inByte, 30, 30); | |
} | |
function serialError(err) { | |
console.log('Something went wrong with the serial port. ' + err); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment