okaeri : welcome home

Made by Praewa Suntiasvaraporn

Okaeri is a travel buddy that creates a sense of warmth from home wherever you go.

Created: December 22nd, 2015

0
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

import org.firmata.*;
import cc.arduino.*;


import processing.serial.*;

Arduino arduino;
Minim minim;
AudioPlayer player;

JSONObject homeLocation;
JSONObject currentLocation;  

String homeCity;
String homeCountry;
String currentCity;
String currentCountry;

int d;
int h;
int tmrDate;
int currentTimeHere;
int [] times = {0,3,6,9,12,15,18,21};

//int rainVolume;
int currentTemp;
int currentRainVolume;
int currentHumidity;
int currentCloudAmount;
int currentWindSpeed;
int homeTemp;
int homeRainVolume;
int homeHumidity;
int homeCloudAmount;
int homeWindSpeed;

int servoPin = 5;
int servoPin2 = 3;
int lightPin = 6;
int heatingPin = 8;
int heatingPin2 = 9;
int fanPin = 10;
int fanPin2 = 11;
int fsrPin = 0;

//int currentSadness;
//int diffInTemp;
int mappedOutput;
int cMappedOutput;
float gain;
float cGain;
int pos = 0;

boolean playing = false;
boolean prevPlay = false;
int timePlayerStarted;

String url = "http://api.openweathermap.org/data/2.5/forecast/city?id=1609350&units=metric&APPID=ea5aa1586746b8e63ef93938af1ab6fe";
String url1 = "http://api.openweathermap.org/data/2.5/weather?id=5206379&units=metric&APPID=ea5aa1586746b8e63ef93938af1ab6fe";

void setup() {
    arduino = new Arduino(this, Arduino.list()[1], 57600);
//  arduino.pinMode(pulsePin, Arduino.OUTPUT);
  arduino.pinMode(heatingPin, Arduino.OUTPUT);
  arduino.pinMode(fsrPin, Arduino.INPUT);
  arduino.pinMode(servoPin, Arduino.SERVO);
  arduino.pinMode(fanPin, Arduino.OUTPUT);
  arduino.pinMode(lightPin, Arduino.OUTPUT);
  arduino.pinMode(heatingPin2, Arduino.OUTPUT);
  arduino.pinMode(servoPin2, Arduino.SERVO);
  arduino.pinMode(fanPin2, Arduino.OUTPUT);
  
  size(1000, 1000);
  
  minim = new Minim(this);
  
  player = minim.loadFile("night.mp3");
  
  player.setGain(cGain);


  h = hour();
  d = day();
  
//  println(h);
  for (int i = 0; i < times.length; i++) {
     
    if (h <= times[i]) {
//      println("times: " +times[i]);
      h = times[i];
      break;
    }
    else {
      h = times[7];
    }
  }
  
  
//  println("time now: " +h);
  
  currentTimeHere = h;
  tmrDate = (d+1);
  
  
//  if(h < 10) {
//     currentTimeHere = "0" +h;
//  }
//  else {
//    currentTimeHere = str(h);
//  }
  mappedOutput = 0;
  
  loadData();
  
  int currentSadness = (currentRainVolume + currentCloudAmount + currentWindSpeed + currentHumidity - currentTemp);
  println("sadness: " +currentSadness);

  int diffInTemp = (homeTemp - currentTemp);
  println("diff in temp: " +diffInTemp);
  
  int multiplied = (diffInTemp * currentSadness);
  println("multiplied val: " +multiplied);
  
  
  
  textSize(50);
  text("Home Location: " +homeCity +", " +homeCountry, 20, 80);
  textSize(30);
  text("Temperature in " +homeCity +" at " +currentTimeHere +":00 : " +homeTemp +"celcius", 20, 120);
  textSize(50);
  text("Current Location: " +currentCity +", " +currentCountry, 10, height/2);
  textSize(30);
  text("Temperature in " +currentCity +" at " +currentTimeHere +":00 : " +currentTemp +"celcius", 10, (height/2) + 40);
  
  mappedOutput = int(map(multiplied, -1000, 1000, 0, 255));
  println("mapped output: " +mappedOutput);

  gain = map(multiplied, -1000, 1000, -80.0, 0); //how to get mapped value that stays within range?
  

}


void loadData() {
  homeLocation = loadJSONObject(url);
  currentLocation = loadJSONObject(url1);
//  println("home location:" +homeLocation);

  JSONObject homeName = homeLocation.getJSONObject("city");
  homeCity = homeName.getString("name");
  homeCountry = homeName.getString("country");
//  println("Current Location: ");
  
  currentCity = currentLocation.getString("name");
  JSONObject currentNameC = currentLocation.getJSONObject("sys");
  currentCountry = currentNameC.getString("country");
  
  JSONObject currentMain = currentLocation.getJSONObject("main");
  currentTemp = currentMain.getInt("temp");
  currentHumidity = currentMain.getInt("humidity");
  println("current temp: " +currentTemp);
  println("current humidity :" +currentHumidity);

  JSONObject currentWind = currentLocation.getJSONObject("wind");
  currentWindSpeed = currentWind.getInt("speed");
  println("current wind: " +currentWindSpeed);
  
  JSONObject currentClouds = currentLocation.getJSONObject("clouds");
  currentCloudAmount = currentClouds.getInt("all");
  println("current clouds: " +currentCloudAmount);
  
  
  if(currentLocation.isNull("rain") == true) {
    currentRainVolume = 0;
    println("current rain: " +currentRainVolume);
  }
  else {
    JSONObject currentRain = currentLocation.getJSONObject("rain");
    
    if(currentRain.isNull("3h") == true) {
         currentRainVolume = 0;
        }
        
        else {
         currentRainVolume = currentRain.getInt("3h");
        }
  }

  
  
  JSONArray homeWeather = homeLocation.getJSONArray("list");
  for (int i = 0; i < homeWeather.size(); i++)
  {
    JSONObject list = homeWeather.getJSONObject(i);
    String dateTime = list.getString("dt_txt");
    String[] dateTimeSplit = dateTime.split(" ");
    String[] dateTimeSplit2 = dateTimeSplit[1].split(":");
    String[] timeSplit = dateTimeSplit[0].split("-");
    int var = parseInt(dateTimeSplit2[0]);
    int day = parseInt(timeSplit[2]);
    
//    println("datee: " +dateTimeSplit[0]);
//    println("date: " +day);
//    println("dayy: " +tmrDate);
//    println("time: " +currentTimeHere);
    
//    println(var);
//    println(currentTimeHere);
//    
//    println(dateTimeSplit[1]);
    
    if(currentTimeHere == var && tmrDate == day) {
      println("Home: ");
      //fetch weather info from current time in current location and that same time in home location.
      
      JSONObject main = list.getJSONObject("main");
      homeTemp = main.getInt("temp");
      homeHumidity = main.getInt("humidity");
      println("temp: " +homeTemp);
      println("humidity: " +homeHumidity);

      JSONObject wind = list.getJSONObject("wind");
      homeWindSpeed = wind.getInt("speed");
      println("wind speed: " +homeWindSpeed);
      
      JSONObject clouds = list.getJSONObject("clouds");
      homeCloudAmount = clouds.getInt("all");
      println("clouds: " +homeCloudAmount);
      
      JSONObject rain = list.getJSONObject("rain");
      if(rain.isNull("3h") == true) {
        homeRainVolume = 0;
      }
      
      else {
        homeRainVolume = rain.getInt("3h");
      }
      
      println("rain: " +homeRainVolume);
      
    }
    else {
//      println("nope");
    }
    



//int diffInHumidity = (homeHumidity - currentHumidity);
//int diffInRain = (homeRain - currentRainVolume);
//int diffInWindSpeed = (homeWindSpeed - currentWindSpeed);
//
//





      
    
    
  }
}

void draw() {
  int fsrReading = arduino.analogRead(fsrPin);
  println("fsrReading: " +fsrReading);
  
  int h = hour();
  
  if(h >= 17) {
    if(fsrReading >= 300 && !prevPlay) {
      println("detected!");
      playing = true;
      cMappedOutput = constrain(mappedOutput, 0, 255);
      println("mapped output: " +cMappedOutput);
      cGain = constrain(gain, -80.0, 0.0);
      println("gain: " +cGain); 
   //if sensed that user has put their head on the pillow//
      arduino.digitalWrite(heatingPin, cMappedOutput); 
      arduino.analogWrite(fanPin, cMappedOutput);
      arduino.analogWrite(lightPin, cMappedOutput);
      
  //    arduino.digitalWrite(heatingPin2, cMappedOutput); 
  //    arduino.analogWrite(fanPin2, cMappedOutput);
      
      
      pos = 90;
      player.loop();
      timePlayerStarted =  millis();
    }
  
    if(playing && ((millis() - timePlayerStarted) > 20000)) {
      playing = false;
      player.pause();
      cMappedOutput=0;
      arduino.digitalWrite(heatingPin, cMappedOutput);
      arduino.analogWrite(fanPin, cMappedOutput);
      arduino.analogWrite(lightPin, cMappedOutput);
      
  //    arduino.digitalWrite(heatingPin2, cMappedOutput);
  //    arduino.analogWrite(fanPin2, cMappedOutput);
  //    
      
      pos = 0;
      exit();
    }
    arduino.servoWrite(servoPin, 0);
    arduino.servoWrite(servoPin2, pos);
    prevPlay = playing;
  }
  
  if(h <= 10) {
    if(fsrReading >= 700 && !prevPlay) {
      println("wake up call!");
      playing = true;
      cMappedOutput = constrain(mappedOutput, 0, 255);
      println("mapped output: " +cMappedOutput);
      cGain = constrain(gain, -80.0, 0.0);
      println("gain: " +cGain); 
   //if sensed that user has put their head on the pillow//
      arduino.analogWrite(lightPin, cMappedOutput);
      arduino.digitalWrite(heatingPin2, cMappedOutput); 
      arduino.analogWrite(fanPin2, cMappedOutput);
      
      pos = 90;
      player.loop(
      );
      timePlayerStarted =  millis();
    }
  
    if(playing && ((millis() - timePlayerStarted) > 20000)) {
      playing = false;
      player.pause();
      cMappedOutput = 0;
      arduino.digitalWrite(heatingPin2, cMappedOutput);
      arduino.analogWrite(fanPin2, cMappedOutput);
      arduino.analogWrite(lightPin, cMappedOutput);
      
  //    arduino.digitalWrite(heatingPin2, cMappedOutput);
  //    arduino.analogWrite(fanPin2, cMappedOutput);
  //    
      
      pos = 0;
      exit();
    }
    arduino.servoWrite(servoPin, pos);
    arduino.servoWrite(servoPin2, 0);
    prevPlay = playing;
  }
}
Click to Expand
x
Share this Project

Courses

48-739 Making Things Interactive

· 0 members

Making Things Interactive (MTI) is a studio course based on physical prototyping and computing. You will develop novel sensing, interaction and display techniques through projects and short assignm...more


About

Okaeri is a travel buddy that creates a sense of warmth from home wherever you go.