TM1637 support

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
MegaBOOBLIK
New user
Posts: 3
Joined: 08 Apr 2016, 15:04

TM1637 support

#1 Post by MegaBOOBLIK » 02 Jun 2016, 10:01

Hi!
ESPEasy it's very great softvare!
But wey dont have support TM1637.
I'`m made very simle clock and want some more, termal sensor with MQTT.
Does some one made something with TM1637 display?


Based on this post http://samopal.pro/esp8266-4/
My code

Code: Select all

#include "TM1637.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

//CLK,DIO
TM1637 tm1637(13, 14);

char ssid[] = "XXX";
char pass[] = "YYY";

#define TIMEZONE 3

unsigned int  localPort = 2390;   // local port to listen for UDP packets
unsigned int ntp_corr = 60;        // how often make correct (min)
unsigned int ntp_count = 0;       // Correct counter
unsigned long ntp_time = 0;       // TIME
bool points   = true;                   // blinking points

IPAddress timeServerIP;
const char* ntpServerName = "time.nist.gov";

const int NTP_PACKET_SIZE = 48;
byte packetBuffer[ NTP_PACKET_SIZE];

WiFiUDP udp;

void setup()
{
  tm1637.init(); // CLEAR DISP
  tm1637.set(7); // DARKEST = 0, BRIGHTEST = 7
  for ( int i = 0; i < 4; i++) {
    tm1637.display(i, i);
    delay(300);
  }

  Serial.begin(115200);

  // WiFi
  if ( !ConnectWiFi(ssid, pass) ) ESP.reset();

  // START UDP connect NTP
  udp.begin(localPort);

  while (ntp_time == 0) GetNTP();
}

void loop() {
  tm1637.display(0, ( ( ntp_time / 3600 ) % 24 ) / 10);
  tm1637.display(1, ( ( ntp_time / 3600 ) % 24 ) % 10);
  tm1637.display(2, ( ( ntp_time / 60 ) % 60 ) / 10);
  tm1637.display(3, ( ( ntp_time / 60 ) % 60 ) % 10);
  ntp_time++;
  ntp_count++;
  tm1637.point(points);
  points = !points;
  delay(1000);
  if ( ntp_count == ( ntp_corr * 60 ) ) GetNTP();
}

// WiFi
bool ConnectWiFi(const char *ssid, const char *pass) {
  for ( int i = 0; i < 4; i++) {
    WiFi.begin(ssid, pass);
    delay(5000);
    if (WiFi.status() == WL_CONNECTED) {
      Serial.print("\nWiFi connect true: ");
      Serial.print(WiFi.localIP());
      Serial.print(" / ");
      Serial.print(WiFi.subnetMask());
      Serial.print(" / ");
      Serial.print(WiFi.gatewayIP());
      Serial.print("\n");
      return true;
    }
    tm1637.init(); // CLEAR DISP
    tm1637.display(i, WiFi.status()); // STATUS
  }
  Serial.println("\nConnect WiFi failed ...");
  return false;
}

// NTP server request
bool GetNTP(void) {
  WiFi.hostByName(ntpServerName, timeServerIP);

  // 
  Serial.println("Sending NTP packet...");

  // clear to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);

  // 
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  // NTP to 123 port
  udp.beginPacket(timeServerIP, 123);
  udp.write(packetBuffer, NTP_PACKET_SIZE);
  udp.endPacket();

  delay(3000);
  int cb = udp.parsePacket();
  if (!cb) {
    Serial.println("No packet...");

    //BAD NTP
    for ( int i = 0; i < 4; i++) {
      tm1637.display(i, 8);
      delay(100);
    }
    delay (100);
    tm1637.init(); // CLEAR DISP
    return false;
  }
  else {
    // Читаем пакет в буфер
    udp.read(packetBuffer, NTP_PACKET_SIZE);
    // 4 байта начиная с 40-го сождержат таймстамп времени - число секунд
    // от 01.01.1900
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // Конвертируем два слова в переменную long
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // Конвертируем в UNIX-таймстамп (число секунд от 01.01.1970
    const unsigned long seventyYears = 2208988800UL;
    unsigned long epoch = secsSince1900 - seventyYears;
    // Делаем поправку на местную тайм-зону
    ntp_time = epoch + TIMEZONE * 3600;
    Serial.print("Unix time = ");
    Serial.println(ntp_time);
    ntp_count = 0;
  }
  return true;
}

NietGiftig
Normal user
Posts: 103
Joined: 16 Sep 2015, 20:32

Re: TM1637 support

#2 Post by NietGiftig » 02 Jun 2016, 12:45

MegaBOOBLIK wrote:Hi!
ESPEasy it's very great softvare!
But wey dont have support TM1637.
I'm not sure if you are in the right forum, this is for the RFLink software

But for your question : Maybe its because you are the only one using the TM1637
But you are free to make a plugin and submit it to ESPEasy software.
https://github.com/ESP8266nu/ESPEasyPluginPlayground

mili14
New user
Posts: 1
Joined: 09 Jan 2017, 19:04

Re: TM1637 support

#3 Post by mili14 » 09 Jan 2017, 19:09

Hello,
I found http://robotdyn.com/catalog/segment/4_d ... _segments/

is any chance for support it?

Post Reply

Who is online

Users browsing this forum: No registered users and 102 guests