tm1637 display decimal point

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
bobbybeans
Normal user
Posts: 119
Joined: 26 Feb 2017, 17:30

tm1637 display decimal point

#1 Post by bobbybeans » 16 Jan 2018, 13:18

Hey guys
I am working on a project that once I have it working will grab temps from either domoticz or other esps(esp easy). currently I am using weather underground just for easy data parsing.
I need help...
I have the ENTIRE code working exactly as I want except the decimal points... :( :(
it grabs data from weather underground then parses it and puts it to a display a 4 digit 7 segment display driven by tm1637 driver. currently there is ALMOST no documentation on it but i got it working.
so it grabs data, parses it and then performs 4 tests on the data to see the temperature
if its single digit positive (8.5)
single digit negative (-4.4)
double digit positive (23.4)
double digit negative (-22.6)
based on the string length it returns a number if its that number it displays then parses the individual digits.
What I don't have working is the decimals on any of them. so its its 30.1C it displays 301 if its -9.5 its -95
I am using the TM1637.h from tubedisplay example because it has the degree symbol in it
there is another one TM1637Display.h which i don't believe has a degree symbol. If it does i dont know how to get it to turn on (help with that would be nice)
I have test code called test display that uses the TM1637Display.h library and it is able to light up the decimals
if i use the library i have it working on currently TM1637 i can perform a "tm1637.point(POINT_ON);" and it turns on the last 2 decimals of the last 2 digits. That command is supposed to turn on the colon for time however my display does not have that nor do i need that.
I can not figure a way to turn on individual dots even though i know they are working.
I tried integrating both libraries into the same code however when i initiate the last one, it takes over TM1637Display.h and then never gives back control to my original TM1637 .h. and even when i put some of the test code from the code below into my design it does not light up any decimal point

How can I get the decimals to turn on in the original TM1637.h or I can convert my code to TM1637Display.h but how can i display a degree sign . Id prefer to stick to what i have. Is there a manual (hex) way to call an individual dot that I am missing?

any help would be greatly appreciated

Code: Select all


//setup tm1637 display
#include <TM1637.h>
#include <TM1637Display.h>
#include <stdio.h>      /* printf */
#include <math.h>       /* floor */
#define CLK 4//pins definitions for TM1637 and can be changed to other ports       
#define DIO 5
TM1637 tm1637(CLK,DIO);
//TM1637Display display(CLK, DIO);

#include <ESP8266WiFi.h>
#ifndef min
#define min(x,y) (((x)<(y))?(x):(y))
#endif
#ifndef max
#define max(x,y) (((x)>(y))?(x):(y))
#endif
#include <ArduinoJson.h>

const char SSID[]     = "-----";
const char PASSWORD[] = "----!";

// Use your own API key by signing up for a free developer account.
// http://www.wunderground.com/weather/api/
#define WU_API_KEY "-------------"

// Specify your favorite location one of these ways.
#define WU_LOCATION "--------------"

// US ZIP code
//#define WU_LOCATION ""
//#define WU_LOCATION "90210"

// Country and city
//#define WU_LOCATION "---------"

// 30 minutes between update checks. The free developer account has a limit
// on the  number of calls so don't go wild.
#define DELAY_NORMAL    (30*60*1000)
// 20 minute delay between updates after an error
#define DELAY_ERROR     (60*60*1000)

#define WUNDERGROUND "api.wunderground.com"

// HTTP request
const char WUNDERGROUND_REQ[] =
    "GET /api/" WU_API_KEY "/conditions/q/" WU_LOCATION ".json HTTP/1.1\r\n"
    "User-Agent: ESP8266/0.1\r\n"
    "Accept: */*\r\n"
    "Host: " WUNDERGROUND "\r\n"
    "Connection: close\r\n"
    "\r\n";

void setup()
{
  tm1637.clearDisplay();
  tm1637.set(7); //BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  Serial.begin(115200);

  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print(F("Connecting to "));
  Serial.println(SSID);

  WiFi.begin(SSID, PASSWORD);

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

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

static char respBuf[4096];

void loop()
{
  // TODO check for disconnect from AP
  
  // Open socket to WU server port 80
  Serial.print(F("Connecting to "));
  Serial.println(WUNDERGROUND);

  // Use WiFiClient class to create TCP connections
  WiFiClient httpclient;
  const int httpPort = 80;
  if (!httpclient.connect(WUNDERGROUND, httpPort)) {
    Serial.println(F("connection failed"));
    delay(DELAY_ERROR);
    return;
  }

  // This will send the http request to the server
  Serial.print(WUNDERGROUND_REQ);
  httpclient.print(WUNDERGROUND_REQ);
  httpclient.flush();

  // Collect http response headers and content from Weather Underground
  // HTTP headers are discarded.
  // The content is formatted in JSON and is left in respBuf.
  int respLen = 0;
  bool skip_headers = true;
  while (httpclient.connected() || httpclient.available()) {
    if (skip_headers) {
      String aLine = httpclient.readStringUntil('\n');
      //Serial.println(aLine);
      // Blank line denotes end of headers
      if (aLine.length() <= 1) {
        skip_headers = false;
      }
    }
    else {
      int bytesIn;
      bytesIn = httpclient.read((uint8_t *)&respBuf[respLen], sizeof(respBuf) - respLen);
      Serial.print(F("bytesIn ")); Serial.println(bytesIn);
      if (bytesIn > 0) {
        respLen += bytesIn;
        if (respLen > sizeof(respBuf)) respLen = sizeof(respBuf);
      }
      else if (bytesIn < 0) {
        Serial.print(F("read error "));
        Serial.println(bytesIn);
      }
    }
    delay(1);
  }
  httpclient.stop();

  if (respLen >= sizeof(respBuf)) {
    Serial.print(F("respBuf overflow "));
    Serial.println(respLen);
    delay(DELAY_ERROR);
    return;
  }
  // Terminate the C string
  respBuf[respLen++] = '\0';
  Serial.print(F("respLen "));
  Serial.println(respLen);
  //Serial.println(respBuf);
 
  if (showWeather(respBuf)) {
    delay(DELAY_NORMAL);
  }
  else {
    delay(DELAY_ERROR);
  }
}

bool showWeather(char *json)
{
  StaticJsonBuffer<3*1024> jsonBuffer;

  // Skip characters until first '{' found
  // Ignore chunked length, if present
  char *jsonstart = strchr(json, '{');
  //Serial.print(F("jsonstart ")); Serial.println(jsonstart);
  if (jsonstart == NULL) {
    Serial.println(F("JSON data missing"));
    return false;
  }
  json = jsonstart;

  // Parse JSON
  JsonObject& root = jsonBuffer.parseObject(json);
  if (!root.success()) {
    Serial.println(F("jsonBuffer.parseObject() failed"));
    return false;
  }

  // Extract weather info from parsed JSON
  JsonObject& current = root["current_observation"];
  const float temp_f = current["temp_f"];
  Serial.print(temp_f, 1); Serial.print(F(" F, "));
  const float temp_c = current["temp_c"];
  //float temp_c = -6.5;
  Serial.print(temp_c, 1); Serial.print(F(" C, "));
  String temp_c_string =  current["temp_c"];
  //"-6.5";
  const char *humi = current[F("relative_humidity")];
  Serial.print(humi);   Serial.println(F(" RH"));
  const char *weather = current["weather"];
  Serial.println(weather);
  const char *pressure_mb = current["pressure_mb"];
  Serial.println(pressure_mb);
  const char *observation_time = current["observation_time_rfc822"];
  Serial.println(observation_time);

  // Extract local timezone fields
  const char *local_tz_short = current["local_tz_short"];
  Serial.println(local_tz_short);
  const char *local_tz_long = current["local_tz_long"];
  Serial.println(local_tz_long);
  const char *local_tz_offset = current["local_tz_offset"];
  Serial.println(local_tz_offset);
 
  int temp_string_length = temp_c_string.length();
  
  if (temp_string_length == 3)
  {
     Serial.println("its single digits positve"); 
    
     float temp_c_single_p = temp_c;
     int temp_c_single_p_add = temp_c_single_p *10;
     int temp_c_single_p_2nd_digit = temp_c_single_p_add - (floor(temp_c_single_p) *10 );     
     tm1637.display(1,floor(temp_c_single_p)); 
    // display.showNumberDecEx(0, (0x80 >> 2), true);
     //tm1637.point(POINT_ON);
     tm1637.display(2,temp_c_single_p_2nd_digit); 
     tm1637.display(3,18); 
     Serial.println(temp_c_single_p);
  }
  else if (temp_string_length == 4) 
  {
    if (temp_c_string.startsWith("-"))
    {
     Serial.println("its single digit negative");
     float temp_c_single_n = temp_c;
     int temp_c_single_n_1st_digit = temp_c;
     int temp_c_single_n_add = temp_c_single_n *10;
     int temp_c_single_n_2nd_digit = temp_c_single_n_add - (ceil(temp_c_single_n) *10 );
     tm1637.display(0,16);   
     tm1637.display(1,temp_c_single_n_1st_digit *-1);  //to change later once resd 
     tm1637.display(2,temp_c_single_n_2nd_digit*-1);
     tm1637.display(3,18);  
     Serial.println(temp_c_single_n);
     Serial.println(temp_c_single_n_1st_digit);
     Serial.println(temp_c_single_n_2nd_digit);
    }
    else
    {
     Serial.println("yay its double digit positive"); 
     float temp_c_double_p = temp_c * 10;
     int temp_c_double_p_final = floor(temp_c_double_p/100);
     int temp_c_double_c_final_2nd_digit = floor((temp_c_double_p -(temp_c_double_p_final * 100))/10); 
     tm1637.display(0,temp_c_double_p_final); 
     tm1637.display(1,temp_c_double_c_final_2nd_digit);  
     //tm1637.display(2,16);
     tm1637.display(3,18); 
     Serial.println(temp_c_double_p);
     Serial.println(temp_c_double_p_final);
     Serial.println(temp_c_double_c_final_2nd_digit);
    }
  }  
  else if(temp_string_length == 5) 
  {
     Serial.println("its double digits negative");
     float temp_c_double_n = temp_c * 10;
     int temp_c_double_n_final = ceil(temp_c_double_n/100);
     int temp_c_double_n_final_2nd_digit = ceil((temp_c_double_n -(temp_c_double_n_final * 100))/10); 
     tm1637.point(POINT_OFF);
     tm1637.display(0,16); 
     tm1637.display(1,(temp_c_double_n_final*-1));  
     tm1637.display(2,(temp_c_double_n_final_2nd_digit*-1));  
     tm1637.display(3,16);   
     Serial.println(temp_c_double_n);
     Serial.println(temp_c_double_n_final);
     Serial.println(temp_c_double_n_final_2nd_digit);
  }   
    //tm1637.display(0,16);  negative sign
    //tm1637.display(1,18); degree symbol
 // Serial.println(temp_c_string); 
 // Serial.println(temp_string_length); //length of charaters in string to perform ifthen 
  return true;
}

test display

Code: Select all

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

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3

// 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);

void setup()
{
}

void loop()
{
  int k;
  uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
  display.setBrightness(0x0f);

  // All segments on
  display.setSegments(data);
  delay(TEST_DELAY);

  // Selectively set different digits
  data[0] = 0b01001001;
  data[1] = display.encodeDigit(1);
  data[2] = display.encodeDigit(2);
  data[3] = display.encodeDigit(3);

  for(k = 3; k >= 0; k--) {
  display.setSegments(data, 1, k);
  delay(TEST_DELAY);
  }

  display.setSegments(data+2, 2, 2);
  delay(TEST_DELAY);

  display.setSegments(data+2, 2, 1);
  delay(TEST_DELAY);

  display.setSegments(data+1, 3, 1);
  delay(TEST_DELAY);


  // Show decimal numbers with/without leading zeros
  bool lz = false;
  for (uint8_t z = 0; z < 2; z++) {
  for(k = 0; k < 10000; k += k*4 + 7) {
    display.showNumberDec(k, lz);
    delay(TEST_DELAY);
  }
  lz = true;
  }

  // Show decimal number whose length is smaller than 4
  for(k = 0; k < 4; k++)
  data[k] = 0;
  display.setSegments(data);

  // Run through all the dots
  for(k=0; k <= 4; k++) {
    display.showNumberDecEx(0, (0x80 >> k), true);
    delay(TEST_DELAY);
  }

  display.showNumberDec(153, false, 3, 1);
  delay(TEST_DELAY);
  display.showNumberDec(22, false, 2, 2);
  delay(TEST_DELAY);
  display.showNumberDec(0, true, 1, 3);
  delay(TEST_DELAY);
  display.showNumberDec(0, true, 1, 2);
  delay(TEST_DELAY);
  display.showNumberDec(0, true, 1, 1);
  delay(TEST_DELAY);
  display.showNumberDec(0, true, 1, 0);
  delay(TEST_DELAY);

  // Brightness Test
  for(k = 0; k < 4; k++)
  data[k] = 0xff;
  for(k = 0; k < 7; k++) {
    display.setBrightness(k);
    display.setSegments(data);
    delay(TEST_DELAY);
  }
  
  // On/Off test
  for(k = 0; k < 4; k++) {
    display.setBrightness(7, false);  // Turn off
    display.setSegments(data);
    delay(TEST_DELAY);
    display.setBrightness(7, true); // Turn on
    display.setSegments(data);
    delay(TEST_DELAY);  
  }

  // Done!
  display.setSegments(SEG_DONE);

  while(1);
}
Last edited by bobbybeans on 16 Jan 2018, 16:09, edited 1 time in total.

papperone
Normal user
Posts: 497
Joined: 04 Oct 2016, 23:16

Re: tm1637 display decimal point

#2 Post by papperone » 16 Jan 2018, 14:59

this is NOT ESPEasy related and, by the way, I already develop a plugin tfor ESPEasy to support such device (and more to come!)
My TINDIE Store where you can find all ESP8266 boards I manufacture --> https://www.tindie.com/stores/GiovanniCas/
My Wiki Project page with self-made PCB/devices --> https://www.letscontrolit.com/wiki/inde ... :Papperone

bobbybeans
Normal user
Posts: 119
Joined: 26 Feb 2017, 17:30

Re: tm1637 display decimal point

#3 Post by bobbybeans » 16 Jan 2018, 16:07

yes it IS esp easy(yes in all technicality I did not mention esp easy, however i was implying that by saying "esps" i have updated it) related because i just told you my intention of gathering info from surrounding esps(THROUGH ESP EASY) and domoticz but this is the start of it by using test data of weather underground. and yes i realize there is support for tm1637's in espeasy mega however it does not do what i want it to do hence my own code. it will also have to control neo pixels.

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests