Κώδικας:
/*
Creator - Author: Ηλιάδης Ηλίας - Iliadis Ilias
e-mail: vilias@yahoo.com
Project Name: LCD2_Relay_withGSM
Project Discripton: Relay Controled based on Temp
Connections Relay1 Connect to arduino digital pin 10
Relay2 Connect to arduino digital pin 11
Relay3 Connect to arduino digital pin 12
DHT connect to arduino pin 9
LCD connect to I2C, Arduino I2C pins are A4 & A5 */
// Include libaries
#include <GSM.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h> // F Malpartida's NewLiquidCrystal library
#include <dht.h>
// GSM Settings
#define PINNUMBER "" //pin disabled
GSM gsmAccess; // include a 'true' parameter for debug enabled
GSM_SMS sms;
char remoteNumber[20] = "0000000000000"; //
char txtMsg[200] = "SMS";
String MySMS, MyTempSMS, MyTempVal, MyHumiSMS, MyHumiVal;
// DHT Settings
dht DHT;
int mytemp = 0;
int myhum = 0;
#define DHT11_PIN 9 // DHT11 Connected on Pin 9 at Arduino
// Timer Settings
unsigned long timeA;
unsigned long timeB;
// Clock Settings
int h = 12;
int m;
int s;
int flag;
int TIME;
const int hs = 8;
const int ms = 9;
// Reley Settings
#define rel1 10 // Relay1 at arduino digital pin 10
#define rel2 11 // Relay2 at arduino digital pin 11
#define rel3 12 // Relay3 at arduino digital pin 12
// LCD I2C Settings
#define I2C_ADDR 0x3F // Define I2C Address for controller
#define BACKLIGHT_PIN 3 // Backlight Pin (P3) |
#define En_pin 2 // En Pin (P2) |
#define Rw_pin 1 // RW Pin (P1) |
#define Rs_pin 0 // RS pin (P0) |
#define D4_pin 4 // D4 Pin (P4) |PCF8574T /AT
#define D5_pin 5 // D5 Pin (P5) |
#define D6_pin 6 // D6 Pin (P6) |
#define D7_pin 7 // D7 Pin (P7) |
#define BL_OFF 0 //Backlight OFF
#define BL_ON 1 //Backlight ON
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
void setup()
{
//digitalWrite(9, HIGH);
Serial.begin(9600); //Strat Serial Port
MyTempSMS = ("Temperature is: "); // Part of MySMS message
MyHumiSMS = ("Humidity is: "); // Part of MySMS message
Serial.println("SMS Messages Sender");
boolean notConnected = true; // connection state
while (notConnected) // Start GSM module
{
if (gsmAccess.begin(PINNUMBER) == GSM_READY) //Verify Pin
notConnected = false;
else
{
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("GSM initialized");
pinMode(rel1, OUTPUT); //Relay 1
pinMode(rel2, OUTPUT); //Reale 2
pinMode(rel3, OUTPUT); //Reale 3
digitalWrite(rel1, LOW); // |
digitalWrite(rel2, LOW); // |- Set all Relays Off
digitalWrite(rel3, LOW); // |
lcd.begin (16, 2); // initialize the lcd - Define 16 Characters, 2 Lines LCD. *** WARNING *** coding line are start from 0 so line1 is 0 while coding and line2 is 1 !!!
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); //Initialize backlight, some LCD are Negative, depent on mamufacture.
lcd.setBacklight(BL_ON); // Switch on the backlight
delay(1000);
lcd.setBacklight(BL_OFF); // Switch off the backlight
delay(500);
lcd.clear();
timeB = millis();
// flag = 23; //Set AM PM manual
// h = 11; //Set Hour manual
// m = 59; //Set Minutes manual
// s = 50; //Set Seconds manual
}
void loop()
{
int chk = DHT.read11(DHT11_PIN); //Read Value from sensor
int mytemp = DHT.temperature; // Keep Temperature value
int myhum = DHT.humidity; // Humitidy Value
MyTempVal = mytemp; // temperature value for MySMS message
MyHumiVal = myhum; // /Humitidy value for MySMS message
MySMS = "Arduino Report " + MyTempSMS + String(MyTempVal) + "C " + MyHumiSMS + String(MyHumiVal) + "%"; // Create MySMS message from all this strings
char copy[55]; // Make a single string for MySMS message
MySMS.toCharArray(copy, 55); // copy all previus to MySMS. Now MySMS is ready to transmit.
timeA = millis();
if (timeA > timeB + 5000) { // 5000 = 5sec Relays will change state 5sec after temperature change
timeB = millis();
// Read Temp and Set Relays
if (mytemp >= 37) { //If temp rich max 37C Relay1 is off and 2&3 are on
digitalWrite(rel1, LOW); //
digitalWrite(rel2, HIGH); // While temp 37C and over Realay1 is off and Relay 2&3 are On
digitalWrite(rel3, HIGH); //
}
else if (mytemp > 35 && mytemp < 37) {
digitalWrite(rel1, HIGH); //
digitalWrite(rel2, LOW); // While temp 35C to 37C Realay1 is on and Realay 2 & 3 are stay Off
digitalWrite(rel3, LOW); //
}
else {
digitalWrite(rel1, LOW); //
digitalWrite(rel2, LOW); // All are fine and Realays are Off
digitalWrite(rel3, LOW); //
}
}
// Reset the display
lcd.clear();
lcd.home();
// Print on the LCD
lcd.backlight();
lcd.setCursor(0, 0); // Cursor location (0,0) (0 character, 0 line) 1st line!!!
lcd.print(myhum, 1); //Print on the LCD the Humidity
lcd.setCursor(4, 0);
lcd.print("% - ");
lcd.setCursor(8, 0);
lcd.print(mytemp, 1); //Print on the LCD the Temperature
lcd.print("C");
if (mytemp >= 37) {
lcd.setCursor(0, 1); // Cursor location (0,1) (0 character, 1 line) 2nd line!!!
lcd.print("R1=off R2&3=on"); // Display state of relay
} // Display state of relay, Off state
else if (mytemp > 35 && mytemp < 37) {
lcd.setCursor(0, 1);
lcd.print("R1=on R2&3=off"); // Display state of relay
}
else {
lcd.setCursor(0, 1);
lcd.print("Relays are off"); // Display state of relay
}
delay(1000);
// Reports on serial monitor
Serial.print("TIME:");
Serial.print(h);
Serial.print(":");
Serial.print(m);
Serial.print(":");
Serial.print(s);
if (flag < 12)Serial.print("AM");
if (flag == 12)Serial.print("PM");
if (flag > 12)Serial.print("PM");
if (flag == 24)flag = 0;
Serial.print(" Humidity " );
Serial.print(DHT.humidity, 1); //Print on the Serial the Humidity
Serial.print(" Temparature ");
Serial.print(DHT.temperature, 1); //Print on the Serial the Temperature
if (mytemp >= 37)
{
Serial.println(" Relay1 = OFF Relay2 = On Relay3 = On"); // Report via serial monitor state of Relays
}
else if (mytemp > 35 && mytemp < 37)
{
Serial.println(" Relay1 = On Relay2 = Off Relay3 = Off"); // Report via serial monitor state of Relays
}
else
{
Serial.println(" Relay1 = Off Relay2 = Off Relay3 = Off"); // Report via serial monitor state of Relays
}
// Clock
s = s + 1;
if (s == 60) {
s = 0;
m = m + 1;
}
if (m == 60)
{
m = 0;
h = h + 1;
flag = flag + 1;
}
if (h == 13)
{
h = 1;
}
if (h == 12 && m == 1 && s == 1)
{
//SMS Code
Serial.print("Message to mobile number: ");
Serial.println(remoteNumber);
Serial.println("Message here");
// sms text
Serial.println("SENDING");
Serial.println();
Serial.println("Message:");
Serial.println(MySMS);
// send the message
sms.beginSMS(remoteNumber);
sms.print(MySMS);
sms.endSMS();
Serial.println("\nCOMPLETE!\n");
}
}