Monitor Suhu dan Tekanan Udara Interface Delphi 7 dan Kendali Relay Wireless HC-12 (Sensor suhu DS18B20) Arduino
Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang menggunakan sistem wireless untuk monitor suhu dan tekanan udara dan juga kendali relay via Delphi 7. alat ini menggunakan sensor DS18b20 dan Arduino. untuk lebih jelasnya berikut adalah koding dan skemanya.
1. Skema Transmitter
2. Skema Receiver
3. Program Arduino Transmitter
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <LiquidCrystal_I2C.h> //i2C LCD Library
#define ONE_WIRE_BUS 6
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2);
int angka;
int relay1 = 4;
int relay2 = 5;
int suhu;
float pressure_pascal;
float pressure_bar;
int pressure_barx;
int dataadc;
int x;
float v;
float kpa;
float mpa;
void setup()
{
Serial.begin(9600);
sensors.begin();
pinMode(relay1,OUTPUT);
pinMode(relay2,OUTPUT);
lcd.begin();
lcd.clear();
lcd.noCursor();
}
void loop()
{
sensors.requestTemperatures();
suhu = sensors.getTempCByIndex(0);
x = analogRead(A0);
v = x*(5.0/1023.0);
pressure_pascal = (3.0*(v-0.47))*1000000.0;
pressure_bar = pressure_pascal/10e5;
pressure_barx = pressure_bar * 100.0;
lcd.setCursor(0,0);
lcd.print("Suhu: ");
lcd.print(suhu);
lcd.print(" C ");
lcd.setCursor(0,1);
lcd.print("Bar: ");
lcd.print(pressure_bar);
lcd.print(" ");
Serial.print("*");
Serial.print(suhu);
Serial.print(",");
Serial.print(pressure_barx);
Serial.println("#");
delay(200);
if (Serial.available() > 0) {
angka = Serial.parseInt();
lcd.setCursor(14,1);
lcd.print(angka);
if(angka == 1){
digitalWrite(relay1,HIGH);
}
if(angka == 2){
digitalWrite(relay1,LOW);
}
if(angka == 3){
digitalWrite(relay2,HIGH);
}
if(angka == 4){
digitalWrite(relay2,LOW);
}
}
}
4. Program Arduino Receiver
#include <Wire.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial HC12(10, 11);
int value1;
int value2;
int suhu = 0;
float bar = 0;
int datain1;
int datain2;
int barx;
char str;
int angka;
String dataIn;
String dt[10];
int i;
boolean parsing=false;
void setup(){
dataIn="";
lcd.begin();
lcd.clear();
lcd.noCursor();
Serial.begin(9600);
HC12.begin(9600);
}
void loop(){
lcd.setCursor(0,0);
lcd.print(suhu);
lcd.print("/");
lcd.print(bar);
lcd.print("/");
lcd.print(angka);
lcd.print(" ");
barx = bar;
Serial.print(suhu),Serial.println ('a');
Serial.print(barx),Serial.println ('b');
if(Serial.available()>0)
{
str = Serial.read();
if(str == '1')
{
angka = 1;
HC12.print(angka);
}
if(str == '2')
{
angka = 2;
HC12.print(angka);
}
if(str == '3')
{
angka = 3;
HC12.print(angka);
}
if(str == '4')
{
angka = 4;
HC12.print(angka);
}
}
delay(1000);
//secara wireless
if(HC12.available()>0) {
char inChar = (char)HC12.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
parsing=false;
dataIn="";
}
}
void parsingData(){
int j=0;
//kirim data yang telah diterima sebelumnya
//Serial.print("data masuk : ");
//Serial.print(dataIn);
//Serial.print("\n");
//inisialisasi variabel, (reset isi variabel)
dt[j]="";
//proses parsing data
for(i=1;i<dataIn.length();i++){
//pengecekan tiap karakter dengan karakter (#) dan (,)
if ((dataIn[i] == '#') || (dataIn[i] == ','))
{
//increment variabel j, digunakan untuk merubah index array penampung
j++;
dt[j]=""; //inisialisasi variabel array dt[j]
}
else
{
//proses tampung data saat pengecekan karakter selesai.
dt[j] = dt[j] + dataIn[i];
}
}
datain1 = dt[0].toInt();
datain2 = dt[1].toInt();
suhu = datain1;
bar = datain2 / 100.0;
}
5. Program Delphi 7
unit TA;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,strutils, TeeProcs, TeEngine, Chart, CPort, mmsystem,
Series, Buttons;
type
TForm1 = class(TForm)
ComPort1: TComPort;
GroupBox1: TGroupBox;
Edit1: TEdit;
Edit2: TEdit;
Chart1: TChart;
Shape1: TShape;
Shape2: TShape;
GroupBox2: TGroupBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
Timer1: TTimer;
Label7: TLabel;
Label8: TLabel;
GroupBox4: TGroupBox;
Edit3: TEdit;
Edit4: TEdit;
BitBtn1: TBitBtn;
Series1: TFastLineSeries;
Series2: TFastLineSeries;
Edit5: TEdit;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Memo1Change(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ComPort1RxChar(Sender: TObject; Count: Integer);
var buff:string;
begin
comport1.ReadStr(buff,count);
memo1.Text:=memo1.Text+buff;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
comport1.ShowSetupDialog;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if button2.Caption='Connect' then
begin
button2.Caption:='Disconnect';
comport1.Open;
end
else if button2.Caption='Disconnect' then
begin
button2.Caption:='Connect';
comport1.Close;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Edit1.Color:=clwhite;
Edit2.Color:=clwhite;
edit1.text:='';
edit2.text:='';
memo1.Text:='';
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
comport1.WriteStr('1');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ThousandSeparator:=',';
DecimalSeparator:='.';
end;
procedure TForm1.Memo1Change(Sender: TObject);
var ruang,kulit:double;
rin,kin:string;
jumlahdata:integer;
begin
shape1.Brush.Color:=clgreen;
shape2.Brush.Color:=clgreen;
jumlahdata:=chart1.GetMaxValuesCount;
form1.Caption:=inttostr(jumlahdata);
if jumlahdata =10000 then
begin
chart1.Series[0].Clear;
chart1.Series[1].Clear;
end;
rin:=memo1.Lines[memo1.Lines.count-2];
kin:=memo1.Lines[memo1.Lines.count-1];
if rightstr(rin,1)= 'a' then
begin
edit1.Text:=leftstr(rin,length(rin)-1);
ruang:=strtofloat(edit1.text);
chart1.Series[0].Add(ruang,'',clwhite) ;
shape1.Brush.Color:=clwhite;
end;
if rightstr(kin,1)='b' then
begin
edit2.Text:=leftstr(kin,length(kin)-1);
kulit:=strtofloat(edit2.text);
chart1.Series[1].Add(kulit,'',clwhite) ;
shape2.Brush.Color:=clwhite;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtofloat(edit1.text);
if ruang<30 then
begin
edit1.Color:=clwhite;
edit3.text:= 'Lembab';
end;
if ruang>31 then
begin
edit1.Color:=clwhite;
edit3.text:= 'Kering';
end;
if (ruang>=31) and (ruang<=30) then
begin
edit1.Color:=clwhite;
edit3.text:= 'Normal';
end;
end;
procedure TForm1.Edit2Change(Sender: TObject);
var kulit:double;
begin
kulit:=strtofloat(edit2.text);
if kulit<60 then
begin
edit2.Color:=clwhite;
edit4.text:= 'Kering';
end;
if kulit>61 then
begin
edit2.Color:=clwhite;
edit4.text:= 'Lembab';
end;
if (kulit>=61) and (kulit<=60) then
begin
edit2.Color:=clwhite;
edit4.text:= 'Normal';
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
label7.Caption:='Date '+ FormatdateTime('d/mm/yyyy',Date);
label8.Caption:='Time '+ FormatDateTime('hh:nn:ss',Time);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
comport1.WriteStr('2');
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
comport1.WriteStr('3');
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
comport1.WriteStr('4');
end;
end.
6. VIDEO HASILNYA