#include <TFT_eSPI.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_MISO 19 // GPIO pin for MISO
#define TFT_MOSI 23 // GPIO pin for MOSI
#define TFT_SCLK 18 // GPIO pin for SCK
#define TFT_CS 15 // GPIO pin for CS
#define TFT_DC 2 // GPIO pin for DC
#define TFT_RST 4 // GPIO pin for RST
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const char* ssid = "Wokwi-GUEST"; // Replace with your network SSID
const char* password = ""; // Replace with your network password
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
const int ledPin = 5;
void setup() {
Serial.begin(115200); // Initialize serial communication
tft.begin();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
pinMode(ledPin, OUTPUT);
connectToWiFi();
client.setServer(mqtt_server, 1883);
connectToMQTT();
displayInfo();
}
void loop() {
if (!client.connected()) {
connectToMQTT();
}
client.loop();
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
tft.setTextColor(TFT_WHITE);
tft.setTextSize(2);
tft.setCursor(20, 100);
tft.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print("."); // Show progress
}
Serial.println("WiFi connected");
tft.setCursor(20, 120);
tft.print("WiFi Connected!");
}
void connectToMQTT() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
tft.setCursor(20, 140);
tft.print("Connecting to MQTT...");
if (client.connect("ESP32Client")) {
Serial.println("Connected to MQTT");
tft.setCursor(20, 160);
tft.print("Connected Successfully");
blinkLED();
} else {
Serial.print("MQTT connect failed, rc=");
Serial.print(client.state());
delay(2000);
}
}
}
void blinkLED() {
for (int i = 0; i < 5; i++) {
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
tft.setCursor(20, 180);
tft.print("LED Blinking...");
tft.setCursor(20, 200);
tft.print("REGISTER NUMBER: 22138044");
}
void displayInfo() {
tft.setCursor(20, 120);
tft.print("REGISTER NUMBER: 22138044"); // Replace with your actual register number
}