Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Home Automation
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Jason Williams
Home Automation
Commits
c056e57d
Commit
c056e57d
authored
Nov 12, 2018
by
Jason Williams
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding code for distance sensors
parent
307a009e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
3 deletions
+43
-3
Garage.ino
Garage/Garage.ino
+43
-3
No files found.
Garage/Garage.ino
View file @
c056e57d
...
...
@@ -11,6 +11,10 @@ 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
};
...
...
@@ -30,12 +34,16 @@ 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
);
...
...
@@ -139,7 +147,7 @@ void loop() {
delay
(
500
);
}
// Check the temperatures every 30 seconds and send an update
// 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
();
...
...
@@ -157,11 +165,43 @@ void loop() {
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
();
}
// TODO: ULTRASONIC DISTANCE SENSORS
// 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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment