Tuesday, April 12, 2016

Real Time Monitor Temperature with Arduino + Processing

Posted by Unknown
selamat malam semuanya.. apa kabar. untuk tutorial kali ini langsung saja bagaimana menampilkan real time monitoring suhu menggunakan processing. mudah dan sangat simple kok. heheheh. apalagi sekarang processing version 2.1.1 sudah release.. diantara nya fix bug readStringUntil untuk komunikasi serial. jkarena sebelumnya bug disitu. terkadang ane msh kesulitan bisa ngakalin nya gmn. kadang bisa kadang enga. ehehe. jadi curhat. langsung saja ya ga banyak cingcong. semoga bisa membantu referensi teman - teman sekalian. hehehehe

Hardware Requirement
- Arduino UNO R3
- Sensor Suhu seperti DHT 11 , DHT 21 atau DHT 22 . (mau pake yang analog juga gpp :D)
- BreadBoard
- beberapa kabel jumper
- dan semangat. :D

sumber : www.hobbyist.co.nz | wiring dht 11 arduino

terus setelah itu kita download versi terbaru processing IDE . nya processing.org . cari yang release terakhir. dan kemarin baru released nya. hahahaha.. setelah itu kita langsung saja coding nya. code berikut ane sedikit modifikasi hanya mengambil value suhu nya saja dan code nya berasal dari examples dari library dht11.

Arduino Code 



 // Example testing sketch for various DHT humidity/temperature sensors  
 // Written by ladyada, public domain  
   
 #include "DHT.h"  
   
 #define DHTPIN 2   // what pin we're connected to  
   
 // Uncomment whatever type you're using!  
 #define DHTTYPE DHT11  // DHT 11   
 //define DHTTYPE DHT22  // DHT 22 (AM2302)  
 //#define DHTTYPE DHT21  // DHT 21 (AM2301)  
   
 // Connect pin 1 (on the left) of the sensor to +5V  
 // Connect pin 2 of the sensor to whatever your DHTPIN is  
 // Connect pin 4 (on the right) of the sensor to GROUND  
 // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor  
   
 DHT dht(DHTPIN, DHTTYPE);  
   
 void setup() {  
  Serial.begin(9600);   
  Serial.println("DHTxx test!");  
    
  dht.begin();  
 }  
   
 void loop() {  
  // Reading temperature or humidity takes about 250 milliseconds!  
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)  
  float t = dht.readTemperature();  
   
  // check if returns are valid, if they are NaN (not a number) then something went wrong!  
  if (isnan(t)) {  
   Serial.println("Failed to read from DHT");  
  } else {  
   Serial.println((byte)t);  
     
  }  
 }  

Processing Code



 import processing.serial.*;  
   
   
 Serial myport;  
   
 int tempC;  
 float[] historytemp = new float[100];  
 float[] historyfahren = new float[100];  
   
 String mystring = null;  
   
 int ydist;  
   
 PFont fontCelcius;  
 PFont fontFahren;  
 PFont indikator;  
   
 void setup(){  
  size(210,200);  
    
  fontCelcius = loadFont("Consolas-16.vlw");  
  //fontFahren = loadFont("Consolas-16.vlw");  
  indikator = loadFont("CenturySchoolbook-Bold-10.vlw");  
  myport = new Serial(this,Serial.list()[0],9600);  
  for(int index=0;index < 100;index++){  
    historytemp[index] = 0;   
    historyfahren[index] =0;  
  }  
    
    
 }  
   
 void draw(){  
  while(myport.available() > 0){  
     
    mystring = myport.readStringUntil('\n');  
    if(mystring != null){     
       mystring = trim(mystring);  
       tempC = int(mystring);  
         
      
    background(123);  
      
    colorMode(RGB, 160);  
    stroke(0);  
    rect(49,19,22,162);  
      
    for(int colorIndex= 0 ; colorIndex <= 160 ; colorIndex++){  
      stroke(160 - colorIndex, 0 , colorIndex);  
      line(50, colorIndex + 20, 70, colorIndex + 20);   
    }  
      
    stroke(0);  
    fill(255,255,255);  
    rect(90, 80, 100, 100);  
      
    for(int index=0 ; index < 100; index++){  
      if(index == 99){  
       historytemp[index] = tempC;  
      // historyfahren[index] = (int)Fahrenheit(tempC);  
      }   
      else {  
       historytemp[index] = historytemp[index + 1];  
       historyfahren[index] = historyfahren[index + 1];  
       fill(random(255),random(255),random(255));  
      point(90 + index, 180 - historytemp[index]);  
      point(90 + index, 180 - historyfahren[index]);  
        
      }  
    }  
      
    fill(0,0,0);  
    textFont(indikator);  
    textAlign(RIGHT);  
    text("100 C", 45, 25);  
    text("0 C", 45, 187);  
      
    ydist = int(160 - ( 160 * (tempC * 0.01)));  
    stroke(0);  
    triangle(75, ydist + 20, 85, ydist + 15, 85, ydist + 25);  
     
    fill(0,0,0);  
    textFont(fontCelcius);  
    textAlign(LEFT);  
    text(str(int(tempC)) + " C",115, 37);  
    /*  
    fill(0,0,0);  
    textFont(fontFahren);  
    textAlign(LEFT);  
    text((int)Fahrenheit(tempC) + " F",115, 50);  
    */  
    }  
  }   
    
 }  
   
 double Fahrenheit(float celcius){  
  return 1.8 * celcius + 32;   
 }  
   
   

source di atas hanya implementasi menampilkan suhu Celcius. . bisa anda kembangkan untuk menampilkan ke fahrenheit, kelvin dan kelembaban nya bisa di tampilkan. dan code di atas modifikasi dari code ini

apabila berhasil berikut hasil nya


selamat mencoba ... dan KEEP SHARING :-)

0 comments:

Post a Comment