Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang berfungsi untuk memonitor tegangan dari potensio slider dan kelembaban tanah / soil moisture dengan menggunakan Arduino. alat ini terdapat bagian master dan bagian slave jadi butuh 2 arduino sebagai master dan slave. komunikasi master - slave menggunakan wireless modul HC-12 dan menggunakan parsing data serial uart untuk memilah dua data yang dikirim oleh master kepada slave. untuk lebih jelasnya berikut adalah komponen dan programnya.
a. Arduino Uno
b. Sensor Kelembaban Tanah / Soil Moisture
c. Potensio Slider
d. Modul Wireless HC-12
f. Program Arduino IDE Master
#include <Wire.h>
int dataadc1;
int dataadc2;
int maxval = 1023;
int minval = 0;
int minvalx = 0;
int maxvalx = 100;
int rawValue;
void setup() {
Serial.begin(9600);
}
void loop() {
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
int panjang = map(dataadc2, maxval, minval, minvalx, maxvalx);
int dataadc1;
int dataadc2;
int maxval = 1023;
int minval = 0;
int minvalx = 0;
int maxvalx = 100;
int rawValue;
void setup() {
Serial.begin(9600);
}
void loop() {
dataadc1 = analogRead(A0);
dataadc2 = analogRead(A1);
int panjang = map(dataadc2, maxval, minval, minvalx, maxvalx);
//secara wireless
Serial.print("*");
Serial.print(dataadc1);
Serial.print(",");
Serial.print(panjang);
Serial.println("#");
Serial.print("*");
Serial.print(dataadc1);
Serial.print(",");
Serial.print(panjang);
Serial.println("#");
//pakai kabel
//Serial.print(dataadc1);
//Serial.print("\n");
//Serial.print(panjang);
//Serial.print("\n");
delay(1000);
}
//Serial.print(dataadc1);
//Serial.print("\n");
//Serial.print(panjang);
//Serial.print("\n");
delay(1000);
}
g. Program Arduino IDE Slave
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x26, 16, 2);
LiquidCrystal_I2C lcd2(0x3D, 16, 2);
LiquidCrystal_I2C lcd3(0x3F, 16, 2);
int value1;
int value2;
int hum = 0;
int miring = 0;
int datain1;
int datain2;
String dataIn;
String dt[10];
int i;
boolean parsing=false;
void setup(){
dataIn="";
lcd1.begin();
lcd1.clear();
lcd1.noCursor();
lcd2.begin();
lcd2.clear();
lcd2.noCursor();
lcd3.begin();
lcd3.clear();
lcd3.noCursor();
Serial.begin(9600);
}
void loop(){
//secara wireless
if(Serial.available()>0) {
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
hum = datain1;
miring = datain2;
Serial.print(hum),Serial.println ('a');
Serial.print(miring),Serial.println ('b');
lcd1.setCursor(0,0);
lcd1.print("Hum: ");
lcd1.print(hum);
lcd1.print(" ");
lcd2.setCursor(0,0);
lcd2.print("Std: ");
lcd2.print(miring);
lcd2.print(" ");
if((miring >= 99)&&(hum > 476)&&(hum < 1023)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Aman ");
}
if((miring >= 96)&&(miring <= 98)&&(hum > 340)&&(hum < 475)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Siaga ");
}
if((miring < 96)&&(hum < 339)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Bahaya ");
}
parsing=false;
dataIn="";
}
/* pakai kabel
while(Serial.available()>0)
{
value1 = Serial.parseInt();
value2 = Serial.parseInt();
hum = value1;
miring = value2;
if(Serial.read()=='\n')
{
Serial.print(hum),Serial.println ('a');
Serial.print(miring),Serial.println ('b');
lcd1.setCursor(0,0);
lcd1.print("Hum: ");
lcd1.print(hum);
lcd1.print(" ");
lcd2.setCursor(0,0);
lcd2.print("Std: ");
lcd2.print(miring);
lcd2.print(" ");
if((miring >= 99)&&(hum > 476)&&(hum < 1023)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Aman ");
}
if((miring >= 96)&&(miring <= 98)&&(hum > 340)&&(hum < 475)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Siaga ");
}
if((miring < 96)&&(hum < 339)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Bahaya ");
}
}
delay(1000);
}
*/
}
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();
//kirim data hasil parsing
//Serial.print("data 1 : ");
//Serial.print(datain1);
//Serial.print("\n");
//Serial.print("data 2 : ");
//Serial.print(datain2);
//Serial.print("\n");
//Serial.print("data 3 : ");
//Serial.print(dt[2].toInt());
//Serial.print("\n\n");
}
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd1(0x26, 16, 2);
LiquidCrystal_I2C lcd2(0x3D, 16, 2);
LiquidCrystal_I2C lcd3(0x3F, 16, 2);
int value1;
int value2;
int hum = 0;
int miring = 0;
int datain1;
int datain2;
String dataIn;
String dt[10];
int i;
boolean parsing=false;
void setup(){
dataIn="";
lcd1.begin();
lcd1.clear();
lcd1.noCursor();
lcd2.begin();
lcd2.clear();
lcd2.noCursor();
lcd3.begin();
lcd3.clear();
lcd3.noCursor();
Serial.begin(9600);
}
void loop(){
//secara wireless
if(Serial.available()>0) {
char inChar = (char)Serial.read();
dataIn += inChar;
if (inChar == '\n') {
parsing = true;
}
}
if(parsing){
parsingData();
hum = datain1;
miring = datain2;
Serial.print(hum),Serial.println ('a');
Serial.print(miring),Serial.println ('b');
lcd1.setCursor(0,0);
lcd1.print("Hum: ");
lcd1.print(hum);
lcd1.print(" ");
lcd2.setCursor(0,0);
lcd2.print("Std: ");
lcd2.print(miring);
lcd2.print(" ");
if((miring >= 99)&&(hum > 476)&&(hum < 1023)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Aman ");
}
if((miring >= 96)&&(miring <= 98)&&(hum > 340)&&(hum < 475)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Siaga ");
}
if((miring < 96)&&(hum < 339)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Bahaya ");
}
parsing=false;
dataIn="";
}
/* pakai kabel
while(Serial.available()>0)
{
value1 = Serial.parseInt();
value2 = Serial.parseInt();
hum = value1;
miring = value2;
if(Serial.read()=='\n')
{
Serial.print(hum),Serial.println ('a');
Serial.print(miring),Serial.println ('b');
lcd1.setCursor(0,0);
lcd1.print("Hum: ");
lcd1.print(hum);
lcd1.print(" ");
lcd2.setCursor(0,0);
lcd2.print("Std: ");
lcd2.print(miring);
lcd2.print(" ");
if((miring >= 99)&&(hum > 476)&&(hum < 1023)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Aman ");
}
if((miring >= 96)&&(miring <= 98)&&(hum > 340)&&(hum < 475)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Siaga ");
}
if((miring < 96)&&(hum < 339)){
lcd3.setCursor(0,0);
lcd3.print("Kondisi Bahaya ");
}
}
delay(1000);
}
*/
}
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();
//kirim data hasil parsing
//Serial.print("data 1 : ");
//Serial.print(datain1);
//Serial.print("\n");
//Serial.print("data 2 : ");
//Serial.print(datain2);
//Serial.print("\n");
//Serial.print("data 3 : ");
//Serial.print(dt[2].toInt());
//Serial.print("\n\n");
}
h. Program Interface 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;
Label3: TLabel;
Label4: TLabel;
BitBtn1: TBitBtn;
Series1: TFastLineSeries;
Series2: TFastLineSeries;
Timer2: TTimer;
Label5: TLabel;
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 Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
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;
timer2.Enabled := True;
end
else if button2.Caption='Disconnect' then
begin
button2.Caption:='Connect';
comport1.Close;
timer2.Enabled := False;
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
close;
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,'',clred) ;
shape1.Brush.Color:=clred;
end;
if rightstr(kin,1)='b' then
begin
edit2.Text:=leftstr(kin,length(kin)-1);
kulit:=strtofloat(edit2.text);
chart1.Series[1].Add(kulit,'',clgreen) ;
shape2.Brush.Color:=clred;
end;
if (kulit >= 99) and (ruang > 476) and (ruang < 1023) then
begin
Label5.Caption := 'AMAN' ;
end;
if (kulit >= 96) and (kulit <= 98) and (ruang > 340) and (ruang < 475) then
begin
Label5.Caption := 'SIAGA';
end;
if (kulit < 96) and (ruang < 339) then
begin
Label5.Caption := 'BAHAYA';
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtofloat(edit1.text);
if ruang<30 then
begin
edit1.Color:=clblue;
edit3.text:= 'Lembab';
end;
if ruang>31 then
begin
edit1.Color:=clred;
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:=clblue;
edit4.text:= 'Kering';
end;
if kulit>61 then
begin
edit2.Color:=clred;
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.Timer2Timer(Sender: TObject);
var
buff:string;
Count:Integer;
begin
comport1.ReadStr(buff,count);
memo1.Text:=memo1.Text+buff;
end;
end.
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;
Label3: TLabel;
Label4: TLabel;
BitBtn1: TBitBtn;
Series1: TFastLineSeries;
Series2: TFastLineSeries;
Timer2: TTimer;
Label5: TLabel;
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 Timer2Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
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;
timer2.Enabled := True;
end
else if button2.Caption='Disconnect' then
begin
button2.Caption:='Connect';
comport1.Close;
timer2.Enabled := False;
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
close;
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,'',clred) ;
shape1.Brush.Color:=clred;
end;
if rightstr(kin,1)='b' then
begin
edit2.Text:=leftstr(kin,length(kin)-1);
kulit:=strtofloat(edit2.text);
chart1.Series[1].Add(kulit,'',clgreen) ;
shape2.Brush.Color:=clred;
end;
if (kulit >= 99) and (ruang > 476) and (ruang < 1023) then
begin
Label5.Caption := 'AMAN' ;
end;
if (kulit >= 96) and (kulit <= 98) and (ruang > 340) and (ruang < 475) then
begin
Label5.Caption := 'SIAGA';
end;
if (kulit < 96) and (ruang < 339) then
begin
Label5.Caption := 'BAHAYA';
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var ruang:double;
begin
ruang:=strtofloat(edit1.text);
if ruang<30 then
begin
edit1.Color:=clblue;
edit3.text:= 'Lembab';
end;
if ruang>31 then
begin
edit1.Color:=clred;
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:=clblue;
edit4.text:= 'Kering';
end;
if kulit>61 then
begin
edit2.Color:=clred;
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.Timer2Timer(Sender: TObject);
var
buff:string;
Count:Integer;
begin
comport1.ReadStr(buff,count);
memo1.Text:=memo1.Text+buff;
end;
end.
i. VIDEO HASILNYA
No comments:
Post a Comment