Data sensor arduino
Sensor adalah instrumen atau komponen yang mampu mendeteksi perubahan kondisi objek dalam jangkauannya. Pengukuran besaran perubahan tersebut harus diubah dahulu menjadi bentuk digital sehingga dapat diproses oleh perangkat digital lainnya.
Data variabel ini bisa dimonitoring secara lokal melalui layar monitor maupun global melalui internet.
Hal yang mendukung keandalan sistem monitoring :
- realtime, yaitu data yang ditampilkan merupakan kondisi masa yang singkat, misalnya di perbarui setiap 1 detik.
- Data memiliki identitas seperti lokasi, waktu.
- Data yang diterima memiliki mekanisme penyaringan sehingga data yang ditampilkan terjamin.
Sensor detektor kebakaran
Terdapat beberapa Indikasi kebakaran yaitu :
- Api dideteksi dengan sensor flame
- Suhu dibaca dengan sensor suhu
- Asap dibaca dengan sensor asap (smoke detector)
Dalam hal pencegahan kebakaran, ketiga variabel ini terus menerus dimonitoring secara lokal.
Monitoring detektor kebakaran ini secara global juga diperlukan untuk memberikan informasi kepada pihak terkait.
Dalam contoh ini ketiga (modul) sensor ini digunakan bersama arduino dan akan dikirimkan ke mysql server.
Akses informasi sensor dengan codeIgniter
Informasi data saat ini dapat dengan mudah diakses dimanapun, namun dengan menggunakan codeIgniter diperoleh beberapa keuntungan yaitu :
- Pengembangan lebih mudah dengan baris program yang dapat disederhanakan.
- Aksesibilitas dapat dengan mudah dikontrol.
- Perlindungan terhadap server terutama server data lebih terjamin.
Skema Monitoring detektor kebakaran dengan codeIgniter
Komponen yang digunakan :
- Arduino Uno
- ESP8266
- Flame detector
- Sensor asap MQ7
- Sensor suhu dht11
- Relay
- Buzzer
Sketch / koding monitoring detektor kebakaran esp8266 – WebServer.
#include "WiFiEsp.h"
#include <SoftwareSerial.h>
#include "DHT.h"
char ssid[] = "****"; // Isi dengan nama profil Wifi
char pass[] = "********"; // password wifi
char server[] = "x.x.x.x";
long waktuMintaData = 1000; //minta data setiap 1000ms
#define pinFlame A0
#define pinMQ A1
#define pinDHT A2
#define pinBuzzer 8
#define pinRelay 9
float setSuhu = 31.0;
String Respon = "";
long waktuMulai;
bool responDariServer = false;
bool prosesKirimDataKeServer = false;
WiFiEspClient client;
int status = WL_IDLE_STATUS;
SoftwareSerial wifi(2,3);
DHT dht(pinDHT, DHT11);
void setup()
{
pinMode(pinFlame, INPUT_PULLUP);
pinMode(pinMQ, INPUT_PULLUP);
pinMode(pinDHT, INPUT_PULLUP);
pinMode(pinBuzzer, OUTPUT);
digitalWrite(pinRelay, HIGH);
pinMode(pinRelay, OUTPUT);
Serial.begin(115200);
wifi.begin(115200);
WiFi.init(&wifi);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
dht.begin();
waktuMulai = millis();
}
void loop()
{
float suhu = dht.readTemperature();
//print status
Serial.println();
Serial.print("Api = ");
Serial.println(digitalRead(pinFlame));
Serial.print("Asap = ");
Serial.println(digitalRead(pinMQ));
Serial.print("suhu = ");
Serial.println(suhu);
if(!digitalRead(pinFlame) && !digitalRead(pinMQ) && (suhu < setSuhu))
{
digitalWrite(pinRelay, HIGH);
digitalWrite(pinBuzzer, LOW);
}
if(digitalRead(pinFlame))
{
digitalWrite(pinRelay, LOW);
digitalWrite(pinBuzzer, HIGH);
}
if(digitalRead(pinMQ))
{
digitalWrite(pinRelay, LOW);
digitalWrite(pinBuzzer, HIGH);
}
if(suhu >= setSuhu)
{
digitalWrite(pinBuzzer, HIGH);
}
//kirim data
if(waktuMintaData < millis() - waktuMulai)
{
waktuMulai = millis();
prosesKirimDataKeServer = kirimDataKeServer();
}
while (client.available())
{
char c = client.read();
Respon += c;
}
Serial.print("prosesKirimDataKeServer = ");
Serial.println(prosesKirimDataKeServer);
if (!client.connected() && prosesKirimDataKeServer) {
Serial.println("Disconnecting from server...");
client.stop();
responDariServer = true;
prosesKirimDataKeServer = false;
}
// penanganan data yang diterima dari server
if(responDariServer)
{
responDariServer = false;
Serial.println(Respon);
int posisiData = Respon.indexOf("\r\n\r\n");
String Data = Respon.substring(posisiData+4);
Data.trim();
String variabel;
String nilai;
Serial.println("Data dari server");
posisiData = Data.indexOf('=');
if(posisiData > 0)
{
variabel = Data.substring(0,posisiData);
nilai = Data.substring(posisiData+1);
//===========Penanganan respon disini
if(variabel == "setSuhu")
{
setSuhu = nilai.toFloat();
}
// Serial.print(variabel);
// Serial.print(" = ");
// Serial.println(nilai);
}
Respon = "";
}
}
bool kirimDataKeServer()
{
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connect(server, 80)) {
Serial.println("Connected to server");
// Make a HTTP request
client.print("GET /index.php/databaseArduino/dariArduino");
client.print("?Api=");
client.print(digitalRead(pinFlame));
client.print("&Asap=");
client.print(digitalRead(pinMQ));
client.print("&Suhu=");
client.print(dht.readTemperature());
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
return true;
}
return false;
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
IPAddress gateway = WiFi.gatewayIP();
Serial.print("gateway:");
Serial.print(gateway);
Serial.println(" ");
}
program codeIgniter untuk monitoring data sensor arduino:
databaseArduino.php
<?php
class databaseArduino extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function dariBrowser() {
$this->load->model('Model_databaseArduino');
$this->Model_databaseArduino->salinDataDariBrowser();
$data['dataSensor'] = $this->Model_databaseArduino->ambilDataDariArduino();
$data['dataParameter'] = $this->Model_databaseArduino->ambilDataDariBrowser();
$this->load->view("data_sensor", $data);
}
public function dariArduino() {
$this->load->model('Model_databaseArduino');
$this->Model_databaseArduino->salinDataDariArduino();
$data['dataParameter'] = $this->Model_databaseArduino->ambilDataDariBrowser();
$this->load->view("data_parameter", $data);
}
}
?>
Model_databaseArduino.php
<?php
class Model_databaseArduino extends CI_Model {
public $title;
public $content;
public $date;
public function ambilDataDariArduino()
{
$this->load->database();
$query = $this->db->query("SELECT * FROM (
SELECT * FROM `data_sensor` ORDER BY `nomor` DESC LIMIT 10
) sub
ORDER BY `nomor` ASC");
$this->db->close();
return $query->result();
}
public function salinDataDariArduino()
{
date_default_timezone_set('Asia/Jakarta'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');
$this->load->database();
$this->db->set('waktu', $now);
$this->db->set('api', $this->input->get('Api'));
$this->db->set('asap', $this->input->get('Asap'));
$this->db->set('suhu', $this->input->get('Suhu'));
$this->db->insert('data_sensor');
$this->db->close();
}
public function ambilDataDariBrowser()
{
$this->load->database();
$query = $this->db->query("SELECT * FROM `data_parameter` ORDER BY `nomor` DESC LIMIT 1");
$this->db->close();
return $query->row();
}
public function salinDataDariBrowser()
{
date_default_timezone_set('Asia/Jakarta'); # add your city to set local time zone
$now = date('Y-m-d H:i:s');
$this->load->database();
$this->db->set('waktu', $now);
$this->db->set('setSuhu', $this->input->post('setSuhu'));
$this->db->insert('data_parameter');
$this->db->close();
}
}
?>
data_sensor.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
if(isset($dataParameter))
{
echo "setSuhu=";
echo $dataParameter->setSuhu;
}
?>
data_parameter.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
<style type="text/css">
::selection { background-color: #E13300; color: white; }
::-moz-selection { background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body {
margin: 0 15px 0 15px;
}
p.footer {
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}
#container {
margin: 10px;
border: 1px solid #D0D0D0;
box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<!--
<form action="/form/data_submitted" method="get">
User Name: <input type="text" name="u_name" placeholder="Please Enter User Name" class="input_box">
<br>
User email: <input type="text" name="u_email" placeholder="Please Enter Email Address" class="input_box">
<input type="submit" value="Submit" class="submit">
</form>
-->
<?php
// echo $this->input->post('setSuhu');
echo form_open('databaseArduino/dariBrowser');
if(isset($dataParameter->setSuhu))
{
echo form_input('setSuhu', $dataParameter->setSuhu);
}
else
{
echo form_input('setSuhu', '30.0');
}
echo form_submit('suhuSubmit', 'Set Temperatur');
echo form_close();
echo "<br>";
echo "<strong>Data pembacaan sensor</strong>";
echo "<br>";
echo "<table>";
echo "<tr>";
echo "<td width='50'>Nomor</td>";
echo "<td width='200'>Waktu</td>";
echo "<td width='50'>Api</td>";
echo "<td width='50'>Asap</td>";
echo "<td width='50'>Suhu</td>";
echo "</tr>";
foreach ($dataSensor as $row)
{
echo "<tr>";
echo "<td>".$row->nomor."</td>";
echo "<td>".$row->waktu."</td>";
echo "<td>".$row->api."</td>";
echo "<td>".$row->asap."</td>";
echo "<td>".$row->suhu."</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
file server : htdocs.zip