Tutoriel ESP32 52/55 - Synchronisation mondiale WS2812 CheerLights MQTT avec écran LCD | Kit IoT SunFounder ESP32
Na boyekoli oyo, tokosala projet ya CheerLights na ESP32, oyo ekosynchroniser ba couleurs na monde mobimba na nzela ya MQTT. Projet oyo ekobongola ba couleurs te kaka na ba entrées ya bato mosusu, kasi ekobéta lisusu couleur ya sika mpe mbala na yango ekomi kobongwana na écran LCD. Résultat ezali amusement ya démonstration ya capacités ya IoT, oyo emonisi ndenge ba appareils ekoki kosala interaction mpe kobatela ba utilisateurs connectés na mosika. Mpo na ndimbola ya kobakisa, tala vidéo na (na vidéo na 00:00).
Explication ya Hardware
Mpo na kosala projet oyo, okosalela microcontrôleur ESP32, bande ya LED WS2812, mpe écran LCD. ESP32 ezali lokola motó ya opération, na kosalela capacités ya Wi-Fi oyo ezali na kati mpo na kokɔta na internet mpe kozwa ba messages MQTT. Boye ekopesa possibilité ya kobongola ba couleurs ya LED na temps réel kolanda entrée globale ya bato mosusu.
Bande ya LED WS2812 ezali choix populaire mpo na ba projets oyo esengeli ba LED RGB oyo bakoki kozwa adresse. Chaque LED ekoki kokamba na ndenge na yango moko, mpo na kopesa ba affichages ya ba couleurs mingi. LCD ekopesa confirmation visuelle ya couleur ya sika mpe motángo ya mbala oyo ebongolami, mpo na kokitisa interaction ya utilisateur.
Détails ya Datasheet
| Fabricant | SunFounder |
|---|---|
| Numéro ya pièce | ESP32 |
| Tension ya logique/IO | 3.3 V |
| Tension ya alimentation | 5 V |
| Courant ya sortie (na canal) | 20 mA |
| Courant ya pointe (na canal) | 60 mA |
| Directive ya fréquence PWM | 400 Hz |
| Seuils ya logique ya entrée | 0.15 V (moké), 0.8 V (moké te) |
| Chute ya tension / RDS(on) / saturation | 0.2 V |
| Limites thermiques | 85 °C |
| Paquet | Module ESP32 |
| Notes / variantes | Ezali na Wi-Fi mpe Bluetooth ya kati |
- Pesa alimentation ya malamu na ESP32 (5 V) mpe bande ya WS2812 (5 V).
- Salela terre commune kati na ESP32 mpe bande ya LED.
- Salela broker MQTT ya malamu mpo na synchronisation ya ba couleurs.
- Zala na likebi na connexion ya pin ya données mpo na WS2812 (pin 14 kolanda code).
- Zala na likebi na motángo ya ba LED; soki okutisi limites ya puissance, esengeli kobakisa ba sources ya énergie.
Instructions ya Câblage


Mpo na kosala câblage ya ba composants, banda na kokɔta na bande ya LED WS2812. Kɔta na pin ya terre (mbala mingi noir) ya bande ya LED na pin ya terre na ESP32. Na sima, kɔta na pin ya VCC (mbala mingi rouge) ya bande ya LED na sortie 5V na ESP32. Na suka, kɔta na pin ya données (mbala mingi jaune) na GPIO pin 14 na ESP32.
Mpo na LCD, kɔta na pin ya terre (mbala mingi noir) na terre na ESP32. Pin ya VCC (mbala mingi rouge) esengeli kokɔta na sortie 5V. Pin ya SDA (mbala mingi gris) ekɔta na GPIO pin 21, ntango pin ya SCL (mbala mingi blanc) ekɔta na GPIO pin 22. Configuration oyo ekesenisi ESP32 na LCD mpo na kopesa information ndenge esengeli.
Ba Exemples ya Code & Explication
Na fonction ya setup, tokomboisa LCD mpe tokɔta na Wi-Fi. Code oyo ezali na nse emonisi ndenge ya définir ba bibliothèques ya nécessaires mpe kobongisa LCD:
#include
#include
LiquidCrystal_I2C lcd(0x27, 16,2); // set the LCD address
void setup() {
Serial.begin(115200);
lcd.init(); // initialize the lcd
lcd.backlight(); // Turns on the LCD backlight.
}
Oyo ekomboisa LCD mpo na kosalela yango, mpo na kopesa ba messages. Extraite oyo elandi emonisi ndenge connexion ya Wi-Fi esalemi:
void setup_wifi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
lcdConnect(); //for LCD
delay(500);
}
Serial.println("WiFi connected");
}
Fonction oyo ekomboisa connexion na réseau Wi-Fi oyo bapesi, mpe ezali kopesa message na LCD ntango ezali kokɔta. Na suka, logique ya kobongola couleur esalemi na fonction callback:
void callback(char* topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
if (String(topic) == "cheerlights") {
setColor(messageTemp);
}
}
Fonction oyo ezali koyoka ba messages oyo ezali koya na topic "cheerlights" mpe ekobongola couleur kolanda yango. Code mobimba ezali na nse ya article, yango wana tala yango mpo na implémentation complète.
Démonstration / Nini Okoki Kozela
Soki projet esili, okoki kozela ete bande ya LED ekobongola ba couleurs kolanda ba entrées ya flux MQTT global. LCD ekobéta kombo ya couleur ya sika mpe motángo ya mbala oyo ebongolami. Soki okati internet, LCD ekobéta "Connecting..." kino connexion ekozonga (na vidéo na 12:30).
Ba erreurs ya mbala mingi ezali na kotalela ete ba pins ya malamu basalelami mpo na connexions ya données mpe koyeba ete ba identifiants ya Wi-Fi ezali ya malamu. Soki ezali na différence na SSID to mot de passe, ESP32 ekokɔta te, mpe LCD ekobéta kaka message ya connexion.
Ba Timestamps ya Vidéo
- 00:00 Ebandeli
- 1:59 Maloba ya liboso ya projɛ
- 6:16 Ndimbola ya ba fils
- 8:13 Ndimbola ya kode ya Arduino
- 14:26 Kopona carte ya ESP32 mpe port ya COM na Arduino IDE
- 16:07 Ndimbola ya CheerLight na LCD
/*********
This is the origianl code from Examples->iot_5_cheerlight of SunFounder
full video instrucions https://youtu.be/xEqmxMiF-E8
📚⬇️ Download and resource page https://robojax.com/RJT685
I have added LCD to display:
1-Color name
2-Count the number of times the color is updated
3-Showon LCD if wifi is connected
4-Show connecting if not connected or disconnected
Written by Ahmad Shamshiri
www.Robojax.com
Dec 29, 2023
:ref: https://randomnerdtutorials.com/esp32-mqtt-publish-subscribe-arduino-ide/
https://docs.sunfounder.com/projects/kepler-kit/en/latest/iotproject/5.mqtt_pub.html
*********/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//SDA->21,SCL->22
LiquidCrystal_I2C lcd(0x27, 16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
int colorCount = 0;
int lastColor=0;
byte connected[] = {
B00001,
B00001,
B00011,
B00111,
B00111,
B01111,
B01111,
B11111
};
#include <WiFi.h>
#include <PubSubClient.h>
//#include <Wire.h>
#include <Adafruit_NeoPixel.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "dars";
const char* password = "5152535455";
// Add your MQTT Broker address:
const char* mqtt_server = "mqtt.cheerlights.com";
const char* unique_identifier = "sunfounder-client-sdgvsasdda";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
int value = 0;
// Define the supported CheerLights colors and their RGB values
String colorName[] = {"red", "pink", "green", "blue", "cyan", "white", "warmwhite", "oldlace", "purple", "magenta", "yellow", "orange"};
int colorRGB[][3] = { 255, 0, 0, // "red"
255, 192, 203, // "pink"
0, 255, 0, // "green"
0, 0, 255, // "blue"
0, 255, 255, // "cyan"
255, 255, 255, // "white"
255, 223, 223, // "warmwhite"
255, 223, 223, // "oldlace"
128, 0, 128, // "purple"
255, 0, 255, // "magenta"
255, 255, 0, // "yellow"
255, 165, 0}; // "orange"
// init rgb strip
#define LED_PIN 13
#define NUM_LEDS 8
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
void setup() {
Serial.begin(115200);
lcd.init();// initialize the lcd
lcd.backlight(); // Turns on the LCD backlight.
// wifi default settings
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// rgb strip begin
pixels.begin();
pixels.show();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
lcdConnect();//for LCD
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
// If a message is received on the topic, you will check this message.
// Changes the output state according to the message
if (String(topic) == "cheerlights") {
Serial.print("Changing color to ");
Serial.println(messageTemp);
setColor(messageTemp);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
lcdConnect();
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(unique_identifier)) {
Serial.println("connected");
// Subscribe
client.subscribe("cheerlights");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setColor(String color) {
// Loop through the list of colors to find the matching color
for (int colorIndex = 0; colorIndex < 12; colorIndex++) {
if (color == colorName[colorIndex]) {
lastColor = colorIndex;//remeber the last color
colorCount++;//increment the count
// Set the color of each NeoPixel on the strip
for (int pixel = 0; pixel < NUM_LEDS; pixel++) {
pixels.setPixelColor(pixel, pixels.Color (colorRGB [colorIndex][0], colorRGB [colorIndex][1], colorRGB [colorIndex][2]));
delay(100);
}
pixels.show();
}
}
}
void lcdConnect()
{
lcd.clear();
lcd.setCursor(0, 0); //line 0
lcd.print("Connecting...");
lcd.setCursor(0, 1);
lcd.print("SSID:"); //line 1
lcd.print(ssid);
}
void loop() {
lcd.clear();
if (!client.connected()) {
reconnect();
}else{
lcd.createChar(0, connected);
lcd.setCursor(15, 0);
lcd.write(byte(0));
}
client.loop();
//first row
lcd.setCursor(0, 0);
lcd.print("Color: ");
lcd.print(colorName[lastColor]);
//second row
lcd.setCursor(0, 1);
lcd.print("Changed ");
lcd.print(colorCount);
lcd.print(" times");
delay(1000);//we must have delay to able to read the display
}
Common Course Links
Common Course Files
Mokili na bisika
-
DokumentasiESP32 Tutorial 52/55- SunFounder doc page for CheerLightdocs.sunfounder.com
Fichiers📁
Aucun fichier disponible.