-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdimmerPir_May16d.ino
269 lines (237 loc) · 7.39 KB
/
dimmerPir_May16d.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
forked from: bruhautomation multisensor on github.ino
- using mqtt and JSON format
- PubSubClient
- ArduinoJSON
Changes by Binh Nguyen from bruhautomation file, updated on May 07, 2018:
- trimed down setColor function. Only analogWrite
- trimed down OTA update
- added back pir to turn to LED
- keep LED on after the last HIGH detected from PIR (30 secs, can be adjusted)
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
/************ WIFI and MQTT INFORMATION (CHANGE THESE FOR YOUR SETUP) ******************/
#define wifi_ssid "your_id_wifi" //type your WIFI information inside the quotes
#define wifi_password "your_wifi_password"
#define mqtt_server "your_ip_mqtt_server"
#define mqtt_user "your_mqtt_username"
#define mqtt_password "your_mqtt_password"
#define mqtt_port 1883
/************* MQTT TOPICS (change these topics as you wish) **************************/
#define light_state_topic "livingroom/kitchenLight"
#define light_set_topic "workingdesk/Controller"
const char* on_cmd = "ON";
const char* off_cmd = "OFF";
/**************************** FOR OTA **************************************************/
#define SENSORNAME "kitchenLight"
/**************************** PIN DEFINITIONS ********************************************/
#define outPin 0
#define pirPin 2
/**************************** SENSOR DEFINITIONS *******************************************/
char message_buff[300];
int calibrationTime = 0;
const int BUFFER_SIZE = 300;
float uptime;
unsigned long prevTime = 0;
/******************************** GLOBALS for fade/flash *******************************/
int intensity = 900;
int lightState = 0;
int pirRead = 0;
unsigned int onRetain = 30; //in seconds after the last movement detected
WiFiClient espClient;
PubSubClient client(espClient);
/********************************** START SETUP*****************************************/
void setup() {
Serial.begin(115200);
flash(ledPin, 5, 100);
delay(1000);
pinMode(outPin, OUTPUT);
pinMode(pirPin, INPUT);
Serial.begin(115200);
delay(10);
Serial.println("Starting Node named " + String(SENSORNAME));
Serial.println("Updated on: May 16, 2018");
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
Serial.println("Ready");
Serial.print("IPess: ");
Serial.println(WiFi.localIP());
reconnect();
}
/********************************** START SETUP WIFI*****************************************/
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/********************************** START CALLBACK*****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char message[length + 1];
for (int i = 0; i < length; i++) {
message[i] = (char)payload[i];
}
message[length] = '\0';
Serial.println(message);
if (!processJson(message)) {
return;
}
sendState();
}
/********************************** START PROCESS JSON*****************************************/
bool processJson(char* message) {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(message);
Serial.print("Processing message:");
Serial.println(message);
if (!root.success()) {
Serial.println("parseObject() failed");
return false;
}
if (pirRead == 0){ //if motion is not detected
lightState = root["Two"]["state"];
intensity = root["Two"]["intensity"];
Serial.print("\tLight state: ");
Serial.print(lightState);
Serial.print("\tintensity: ");
Serial.println(intensity);
} else {
lightState = 1;
intensity = 900; //any values less than 1024 would work
}
return true;
}
/********************************** START SEND STATE*****************************************/
void sendState() {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["uptime"] = uptime;
root["sensor"] = SENSORNAME;
root["state"] = (lightState == 1) ? on_cmd : off_cmd;
root["intensity"] = intensity;
root["pir"] = pirRead;
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
String sPayload = "";
root.printTo(sPayload);
char* cPayload = &sPayload[0u];
client.publish(light_state_topic, buffer, true);
Serial.println("\nPushed MQTT: " + String(buffer));
}
/********************************** START RECONNECT*****************************************/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(SENSORNAME, mqtt_user, mqtt_password)) {
Serial.println("connected");
client.subscribe(light_set_topic);
sendState();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/********************************** START MAIN LOOP***************************************/
void loop() {
if (!client.connected()) {
reconnect();
delay(3000);
}
if (!client.connected()){
Serial.print("\n: Restarting the ESP");
software_Reset();
}
// client.loop();
uptime = millis()/1000/3600.00;
pirRead = digitalRead(pirPin);
// Serial.print("\tpirRead: ");
// Serial.print(pirRead);
//
if (pirRead == 1){
if (lightState == 0){
lightState = 1;
intensity = 900;
send2LEDs();
sendState();
}
} else { //pirRead is zero
if (lightState == 1){
for (int i=0; i < onRetain; i++){
pirRead = digitalRead(pirPin);
if (pirRead == 1) {
i= 0;
};
delay(1000);
}
lightState = 0;
send2LEDs();
sendState();
} else {
client.loop();
send2LEDs();
}
}
// Serial.print("\tLightState: ");
// Serial.print(lightState);
// Serial.print("\n");
delay(100);
}
/*_________________flash pattern________________________________*/
void flash(int ledPin, int times, int interval){
for (int j=0; j < times; j++){
digitalWrite(ledPin, HIGH);
delay(interval);
digitalWrite(ledPin, LOW);
delay(interval);
}
}
/*_________________WRITE OUTPUT TO LEDS________________________________*/
void send2LEDs(){
if (lightState == 1){
analogWrite(outPin, intensity);
Serial.print("\t>> analogWrite:\t");
Serial.print(intensity);
} else {
analogWrite(outPin, 0);
}
}
/*_________________UPDATE STATUS TO MQTT________________________________*/
bool isStateChange(int lightState, int pirRead){
if (((pirRead == 1) && (lightState == 0)) || ((pirRead == 0) && (lightState == 1))) { //if digitalRead pir Pin is HIGH, and the light is OFF
return true;
}
}
/*_________________SOFT RESET________________________________*/
void software_Reset() {
Serial.print("\tWill try reset");
delay(2000);
while (!client.connected()){
Serial.print("\Reconnecting");
delay(5000);
ESP.reset();
};
}