Translate

Arduino Visual Basic 6.0 (VB 6) Control dan Monitor Suhu DHT22 Interface + Database Ms. Access

Arduino Visual Basic 6.0 (VB 6) Control dan Monitor Suhu DHT22 Interface + Database Ms. Access
 

          Pada kesempatan kali ini saya akan menjelaskan mengenai bagimana cara membuat sebuah alat yang dapat memonitor suhu udara dan kendali 2 buah LED yang mana tiap LED tersebut mewakili motor DC dan Heater. untuk lebih jelasnya berikut adalah koding dan komponennya.
 
 
a. Komponen ver 1


b. Program Arduino ver 1

#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

int msg[1];
int datain;
int motor1 = 6;
int heater1 = 7;
int suhu;

#define DHTPIN 8     
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  dht.begin();
  pinMode(motor1,OUTPUT);
  pinMode(heater1,OUTPUT);
  digitalWrite(motor1,LOW);
  digitalWrite(heater1,LOW);
}

void loop() {

int t = dht.readTemperature();

if (Serial.available() > 0)
{

datain = Serial.parseInt();
lcd.setCursor(0,0);
lcd.print(datain);

if(datain > 10){
suhu = datain;
}

//Serial.println(datain);
if(datain == 1){
  digitalWrite(motor1,LOW);
  }

if(datain == 2){
  digitalWrite(motor1,HIGH);
  }

if(datain == 3){
  digitalWrite(heater1,LOW);
  }

if(datain == 4){
  digitalWrite(heater1,HIGH);
}
}

if(t > suhu){
  digitalWrite(motor1,HIGH);
  digitalWrite(heater1,HIGH);
}

  Serial.println(t);
  delay(1000);
}


c. Program Arduino ver 2

#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

int msg[1];
int datain;
int motor1 = 4;
int motor2 = 5;
int heater1 = 6;
int heater2 = 7;
int suhu;
int speed1 = 10;
int speed2 = 9;

#define DHTPIN 8     
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  dht.begin();
  pinMode(motor1,OUTPUT);
  pinMode(motor2,OUTPUT);
  pinMode(heater1,OUTPUT);
  pinMode(heater2,OUTPUT);
  pinMode(speed1,OUTPUT);
  pinMode(speed2,OUTPUT);
  digitalWrite(motor1,LOW);
  digitalWrite(motor2,LOW);
  digitalWrite(heater1,LOW);
  digitalWrite(heater2,LOW);
}

void loop() {

int t = dht.readTemperature();

if (Serial.available() > 0)
{

datain = Serial.parseInt();
lcd.setCursor(0,0);
lcd.print(datain);

if(datain > 10){
suhu = datain;
}

//Serial.println(datain);
if(datain == 1){
   analogWrite(speed1,200);
   digitalWrite(motor1,HIGH);
   digitalWrite(motor2,LOW);
  }

if(datain == 2){
   digitalWrite(motor1,LOW);
   digitalWrite(motor2,LOW);
  }

if(datain == 3){
   analogWrite(speed2,200);
   digitalWrite(heater1,HIGH);
   digitalWrite(heater2,LOW);
  }

if(datain == 4){
   digitalWrite(heater1,LOW);
   digitalWrite(heater2,LOW);
}
}

if(t > suhu){
   digitalWrite(heater1,LOW);
   digitalWrite(heater2,LOW);
   digitalWrite(motor1,LOW);
   digitalWrite(motor2,LOW);
}

  Serial.println(t);
  delay(1000);
}


d. Interface Visual Basic 6

1. Module1.bas

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\ADMIN B106\Desktop\interface\Database1.mdb;Persist Security Info=False"
End Function


2. TA.frm

Option Explicit
Dim arrdata()
Dim TotalBaca As Integer
Dim BMI As Single
Dim Keterangan As String
Const MAKSBACA = 10

Dim Conn1 As ADODB.Connection
Dim Cmd1 As ADODB.Command
Dim Param1 As ADODB.Parameter
Dim Rs1 As ADODB.Recordset

Private Sub CBHEATER_Click()
MSComm2.Output = "2" + Chr(13)  'mengirimkan b ke serial
Shape2.FillColor = vbBlack
End Sub

Private Sub cbinsert_Click()
Dim SQl As String
    
   MSComm2.Output = tbtemp.Text + Chr(13)
   
   Call dbConnect
    
    SQl = "INSERT INTO Table1 VALUES('" & tbtagno.Text & "','" & tbdescription.Text & "','" & tbarea.Text & "','" & tbtemp.Text & "','" & tbspeed.Text & "','" & tbtimer.Text & "')"
    
    Dim Rs As Recordset
    Set Rs = New ADODB.Recordset
    Rs.Open SQl, Conn, adOpenDynamic
    'tbtagno.Text = ""
    'tbdescription.Text = ""
    'tbarea.Text = ""
    'tbtemp.Text = ""
    'tbspeed.Text = ""
    'tbtimer.Text = ""
    
    
End Sub

Private Sub CBOFF_Click()
MSComm2.Output = "4" + Chr(13)  'mengirimkan d ke serial
Shape2.FillColor = vbBlack
End Sub

Private Sub CBSTART_Click()
MSComm2.Output = "1" + Chr(13)  'mengirimkan a ke serial
Shape2.FillColor = vbGreen
End Sub

Private Sub CBSTOP_Click()
MSComm2.Output = "3" + Chr(13)  'mengirimkan c ke serial
Shape1.FillColor = vbGreen
End Sub

Private Sub cmdConnect_Click()
Dim port As Integer
On Error GoTo errcode

If MSComm2.PortOpen = False Then
    MSComm2.CommPort = Combo1.ListIndex + 1
    MSComm2.RThreshold = 1
    MSComm2.InputLen = 0
    MSComm2.Settings = Combo2.List(Combo2.ListIndex) & ",N,8,1"
    MSComm2.PortOpen = True
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
Timerbaca.Enabled = True
End If
 
Exit Sub

errcode:
    MsgBox "Port Salah !", vbOKOnly, "Peringatan"
    Combo1.SetFocus

End Sub

Private Sub cmdDisconnect_Click()
If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False
End If
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False

End Sub

Private Sub Command1_Click()
Unload Me
End Sub

Private Sub Form_Load()
Dim i As Byte
For i = 1 To 16
Combo1.AddItem (i)
Next i
With Combo2
        .AddItem "2400"
        .AddItem "4800"
        .AddItem "9600"
        .AddItem "19200"
        .AddItem "38400"
        .AddItem "56600"
        
End With
Timer2.Enabled = False
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
End Sub

Private Sub Timer1_Timer()
Adodc1.Refresh
End Sub

Private Sub Timer2_Timer()
MSComm2_OnComm
End Sub

Private Sub Timerbaca_Timer()
Dim strInput As String
Dim strPotong As String
Dim singleInput As Single
Dim u As Integer
Dim suhu As Integer
Dim batassuhu As Integer

    strInput = MSComm2.Input
        strPotong = strInput
        Text1.Text = Text1.Text + strPotong + vbCrLf
        singleInput = Val(strPotong)
        Text2.Text = singleInput
        If TotalBaca >= MAKSBACA Then
            Timerbaca.Enabled = False
            If MSComm2.PortOpen = True Then MSComm2.PortOpen = False
            Call cmdDisconnect_Click
        End If
        
        suhu = CInt(Text2.Text)
        batassuhu = CInt(tbtemp.Text)
        
        If suhu >= batassuhu Then
        Shape1.FillColor = vbRed
        Shape2.FillColor = vbRed
        End If
               
End Sub


e. VIDEO HASILNYA


No comments:

Post a Comment