Advertisement
Guest User

Untitled

a guest
Nov 15th, 2014
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. import java.util.Date;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.HashMap;
  5. import java.net.HttpURLConnection;
  6. import java.io.DataOutputStream;
  7. import java.io.Reader;
  8. import java.io.DataOutputStream;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13.  
  14. import processing.serial.*;
  15.  
  16. Serial myPort;  // Create object from Serial class
  17.  
  18. Map<String,Float> readings;
  19. long prevTime;
  20. long lastPostToDatabase;
  21.  
  22. void setup()
  23. {
  24.   size(300, 650);
  25.   myPort = new Serial(this, "COM16", 115200);
  26.   println(Serial.list());
  27.   readings = new HashMap<String, Float>();
  28.   PFont f = createFont("Georgia", 20);
  29.   textFont(f);  
  30. }
  31.  
  32. void draw()
  33. {
  34.   background(255);
  35.   try{
  36.       String serialText = "";
  37.       while ( myPort.available() > 0) {  // If data is available,
  38.         int charCode = myPort.read();
  39.         if (charCode == 10)
  40.           break;
  41.        
  42.         while (myPort.available() == 0 && charCode != 10){
  43.           //println("waiting for end of line char");
  44.           delay(10);
  45.         }
  46.        
  47.         if (charCode != 10)
  48.           serialText += (char)charCode;
  49.       }
  50.    
  51.       if (serialText.length() > 0){
  52.        
  53.         prevTime = (new Date()).getTime();
  54.        
  55.         String variableName = serialText.substring(0,serialText.indexOf(":"));
  56.         String variableValue = serialText.replaceAll("[^\\d.-]","");
  57.        
  58.         Float floatValue = Float.parseFloat(variableValue);
  59.         readings.put(variableName, floatValue);
  60.      
  61.         //println(serialText);
  62.       }
  63.   }
  64.   catch(Exception e){}
  65.  
  66.   if (readings.size() > 0){
  67.     fill(0);
  68.     String displayOutput = "Inside My Room\n";
  69.     if (readings.get("TempMyRoom") != null){
  70.       displayOutput += "Temperature: " + readings.get("TempMyRoom") + "C ";
  71.       displayOutput += String.format("%.1f", cToF(readings.get("TempMyRoom"))) + "F";
  72.     }
  73.     if (readings.get("HumMyRoom") != null)
  74.       displayOutput += "\nHumidity: " + round(readings.get("HumMyRoom")) + "%";
  75.      
  76.     if (readings.get("visLightMyRoom") != null)
  77.       displayOutput += "\nLight: " + round(readings.get("visLightMyRoom")/(1024*3.3/5)*100) + "%";
  78.    
  79.     if (readings.get("MotionDetectedMyRoom") != null){
  80.       displayOutput += "\nMotion Detected: ";
  81.       if (readings.get("MotionDetectedMyRoom") == 1)
  82.         displayOutput += "true";
  83.       else
  84.         displayOutput += "false";
  85.     }
  86.    
  87.     displayOutput += "\n\nOutside\n";
  88.     if (readings.get("Temperature") != null){
  89.       displayOutput += "Temperature: " + readings.get("Temperature") + "C ";
  90.       displayOutput += String.format("%.1f", cToF(readings.get("Temperature"))) + "F";
  91.     }
  92.     if (readings.get("Humidity") != null)
  93.       displayOutput += "\nHumidity: " + round(readings.get("Humidity")) + "%";
  94.     if (readings.get("Dew Point") != null){
  95.       displayOutput += "\nDew Point: " + round(readings.get("Dew Point")) + "C ";
  96.       displayOutput += String.format("%.1f", cToF(readings.get("Dew Point"))) + "F";
  97.     }
  98.     if (readings.get("Rain Gauge") != null)
  99.       displayOutput += "\nRain Gauge: " + round(readings.get("Rain Gauge"));
  100.     if (readings.get("Rain Drop Sensor") != null)
  101.       displayOutput += "\nRain Drop Sensor: " + round(readings.get("Rain Drop Sensor"));  
  102.     if (readings.get("Pressure") != null)
  103.       displayOutput += "\nPressure: " + String.format("%.2f", readings.get("Pressure")) + " millibars";
  104.     if (readings.get("Visible Light") != null)
  105.       displayOutput += "\nVisible Light: " + round(readings.get("Visible Light"));
  106.     if (readings.get("IR Light") != null)
  107.       displayOutput += "\nIR Light: " + round(readings.get("IR Light"));
  108.     if (readings.get("UV Index") != null)
  109.       displayOutput += "\nUV Index: " +  String.format("%.2f", readings.get("UV Index")) + " of 11+";
  110.     if (readings.get("IR Distance") != null)
  111.       displayOutput += "\nIR Distance: " + round(readings.get("IR Distance"));
  112.     if (readings.get("Loops") != null)
  113.       displayOutput += "\nLoops: " +  round(readings.get("Loops"));
  114.    
  115.     long lastUpdate = ((new Date()).getTime()-prevTime);
  116.     displayOutput += "\n\nLast Update: "+lastUpdate+"ms";
  117.  
  118.     text(displayOutput, 10, 30);
  119.     //println(readings);
  120.    
  121.     if (millis()-lastPostToDatabase > 30*1000){
  122.       postToDatabase(readings);
  123.       lastPostToDatabase = millis();
  124.     }
  125.    
  126.     /*
  127.     Iterator it = readings.entrySet().iterator();
  128.     int i=0;
  129.     while(it.hasNext()){
  130.       Map.Entry keyValuePair = (Map.Entry) it.next();
  131.       text(keyValuePair.getKey() + ": " + keyValuePair.getValue(), 10, ++i*30);
  132.     }*/
  133.    
  134.   }
  135. }
  136.  
  137. float cToF(float C){
  138.   return (float)9/(float)5*C+(float)32;
  139. }
  140.  
  141. void postToDatabase(Map<String,Float> map){
  142.   println("\nposting to database at "+month()+"/"+day()+"/"+year()+" "+hour()+":"+minute()+":"+second());
  143.   String urlParameters = "";
  144.  
  145.   Iterator it = map.entrySet().iterator();
  146.   while(it.hasNext()){
  147.     Map.Entry pairs = (Map.Entry)it.next();
  148.     urlParameters += pairs.getKey() + "=" + pairs.getValue() + "&";
  149.   }
  150.  
  151.   urlParameters = urlParameters.substring(0, urlParameters.length()-1);
  152.   urlParameters = urlParameters.replaceAll(" ", "+");
  153.  
  154.   try{
  155.       String request = "[removed]";
  156.       URL url = new URL(request);
  157.       HttpURLConnection connection = (HttpURLConnection) url.openConnection();          
  158.       connection.setDoInput(true);
  159.       connection.setDoOutput(true);
  160.       connection.setInstanceFollowRedirects(false);
  161.       connection.setRequestMethod("POST");
  162.       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  163.       connection.setRequestProperty("charset", "utf-8");
  164.       connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
  165.       connection.setUseCaches (false);
  166.       connection.getOutputStream().write(urlParameters.getBytes("UTF-8"));
  167.       Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
  168.       for (int c; (c = in.read()) >= 0; System.out.print((char)c));
  169.  
  170.     } catch(Exception e){    
  171.       System.out.println(e);
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement