import processing.serial.*; import controlP5.*; ControlP5 cp5; Serial myPort; controlP5.Textfield cityfield; Textlabel citylabel; controlP5.Textfield statefield; Textlabel statelabel; Textlabel currentlabel; Textlabel hourlylabel; Textlabel dailylabel; Textlabel on; Textlabel off; RadioButton r; boolean mancontrol = false; String lat; String lng; String city; String state; String currentweather; JSONArray dailydata; JSONArray hourlydata; int raincontrol = 0; int cloudcontrol = 0; int suncontrol = 0; int raincast = 0; int cloudcast = 0; int suncast = 0; int rain = 0; int cloud = 0; int sun = 0; boolean cur = false; void setup() { String portName = Serial.list()[1]; myPort = new Serial(this, portName, 9600); background(100); size(500,500); cp5 = new ControlP5(this); PFont pfont1 = createFont("Arial",20,true); // use true/false for smooth/no-smooth ControlFont font1 = new ControlFont(pfont1,20); citylabel = cp5.addTextlabel("city label") .setText("City:") .setPosition(10,10) .setFont(font1) ; cityfield = cp5.addTextfield("City") .setPosition(60,10) .setSize(150,25) .setFont(font1) .setFocus(true) .setColor(color(255,255,255)) .setLabel(" ") ; statelabel = cp5.addTextlabel("state label") .setText("State:") .setPosition(235,10) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; statefield = cp5.addTextfield("State") .setPosition(295,10) .setSize(50,25) .setFont(font1) .setAutoClear(false) .setColor(color(255,255,255)) .setLabel(" ") ; cp5.addBang("bang") .setPosition(375, 10) .setLabel("set") .setSize(115, 25) .setTriggerEvent(Bang.RELEASE) .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER) .setFont(font1) ; r = cp5.addRadioButton("weathertype") .setPosition(10,50) .setSize(158,100) .setColorForeground(color(200)) .setColorActive(color(255)) .setColorLabel(color(255)) .setItemsPerRow(3) .setSpacingColumn(3) .addItem(" ",1) .addItem(" ",2) .addItem(" ",3) ; currentlabel = cp5.addTextlabel("current") .setText("Current \nWeather") .setPosition(10,150) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; hourlylabel = cp5.addTextlabel("hourly") .setText("Hourly \nForecast") .setPosition(171,150) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; dailylabel = cp5.addTextlabel("daily") .setText("Daily \nForecast") .setPosition(332,150) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; cp5.addToggle("ManualControl") .setPosition(50,260) .setSize(40,30) .setValue(0) .setMode(ControlP5.SWITCH) .setColorBackground(color(100)) .setColorForeground(color(100)) .setColorActive(color(255)) .captionLabel().setControlFont(font1) ; on = cp5.addTextlabel("off") .setText("OFF") .setPosition(100,260) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; off = cp5.addTextlabel("on") .setText("ON") .setPosition(10,260) .setFont(createFont("Arial",20,true)) .setColor(color(255,255,255)) ; cp5.addToggle("Rain") .setValue(0) .setPosition(332,350) .setSize(158,100) .setColorBackground(color(255)) .setColorForeground(color(200)) .setColorActive(color(0,100,255)) .captionLabel().setControlFont(font1) ; cp5.addToggle("Clouds") .setValue(0) .setPosition(171,350) .setSize(158,100) .setColorBackground(color(255)) .setColorForeground(color(200)) .setColorActive(color(0,255,100)) .captionLabel().setControlFont(font1) ; cp5.addToggle("Sun") .setPosition(10,350) .setSize(158,100) .setValue(0) .setColorBackground(color(255)) .setColorForeground(color(200)) .setColorActive(color(255,255,0)) .captionLabel().setControlFont(font1) ; } public void bang() { city = cp5.get(Textfield.class, "City").getText(); state = cp5.get(Textfield.class, "State").getText(); JSONObject root = loadJSONObject("https://maps.googleapis.com/maps/api/geocode/json?address=+"+city+",+"+state+"&key=AIzaSyDo9zAzZ1r8rjslB83wA7gUhQfRYgXsZKo"); JSONObject location = root.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location"); lat = str(location.getFloat("lat")); lng = str(location.getFloat("lng")); JSONObject weather = loadJSONObject("https://api.forecast.io/forecast/5f5c8bf3804811344af7a8eb69a6f047/"+lat+","+lng); JSONObject daily = weather.getJSONObject("daily"); JSONObject hourly = weather.getJSONObject("hourly"); JSONObject currently = weather.getJSONObject("currently"); dailydata = daily.getJSONArray("data"); hourlydata = hourly.getJSONArray("data"); currentweather = currently.getString("icon"); } void draw() { noFill(); stroke(200); rect(49,259,42,32); line(0,250,500,250); if (mancontrol) { sun = suncontrol; cloud = cloudcontrol; rain = raincontrol; } else { if (cur) { if (second() == 0) { bang(); weathertype(1); } } else { } sun = suncast; cloud = cloudcast; rain = raincast; } String out = str(sun)+str(cloud)+str(rain); myPort.write(out); println(out); delay(500); } public void Sun(int theValue) { if (theValue == 1) { suncontrol = 1; } else { suncontrol = 0; } } public void Rain(int theValue) { if (theValue == 1) { raincontrol = 3; } else { raincontrol = 2; } } public void Clouds(int theValue) { if (theValue == 1) { cloudcontrol = 5; } else { cloudcontrol = 4; } } public void ManualControl(int theValue) { if (theValue == 1) { mancontrol = true; } else { mancontrol = false; } } public int[] checkcode(String st) { String ws = st; int s = 0; int c = 4; int r = 2; if (ws.equals("cloudy")) { c = 5; } else if (ws.equals("clear-day")) { s = 1; } else if (ws.equals("rain")) { r = 3; } else if (ws.equals("partly-cloudy-day")) { s = 1; c = 5; } else if (ws.equals("partly-cloudy-night")) { c = 5; } else if (ws.equals("fog")) { c = 5; } else { } int[] val = {s,c,r}; return val; } public void weathertype(int a) { if (a == 1) { //Current Weather cur = true; int[] codes = checkcode(currentweather); suncast = codes[0]; cloudcast = codes[1]; raincast = codes[2]; println(currentweather); } else if (a == 2) { cur = false; } else if (a == 3) { cur = false; } else { cur = false; } }