Back to Parent

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

//create json object, which will store the json file
JSONObject json;

//this is the URL endpoint that we want to call to retrieve the JSON
String rainURL = "https://www.kimonolabs.com/api/ap0udgpq?apikey=BmxyBLtmAMyAv6DsiArPhZE37J5QDm3P";
Arduino myarduino;
int start = 0;
int maxpercent = 1;

void setup() {
  size(200, 200);
  println(Arduino.list());
  myarduino = new Arduino(this, Arduino.list()[0], 57600);
  myarduino.pinMode(5, Arduino.OUTPUT);
  myarduino.pinMode(6, Arduino.OUTPUT);
  myarduino.pinMode(9, Arduino.SERVO);                   //servo temperature
  myarduino.pinMode(10, Arduino.SERVO);                  //servo percip
  myarduino.pinMode(0, Arduino.INPUT);                   //distance sensor
}

void draw() {
  background(210, 225, 250); 
  float i = myarduino.analogRead(0);
  float val = (6762/(i-9))-4;
  //println(val);
  if (val >= 6 && val <= 13 && start==0) {
     start = 1; 
     println(start);
     loadData();
  }
  }



void loadData() {
  //get the JSON file
  json = loadJSONObject(rainURL);
  println("we got JSON!");
  println(json);
  
  //get the JSON object named "results", which contains the array we want!
  JSONObject results = json.getJSONObject("results");
  
  //get the array titled "collection1"
  JSONArray array = results.getJSONArray("collection1");
  println("length of array: " + array.size());
  
  
  int totaltemp = 0;
  int avgtemp = 0;
  
  int start = 0;

  if(array.size() > 0)
  {
    for(int i = 0; i < array.size(); i++)                       //go through each section and find the "amnt"
    {
      JSONObject currentindex = array.getJSONObject(i);         //get the first dataGroup in the "collection1" array
      String percentage = currentindex.getString("percip");
      percentage = percentage.replaceAll("%","");
      int percent = parseInt(percentage);                       //percentage of rain
      println("percip percentage " + i + ": " + percent);
      if (percent >= maxpercent) {
         maxpercent = percent;
         println("maxpercent is" + maxpercent);    
      }
      int temperature = currentindex.getInt("temp");            //temperature
      println("temperature is" + temperature);
      totaltemp = totaltemp + temperature;
    }        
   }
    myarduino.digitalWrite(5, Arduino.HIGH);                    //turn on light regardless
    delay(1000);
    myarduino.digitalWrite(6, Arduino.HIGH);                    //turn on light regardless
    delay(1000);
          
    avgtemp = (totaltemp)/ array.size();
    println("avg temp is " + avgtemp);
    
    int mappedTemp = (int)map(avgtemp, 30, 100, 70, 110);  
    println("mappedTemp is " + mappedTemp);
    myarduino.analogWrite(9, mappedTemp);
    
    int mappedPercip = (int)map(maxpercent, 0, 100, 70, 110);
    println("mappedPercip is " + mappedPercip);
    myarduino.analogWrite(10, mappedPercip);
  }
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0