personas:johnny:proyectos:matrices-led
                Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| personas:johnny:proyectos:matrices-led [2021/10/16 18:03] – kz | personas:johnny:proyectos:matrices-led [2021/10/16 18:54] (actual) – kz | ||
|---|---|---|---|
| Línea 10: | Línea 10: | ||
| **Code** | **Code** | ||
| + | |||
| + | Este código hace que el esp sirva una página web, donde se escribe un mensaje y se presenta en la pantalla | ||
| <code c++> | <code c++> | ||
| - | void init() { | + | #include < | 
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define PRINT_CALLBACK | ||
| + | #define DEBUG 0 | ||
| + | #define LED_HEARTBEAT 0 | ||
| + | |||
| + | #if DEBUG | ||
| + | #define PRINT(s, v) { Serial.print(F(s)); | ||
| + | #define PRINTS(s) | ||
| + | #else | ||
| + | #define PRINT(s, v) | ||
| + | #define PRINTS(s) | ||
| + | #endif | ||
| + | |||
| + | |||
| + | #if LED_HEARTBEAT | ||
| + | #define HB_LED | ||
| + | #define HB_LED_TIME 500 // in milliseconds | ||
| + | #endif | ||
| + | |||
| + | // Define the number of devices we have in the chain and the hardware interface | ||
| + | // NOTE: These pin numbers will probably not work with your hardware and may | ||
| + | // need to be adapted | ||
| + | #define HARDWARE_TYPE MD_MAX72XX:: | ||
| + | #define MAX_DEVICES 4 | ||
| + | |||
| + | #define CLK_PIN | ||
| + | #define DATA_PIN | ||
| + | #define CS_PIN | ||
| + | |||
| + | // SPI hardware interface | ||
| + | MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, | ||
| + | // Arbitrary pins | ||
| + | // | ||
| + | |||
| + | // WiFi login parameters - network name and password | ||
| + | const char* ssid = "name wifi"; | ||
| + | const char* password = "clave wifi"; | ||
| + | |||
| + | // WiFi Server object and parameters | ||
| + | WiFiServer server(80); | ||
| + | |||
| + | // Global message buffers shared by Wifi and Scrolling functions | ||
| + | const uint8_t MESG_SIZE = 255; | ||
| + | const uint8_t CHAR_SPACING = 1; | ||
| + | const uint8_t SCROLL_DELAY = 75; | ||
| + | |||
| + | char curMessage[MESG_SIZE]; | ||
| + | char newMessage[MESG_SIZE]; | ||
| + | bool newMessageAvailable = false; | ||
| + | |||
| + | const char WebResponse[] = " | ||
| + | |||
| + | const char WebPage[] = | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "html, body" \ | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | "# | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | "</ | ||
| + | "< | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | "</ | ||
| + | "</ | ||
| + | "< | ||
| + | "< | ||
| + | |||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "</ | ||
| + | "< | ||
| + | "< | ||
| + | "</ | ||
| + | "</ | ||
| + | "</ | ||
| + | |||
| + | const char *err2Str(wl_status_t code) | ||
| + | { | ||
| + | switch (code) | ||
| + | { | ||
| + | case WL_IDLE_STATUS: | ||
| + | case WL_NO_SSID_AVAIL: | ||
| + | case WL_CONNECTED: | ||
| + | case WL_CONNECT_FAILED: | ||
| + | case WL_DISCONNECTED: | ||
| + | default: return("??" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | uint8_t htoi(char c) | ||
| + | { | ||
| + | c = toupper(c); | ||
| + | if ((c >= ' | ||
| + | if ((c >= ' | ||
| + | return(0); | ||
| + | } | ||
| + | |||
| + | boolean getText(char *szMesg, char *psz, uint8_t len) | ||
| + | { | ||
| + | boolean isValid = false; | ||
| + | char *pStart, *pEnd; | ||
| + | |||
| + | // get pointer to the beginning of the text | ||
| + | pStart = strstr(szMesg, | ||
| + | |||
| + | if (pStart != NULL) | ||
| + | { | ||
| + | pStart += 6; // skip to start of data | ||
| + | pEnd = strstr(pStart, | ||
| + | |||
| + | if (pEnd != NULL) | ||
| + | { | ||
| + | while (pStart != pEnd) | ||
| + | { | ||
| + | if ((*pStart == ' | ||
| + | { | ||
| + | // replace %xx hex code with the ASCII character | ||
| + | char c = 0; | ||
| + | pStart++; | ||
| + | c += (htoi(*pStart++) << 4); | ||
| + | c += htoi(*pStart++); | ||
| + | *psz++ = c; | ||
| + | } | ||
| + | else | ||
| + | *psz++ = *pStart++; | ||
| + | } | ||
| + | |||
| + | *psz = ' | ||
| + | isValid = true; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | return(isValid); | ||
| + | } | ||
| + | |||
| + | void handleWiFi(void) | ||
| + | { | ||
| + | static enum { S_IDLE, S_WAIT_CONN, | ||
| + | static char szBuf[1024]; | ||
| + | static uint16_t idxBuf = 0; | ||
| + | static WiFiClient client; | ||
| + | static uint32_t timeStart; | ||
| + | |||
| + | switch (state) | ||
| + | { | ||
| + | case S_IDLE: | ||
| + | PRINTS(" | ||
| + | idxBuf = 0; | ||
| + | state = S_WAIT_CONN; | ||
| + | break; | ||
| + | |||
| + | case S_WAIT_CONN: | ||
| + | { | ||
| + | client = server.available(); | ||
| + | if (!client) break; | ||
| + | if (!client.connected()) break; | ||
| + | |||
| + | #if DEBUG | ||
| + | char szTxt[20]; | ||
| + | sprintf(szTxt, | ||
| + | PRINT(" | ||
| + | #endif | ||
| + | |||
| + | timeStart = millis(); | ||
| + | state = S_READ; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | case S_READ: // get the first line of data | ||
| + | PRINTS(" | ||
| + | while (client.available()) | ||
| + | { | ||
| + | char c = client.read(); | ||
| + | if ((c == ' | ||
| + | { | ||
| + | szBuf[idxBuf] = ' | ||
| + | client.flush(); | ||
| + | PRINT(" | ||
| + | state = S_EXTRACT; | ||
| + | } | ||
| + | else | ||
| + | szBuf[idxBuf++] = (char)c; | ||
| + | } | ||
| + | if (millis() - timeStart > 1000) | ||
| + | { | ||
| + | PRINTS(" | ||
| + | state = S_DISCONN; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | |||
| + | case S_EXTRACT: // extract data | ||
| + | PRINTS(" | ||
| + | // Extract the string from the message if there is one | ||
| + | newMessageAvailable = getText(szBuf, | ||
| + | PRINT(" | ||
| + | state = S_RESPONSE; | ||
| + | break; | ||
| + | |||
| + | case S_RESPONSE: // send the response to the client | ||
| + | PRINTS(" | ||
| + | // Return the response to the client (web page) | ||
| + | client.print(WebResponse); | ||
| + | client.print(WebPage); | ||
| + | state = S_DISCONN; | ||
| + | break; | ||
| + | |||
| + | case S_DISCONN: // disconnect client | ||
| + | PRINTS(" | ||
| + | client.flush(); | ||
| + | client.stop(); | ||
| + | state = S_IDLE; | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void scrollDataSink(uint8_t dev, MD_MAX72XX:: | ||
| + | // Callback function for data that is being scrolled off the display | ||
| + | { | ||
| + | #if PRINT_CALLBACK | ||
| + | Serial.print(" | ||
| + | Serial.print(dev); | ||
| + | Serial.print(' | ||
| + | Serial.print(t); | ||
| + | Serial.print(' | ||
| + | Serial.println(col); | ||
| + | #endif | ||
| + | } | ||
| + | |||
| + | uint8_t scrollDataSource(uint8_t dev, MD_MAX72XX:: | ||
| + | // Callback function for data that is required for scrolling into the display | ||
| + | { | ||
| + | static enum { S_IDLE, S_NEXT_CHAR, | ||
| + | static char *p; | ||
| + | static uint16_t curLen, showLen; | ||
| + | static uint8_t | ||
| + | uint8_t colData = 0; | ||
| + | |||
| + | // finite state machine to control what we do on the callback | ||
| + | switch (state) | ||
| + | { | ||
| + | case S_IDLE: // reset the message pointer and check for new message to load | ||
| + | PRINTS(" | ||
| + | p = curMessage; | ||
| + | if (newMessageAvailable) | ||
| + | { | ||
| + | strcpy(curMessage, | ||
| + | newMessageAvailable = false; | ||
| + | } | ||
| + | state = S_NEXT_CHAR; | ||
| + | break; | ||
| + | |||
| + | case S_NEXT_CHAR: | ||
| + | PRINTS(" | ||
| + | if (*p == ' | ||
| + | state = S_IDLE; | ||
| + | else | ||
| + | { | ||
| + | showLen = mx.getChar(*p++, | ||
| + | curLen = 0; | ||
| + | state = S_SHOW_CHAR; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | case S_SHOW_CHAR: | ||
| + | PRINTS(" | ||
| + | colData = cBuf[curLen++]; | ||
| + | if (curLen < showLen) | ||
| + | break; | ||
| + | |||
| + | // set up the inter character spacing | ||
| + | showLen = (*p != ' | ||
| + | curLen = 0; | ||
| + | state = S_SHOW_SPACE; | ||
| + | // fall through | ||
| + | |||
| + | case S_SHOW_SPACE: | ||
| + | PRINT(" | ||
| + | PRINT("/", | ||
| + | curLen++; | ||
| + | if (curLen == showLen) | ||
| + | state = S_NEXT_CHAR; | ||
| + | break; | ||
| + | |||
| + | default: | ||
| + | state = S_IDLE; | ||
| + | } | ||
| + | |||
| + | return(colData); | ||
| + | } | ||
| + | |||
| + | void scrollText(void) | ||
| + | { | ||
| + | static uint32_t prevTime = 0; | ||
| + | |||
| + | // Is it time to scroll the text? | ||
| + | if (millis() - prevTime >= SCROLL_DELAY) | ||
| + | { | ||
| + | mx.transform(MD_MAX72XX:: | ||
| + | prevTime = millis(); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | #if DEBUG | ||
| + | Serial.begin(115200); | ||
| + | PRINTS(" | ||
| + | #endif | ||
| + | |||
| + | #if LED_HEARTBEAT | ||
| + | pinMode(HB_LED, | ||
| + | digitalWrite(HB_LED, | ||
| + | #endif | ||
| + | |||
| + | // Display initialization | ||
| + | mx.begin(); | ||
| + | mx.setShiftDataInCallback(scrollDataSource); | ||
| + | mx.setShiftDataOutCallback(scrollDataSink); | ||
| + | |||
| + | curMessage[0] = newMessage[0] = ' | ||
| + | |||
| + | // Connect to and initialize WiFi network | ||
| + | PRINT(" | ||
| + | |||
| + | WiFi.begin(ssid, | ||
| + | |||
| + | while (WiFi.status() != WL_CONNECTED) | ||
| + | { | ||
| + | PRINT(" | ||
| + | delay(500); | ||
| + | } | ||
| + | PRINTS(" | ||
| + | |||
| + | // Start the server | ||
| + | server.begin(); | ||
| + | PRINTS(" | ||
| + | |||
| + | // Set up first message as the IP address | ||
| + | sprintf(curMessage, | ||
| + | PRINT(" | ||
| + | } | ||
| + | |||
| + | void loop() | ||
| + | { | ||
| + | #if LED_HEARTBEAT | ||
| + | static uint32_t timeLast = 0; | ||
| + | |||
| + | if (millis() - timeLast >= HB_LED_TIME) | ||
| + | { | ||
| + | digitalWrite(HB_LED, | ||
| + | timeLast = millis(); | ||
| + | } | ||
| + | #endif | ||
| + | |||
| + | handleWiFi(); | ||
| + | scrollText(); | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | Este otro código es tomado de internet del señor [[https:// | ||
| + | |||
| + | <code c++> | ||
| + | /* | ||
| + | Programa: Wifi controlled LED matrix display | ||
| + | Autor: | ||
| + | Web: www.humbertohiginio.com | ||
| + | Canal de Youtube: https:// | ||
| + | Video Exclusivo para mi canal de Youtube | ||
| + | Todos los Derechos Reservados - 2018 | ||
| + | Código de Dominio Público | ||
| + | |||
| + | Wemos D1 Mini o NodeMCU pines -> Matrix pines | ||
| + | MOSI-D7-GPIO13 | ||
| + | CLK-D5-GPIO14 | ||
| + | GPIO0-D3 | ||
| + | |||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define SSID " | ||
| + | #define PASS " | ||
| + | // ******************* String form to sent to the client-browser ************************************ | ||
| + | String form = | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "< | ||
| + | "</ | ||
| + | |||
| + | ESP8266WebServer server(80); | ||
| + | long period; | ||
| + | int offset=1, | ||
| + | int pinCS = 0; // Attach CS to this pin, DIN to MOSI and CLK to SCK (cf http:// | ||
| + | int numberOfHorizontalDisplays = 8; | ||
| + | int numberOfVerticalDisplays = 1; | ||
| + | String decodedMsg; | ||
| + | Max72xxPanel matrix = Max72xxPanel(pinCS, | ||
| + | |||
| + | String tape = " | ||
| + | int wait = 20; // In milliseconds | ||
| + | |||
| + | int spacer = 2; | ||
| + | int width = 5 + spacer; // The font width is 5 pixels | ||
| + | |||
| + | /* | ||
| + | handles the messages coming from the webbrowser, restores a few special characters and | ||
| + | constructs the strings that can be sent to the oled display | ||
| + | */ | ||
| + | void handle_msg() { | ||
| + |  | ||
| + | matrix.fillScreen(LOW); | ||
| + | server.send(200, | ||
| + | refresh=1; | ||
| + | // Display msg on Oled | ||
| + | String msg = server.arg(" | ||
| + | Serial.println(msg); | ||
| + | decodedMsg = msg; | ||
| + | // Restore special characters that are misformed to %char by the client browser | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | decodedMsg.replace(" | ||
| + | // | ||
| + | |||
| + | |||
| + |  | ||
| + | // | ||
| + | } | ||
| + | |||
| + | void setup(void) { | ||
| + | matrix.setIntensity(10); | ||
| + | |||
| + | // Adjust to your own needs | ||
| + | //  matrix.setPosition(0, | ||
| + | //  matrix.setPosition(1, | ||
| + | |||
| + | // Adjust to your own needs | ||
| + | matrix.setPosition(0, | ||
| + | matrix.setPosition(1, | ||
| + | matrix.setPosition(2, | ||
| + | matrix.setPosition(3, | ||
| + | matrix.setPosition(4, | ||
| + | matrix.setPosition(5, | ||
| + | matrix.setPosition(6, | ||
| + | matrix.setPosition(7, | ||
| + |  | ||
| + | matrix.setRotation(0, | ||
| + | matrix.setRotation(1, | ||
| + | matrix.setRotation(2, | ||
| + | matrix.setRotation(3, | ||
| + | matrix.setRotation(4, | ||
| + | matrix.setRotation(5, | ||
| + | matrix.setRotation(6, | ||
| + | matrix.setRotation(7, | ||
| + |  | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | // | ||
| + | Serial.begin(115200); | ||
| + | |||
| + | WiFi.begin(SSID, | ||
| + | while (WiFi.status() != WL_CONNECTED) { // Wait for connection | ||
| + | delay(500); | ||
| + | Serial.print(" | ||
| + | } | ||
| + | // Set up the endpoints for HTTP server, | ||
| + | server.on("/", | ||
| + | server.send(200, | ||
| + | }); | ||
| + | server.on("/ | ||
| + | server.begin(); | ||
| + | |||
| + | |||
| + | Serial.print(" | ||
| + | Serial.println(SSID); | ||
| + | |||
| + | char result[16]; | ||
| + | sprintf(result, | ||
| + | Serial.println(); | ||
| + | Serial.println(result); | ||
| + | decodedMsg = result; | ||
| + | Serial.println(" | ||
| + | |||
| + | Serial.println(WiFi.localIP()); | ||
| + | Serial.print(analogRead(A0)); | ||
| + |  | ||
| + | } | ||
| + | |||
| + | |||
| + | void loop(void) { | ||
| + | |||
| + | for ( int i = 0 ; i < width * decodedMsg.length() + matrix.width() - 1 - spacer; i++ ) { | ||
| + | server.handleClient(); | ||
| + | if (refresh==1) i=0; | ||
| + | refresh=0; | ||
| + | matrix.fillScreen(LOW); | ||
| + | |||
| + | int letter = i / width; | ||
| + | int x = (matrix.width() - 1) - i % width; | ||
| + | int y = (matrix.height() - 8) / 2; // center the text vertically | ||
| + | |||
| + | while ( x + width - spacer >= 0 && letter >= 0 ) { | ||
| + | if ( letter < decodedMsg.length() ) { | ||
| + | matrix.drawChar(x, | ||
| + | } | ||
| + | |||
| + | letter--; | ||
| + | x -= width; | ||
| + | } | ||
| + | |||
| + | matrix.write(); | ||
| + | |||
| + | delay(wait); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | * Otros enlaces: [[https:// | ||
| + | * [[https:// | ||
| + | |||
personas/johnny/proyectos/matrices-led.1634407422.txt.gz · Última modificación:  por kz
                
                