Codementor Events

NodeMCU + TM1637 Display

Published Sep 27, 2022
NodeMCU + TM1637 Display

Hello, my name is Alex Polymath. And I'm making cool stuff.

Here is a simple example of NodeMCU + TM1637.
I use it to show statistics for my service - colorize.cc
It's pretty cool when you can make physical dashboard.

The scheme is simple. NodeMCU connects to wi-fi and then make GET request to my server every 10 seconds. Unfortunately it's pain to set up https connection. So I needed to modify my server to output it via http.

#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <ESP8266WiFiMulti.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include <TM1637Display.h>

const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display

const char* ssid     = "wifi network name";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "password wifi";     // The password of the Wi-Fi network
#include <ESP8266HTTPClient.h>

#include <Hash.h>
#define USE_SERIAL Serial

TM1637Display display(CLK, DIO); //set up the 4-Digit Display.



void setup() {
  display.setBrightness(0x0a);
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(10);
  Serial.println('\n');

  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }

  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer


}

void loop() { 
    
          if(WiFi.status()== WL_CONNECTED){
            
            WiFiClient client;
            HTTPClient http;
            //your server address 
            String serverPath =  "http://x.x.x.x";
            http.begin(client, serverPath.c_str());

            int httpResponseCode = http.GET();

      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload.toInt());
        display.showNumberDec(payload.toInt()); //Display the numCounter value;
        
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();            
            
          }
          
         delay(10000);
        

  
  }

  String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;
    
  // Your IP address with path or Domain name with URL path 
  http.begin(client, serverName);
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "{}"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}
Discover and read more posts from Alex Polymath
get started
post commentsBe the first to share your opinion
Show more replies