Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cpresser
Esp32 Dimmer
Commits
7a68d727
Commit
7a68d727
authored
Mar 03, 2021
by
Carsten Presser
Browse files
Software-version for 1xWhite Lamp
parent
6d2aaedc
Changes
2
Hide whitespace changes
Inline
Side-by-side
software/main.py
View file @
7a68d727
...
...
@@ -13,13 +13,16 @@ UID = ubinascii.hexlify(machine.unique_id()).decode()
MQTT
=
mqtt
.
MQTTClient
(
"Sensor_{}"
.
format
(
UID
),
"mqtt.intern.ca.rstenpresser.de"
)
I2C
=
machine
.
I2C
(
0
)
PCA
=
pca9685
.
PCA9685
(
I2C
)
print
(
"PCA-Frequ"
+
str
(
PCA
.
freq
()))
PCA
.
freq
(
2000
)
BME
=
None
PWM2
=
machine
.
PWM
(
machine
.
Pin
(
2
),
freq
=
250000
,
duty
=
1
)
red
=
machine
.
Pin
(
12
,
machine
.
Pin
.
OUT
)
green
=
machine
.
Pin
(
14
,
machine
.
Pin
.
OUT
)
oe
=
machine
.
Pin
(
16
,
machine
.
Pin
.
OUT
)
tim0
=
machine
.
Timer
(
0
)
id_re
=
re
.
compile
(
"homeassistant/light\/{}
_rgbw(\d)
\/(\w+)"
.
format
(
UID
))
id_re
=
re
.
compile
(
"homeassistant/light\/{}\/(\w+)"
.
format
(
UID
))
# document used for autoconfiguration with home assistant
json_autoconf
=
{
...
...
@@ -27,7 +30,7 @@ json_autoconf = {
"device"
:
{
"manufacturer"
:
"cpresser"
,
"name"
:
UID
,
"model"
:
"ESP32Dimmer,
4x4 (RGBW)
"
,
"model"
:
"ESP32Dimmer,
1xWhite
"
,
"sw_version"
:
"42"
,
"identifiers"
:
UID
# override before sending!
},
...
...
@@ -35,46 +38,11 @@ json_autoconf = {
"cmd_t"
:
"~/set"
,
"stat_t"
:
"~/state"
,
"schema"
:
"json"
,
"rgb"
:
True
,
"white_value"
:
True
,
"brightness"
:
True
,
}
# document that tracks the current state
json_state
=
[
{
"white_value"
:
0
,
"brightness"
:
0
,
"state"
:
"OFF"
,
"color"
:
{
"r"
:
0
,
"b"
:
0
,
"g"
:
0
}},
{
"white_value"
:
0
,
"state"
:
"OFF"
,
"brightness"
:
0
,
"color"
:
{
"r"
:
0
,
"b"
:
0
,
"g"
:
0
}},
{
"white_value"
:
0
,
"brightness"
:
0
,
"state"
:
"OFF"
,
"color"
:
{
"r"
:
0
,
"b"
:
0
,
"g"
:
0
}},
{
"white_value"
:
0
,
"brightness"
:
0
,
"state"
:
"OFF"
,
"color"
:
{
"r"
:
0
,
"b"
:
0
,
"g"
:
0
}}
]
json_state
=
{
"brightness"
:
0
,
"state"
:
"OFF"
}
# red and green are inverted.
# show that we are not ready by lighting up red
...
...
@@ -115,10 +83,28 @@ def init_wifi():
print
(
"WIFI connection established"
)
red
.
on
()
def
pwm_set
(
channel
,
color
,
value
=
0
,
bright
=
0
):
chan
=
channel
*
4
+
color
v
=
int
(
value
*
bright
/
255
)
PCA
.
duty
(
chan
,
g8
[
v
])
def
pwm_set
(
value
=
0
):
# set LEDs
print
(
"LED: "
+
str
(
g8
[
value
]))
for
channel
in
[
0
,
2
,
3
]:
PCA
.
duty
(
channel
*
4
+
0
,
g8
[
value
])
PCA
.
duty
(
channel
*
4
+
1
,
g8
[
value
])
PCA
.
duty
(
channel
*
4
+
2
,
g8
[
value
])
PCA
.
duty
(
channel
*
4
+
3
,
g8
[
value
])
# have 3 stages for the fan
# set them at an arbitrary threshold
if
g8
[
value
]
>
4048
:
# power
fan
=
200
elif
g8
[
value
]
>
2048
:
# silent
fan
=
130
else
:
# off
fan
=
1
print
(
"Fan: "
+
str
(
fan
))
PWM2
.
duty
(
fan
)
def
mqtt_cb
(
topic
,
msg
):
t
=
id_re
.
match
(
topic
.
decode
(
'utf-8'
))
...
...
@@ -128,54 +114,37 @@ def mqtt_cb(topic, msg):
except
ValueError
:
green_blink
(
period
=
50
,
led
=
red
)
return
i
=
int
(
t
.
group
(
1
))
idd
=
"{}_rgbw{}"
.
format
(
UID
,
i
)
base
=
"homeassistant/light/"
+
idd
base
=
"homeassistant/light/{}"
.
format
(
UID
)
# blink to show we received data
green_blink
()
if
t
.
group
(
2
)
==
"state"
:
if
t
.
group
(
1
)
==
"state"
:
pass
elif
t
.
group
(
2
)
==
"set"
:
elif
t
.
group
(
1
)
==
"set"
:
if
"transition"
in
pl
:
json_state
[
i
][
'transition'
]
=
pl
[
'transition'
]
json_state
[
'transition'
]
=
pl
[
'transition'
]
print
(
"YAYYYYYYYY!!!!!!"
+
pl
[
'transition'
])
else
:
json_state
[
i
][
'transition'
]
=
None
if
"white_value"
in
pl
:
json_state
[
i
][
'white_value'
]
=
pl
[
'white_value'
]
json_state
[
'transition'
]
=
None
if
"brightness"
in
pl
:
json_state
[
i
][
'brightness'
]
=
pl
[
'brightness'
]
if
"color"
in
pl
:
if
"r"
in
pl
[
'color'
]:
json_state
[
i
][
'color'
][
'r'
]
=
pl
[
'color'
][
'r'
]
if
"b"
in
pl
[
'color'
]:
json_state
[
i
][
'color'
][
'b'
]
=
pl
[
'color'
][
'b'
]
if
"g"
in
pl
[
'color'
]:
json_state
[
i
][
'color'
][
'g'
]
=
pl
[
'color'
][
'g'
]
json_state
[
'brightness'
]
=
pl
[
'brightness'
]
if
"state"
in
pl
:
if
pl
[
'state'
]
==
'ON'
:
json_state
[
i
][
'state'
]
=
'ON'
pwm_set
(
i
,
0
,
json_state
[
i
][
'white_value'
],
json_state
[
i
][
'brightness'
])
pwm_set
(
i
,
1
,
json_state
[
i
][
'color'
][
'g'
],
json_state
[
i
][
'brightness'
])
pwm_set
(
i
,
2
,
json_state
[
i
][
'color'
][
'b'
],
json_state
[
i
][
'brightness'
])
pwm_set
(
i
,
3
,
json_state
[
i
][
'color'
][
'r'
],
json_state
[
i
][
'brightness'
])
json_state
[
'state'
]
=
'ON'
pwm_set
(
json_state
[
'brightness'
])
else
:
json_state
[
i
][
'state'
]
=
'OFF'
pwm_set
(
i
,
0
,
0
)
pwm_set
(
i
,
1
,
0
)
pwm_set
(
i
,
2
,
0
)
pwm_set
(
i
,
3
,
0
)
json_state
[
'state'
]
=
'OFF'
pwm_set
(
0
)
# print the current value for debug purposes
print
(
"
chan: "
,
i
,
"
->"
,
json_state
[
i
]
)
MQTT
.
publish
(
base
+
"/state"
,
json
.
dumps
(
json_state
[
i
]
))
print
(
"
set
->"
,
json_state
)
MQTT
.
publish
(
base
+
"/state"
,
json
.
dumps
(
json_state
))
def
init_mqtt
(
mqtt
):
mqtt
.
connect
()
mqtt
.
set_callback
(
mqtt_cb
)
for
i
in
[
0
,
1
,
2
,
3
]:
for
i
in
[
0
]:
# prepare mqtt base and json autoconf payload
idd
=
"{}
_rgbw{}
"
.
format
(
UID
,
i
)
idd
=
"{}"
.
format
(
UID
)
base
=
"homeassistant/light/"
+
idd
json_autoconf
[
"~"
]
=
base
json_autoconf
[
"unique_id"
]
=
idd
...
...
@@ -184,13 +153,13 @@ def init_mqtt(mqtt):
# subsribe to mqtt, pubish the autoconf data
mqtt
.
subscribe
(
base
+
"/set"
)
mqtt
.
publish
(
base
+
"/config"
,
json
.
dumps
(
json_autoconf
))
mqtt
.
publish
(
base
+
"/state"
,
json
.
dumps
(
json_state
[
i
]))
print
(
base
)
print
(
"MQTT connection established"
)
green
.
off
()
print
(
"This is node {}"
.
format
(
UID
))
pwm_set
(
value
=
0
)
init_wifi
()
init_mqtt
(
MQTT
)
...
...
software/pca9685.py
View file @
7a68d727
...
...
@@ -21,6 +21,7 @@ class PCA9685:
if
freq
is
None
:
return
int
(
25000000.0
/
4096
/
(
self
.
_read
(
0xfe
)
-
0.5
))
prescale
=
int
(
25000000.0
/
4096.0
/
freq
+
0.5
)
prescale
=
min
(
3
,
prescale
)
old_mode
=
self
.
_read
(
0x00
)
# Mode 1
self
.
_write
(
0x00
,
(
old_mode
&
0x7F
)
|
0x10
)
# Mode 1, sleep
self
.
_write
(
0xfe
,
prescale
)
# Prescale
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment