ESP8266 สร้าง QR code Oled 0.96



ESP8266 สร้าง QR code Oled 0.96

อุปกรณ์ที่จำเป็น

1.บอร์ด ESP8266 

2.จอ oled0.96 128x64 pixels  I2C

3.Breadboard (ถ้ามี)

4.แหล่งจ่ายไฟ (ถ้าไม่ใช้จาก adaptor power) usb ต่อคอมพิวเตอร์


การเชื่อมต่อ

ESP8266   -> Oled 0.96

D1(GPIO5) -> SCL

D2(GPIO4) -> SDA

3V        -> VCC

GND       -> GND

ติดตั้ง ไลบราลี่ที่จำเป็น Oled0.96 ในโปรแกรม Arduio IDE 

1. Open your Arduino IDE and go to Sketch > Include Library > Manage Libraries

2. Type “SSD1306” in the search box and install the SSD1306 library from Adafruit.

3. Type “GFX” in the search box and install the library.

ติดตั้ง ไลบราลี่ที่จำเป็น QR Code ในโปรแกรม Arduio IDE 

QRcode Library

https://github.com/ricmoo/QRCode

Download Sketch->Include Library->Add .ZIP Library 


// Complete code

#include <Wire.h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

#include <qrcode.h>


// OLED display dimensions

#define OLED_WIDTH 128

#define OLED_HEIGHT 64


// OLED display object

Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);


void setup() {

  Serial.begin(115200);

  // Initialize the OLED display

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  display.clearDisplay();


  // Display the QR code for "Marios Id"

  generateQRCode("https://www.youtube.com/@omit369");

}


void loop() {

  // Nothing to do here

}


void generateQRCode(const char* text) {

  // Create a QR code object

  QRCode qrcode;

  

  // Define the size of the QR code (1-40, higher means bigger size)

  uint8_t qrcodeData[qrcode_getBufferSize(3)];

  qrcode_initText(&qrcode, qrcodeData, 3, 0, text);


  // Clear the display

  display.clearDisplay();


  // Calculate the scale factor

  int scale = min(OLED_WIDTH / qrcode.size, OLED_HEIGHT / qrcode.size);

  

  // Calculate horizontal shift

  //           (128 - 58)/2  = 35  ,58=29x2 1 qrcode use 2 pixel

  int shiftX = (OLED_WIDTH - qrcode.size*scale)/2;

  

  // Calculate horizontal shift

  //           (64 - 58)/2 = 3  

  int shiftY = (OLED_HEIGHT - qrcode.size*scale)/2;


  // Draw the QR code on the display

  for (uint8_t y = 0; y < qrcode.size; y++) {

    for (uint8_t x = 0; x < qrcode.size; x++) {

      if (qrcode_getModule(&qrcode, x, y)) {

        display.fillRect(shiftX+x * scale, shiftY + y*scale, scale, scale, WHITE);

      }

    }

  }


  // Update the display

  display.display();

}


//////////////////////////////


Relate Link :

Clip ESP8266 Oled0.96

https://youtu.be/1vFYtOkSz0o


QR code

https://www.youtube.com/watch?v=sSSpmnxdzPM

https://github.com/ricmoo/QRCode


Tag:

ESP8266,Arduio IDE,NodeMCU,OLED0.96,qr code,generator,

Humidity,Temperature,qr code,


ความคิดเห็น