Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Date;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.HashMap;
- import java.net.HttpURLConnection;
- import java.io.DataOutputStream;
- import java.io.Reader;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import processing.serial.*;
- Serial myPort; // Create object from Serial class
- Map<String,Float> readings;
- long prevTime;
- long lastPostToDatabase;
- void setup()
- {
- size(300, 650);
- myPort = new Serial(this, "COM16", 115200);
- println(Serial.list());
- readings = new HashMap<String, Float>();
- PFont f = createFont("Georgia", 20);
- textFont(f);
- }
- void draw()
- {
- background(255);
- try{
- String serialText = "";
- while ( myPort.available() > 0) { // If data is available,
- int charCode = myPort.read();
- if (charCode == 10)
- break;
- while (myPort.available() == 0 && charCode != 10){
- //println("waiting for end of line char");
- delay(10);
- }
- if (charCode != 10)
- serialText += (char)charCode;
- }
- if (serialText.length() > 0){
- prevTime = (new Date()).getTime();
- String variableName = serialText.substring(0,serialText.indexOf(":"));
- String variableValue = serialText.replaceAll("[^\\d.-]","");
- Float floatValue = Float.parseFloat(variableValue);
- readings.put(variableName, floatValue);
- //println(serialText);
- }
- }
- catch(Exception e){}
- if (readings.size() > 0){
- fill(0);
- String displayOutput = "Inside My Room\n";
- if (readings.get("TempMyRoom") != null){
- displayOutput += "Temperature: " + readings.get("TempMyRoom") + "C ";
- displayOutput += String.format("%.1f", cToF(readings.get("TempMyRoom"))) + "F";
- }
- if (readings.get("HumMyRoom") != null)
- displayOutput += "\nHumidity: " + round(readings.get("HumMyRoom")) + "%";
- if (readings.get("visLightMyRoom") != null)
- displayOutput += "\nLight: " + round(readings.get("visLightMyRoom")/(1024*3.3/5)*100) + "%";
- if (readings.get("MotionDetectedMyRoom") != null){
- displayOutput += "\nMotion Detected: ";
- if (readings.get("MotionDetectedMyRoom") == 1)
- displayOutput += "true";
- else
- displayOutput += "false";
- }
- displayOutput += "\n\nOutside\n";
- if (readings.get("Temperature") != null){
- displayOutput += "Temperature: " + readings.get("Temperature") + "C ";
- displayOutput += String.format("%.1f", cToF(readings.get("Temperature"))) + "F";
- }
- if (readings.get("Humidity") != null)
- displayOutput += "\nHumidity: " + round(readings.get("Humidity")) + "%";
- if (readings.get("Dew Point") != null){
- displayOutput += "\nDew Point: " + round(readings.get("Dew Point")) + "C ";
- displayOutput += String.format("%.1f", cToF(readings.get("Dew Point"))) + "F";
- }
- if (readings.get("Rain Gauge") != null)
- displayOutput += "\nRain Gauge: " + round(readings.get("Rain Gauge"));
- if (readings.get("Rain Drop Sensor") != null)
- displayOutput += "\nRain Drop Sensor: " + round(readings.get("Rain Drop Sensor"));
- if (readings.get("Pressure") != null)
- displayOutput += "\nPressure: " + String.format("%.2f", readings.get("Pressure")) + " millibars";
- if (readings.get("Visible Light") != null)
- displayOutput += "\nVisible Light: " + round(readings.get("Visible Light"));
- if (readings.get("IR Light") != null)
- displayOutput += "\nIR Light: " + round(readings.get("IR Light"));
- if (readings.get("UV Index") != null)
- displayOutput += "\nUV Index: " + String.format("%.2f", readings.get("UV Index")) + " of 11+";
- if (readings.get("IR Distance") != null)
- displayOutput += "\nIR Distance: " + round(readings.get("IR Distance"));
- if (readings.get("Loops") != null)
- displayOutput += "\nLoops: " + round(readings.get("Loops"));
- long lastUpdate = ((new Date()).getTime()-prevTime);
- displayOutput += "\n\nLast Update: "+lastUpdate+"ms";
- text(displayOutput, 10, 30);
- //println(readings);
- if (millis()-lastPostToDatabase > 30*1000){
- postToDatabase(readings);
- lastPostToDatabase = millis();
- }
- /*
- Iterator it = readings.entrySet().iterator();
- int i=0;
- while(it.hasNext()){
- Map.Entry keyValuePair = (Map.Entry) it.next();
- text(keyValuePair.getKey() + ": " + keyValuePair.getValue(), 10, ++i*30);
- }*/
- }
- }
- float cToF(float C){
- return (float)9/(float)5*C+(float)32;
- }
- void postToDatabase(Map<String,Float> map){
- println("\nposting to database at "+month()+"/"+day()+"/"+year()+" "+hour()+":"+minute()+":"+second());
- String urlParameters = "";
- Iterator it = map.entrySet().iterator();
- while(it.hasNext()){
- Map.Entry pairs = (Map.Entry)it.next();
- urlParameters += pairs.getKey() + "=" + pairs.getValue() + "&";
- }
- urlParameters = urlParameters.substring(0, urlParameters.length()-1);
- urlParameters = urlParameters.replaceAll(" ", "+");
- try{
- String request = "[removed]";
- URL url = new URL(request);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setDoInput(true);
- connection.setDoOutput(true);
- connection.setInstanceFollowRedirects(false);
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
- connection.setRequestProperty("charset", "utf-8");
- connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
- connection.setUseCaches (false);
- connection.getOutputStream().write(urlParameters.getBytes("UTF-8"));
- Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
- for (int c; (c = in.read()) >= 0; System.out.print((char)c));
- } catch(Exception e){
- System.out.println(e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement