Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
group project 2025
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Evan Jones
group project 2025
Commits
7b6d5cc9
Commit
7b6d5cc9
authored
4 months ago
by
Evan Jones
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
1c49ba07
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.py
+149
-0
149 additions, 0 deletions
main.py
with
149 additions
and
0 deletions
main.py
0 → 100644
+
149
−
0
View file @
7b6d5cc9
import
time
import
grovepi
from
grove_rgb_lcd
import
*
import
requests
import
json
# --- Pin Definitions ---
pir_pin
=
2
button_pin
=
3
led_pin
=
4
servo_pin
=
5
ultrasonic_pin
=
6
light_sensor_pin
=
0
# Analog A0
# --- ThingsBoard Settings ---
THINGSBOARD_URL
=
"
http://thingsboard.cs.cf.ac.uk
"
ACCESS_TOKEN
=
"
YOUR_DEVICE_TOKEN
"
# --- Initialization ---
def
initialize_sensors
():
print
(
"
[INIT] Initializing sensors...
"
)
grovepi
.
pinMode
(
pir_pin
,
"
INPUT
"
)
grovepi
.
pinMode
(
button_pin
,
"
INPUT
"
)
grovepi
.
pinMode
(
led_pin
,
"
OUTPUT
"
)
grovepi
.
pinMode
(
servo_pin
,
"
OUTPUT
"
)
grovepi
.
pinMode
(
ultrasonic_pin
,
"
INPUT
"
)
print
(
"
[INIT] Sensors initialized.
"
)
def
update_lcd_display
(
message
,
r
=
0
,
g
=
255
,
b
=
0
):
try
:
print
(
f
"
[LCD] Displaying message:
'
{
message
}
'"
)
setRGB
(
r
,
g
,
b
)
setText
(
message
)
except
Exception
as
e
:
print
(
f
"
[ERROR] LCD display failed:
{
e
}
"
)
# --- Sensor Read Functions ---
def
read_motion
():
try
:
motion
=
grovepi
.
digitalRead
(
pir_pin
)
print
(
f
"
[READ] PIR motion:
{
motion
}
"
)
return
motion
except
Exception
as
e
:
print
(
f
"
[ERROR] Motion sensor read failed:
{
e
}
"
)
return
0
def
read_button
():
try
:
button
=
grovepi
.
digitalRead
(
button_pin
)
print
(
f
"
[READ] Button state:
{
button
}
"
)
return
button
except
Exception
as
e
:
print
(
f
"
[ERROR] Button read failed:
{
e
}
"
)
return
0
def
read_light_level
():
try
:
light
=
grovepi
.
analogRead
(
light_sensor_pin
)
print
(
f
"
[READ] Light level:
{
light
}
"
)
return
light
except
Exception
as
e
:
print
(
f
"
[ERROR] Light sensor read failed:
{
e
}
"
)
return
-
1
def
read_distance
():
try
:
distance
=
grovepi
.
ultrasonicRead
(
ultrasonic_pin
)
print
(
f
"
[READ] Distance:
{
distance
}
cm
"
)
return
distance
except
Exception
as
e
:
print
(
f
"
[ERROR] Distance read failed:
{
e
}
"
)
return
-
1
# --- Actuators ---
def
trigger_alert
():
print
(
"
[ACTION] Triggering visual/audio alert...
"
)
for
_
in
range
(
3
):
grovepi
.
digitalWrite
(
led_pin
,
1
)
time
.
sleep
(
0.2
)
grovepi
.
digitalWrite
(
led_pin
,
0
)
time
.
sleep
(
0.2
)
print
(
"
[ACTION] Alert complete.
"
)
def
trigger_vibration
(
duration
=
2
):
print
(
f
"
[ACTION] Triggering vibration for
{
duration
}
seconds...
"
)
grovepi
.
analogWrite
(
servo_pin
,
255
)
time
.
sleep
(
duration
)
grovepi
.
analogWrite
(
servo_pin
,
0
)
print
(
"
[ACTION] Vibration complete.
"
)
# --- ThingsBoard Communication ---
def
send_to_thingsboard
(
data
):
try
:
print
(
f
"
[SEND] Sending data to ThingsBoard:
{
data
}
"
)
url
=
f
"
{
THINGSBOARD_URL
}
/api/v1/
{
ACCESS_TOKEN
}
/telemetry
"
headers
=
{
'
Content-Type
'
:
'
application/json
'
}
requests
.
post
(
url
,
data
=
json
.
dumps
(
data
),
headers
=
headers
)
print
(
"
[SEND] Data sent successfully.
"
)
except
Exception
as
e
:
print
(
f
"
[ERROR] Failed to send data to ThingsBoard:
{
e
}
"
)
# --- Event Logger ---
def
log_event
(
event_type
,
details
):
timestamp
=
time
.
strftime
(
"
%Y-%m-%d %H:%M:%S
"
)
print
(
f
"
[LOG] [
{
timestamp
}
]
{
event_type
}
:
{
details
}
"
)
# --- Main Loop ---
def
main_loop
():
print
(
"
[SYSTEM] Smart Doorbell is running (DEBUG MODE)
"
)
update_lcd_display
(
"
System Ready
"
,
0
,
255
,
0
)
while
True
:
motion
=
read_motion
()
button
=
read_button
()
light
=
read_light_level
()
distance
=
read_distance
()
if
motion
:
log_event
(
"
Motion Detected
"
,
"
Movement sensed near door
"
)
update_lcd_display
(
"
Motion Detected
"
,
255
,
165
,
0
)
trigger_alert
()
trigger_vibration
()
send_to_thingsboard
({
"
event
"
:
"
motion
"
,
"
light_level
"
:
light
,
"
distance_cm
"
:
distance
})
if
button
:
log_event
(
"
Button Pressed
"
,
"
Doorbell was pressed
"
)
update_lcd_display
(
"
Visitor at Door
"
,
0
,
100
,
255
)
trigger_alert
()
trigger_vibration
()
send_to_thingsboard
({
"
event
"
:
"
doorbell
"
,
"
light_level
"
:
light
,
"
distance_cm
"
:
distance
})
time
.
sleep
(
0.5
)
# --- Main Entry ---
if
__name__
==
"
__main__
"
:
try
:
initialize_sensors
()
main_loop
()
except
KeyboardInterrupt
:
print
(
"
[SYSTEM] Shutdown requested. Exiting...
"
)
update_lcd_display
(
"
Shutting Down
"
,
255
,
0
,
0
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment