Skip to content

Instantly share code, notes, and snippets.

@cowchimp
Last active July 13, 2024 05:23
Show Gist options
  • Save cowchimp/f9345a4b0735391cf72fa5383e5770c6 to your computer and use it in GitHub Desktop.
Save cowchimp/f9345a4b0735391cf72fa5383e5770c6 to your computer and use it in GitHub Desktop.
#include <WiFi.h>
#include <PubSubClient.h>
// a
// ---
// | |
// f | | b
// | |
// --- <----- g
// | |
// e | | c
// | |
// ---
// d
// https://docs.espressif.com/projects/esp-idf/en/stable/esp32c3/_images/esp32-c3-devkitc-02-v1-pinout.png
const int a=10;
const int b=20;
const int c=7;
const int d=5;
const int e=4;
const int f=1;
const int g=6;
const int dp=3;
const int resetPin = 2;
int prevResetState = HIGH;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
pinMode(dp,OUTPUT);
pinMode(resetPin, INPUT_PULLUP);
setupWifi("ssid", "password");
client.setServer("homeassistant.local", 1883);
client.setCallback(callback);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("7SegmentDisplay")) {
Serial.println("connected");
client.publish("counter:client", "setup");
client.subscribe("counter:server");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
String payloadStr = "";
for (int i = 0; i < length; i++) {
payloadStr += (char)payload[i];
}
int counterValue = payloadStr.toInt();
displayNumber(counterValue);
client.publish("counter:client", "ack");
}
void loop() {
int resetState = digitalRead(resetPin);
if (resetState == LOW && prevResetState == HIGH) {
client.publish("counter:client", "reset");
}
prevResetState = resetState;
if (!client.connected()) {
reconnect();
}
client.loop();
delay(2); // allow the CPU to switch to other tasks
}
void setupWifi(const char* ssid, const char* password) {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void displayNumber(int number) {
if (number == 0) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
return;
}
if (number == 1) {
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
return;
}
if (number == 2) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 3) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 4) {
digitalWrite(a,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 5) {
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 6) {
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 7) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
return;
}
if (number == 8) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
if (number == 9) {
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
return;
}
// E
digitalWrite(a,HIGH);
digitalWrite(b,LOW);
digitalWrite(c,LOW);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
- alias: mqtt count changed
description: when counter value changes then publish message to board
trigger:
- platform: state
entity_id:
- counter.hearing_aid_battery_days
condition: []
action:
- service: mqtt.publish
metadata: {}
data:
topic: counter:server
payload: '{{ states(''counter.hearing_aid_battery_days'') }}'
- wait_for_trigger:
- platform: mqtt
topic: counter:client
payload: ack
timeout:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- if:
- condition: template
value_template: '{{ wait.trigger == none }}'
then:
- service: notify.pushover
data:
title: Did not receive ACK from counter board!
message: :(
mode: single
- alias: mqtt counter inc at 1am
description: increment the counter value at 1am every day
trigger:
- platform: time
at: 01:00:00
condition: []
action:
- service: counter.increment
metadata: {}
data: {}
target:
entity_id: counter.hearing_aid_battery_days
mode: single
- alias: mqtt counter reset
description: reset the counter back to zero when a reset message is received
trigger:
- platform: mqtt
topic: counter:client
payload: reset
condition: []
action:
- service: counter.reset
target:
entity_id:
- counter.hearing_aid_battery_days
data: {}
mode: single
- alias: mqtt counter setup
description: publish the counter value to the board when a setup message is received
trigger:
- platform: mqtt
topic: counter:client
payload: setup
condition: []
action:
- delay:
hours: 0
minutes: 0
seconds: 5
milliseconds: 0
- service: mqtt.publish
metadata: {}
data:
topic: counter:server
payload: '{{ states(''counter.hearing_aid_battery_days'') }}'
mode: single
# Loads default set of integrations. Do not remove.
default_config:
# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
counter:
hearing_aid_battery_days:
name: 'Hearing aid Battery Days'
icon: 'mdi:battery-clock-outline'
initial: 0
step: 1
minimum: 0
maximum: 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment