Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang berfungsi untuk monitoring suhu dan kelembaban dengan sensor dht22 via sms gateway, jadi alat ini akan mengirimkan sms setiap 1 jam sekali ke handphone yang dituju, alat ini dilengkapi dengan RTC sehingga untuk waktu bisa lebih akurat. untuk lebih jelasnya berikut adalah daftar komponen dan programnya.
a. Arduino Uno
b. Modul SMS SIM900A
c. RTC DS3231
d. Sensor DHT22
e. LCD + I2C
f. Program Arduino IDE
#include "Wire.h"
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
#define DHTPIN 8 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22
#define DS3231_I2C_ADDRESS 0x68
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
byte savejam;
int mark = 0;
char string[160];
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
int tanda = 0;
int tanda2 = 0;
void setup() {
lcd.begin();
lcd.clear();
lcd.noCursor();
lcd.setCursor(0,0);
lcd.print("LOADING......");
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
if(started) {
if (sms.SendSMS("085123123123", "Alat Ready"))
Serial.println("\nSMS sent OK");
}
dht.begin();
Serial.begin(9600);
while (!Serial) {
}
lcd.clear();
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,59,5,6,23,11,18);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
lcd.setCursor(0,1);
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void loop(){
displayTime();
int h = dht.readHumidity();
int t = dht.readTemperature();
int f = dht.readTemperature(true);
int hum = h ;
int suhu = t;
sprintf(string,"suhu = %d dan kelembaban = %d ", t , h);
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);
lcd.setCursor(0,0);
lcd.print("s/h=");
lcd.print(suhu);
lcd.print("/");
lcd.print(hum);
lcd.print(" ");
if(hour != savejam){
Serial.println("KIRIM SMS");
if (sms.SendSMS("085123123123", string));
savejam = hour;
}
if(suhu > 60){
if (sms.SendSMS("085123123123", "TANDA BAHAYA SUHU > 60"))
Serial.println("\nSMS sent OK");
}
if(hum < 15){
if (sms.SendSMS("085123123123", "TANDA BAHAYA KELEMBABAN < 15"))
Serial.println("\nSMS sent OK");
}
delay(1000);
}
#include <LiquidCrystal_I2C.h>
#include "DHT.h"
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;
#define DHTPIN 8 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22
#define DS3231_I2C_ADDRESS 0x68
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}
byte savejam;
int mark = 0;
char string[160];
int numdata;
boolean started=false;
char smsbuffer[160];
char n[20];
int tanda = 0;
int tanda2 = 0;
void setup() {
lcd.begin();
lcd.clear();
lcd.noCursor();
lcd.setCursor(0,0);
lcd.print("LOADING......");
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(2400)) {
Serial.println("\nstatus=READY");
started=true;
} else Serial.println("\nstatus=IDLE");
if(started) {
if (sms.SendSMS("085123123123", "Alat Ready"))
Serial.println("\nSMS sent OK");
}
dht.begin();
Serial.begin(9600);
while (!Serial) {
}
lcd.clear();
Wire.begin();
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
setDS3231time(0,59,5,6,23,11,18);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime()
{
lcd.setCursor(0,1);
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
// send it to the serial monitor
lcd.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
lcd.print(":");
if (minute<10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second<10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.print(" ");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(year, DEC);
Serial.print(" Day of week: ");
switch(dayOfWeek){
case 1:
Serial.println("Sunday");
break;
case 2:
Serial.println("Monday");
break;
case 3:
Serial.println("Tuesday");
break;
case 4:
Serial.println("Wednesday");
break;
case 5:
Serial.println("Thursday");
break;
case 6:
Serial.println("Friday");
break;
case 7:
Serial.println("Saturday");
break;
}
}
void loop(){
displayTime();
int h = dht.readHumidity();
int t = dht.readTemperature();
int f = dht.readTemperature(true);
int hum = h ;
int suhu = t;
sprintf(string,"suhu = %d dan kelembaban = %d ", t , h);
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);
lcd.setCursor(0,0);
lcd.print("s/h=");
lcd.print(suhu);
lcd.print("/");
lcd.print(hum);
lcd.print(" ");
if(hour != savejam){
Serial.println("KIRIM SMS");
if (sms.SendSMS("085123123123", string));
savejam = hour;
}
if(suhu > 60){
if (sms.SendSMS("085123123123", "TANDA BAHAYA SUHU > 60"))
Serial.println("\nSMS sent OK");
}
if(hum < 15){
if (sms.SendSMS("085123123123", "TANDA BAHAYA KELEMBABAN < 15"))
Serial.println("\nSMS sent OK");
}
delay(1000);
}
g. VIDEO HASILNYA