Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang mana alat ini digunakan untuk monitoring suhu dan kelembaban via ethernet web server namun servernya menggunakan LOCALHOST komputer jadi tidak menggunakan cloud atau wifi, alat ini menggunakan sensor DHT11 untuk sensing suhu dan kelembaban kemudian dikirim ke server LOCALHOST komputer, untuk lebih jelsnya berikut adalah program dan skemanya.
a. Arduino Uno + Ethernet Shield
b. Sensor DHT11
c. LCD 16x2
d. Program Arduino IDE tanpa LCD
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int y;
int currx;
String txData="";
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,99);
EthernetClient client;
void setup() {
dht.begin();
Serial.begin(9600);
while (!Serial) {
; //wait for serial port to connect. Needed for Leonardo only
}
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
// }
delay(1000);
Serial.println("connecting...");
kirim ();
}
void loop()
{
kirim ();
}
void kirim (){
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
int hum = h;
int suhu = t;
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
EthernetClient client;
if (client.connect("192.168.1.111", 80)){
txData = "hum="+ (String (hum)) + "&suhu="+ (String (suhu));
Serial.println("connected");
Serial.print(txData);
client.println("POST /monitoring/update.php HTTP/1.1");
//jika menggunakan insert pakai client.println("POST /antri/insert.php HTTP/1.1");
client.println("Host: 192,168,1,111");
client.println("Connection: close");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(txData.length());
client.print("\n\n");
client.print(txData);
Serial.println(txData);
delay (2000);
}
else{
Serial.println("Connection Failed.");
Serial.println();
delay (1500);
}
}
#include <Ethernet.h>
#include "DHT.h"
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
int y;
int currx;
String txData="";
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,99);
EthernetClient client;
void setup() {
dht.begin();
Serial.begin(9600);
while (!Serial) {
; //wait for serial port to connect. Needed for Leonardo only
}
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
// }
delay(1000);
Serial.println("connecting...");
kirim ();
}
void loop()
{
kirim ();
}
void kirim (){
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
int hum = h;
int suhu = t;
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
EthernetClient client;
if (client.connect("192.168.1.111", 80)){
txData = "hum="+ (String (hum)) + "&suhu="+ (String (suhu));
Serial.println("connected");
Serial.print(txData);
client.println("POST /monitoring/update.php HTTP/1.1");
//jika menggunakan insert pakai client.println("POST /antri/insert.php HTTP/1.1");
client.println("Host: 192,168,1,111");
client.println("Connection: close");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(txData.length());
client.print("\n\n");
client.print(txData);
Serial.println(txData);
delay (2000);
}
else{
Serial.println("Connection Failed.");
Serial.println();
delay (1500);
}
}
e. Program Arduino IDE + LCD
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#include <Wire.h> // i2C Conection Library
#include <LiquidCrystal_I2C.h> //i2C LCD Library
#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
int y;
int currx;
String txData="";
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,99);
EthernetClient client;
void setup() {
lcd.begin();
dht.begin();
Serial.begin(9600);
while (!Serial) {
; //wait for serial port to connect. Needed for Leonardo only
}
// if (Ethernet.begin(mac) == 0) {
// Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
// }
delay(1000);
Serial.println("connecting...");
kirim ();
}
void loop()
{
kirim ();
}
void kirim (){
float h = dht.readHumidity();
float t = dht.readTemperature();
float f = dht.readTemperature(true);
int hum = h;
int suhu = t;
lcd.setCursor(0,0);
lcd.print("Temp= ");
lcd.print(suhu);
lcd.print(" C ");
lcd.setCursor(0,1);
lcd.print("Hum= ");
lcd.print(hum);
lcd.print(" % ");
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(f, h);
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C ");
EthernetClient client;
if (client.connect("192.168.1.111", 80)){
txData = "hum="+ (String (hum)) + "&suhu="+ (String (suhu));
Serial.println("connected");
Serial.print(txData);
client.println("POST /monitoring/update.php HTTP/1.1");
//jika menggunakan insert pakai client.println("POST /antri/insert.php HTTP/1.1");
client.println("Host: 192,168,1,111");
client.println("Connection: close");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(txData.length());
client.print("\n\n");
client.print(txData);
Serial.println(txData);
delay (2000);
}
else{
Serial.println("Connection Failed.");
Serial.println();
delay (1500);
}
}
f. Cara Setting Komputer
1. Aktifkan XAMPP
2. Buat tiga buah file PHP berikut
3. Buat database phpmyadmin
nama database = "monitordht"
nama tabel = "tbmonitor"
4. Setting IP LAN
g. Program PHP
1. config.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
//$dbpass = '';
$dbname = 'monitordht'; //This is the name of your database on the mysql server
$conn = mysql_connect($dbhost,$dbuser) or die ('Error connecting to mysql');
mysql_select_db($dbname);
if(!$conn){
// echo "database gak konek";
}else{
// echo "database konek";
}
?>
$dbhost = 'localhost';
$dbuser = 'root';
//$dbpass = '';
$dbname = 'monitordht'; //This is the name of your database on the mysql server
$conn = mysql_connect($dbhost,$dbuser) or die ('Error connecting to mysql');
mysql_select_db($dbname);
if(!$conn){
// echo "database gak konek";
}else{
// echo "database konek";
}
?>
2. index.php
<html>
<head>
<meta http-equiv="refresh" content="3">
<style>
.bordered { border-style:solid; }
</style>
<link rel="stylesheet" type="text/css" href="latweb.css" />
<title >MONITORING</title>
</head>
<body>
<CENTER>
<p><h2> MONITORING SUHU DAN KELEMBABAN</h2></p>
</CENTER>
<CENTER>
<p><h3>
KELEMBABAN =
<?php
include "config.php";
$tampil = @mysql_query("SELECT * FROM tbmonitor WHERE no IN (SELECT MAX(no) FROM tbmonitor)");
while ($r=mysql_fetch_array($tampil)){
echo "$r[hum]";
}
?>
%
<br>
SUHU =
<?php
include "config.php";
$tampil = @mysql_query("SELECT * FROM tbmonitor WHERE no IN (SELECT MAX(no) FROM tbmonitor)");
while ($r=mysql_fetch_array($tampil)){
echo "$r[suhu]";
}
?>
C
</CENTER>
</body>
</html>
<head>
<meta http-equiv="refresh" content="3">
<style>
.bordered { border-style:solid; }
</style>
<link rel="stylesheet" type="text/css" href="latweb.css" />
<title >MONITORING</title>
</head>
<body>
<CENTER>
<p><h2> MONITORING SUHU DAN KELEMBABAN</h2></p>
</CENTER>
<CENTER>
<p><h3>
KELEMBABAN =
<?php
include "config.php";
$tampil = @mysql_query("SELECT * FROM tbmonitor WHERE no IN (SELECT MAX(no) FROM tbmonitor)");
while ($r=mysql_fetch_array($tampil)){
echo "$r[hum]";
}
?>
%
<br>
SUHU =
<?php
include "config.php";
$tampil = @mysql_query("SELECT * FROM tbmonitor WHERE no IN (SELECT MAX(no) FROM tbmonitor)");
while ($r=mysql_fetch_array($tampil)){
echo "$r[suhu]";
}
?>
C
</CENTER>
</body>
</html>
3. update.php
<?php
require("config.php");
$query = "UPDATE tbmonitor SET hum='$_POST[hum]', suhu='$_POST[suhu]' WHERE no=1";
if(!@mysql_query($query))
{
echo "&Answer; SQL Error - ".mysql_error();
return;
}
?>
require("config.php");
$query = "UPDATE tbmonitor SET hum='$_POST[hum]', suhu='$_POST[suhu]' WHERE no=1";
if(!@mysql_query($query))
{
echo "&Answer; SQL Error - ".mysql_error();
return;
}
?>
h. Tampilan Server
i. VIDEO HASILNYA
No comments:
Post a Comment