Uploaded by SWAG BOYI

source code for project

advertisement
SOURCE CODE
/* For BME280 sensor */
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
const byte rxPin = 16; //rx2
const byte txPin = 17; //tx2
HardwareSerial dwin(1);
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
/* appliances pins */
int light = 2;
int fan = 4;
int ac = 5;
int tv = 18;
/* Adresses of all sensors */
unsigned char Buffer[9];
#define temperature_add 0x61
#define humidity_add 0x62
#define pressure_add 0x63
#define dewpoint_add 0x64
unsigned char Temperature[8] = {0x5a, 0xa5, 0x05, 0x82, temperature_add , 0x00, 0x00, 0x00};
unsigned char Humidity[8] = {0x5a, 0xa5, 0x05, 0x82, humidity_add, 0x00, 0x00, 0x00};
unsigned char Pressure[8] = {0x5a, 0xa5, 0x05, 0x82, pressure_add , 0x00, 0x00, 0x00};
unsigned char DewPoint[8] = {0x5a, 0xa5, 0x05, 0x82, dewpoint_add, 0x00, 0x00, 0x00};
void setup()
{
Serial.begin(115200);
//Begin serial communication DWIN
dwin.begin(115200, SERIAL_8N1, rxPin, txPin);
unsigned status;
status = bme.begin(0x76);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
while (1) delay(10);
}
pinMode(light, OUTPUT);
pinMode(fan, OUTPUT);
pinMode(ac, OUTPUT);
pinMode(tv, OUTPUT);
digitalWrite(light, LOW);
digitalWrite(fan, LOW);
digitalWrite(ac, LOW);
digitalWrite(tv, LOW);
}
void loop()
{
sensor_data();
delay(10);
realy_control();
delay(50);
}
void sensor_data()
{
int t = bme.readTemperature();
int h = bme.readHumidity();
int p = bme.readPressure() / 100.0F;
int d = dewPointFast(t, h);
/*------Print data to Serial Monitor------*/
Serial.print("Temperature = ");
Serial.print(t);
Serial.println(" °C");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println(" %");
Serial.print("Pressure = ");
Serial.print(p);
Serial.println(" hPa");
Serial.print("DewPoint = ");
Serial.print(d);
Serial.println(" °C");
Serial.println();
Temperature[6] = highByte(t);
Temperature[7] = lowByte(t);
dwin.write(Temperature, 8);
Humidity[6] = highByte(h);
Humidity[7] = lowByte(h);
dwin.write(Humidity, 8);
Pressure[6] = highByte(p);
Pressure[7] = lowByte(p);
dwin.write(Pressure, 8);
DewPoint[6] = highByte(d);
DewPoint[7] = lowByte(d);
dwin.write(DewPoint, 8);
}
/*----------DewPoint Calculation--------*/
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity * 0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
void realy_control()
{
if (dwin.available())
{
for (int i = 0; i <= 8; i++) //this loop will store whole frame in buffer array.
{
Buffer[i] = dwin.read();
}
if (Buffer[0] == 0X5A)
{
switch (Buffer[4])
{
case 0x65: //for light
if (Buffer[8] == 1)
{
digitalWrite(light, HIGH);
Serial.println("Relay1 ON");
}
else
{
digitalWrite(light, LOW);
Serial.println("Relay2 OFF");
}
break;
case 0x66: //for fan
if (Buffer[8] == 1)
{
Serial.println(Buffer[8]);
digitalWrite(fan, HIGH);
Serial.println("Relay2 ON");
}
else
{
digitalWrite(fan, LOW);
Serial.println("Relay2 OFF");
}
break;
case 0x67: //for AC
if (Buffer[8] == 1)
{
Serial.println(Buffer[8]);
digitalWrite(ac, HIGH);
Serial.println("Relay3 ON");
}
else
{
digitalWrite(ac, LOW);
Serial.println("Relay3 OFF");
}
break;
case 0x68: //for TV
if (Buffer[8] == 1)
{
Serial.println(Buffer[8]);
digitalWrite(tv, HIGH);
Serial.println("Relay4 ON");
}
else
{
digitalWrite(tv, LOW);
Serial.println("Relay4 OFF");
}
break;
default:
Serial.println("No data..");
}
}
}
}
Download