-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS226_Lab.ino
120 lines (106 loc) · 3.69 KB
/
CS226_Lab.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "UbidotsESPMQTT.h"
// Please note that, above header doesn't mean it is official UbidotsESPMQTT Library.
// If you want to run this locally, Download my fork as zip file and Add the zip library
// Download Link https://github.com/CITIZENDOT/ubidots-mqtt-esp/archive/refs/heads/master.zip
#include <DHT.h>
#include <stdio.h>
/****************************************
Define Constants
****************************************/
#define BUZZER D7
#define LDR A0
#define TEMP D5
#define RELAY D4
#define DHTTYPE DHT11 // DHT 11
#define DHTPIN D5
#define WIFISSID "My A" // Put your WifiSSID here
#define PASSWORD "appajichintimi" // Put your wifi password here
#define TOKEN "BBFF-YSfLdMkT52zPM7GbMCFGank2tJXVWj" // Ubidots Token
#define VARIABLE_TO_SUBSCRIBE "home_bulb"
#define DEVICE_LABEL "esp8266"
Ubidots client(TOKEN);
DHT dht = DHT(DHTPIN, DHTTYPE);
bool isMailSent = false;
/****************************************
Auxiliary Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
// Controlling Bulb with Website
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
if ((char)payload[0] == '0')
{
Serial.println("Since, Output is 0, I'm going to turn off LED");
digitalWrite(RELAY, LOW);
}
else if ((char)payload[0] == '1')
{
Serial.println("Since, Output is 1, I'm going to turn on LED");
digitalWrite(RELAY, HIGH);
}
else {
Serial.print(" Ubidots Value is ");
Serial.println((char)payload[0]);
}
}
/****************************************
Main Functions
****************************************/
void setup() {
Serial.begin(115200);
pinMode(BUZZER, OUTPUT);
pinMode(RELAY, OUTPUT);
pinMode(TEMP, INPUT);
client.ubidotsSetBroker("industrial.api.ubidots.com");
client.setDebug(true);
client.wifiConnection(WIFISSID, PASSWORD);
client.begin(callback);
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_TO_SUBSCRIBE);
if (client.connected())
{
digitalWrite(BUZZER, HIGH);
delay(200);
digitalWrite(BUZZER, LOW);
}
dht.begin();
}
void loop() {
int LDRValue = analogRead(LDR);
Serial.println(LDRValue);
if (LDRValue < 400) {
// Light is falling on LDR
digitalWrite(BUZZER, LOW);
if (isMailSent) {
isMailSent = false;
}
}
else {
digitalWrite(BUZZER, HIGH);
// Interuppted
if (!isMailSent) {
String mailSubject = "Hi,\nI am your personal asssistant at Home, monitoring LDR Sensor.\n";
mailSubject += "It looks like someone interuppted the Signal.\n";
mailSubject += "Could you check what happened?\n";
// Reading Temperature and Humidity
mailSubject += "For your Information, The current Room temperature is " + String(dht.readTemperature()) + " Degree Celsius" + "\n";
mailSubject += "The current Room Relative Humidity is " + String(dht.readHumidity()) + " %\n";
Serial.print("Starting to Send Mail...");
isMailSent = client.sendMail("mail.smtp2go.com", 2525, "Q0lUSVpFTkRPVA==", "S3VYTFB4SkQ0YTZP",
"Mail from NodeMCU", mailSubject);
if (isMailSent) {
Serial.println("Mail Sent Successfully!");
}
}
}
if (!client.connected()) {
client.reconnect();
client.ubidotsSubscribe(DEVICE_LABEL, VARIABLE_TO_SUBSCRIBE);
}
client.loop();
delay(100);
}