...
 
Commits (31)
No preview for this file type
// WiFi & Connectivity Includes
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Connection pins
const int pin_dooropen = D0;
const int pin_doorclosed = D1;
const int pin_sidedoor = D2;
const int pin_doortoggle = D6;
const int pin_temperature = D7;
const int pin_trigger1 = D3;
const int pin_echo1 = D4;
const int pin_trigger2 = D5;
const int pin_echo2 = D8;
DeviceAddress ambient = { 0x28, 0x6F, 0x08, 0x0A, 0x31, 0x14, 0x01, 0x20 };
DeviceAddress freezer = { 0x28, 0x6C, 0x96, 0x00, 0x31, 0x14, 0x01, 0x77 };
// WiFi & Connectivity Settings
#include "secrets.h"
......@@ -12,10 +28,27 @@
const char* mqtt_user;
const char* mqtt_pass;
*/
const char* toggle_topic = "garage/pushbutton";
//const char* toggle_topic = "garage/pushbutton";
const char* topic_doorcommand = "garage/door/command";
const char* topic_doorstate = "garage/door/state";
const char* topic_sidedoor = "garage/sidedoor";
const char* topic_ambienttemp = "garage/temperature";
const char* topic_freezertemp = "garage/freezer";
const char* topic_spot1 = "garage/spot1";
const char* topic_spot2 = "garage/spot2";
// Currently reported states and/or times
bool state_sidedoor = 1;
int state_door = 0;
unsigned long sent_temperature = millis();
unsigned long periodic_update = millis();
long duration;
int distance;
WiFiClient espClient;
PubSubClient client(espClient);
OneWire oneWire(pin_temperature);
DallasTemperature DallasTemp(&oneWire);
void setup() {
Serial.begin(115200);
......@@ -25,11 +58,19 @@ void setup() {
client.setCallback(callback);
setup_mqtt();
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D2, OUTPUT);
digitalWrite(D2, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
client.publish(toggle_topic, "idle");
pinMode(pin_dooropen, INPUT);
pinMode(pin_doorclosed, INPUT);
pinMode(pin_doortoggle, OUTPUT);
pinMode(pin_sidedoor, INPUT);
pinMode(pin_trigger1, OUTPUT);
pinMode(pin_trigger2, OUTPUT);
pinMode(pin_echo1, INPUT);
pinMode(pin_echo2, INPUT);
digitalWrite(pin_doortoggle, HIGH);
client.publish(topic_sidedoor, "OFF");
client.publish(topic_doorstate, "CLOSED");
}
void setup_wifi() {
......@@ -58,7 +99,7 @@ void setup_mqtt() {
while (!client.connected()) {
if (client.connect("MCUTestClient", mqtt_user, mqtt_pass)) {
Serial.println("MQTT Connected");
client.subscribe(toggle_topic);
client.subscribe(topic_doorcommand);
} else {
Serial.print("MQTT Failed, rc=");
Serial.print(client.state());
......@@ -74,25 +115,158 @@ void loop() {
setup_mqtt();
}
// Monitor the garage door and publish status
if (digitalRead(pin_dooropen) == 0 && digitalRead(pin_doorclosed) == 1 && state_door != 2) {
state_door = 2;
client.publish(topic_doorstate, "OPEN");
Serial.println("Garage door is open");
delay(500);
} else if (digitalRead(pin_dooropen) == 1 && digitalRead(pin_doorclosed) == 0 && state_door != 0) {
state_door = 0;
client.publish(topic_doorstate, "CLOSED");
Serial.println("Garage door is closed");
delay(500);
} else if (digitalRead(pin_dooropen) == 1 && digitalRead(pin_doorclosed) == 1 && state_door != 1) {
state_door = 1;
client.publish(topic_doorstate, "MOVING");
Serial.println("Garage door is moving");
delay(500);
} else if (digitalRead(pin_dooropen) == 0 && digitalRead(pin_doorclosed) == 0 && state_door != 3) {
state_door = 3;
client.publish(topic_doorstate, "ERROR");
Serial.println("Garage door error");
delay(500);
}
// Monitor the side door and publish status
if (digitalRead(pin_sidedoor) == 1 && state_sidedoor == 0) {
state_sidedoor = 1;
client.publish(topic_sidedoor, "OFF");
Serial.println("Side door closed");
delay(500);
} else if (digitalRead(pin_sidedoor) == 0 && state_sidedoor == 1) {
state_sidedoor = 0;
client.publish(topic_sidedoor, "ON");
Serial.println("Side door opened");
delay(500);
}
// Check the temperatures and parkgin spots every 30 seconds and send an update
unsigned long elapsed_temperature = millis() - sent_temperature;
if(elapsed_temperature >= 30000) {
DallasTemp.requestTemperatures();
char ambtempstr[6], frztempstr[6];
sprintf(ambtempstr, "%.1f", DallasTemp.getTempC(ambient));
sprintf(frztempstr, "%.1f", DallasTemp.getTempC(freezer));
client.publish(topic_ambienttemp, ambtempstr);
client.publish(topic_freezertemp, frztempstr);
Serial.print("[Temperature] Ambient temperature is ");
Serial.print(ambtempstr);
Serial.println(" degrees");
Serial.print("[Temperature] Freezer temperature is ");
Serial.print(frztempstr);
Serial.println(" degrees");
// Clear the triggers
digitalWrite(pin_trigger1, LOW);
digitalWrite(pin_trigger2, LOW);
delayMicroseconds(2);
char dist1str[4], dist2str[4];
// Take a reading on sensor 1
digitalWrite(pin_trigger1, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger1, LOW);
duration = pulseIn(pin_echo1, HIGH);
distance = duration*0.017;
// Publish sensor 1 reading
sprintf(dist1str, "%d", distance);
client.publish(topic_spot1, dist1str);
Serial.print("[Distance] Sensor 1 at ");
Serial.print(distance);
Serial.println("cm");
// Take a reading on sensor 2
digitalWrite(pin_trigger2, HIGH);
delayMicroseconds(10);
digitalWrite(pin_trigger2, LOW);
duration = pulseIn(pin_echo2, HIGH);
distance = duration*0.017;
// Publish sensor 2 reading
sprintf(dist2str, "%d", distance);
client.publish(topic_spot2, dist2str);
Serial.print("[Distance] Sensor 2 at ");
Serial.print(distance);
Serial.println("cm");
sent_temperature = millis();
}
// Check how long it's been since the last periodic update, and send another one if required
unsigned long elapsed_since_update = millis() - periodic_update;
if (elapsed_since_update >= 120000) {
// Update the side door status
if (state_sidedoor == 0) client.publish(topic_sidedoor, "ON");
else if (state_sidedoor == 1) client.publish(topic_sidedoor, "OFF");
// Update the garage door
if (state_door == 0) client.publish(topic_doorstate, "CLOSED");
else if (state_door == 1) client.publish(topic_doorstate, "MOVING");
else if (state_door == 2) client.publish(topic_doorstate, "OPEN");
else client.publish(topic_doorstate, "ERROR");
// Check for software updates
updateCheck();
periodic_update = millis();
}
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print("Control message arrived on ");
Serial.print(topic);
Serial.print("] ");
Serial.print(": ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, topic_doorcommand) == 0) {
payload[length] = '\0';
if (strcmp((char*)payload, "OPEN") == 0 && state_door == 0) {
Serial.println("DOOR OPENING");
movedoor();
} else if (strcmp((char*)payload, "CLOSE") == 0 && state_door == 2) {
Serial.println("DOOR CLOSING");
movedoor();
}
}
}
payload[length] = '\0';
if (strcmp(topic, toggle_topic) == 0 && strcmp((char*)payload, "go") == 0) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(D2, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(D2, HIGH);
client.publish(toggle_topic, "idle");
void movedoor() {
digitalWrite(pin_doortoggle, LOW);
delay(500);
digitalWrite(pin_doortoggle, HIGH);
}
void updateCheck() {
t_httpUpdate_return ret = ESPhttpUpdate.update("http://internal.jnf.me/public/pages/updater/");
switch(ret) {
case HTTP_UPDATE_FAILED:
Serial.print("[Update] Update failed: ");
Serial.println(ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("[Update] No update required");
break;
case HTTP_UPDATE_OK:
Serial.println("[Update] Update OK");
break;
}
}
Garage/Garage.png

448 KB | W: | H:

Garage/Garage.png

436 KB | W: | H:

Garage/Garage.png
Garage/Garage.png
Garage/Garage.png
Garage/Garage.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -304,10 +304,25 @@ sensor:
name: basement-temp
unit_of_measurement: '°C'
state_topic: "basement/temperature"
- platform: mqtt
name: garage-door-state
state_topic: "garage/door/state"
- platform: mqtt
name: garage-temp
unit_of_measurement: '°C'
state_topic: "garage/temperature"
- platform: mqtt
name: garage-freezer
unit_of_measurement: '°C'
state_topic: "garage/freezer"
- platform: mqtt
name: spot1
unit_of_measurement: 'cm'
state_topic: garage/spot1
- platform: mqtt
name: spot2
unit_of_measurement: 'cm'
state_topic: garage/spot2
binary_sensor:
- platform: mqtt
......
......@@ -29,6 +29,8 @@ script.stop_all_media_players:
icon: mdi:stop
sensor.basementtemp:
friendly_name: Furnace Room
sensor.garagefreezer:
friendly_name: Freezer
sensor.garagetemp:
friendly_name: Garage
sensor.travel_time_to_work:
......@@ -49,8 +51,5 @@ sensor.dark_sky_hourly_summary:
friendly_name: Short-Term
sensor.dark_sky_daily_summary:
friendly_name: Long-Term
script.trigger_garage_door:
friendly_name: Open/Close Garage Door
icon: mdi:garage
weather.dark_sky:
friendly_name: Calgary Weather
......@@ -20,6 +20,10 @@ views:
- climate.thermostat759fb6
- sensor.basementtemp
- sensor.garagetemp
- type: entities
title: Garage Freezer
entities:
- sensor.garagefreezer
- type: entities
title: Ventilation
show_header_toggle: false
......
{"devices":[{"name":"Basement","mac":"00:00:00:00:00:01"},{"name":"Garage","mac":"00:00:00:00:00:02"},{"name":"Laundry","mac":"00:00:00:00:00:03"},{"name":"Test Device","mac":"84:F3:EB:6F:93:45"}],"deployments":["84f3eb6f93451541987019"]}
\ No newline at end of file
{"devices":[{"name":"Basement","mac":"00:00:00:00:00:01"},{"name":"Garage","mac":"18:FE:34:E1:3F:24"},{"name":"Laundry","mac":"00:00:00:00:00:03"},{"name":"Test Device","mac":"84:F3:EB:6F:93:45"}],"deployments":["84f3eb6f93451541987019","84f3eb6f93451542048129","18fe34e13f241542060430","18fe34e13f241542062049","18fe34e13f241542069135"]}
\ No newline at end of file