Skip to content

Instantly share code, notes, and snippets.

@auckenox
Last active December 26, 2022 22:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save auckenox/5ded017fbad1061c32d6b87bdf870eb9 to your computer and use it in GitHub Desktop.
Save auckenox/5ded017fbad1061c32d6b87bdf870eb9 to your computer and use it in GitHub Desktop.
ESP8266 I2C 7 segment display TM1637 http webserver
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <TM1637Display.h>
/*
thanks to:
https://github.com/avishorp/TM1637
http://www.esp8266learning.com/tm1637-7-segment-display-example.php
*/
// wifi data
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Module connection pins (Digital Pins)
const int CLK = D6; //Set the CLK pin connection to the display
const int DIO = D5; //Set the DIO pin connection to the display
ESP8266WebServer server(80);
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
};
const uint8_t SEG_START[] = {
SEG_A | SEG_F | SEG_G | SEG_C | SEG_D, // S
SEG_F | SEG_E | SEG_D | SEG_G, // t
SEG_E | SEG_G, // r
SEG_F | SEG_E | SEG_D | SEG_G // t
};
const uint8_t SEG_ON[] = {
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_C | SEG_E | SEG_G, // n
0x0, // segment inactive
0x0 // segment inactive
};
const uint8_t SEG_OFF[] = {
SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F, // O
SEG_A | SEG_E | SEG_F | SEG_G, // F
SEG_A | SEG_E | SEG_F | SEG_G, // F
0x0 // segment inactive
};
TM1637Display display(CLK, DIO);
void handleRoot() {
server.send(200, "text/html", "<html><body>feel free to try these: <br><ul><li><a href='/num?value=1338' target='f1'>set NUM to 1338</a></li> <li><a href='/num?value=8620' target='f1'>set NUM to 8620</a></li> <li><a href='/txt?value=DONE' target='f1'>set TXT to DONE</a></li> <li><a href='/txt?value=START' target='f1'>set TXT to START</a></li> <li><a href='/txt?value=ON' target='f1'>set TXT to ON</a></li> <li><a href='/txt?value=OFF' target='f1'>set TXT to OFF</a></li> <li>brightness: <a href='/bri?value=1' target='f1'>1</a> - <a href='/bri?value=4' target='f1'>4</a> - <a href='/bri?value=7' target='f1'>7</a> </li></ul> <iframe name='f1' src='/txt?value=START'></iframe></body></html>");
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (int i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void handle_brightness() {
int new_bri = server.arg("value").toInt();
Serial.print("handle_brightness "+new_bri);
display.setBrightness(new_bri);
server.send(200, "text/plain", String("changed brightness to ")+int(new_bri));
}
void handle_display_number() {
int dispval = server.arg("value").toInt();
Serial.print("handle_display_number "+dispval);
display.showNumberDec(dispval);
server.send(200, "text/plain", String("changed display to ")+int(dispval));
}
void handle_display_text() {
String txt_val = server.arg("value");
Serial.print("handle_display_text "+txt_val);
int validTextvalue = 0;
if(txt_val=="START") { display.setSegments(SEG_START); validTextvalue=1;}
if(txt_val=="DONE") { display.setSegments(SEG_DONE); validTextvalue=1;}
if(txt_val=="ON") { display.setSegments(SEG_ON); validTextvalue=1;}
if(txt_val=="OFF") { display.setSegments(SEG_OFF); validTextvalue=1;}
if(validTextvalue==1) {
server.send(200, "text/plain", String("changed display text to ")+txt_val);
}
else
{
// invalid text was choosen
server.send(200, "text/plain", String("unsupportet text: ")+txt_val+String(" please try: START, DONE, ON, OFF"));
}
}
void setup(void){
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/bri", handle_brightness);
server.on("/num", handle_display_number);
server.on("/txt", handle_display_text);
// setting start values
display.setBrightness(7); // set display to max brightness (from 1 to 7 where 7 is max brightness)
display.setSegments(SEG_START);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment