TM1637 7 Segment Module (with mqtt )

Last Updated or created 2023-01-05

These tiny modules use a Clock and Data signal.
The rest of the pins are for 3.3V and Gnd.

There are many libraries you can choose from, i’ve tried several.
For now, i ended up with this one.

Another promising one is https://github.com/AKJ7/TM1637
Another one i’ve tested https://github.com/bxparks/AceSegment

Now i have to add Wifi and Mqtt so it can remotely controlled.

Connected like this

Code with Wifi/Mqtt

#include <Arduino.h>
#include <TM1637Display.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "MYSSID";
const char* password = "MYSSIDPASS";
const char* mqtt_server = "MQTT-SERVER-IP";
 
const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display
 
int numCounter = 0;
int mydata = 0;
 
// The amount of time (in milliseconds) between tests
#define TEST_DELAY   2000

const uint8_t SEG_DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
  };

TM1637Display display(CLK, DIO);

WiFiClient espClient;
PubSubClient client(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE  (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;


void setup()
{
 display.setBrightness(0x0a); //set the diplay to maximum brightness
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}


void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);


  randomSeed(micros());

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}



void callback(char* topic, byte* payload, unsigned int length) {
  char buffer[4];
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    buffer[i] = int(payload[i]);
  }
  Serial.println();
//  mydata=int(payload[0])+int(payload[1]*10);
int  n;
n = atoi(buffer);

  display.showNumberDec(n); //Display the numCounter value;


}




void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("escape/seg1ping", "seg1alive");
      // ... and resubscribe
      client.subscribe("escape/seg1data");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  unsigned long now = millis();
  if (now - lastMsg > 2000) {
    lastMsg = now;
    ++value;
    snprintf (msg, MSG_BUFFER_SIZE, "seg1alive #%ld", value);
    Serial.print("Publish message: ");
    Serial.println(msg);
    client.publish("escape/seg1ping", msg);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *