You are on page 1of 12

#include <LiquidCrystal.

h>

#include <DHT.h>

#include <Wire.h>

#include "RTClib.h"

//LCD Part

const int rs = 7, rw = 9, en = 8, d4 = 11, d5 = 10, d6 = 13, d7 = 12;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// DHT Part

DHT dht;

// RTC Part

RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",


"Saturday"};

int sensorPin1 = A0;

int sensorPin2 = A1;

int sensorValue1 = 0;

int sensorValue2 = 0;

const int buttonPin1 = 2;

const int buttonPin2 = 3;

const int buttonPin3 = 4;

const int buttonPin4 = 5;

String temp, temp1, temp2, temp3;

int tempInt, tempInt1;


int lowTemp = 30, lowHum = 100;

char phoneNumber[] = "081221574840";

int indexOk, indexEnd, indexTotal;

void setup() {

pinMode(rw, OUTPUT);

digitalWrite(rw, LOW);

dht.setup(6);

lcd.begin(16, 2);

pinMode(buttonPin1, INPUT_PULLUP);

pinMode(buttonPin2, INPUT_PULLUP);

pinMode(buttonPin3, INPUT_PULLUP);

pinMode(buttonPin4, INPUT_PULLUP);

#ifndef ESP8266

while (!Serial); // for Leonardo/Micro/Zero

#endif

if (! rtc.begin()) {

lcd.setCursor(0, 0);

lcd.print("Couldn't find RTC");

while (1);

if (rtc.lostPower()) {

lcd.clear();

lcd.setCursor(0, 0);
lcd.print("RTC lost power");

lcd.setCursor(0, 1);

lcd.print("lets set the time!");

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

// ------------------------------------------

// Main Program

void loop() {

//

// temp = ""; temp1 = ""; tempInt = 0; tempInt1 = 0;

// temp = getDayStr();

// temp1 = getTime();

// tempInt = getTemp();

// tempInt1 = getHum();

// sensorValue0 = analogRead(analogInPin0);

// sensorValue1 = analogRead(analogInPin1);

// lcd.clear();

// lcd.setCursor(0, 0);

// lcd.print(temp);

// lcd.setCursor(8, 0);

// lcd.print(temp1);
// lcd.setCursor(0, 0);

// lcd.print("Sens 1");

// lcd.setCursor(9, 0);

// lcd.print("Sens 2");

// lcd.setCursor(0, 1);

// lcd.print(millis() / 1000);

// lcd.setCursor(9, 1);

// lcd.print(sensorValue1);

// lcd.setCursor(8, 1);

// lcd.print(tempInt);

// lcd.setCursor(10, 1);

// lcd.print("C");

// lcd.setCursor(13, 1);

// lcd.print(tempInt1);

// lcd.setCursor(15, 1);

// lcd.print("%");

// delay(1);

// delay(2000);

// sendSms(true);

String returnData;

lcd.setCursor(0, 0);

Serial.println("AT");

while (Serial.available())

returnData.concat((char)Serial.read());

indexOk = returnData.indexOf("OK");

indexEnd = indexOk + 2;

if (returnData.substring(indexOk, indexEnd) == "OK") {


lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Modem Ready");

returnData = "";

delay(2000);

Serial.println("AT+CMGF=1");

while (Serial.available())

returnData.concat((char)Serial.read());

delay(500);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("SMS Mode : Text");

returnData = "";

delay(1000);

Serial.print("AT+CMGS=");

Serial.write((byte)34);

Serial.print(phoneNumber);

Serial.write((byte)34);

Serial.println();

while (Serial.available())

returnData.concat((char)Serial.read());

delay(500);

Serial.print("Tanggal ##/##/#### \n");

Serial.print("Pukul ##/## WIT \n");

Serial.print("Temp. ## C \n");

Serial.print("Kelembapan. ## % \n");
Serial.print("Suara. ## dB \n");

Serial.print("Cahaya. Tidak/Ada . \n");

// Serial.print("Tanggal ");

// Serial.print(getDate());

// Serial.print("/n");

// Serial.print("Pukul ");

// Serial.print(getTime());

// Serial.print(" WIT /n");

// Serial.print("Temp. ");

// Serial.print(getTemp());

// Serial.print(" C /n");

// Serial.print("Kelembapan ");

// Serial.print(getHum());

// Serial.print(" % /n");

// Serial.print("Suara ");

// Serial.print("Cahaya. Tidak/Ada . \n");

Serial.write((byte)26);

while (Serial.available())

returnData.concat((char)Serial.read());

delay(2000);

if (returnData.indexOf("ERROR") > 0) {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("SMS Gagal");

delay(5000);

} else {
lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Done");

returnData = "";

delay(30000);

} else {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Modem Not Ready");

delay(3000);

// END Program

// -------------------------------------------

String getTime() {

String result;

DateTime now = rtc.now();

result.concat(now.hour());

result += ":";

result.concat(now.minute());

result += ":";
result.concat(now.second());

return result;

String getDayStr() {

String result;

DateTime now = rtc.now();

result.concat(daysOfTheWeek[now.dayOfTheWeek()]);

return result;

String getDate() {

String result;

DateTime now = rtc.now();

result.concat(now.day());

result += "/";

result.concat(now.month());

result += "/";

result.concat(now.year());

return result;

int getTemp() {

int result;

delay(dht.getMinimumSamplingPeriod());

float temperature = dht.getTemperature();

result = (int) temperature;

return result;

}
int getHum() {

int result;

delay(dht.getMinimumSamplingPeriod());

float humidity = dht.getHumidity();

result = (int) humidity;

return result;

int getLight() {

sensorValue1 = analogRead(sensorPin1);

sensorValue2 = analogRead(sensorPin2);

int result = (sensorValue1 + sensorValue2) / 2;

return result;

void sendSms(boolean flag) {

String returnData;

lcd.setCursor(0, 0);

while (returnData.indexOf("OK") > 0) {

Serial.println("AT");

while (Serial.available())

returnData.concat((char)Serial.read());

delay(1000);

indexOk = returnData.indexOf("OK");

indexEnd = indexOk + 2;

if (returnData.substring(indexOk, indexEnd) == "OK") {

lcd.clear();
lcd.setCursor(0, 0);

lcd.print("Modem Ready");

returnData = "";

delay(2000);

Serial.println("AT+CMGF=1");

while (Serial.available())

returnData.concat((char)Serial.read());

delay(500);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("SMS Mode : Text");

returnData = "";

delay(1000);

Serial.print("AT+CMGS=");

Serial.write((byte)34);

Serial.print(phoneNumber);

Serial.write((byte)34);

Serial.println();

while (Serial.available())

returnData.concat((char)Serial.read());

delay(500);

if (flag == true) {

Serial.print("Status : Warning ! \n");

} else {

Serial.print("Status : Normal ! \n");

}
Serial.print("Tanggal ");

Serial.print(getDate());

Serial.print("/n");

Serial.print("Pukul ");

Serial.print(getTime());

Serial.print(" WIT /n");

Serial.print("Temp. ");

Serial.print(getTemp());

Serial.print(" C /n");

Serial.print("Kelembapan ");

Serial.print(getHum());

Serial.print(" % /n");

Serial.print("Suara ");

Serial.print("Cahaya. Tidak/Ada . \n");

Serial.write((byte)26);

while (Serial.available())

returnData.concat((char)Serial.read());

delay(2000);

if (returnData.indexOf("ERROR") > 0) {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("SMS Gagal");

delay(5000);

} else {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Done");

returnData = "";
delay(5000);

} else {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Modem Not Ready");

delay(3000);