Điều khiển LED ma trận Max7219 với module WIFI NodeMCU

Một phần của tài liệu EBOOK Tài liệu IOT ESP8266 (Trang 60 - 65)

II. Bài học mẫu ứng dụng lập trình esp8266:

13.Điều khiển LED ma trận Max7219 với module WIFI NodeMCU

Mục tiêu:

 Lập trình chạy chữ led ma trận Max7219 sử dụng module Wifi NodeMCU web server.

Phần mền cần chuẩn bị

 Cài đặt phần mềm arduino IDE.

Phần cứng cần chuẩn bị:

 1 Mạch Node MCU.  Dây cắm test board.

Lắp mạch nguyên lý: Code chương trình: https://drive.google.com/open?id=19k3z4h7CQalLODKQOUCFj8AzO_tVpC7J #include <LEDMatrixDriver.hpp> #include <ESP8266WiFi.h> #include <WiFiClient.h>

const char* ssid = "Trinh Quang Nam."; // Thay đôi tên wifi const char* password = "bubu123789"; // Thay đổi pass wifi // TCP server at port 80 will respond to HTTP requests WiFiServer server(80);

const uint8_t LEDMATRIX_CS_PIN = 15;

// Define LED Matrix dimensions (0-n) - eg: 32x8 = 31x7 const int LEDMATRIX_WIDTH = 31;

const int LEDMATRIX_HEIGHT = 7; const int LEDMATRIX_SEGMENTS = 4; // The LEDMatrixDriver class instance

LEDMatrixDriver lmd(LEDMATRIX_SEGMENTS, LEDMATRIX_CS_PIN); void displayText ( char * theText)

{

{

myTime=millis();

// Draw the text to the current position drawString(theText, len, x, 0);

// In case you wonder why we don't have to call lmd.clear() in every loop: The font has a opaque (black) background...

// Toggle display of the new framebuffer lmd.display();

// Advance to next coordinate if( --x < len * -8 ) { x = LEDMATRIX_WIDTH; lmd.clear(); } } } ……….. /**

* This function draws a string of the given length to the given position. */

void drawString(char* text, int len, int x, int y ) {

for( int idx = 0; idx < len; idx ++ ) {

int c = text[idx] - 32;

// stop if char is outside visible area if( x + idx * 8 > LEDMATRIX_WIDTH ) return;

// only draw if char is visible if( 8 + x + idx * 8 > 0 )

drawSprite( font[c], x + idx * 8, y, 8, 8 ); }

} /**

* This draws a sprite to the given position using the width and height supplied (usually 8x8)

*/

void drawSprite( byte* sprite, int x, int y, int width, int height ) { (adsbygoogle = window.adsbygoogle || []).push({});

// The mask is used to get the column bit from the sprite row byte mask = B10000000;

for( int iy = 0; iy < height; iy++ ) {

for( int ix = 0; ix < width; ix++ ) {

//Yes my font is backwards so I swap it around.

//lmd.setPixel(x + ix, y + iy, (bool)(sprite[iy] & mask ));

lmd.setPixel(x + (width - ix), y + iy, (bool)(sprite[iy] & mask )); // shift the mask by one pixel to the right

mask = mask >> 1; }

// reset column mask mask = B10000000; }

}

Sau khi nạp code chương trình cho NodeMCU thì trên màn hình led sẽ xuất hiện địa

chỉ IP, ví dụ : 192.168.1.70

Khi đã có địa chỉ IP ta dùng máy tính hay điện thoại ta truy cập vào địa chỉ đó để điều khiển dòng chữ chạy trên led ma trận.

Một phần của tài liệu EBOOK Tài liệu IOT ESP8266 (Trang 60 - 65)