Skip to content

Commit cc0ae07

Browse files
Example of communicating to Home Assistant via a d1-mini esphome device.
1 parent a3ba840 commit cc0ae07

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

additional/bulb-off.png

4.38 KB
Loading

additional/bulb-on.png

4.23 KB
Loading

additional/esphome.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Example that connects to an esphome coprocessor
2+
# The D1 Mini is connected over the stemma port
3+
4+
import badger2040
5+
import time
6+
import pngdec
7+
from machine import Pin
8+
9+
BUTTON_A = "buttonA"
10+
BUTTON_B = "buttonB"
11+
state = {BUTTON_A: 0, BUTTON_B: 0}
12+
13+
display = badger2040.Badger2040()
14+
display.led(128)
15+
display.clear()
16+
17+
display.set_update_speed(badger2040.UPDATE_FAST)
18+
png = pngdec.PNG(display.display)
19+
20+
changed = True
21+
22+
def wait_for_user_to_release_buttons():
23+
while display.pressed_any():
24+
time.sleep(0.01)
25+
26+
def draw_badge():
27+
display.clear()
28+
29+
if state[BUTTON_A] == 1:
30+
BADGE_IMAGE = "bulb-on.png"
31+
else:
32+
BADGE_IMAGE = "bulb-off.png"
33+
34+
try:
35+
png.open_file(BADGE_IMAGE)
36+
png.decode(0, 0)
37+
except OSError:
38+
print("Badge background error")
39+
40+
display.update()
41+
42+
43+
# Update the pins on the switch
44+
def update_pins(a, b):
45+
wait_for_user_to_release_buttons()
46+
47+
buttonA = Pin(4, Pin.OUT)
48+
buttonB = Pin(5, Pin.OUT)
49+
50+
buttonA.value(a)
51+
buttonB.value(b)
52+
53+
54+
# Main loop
55+
# Note this implementation is super power innefficient
56+
# as it's for on-stage use.
57+
58+
while True:
59+
display.keepalive()
60+
61+
if display.pressed(badger2040.BUTTON_A):
62+
if state[BUTTON_A] == 1:
63+
state[BUTTON_A] = 0
64+
else:
65+
state[BUTTON_A] = 1
66+
changed = True
67+
68+
if display.pressed(badger2040.BUTTON_B):
69+
if state[BUTTON_B] == 1:
70+
state[BUTTON_B] = 0
71+
else:
72+
state[BUTTON_B] = 1
73+
changed = True
74+
75+
if changed:
76+
update_pins(state[BUTTON_A],state[BUTTON_B])
77+
draw_badge()
78+
changed = False
79+

additional/esphome.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
substitutions:
2+
name: esphome-web-8e7525-CHANGEME
3+
friendly_name: Badger
4+
5+
esphome:
6+
name: ${name}
7+
friendly_name: ${friendly_name}
8+
min_version: 2024.6.0
9+
name_add_mac_suffix: false
10+
project:
11+
name: esphome.web
12+
version: dev
13+
14+
esp8266:
15+
board: d1_mini_lite
16+
17+
# Enable logging
18+
logger:
19+
20+
# Enable Home Assistant API
21+
api:
22+
23+
# Allow Over-The-Air updates
24+
ota:
25+
- platform: esphome
26+
27+
# Allow provisioning Wi-Fi via serial
28+
improv_serial:
29+
30+
wifi:
31+
# Set up a wifi access point
32+
ap: {}
33+
34+
# In combination with the `ap` this allows the user
35+
# to provision wifi credentials to the device via WiFi AP.
36+
captive_portal:
37+
38+
dashboard_import:
39+
package_import_url: github://esphome/example-configs/esphome-web/esp8266.yaml@main
40+
import_full_config: true
41+
42+
# To have a "next url" for improv serial
43+
web_server:
44+
45+
binary_sensor:
46+
- platform: gpio
47+
pin: D4
48+
name: "BadgerB"
49+
device_class: light
50+
- platform: gpio
51+
pin: D3
52+
name: "BadgerA"
53+
device_class: light

0 commit comments

Comments
 (0)