Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat digunakan untuk monitor suhu dan kelembaban dengan disertai datalogger yang berupa interface VB 6 dan database menggunakan ms. access, alat ini menggunakan output relay sebagai aktuatornya untuk kendali kipas, untuk lebih jelasnya berikut adalah skema dan programnya.
a. Arduino Uno
b. Sensor DHT11
c. Lcd 16x2 I2C
d. Modul Relay
e. Program Arduino IDE
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5; //full speed
int t1;
int h1;
int t2;
int h2;
int ledm1 = 8;
int ledb1 = 9;
int ledm2 = 10;
int ledk2 = 11;
int ledh2 = 12;
int ledb2 = 13;
int tombol = A0;
int mark;
int mode;
int tombolx;
void setup() {
Serial.begin(9600);
pinMode(ledm1,OUTPUT);
pinMode(ledb1,OUTPUT);
pinMode(ledm2,OUTPUT);
pinMode(ledk2,OUTPUT);
pinMode(ledh2,OUTPUT);
pinMode(ledb2,OUTPUT);
pinMode(tombol,INPUT_PULLUP);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
lcd.begin();
lcd.noCursor();
lcd.clear();
}
void loop() {
tombolx = digitalRead(tombol);
if((tombolx == 0)&&(mark == 0)){
mode = 1;
tombolx = 1;
mark = 1;
}
satu();
tombolx = digitalRead(tombol);
if((tombolx == 0)&&(mark == 1)){
mode = 2;
tombolx = 1;
mark = 0;
}
dua();
lcd.setCursor(0,0);
lcd.print("H/T1:");
lcd.print(h1);
lcd.print("/");
lcd.print(t1);
lcd.setCursor(0,1);
lcd.print("H/T2:");
lcd.print(h2);
lcd.print("/");
lcd.print(t2);
Serial.println(t1);
Serial.println(h1);
Serial.println(t2);
Serial.println(h2);
Serial.println(mode);
if (mode == 1){
lcd.setCursor(11,0);
lcd.print("SMPN");
if(h1 > 62){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,HIGH);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
if((h1 > 60)&&(h1 < 62)){
digitalWrite(relay1,LOW);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,HIGH);
}
if((h1 > 55)&&(h1 < 60)){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,LOW);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,HIGH);
digitalWrite(ledb2,LOW);
}
if(h1 < 55){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,LOW);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,HIGH);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
}
if (mode == 2){
lcd.setCursor(11,0);
lcd.print("KRNG");
if(h1 >= 70){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,HIGH);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
if(h1 < 70){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,HIGH);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
}
}
void satu(){
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
delay(1000);
h1 = dht.readHumidity();
t1 = dht.readTemperature();
}
void dua(){
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
delay(1000);
h2 = dht.readHumidity();
t2 = dht.readTemperature();
}
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
LiquidCrystal_I2C lcd(0x3F, 16, 2);
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay4 = 5; //full speed
int t1;
int h1;
int t2;
int h2;
int ledm1 = 8;
int ledb1 = 9;
int ledm2 = 10;
int ledk2 = 11;
int ledh2 = 12;
int ledb2 = 13;
int tombol = A0;
int mark;
int mode;
int tombolx;
void setup() {
Serial.begin(9600);
pinMode(ledm1,OUTPUT);
pinMode(ledb1,OUTPUT);
pinMode(ledm2,OUTPUT);
pinMode(ledk2,OUTPUT);
pinMode(ledh2,OUTPUT);
pinMode(ledb2,OUTPUT);
pinMode(tombol,INPUT_PULLUP);
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
pinMode(relay3,OUTPUT);
pinMode(relay4,OUTPUT);
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
lcd.begin();
lcd.noCursor();
lcd.clear();
}
void loop() {
tombolx = digitalRead(tombol);
if((tombolx == 0)&&(mark == 0)){
mode = 1;
tombolx = 1;
mark = 1;
}
satu();
tombolx = digitalRead(tombol);
if((tombolx == 0)&&(mark == 1)){
mode = 2;
tombolx = 1;
mark = 0;
}
dua();
lcd.setCursor(0,0);
lcd.print("H/T1:");
lcd.print(h1);
lcd.print("/");
lcd.print(t1);
lcd.setCursor(0,1);
lcd.print("H/T2:");
lcd.print(h2);
lcd.print("/");
lcd.print(t2);
Serial.println(t1);
Serial.println(h1);
Serial.println(t2);
Serial.println(h2);
Serial.println(mode);
if (mode == 1){
lcd.setCursor(11,0);
lcd.print("SMPN");
if(h1 > 62){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,HIGH);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
if((h1 > 60)&&(h1 < 62)){
digitalWrite(relay1,LOW);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,HIGH);
}
if((h1 > 55)&&(h1 < 60)){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,LOW);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,HIGH);
digitalWrite(ledb2,LOW);
}
if(h1 < 55){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,LOW);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,HIGH);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
}
if (mode == 2){
lcd.setCursor(11,0);
lcd.print("KRNG");
if(h1 >= 70){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,HIGH);
digitalWrite(ledb1,LOW);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
if(h1 < 70){
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
digitalWrite(ledm1,LOW);
digitalWrite(ledb1,HIGH);
digitalWrite(ledm2,LOW);
digitalWrite(ledk2,LOW);
digitalWrite(ledh2,LOW);
digitalWrite(ledb2,LOW);
}
}
}
void satu(){
#define DHTPIN 6
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
delay(1000);
h1 = dht.readHumidity();
t1 = dht.readTemperature();
}
void dua(){
#define DHTPIN 7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
dht.begin();
delay(1000);
h2 = dht.readHumidity();
t2 = dht.readTemperature();
}
f. Program Interface VB 6
1. Module
Public Conn As New ADODB.Connection
Public Rs As New ADODB.Recordset
Public SQl As String
Public i As Byte
Public Sub dbConnect()
Set Conn = New ADODB.Connection
Conn.ConnectionString = strConn
Conn.Open
End Sub
Public Function strConn() As String
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\yanuar\Desktop\vbku\database\dbsuhu.mdb;Persist Security Info=False"
End Function
Public Rs As New ADODB.Recordset
Public SQl As String
Public i As Byte
Public Sub dbConnect()
Set Conn = New ADODB.Connection
Conn.ConnectionString = strConn
Conn.Open
End Sub
Public Function strConn() As String
Set Conn = New ADODB.Connection
Set Rs = New ADODB.Recordset
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\yanuar\Desktop\vbku\database\dbsuhu.mdb;Persist Security Info=False"
End Function
2. Form
Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 15
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
End Sub
Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer
If MSComm2.CommEvent = comEvReceive Then
buffer = MSComm2.Input
pisah = Split(buffer, Chr$(13))
On Error Resume Next
If buffer <> " " Then
With Text1
.SelStart = Len(.Text)
.SelText = buffer
End With
For i = 0 To 4
If 0 < 5 Then
Text2(i).Text = CStr(pisah(i))
End If
Next i
Else
buffer = " "
End If
End If
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 17
MSComm2.InputLen = 17
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub Timer1_Timer()
Dim SQl As String
Call dbConnect
SQl = "INSERT INTO tbsuhu VALUES('" & Text2(1).Text & "','" & Text2(3).Text & "','" & Text2(0).Text & "','" & Text2(2).Text & "','" & Text4.Text & "','" & Label4.Caption & "')"
Dim Rs As Recordset
Set Rs = New ADODB.Recordset
Rs.Open SQl, Conn, adOpenDynamic
End Sub
Private Sub Timer2_Timer()
Adodc1.Refresh
Dim ok As Integer
Dim mode As Integer
ok = Val(Text2(1).Text)
mode = Val(Text2(4).Text)
If mode = 1 Then
Text5.Text = "SIMPAN"
End If
If mode = 2 Then
Text5.Text = "KERING"
End If
If ok > 63 And mode = 1 Then
Text4.Text = "OFF"
End If
If ok <= 63 And mode = 1 Then
Text4.Text = "ON"
End If
If ok > 70 And mode = 2 Then
Text4.Text = "OFF"
End If
If ok <= 70 And mode = 2 Then
Text4.Text = "ON"
End If
End Sub
Private Sub Timer3_Timer()
MSComm2_OnComm
End Sub
Private Sub Timer4_Timer()
Label4.Caption = Format(Time, "hh:mm:ss")
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 15
Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset
Private Sub berhenti_Click()
start.Enabled = True
berhenti.Enabled = False
TimerBaca.Enabled = False
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
ComboCOM.AddItem (i)
Next i
End Sub
Private Sub MSComm2_OnComm()
Dim buffer As String
Dim temp As String
Dim pisah() As String
Dim i As Integer
If MSComm2.CommEvent = comEvReceive Then
buffer = MSComm2.Input
pisah = Split(buffer, Chr$(13))
On Error Resume Next
If buffer <> " " Then
With Text1
.SelStart = Len(.Text)
.SelText = buffer
End With
For i = 0 To 4
If 0 < 5 Then
Text2(i).Text = CStr(pisah(i))
End If
Next i
Else
buffer = " "
End If
End If
End Sub
Private Sub start_Click()
Dim u As Integer
On Error GoTo ada_eror
MSComm2.CommPort = ComboCOM.Text
MSComm2.Settings = "9600,N,8,1"
MSComm2.RThreshold = 17
MSComm2.InputLen = 17
MSComm2.InputMode = comInputModeText
MSComm2.PortOpen = True ' buka port
ReDim arrdata(1 To MAKSBACA + 1)
For u = 1 To MAKSBACA
arrdata(u) = 0
Next
TotalBaca = 0
start.Enabled = False
berhenti.Enabled = True
TimerBaca.Enabled = True
Timer1.Enabled = True
Timer2.Enabled = True
Timer3.Enabled = True
ada_eror:
If ComboCOM.ListIndex < 1 Then
MsgBox "COM berada di Nomor : " & Err.Number & vbCrLf & Err.Description, vbCritical + vbOKOnly, "ERROR"
start.Enabled = True
berhenti.Enabled = True
ComboCOM.Text = "Pilih COM"
End If
End Sub
Private Sub Timer1_Timer()
Dim SQl As String
Call dbConnect
SQl = "INSERT INTO tbsuhu VALUES('" & Text2(1).Text & "','" & Text2(3).Text & "','" & Text2(0).Text & "','" & Text2(2).Text & "','" & Text4.Text & "','" & Label4.Caption & "')"
Dim Rs As Recordset
Set Rs = New ADODB.Recordset
Rs.Open SQl, Conn, adOpenDynamic
End Sub
Private Sub Timer2_Timer()
Adodc1.Refresh
Dim ok As Integer
Dim mode As Integer
ok = Val(Text2(1).Text)
mode = Val(Text2(4).Text)
If mode = 1 Then
Text5.Text = "SIMPAN"
End If
If mode = 2 Then
Text5.Text = "KERING"
End If
If ok > 63 And mode = 1 Then
Text4.Text = "OFF"
End If
If ok <= 63 And mode = 1 Then
Text4.Text = "ON"
End If
If ok > 70 And mode = 2 Then
Text4.Text = "OFF"
End If
If ok <= 70 And mode = 2 Then
Text4.Text = "ON"
End If
End Sub
Private Sub Timer3_Timer()
MSComm2_OnComm
End Sub
Private Sub Timer4_Timer()
Label4.Caption = Format(Time, "hh:mm:ss")
End Sub
Private Sub TimerBaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
strInput = MSComm2.Input
strPotong = strInput
If TotalBaca >= MAKSBACA Then
TimerBaca.Enabled = False
If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
Call berhenti_Click
End If
End Sub
g. VIDEO HASILNYA
No comments:
Post a Comment