I tried combining an Oled LCD and a TM1637. The code cannot display the display on the LCD. Where did it go wrong?
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <TM1637Display.h>
#include <TimeLib.h>
#include <FS.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Define the connections pins
#define CLK D6
#define DIO D5
// Create a display object of type TM1637Display
TM1637Display displayled = TM1637Display(CLK, DIO);
// Create an array that turns all segments ON
const uint8_t allON[] = {0xff, 0xff, 0xff, 0xff};
// Create an array that turns all segments OFF
const uint8_t allOFF[] = {0x00, 0x00, 0x00, 0x00};
unsigned long clockMillis = 0;
unsigned long interval = 1000;
unsigned long sensorMillis1 = 0;
const long sensorInterval1 = 1000;
// Initialize Web Server on port 80
AsyncWebServer server(80);
// Function to handle GET request to /getnow
void handleGetNow(AsyncWebServerRequest *request) {
if (request->hasParam("hour") && request->hasParam("minute")) {
int h = request->getParam("hour")->value().toInt();
int m = request->getParam("minute")->value().toInt();
setTime(h, m, 0, day(), month(), year());
// Display time on TM1637
displayTime(h, m);
}
request->send(200, "text/plain", "Time updated");
}
// Function to display time on TM1637
void displayTime(int hour, int minute) {
int displayTime = hour * 100 + minute;
displayled.showNumberDecEx(displayTime, 0b01000000, true, 4, 0);
}
void setup() {
Serial.begin(115200);
// Setup WiFi as Access Point
WiFi.softAP("ESP8266_Time_Setter");
// Initialize SPIFFS
if (!SPIFFS.begin()) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Set the brightness to 7 (0=dimmest 7=brightest)
displayled.setBrightness(7);
// Set all segments ON
displayled.setSegments(allON);
displayled.clear();
display.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, BLACK);
display.display();
// Initialize time to 00:00:00
setTime(0, 0, 0, 1, 1, 1970);
// Serve static files
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.on("/getnow", HTTP_GET, handleGetNow);
server.begin();
Serial.println("HTTP server started");
}
const unsigned char epd_bitmap_temperature [] PROGMEM = {
0xff, 0xf8, 0xf8, 0xf8, 0xf2, 0x78, 0xf7, 0x78, 0xf5, 0x78, 0xf5, 0x78, 0xf5, 0x78, 0xe8, 0xb8,
0xe8, 0xb8, 0xe8, 0xb8, 0xf7, 0x78, 0xfd, 0xf8, 0xff, 0xf8
};
const unsigned char epd_bitmap_barometer [] PROGMEM = {
0xff, 0xf8, 0xf0, 0x78, 0xe0, 0x38, 0xe7, 0x38, 0xc2, 0x18, 0xc2, 0x18, 0xe0, 0x38, 0xe0, 0x38,
0xf8, 0xf8, 0xf8, 0xf8, 0x80, 0x08, 0x80, 0x08, 0xff, 0xf8
};
const unsigned char epd_bitmap_height [] PROGMEM = {
0xff, 0xf8, 0xfd, 0xf8, 0xf8, 0xf8, 0xfd, 0xf8, 0xfd, 0xf8, 0xfd, 0xf8, 0xfd, 0xf8, 0xfd, 0xf8,
0xfd, 0xf8, 0xfd, 0xf8, 0xf8, 0xf8, 0xfd, 0xf8, 0xff, 0xf8
};
void loop() {
if (millis() - clockMillis >= interval) {
clockMillis = millis();
if (timeStatus() == timeNotSet) {
// Manually increment time by one second if time has not been set
adjustTime(1);
}
int h = hour();
int m = minute();
displayTime(h, m);
}
display.setTextSize(1);
display.setTextColor(WHITE);
String displayHeader = "PORTABLE WEATHER";
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(displayHeader, 0, 0, &x1, &y1, &w, &h);
display.setCursor((SCREEN_WIDTH - w) / 2, 0); // Center the text
display.println(displayHeader);
display.display();
if(millis() - sensorMillis1 >= sensorInterval1) {
sensorMillis1 = millis();
// Hapus hanya area sensor dari layar
display.fillRect(0, 16, SCREEN_WIDTH, SCREEN_HEIGHT - 16, BLACK);
display.setTextSize(1);
display.setTextColor(WHITE);
display.drawBitmap(0, 16, epd_bitmap_temperature, 13, 13, 1);
display.setCursor(20, 18);
display.print("50");
display.println(" *C");
display.drawBitmap(0, 32, epd_bitmap_barometer, 13, 13, 1);
display.setCursor(20, 34);
display.print("50");
display.println(" hPa");
display.drawBitmap(0, 48, epd_bitmap_height, 13, 13, 1);
display.setCursor(20, 50);
display.print("50");
display.println(" m");
display.display(); // Show data
}
}