#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <PubSubClient.h>
const char* yourName = "turin";
/****************************************************************************/
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
// Replace with your network credentials
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Replace with your MQTT broker details
const char* mqttServer = "mqtt.iotserver.uz";
const int mqttPort = 1883;
const char* mqttUser = "userTTPU";
const char* mqttPass = "mqttpass";
const char* rcvTopic = "ttpu/lcd/";
const char* sendTopic = "ttpu/tel/";
// Replace with your LED pin
const int redLed = 19;
const int blueLed = 18;
const int yellowLed = 5;
WiFiClient espClient;
PubSubClient client(espClient);
String clientId; // To hold the unique MQTT client ID
/****************************************************************************/
void setup_wifi(void);
void callback(char* topic, byte* message, unsigned int length);
void reconnect(void);
String generateClientId(void);
void displayMessage(String msg);
/****************************************************************************/
void setup() {
Serial.begin(115200);
delay(10);
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.clear(); // clear display
lcd.setCursor(0, 0); // move cursor to (0, 0)
lcd.print("Hello"); // print message at (0, 0)
lcd.setCursor(0, 1); // move cursor to (2, 1)
lcd.print("Turin"); // print message at (2, 1)
pinMode(redLed, OUTPUT);
pinMode(blueLed, OUTPUT);
pinMode(yellowLed, OUTPUT);
setup_wifi();
clientId = generateClientId(); // Generate a unique client ID for this session
Serial.print("Generated client ID: ");
Serial.println(clientId);
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}
/****************************************************************************/
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Publish message every 5 seconds
static unsigned long lastPublish = 0;
if (millis() - lastPublish > 5000) {
String message = "Hello from " + String(yourName);
Serial.print("Publishing message: ");
Serial.println(message);
String pubTopic = String(sendTopic) + String(yourName);
client.publish(pubTopic.c_str(), message.c_str());
lastPublish = millis();
}
}
/****************************************************************************/
void setup_wifi(void)
{
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
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 messageStr;
String topicStr = String(topic);
for (int i = 0; i < length; i++) {
messageStr += (char)message[i];
}
Serial.println(messageStr);
// Handle the received message here
displayMessage(messageStr);
messageStr.toLowerCase(); // Convert to lowercase
if (messageStr == "red off"){
digitalWrite(redLed, LOW);
}
else if (messageStr == "red on"){
digitalWrite(redLed, HIGH);
}
else if (messageStr == "blue off"){
digitalWrite(blueLed, LOW);
}
else if (messageStr == "blue on"){
digitalWrite(blueLed, HIGH);
}
else if (messageStr == "yellow off"){
digitalWrite(yellowLed, LOW);
}
else if (messageStr == "yellow on"){
digitalWrite(yellowLed, HIGH);
}
}
/****************************************************************************/
void reconnect(void)
{
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientId.c_str(), mqttUser, mqttPass)) {
Serial.println("connected");
// Subscribe to topic once connected
String subTopic = String(rcvTopic) + String(yourName);
client.subscribe(subTopic.c_str());
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/****************************************************************************/
// Generate a unique MQTT client ID
String generateClientId(void)
{
uint32_t randomNum = random(0xffff); // Generate a random number
uint32_t chipId = ESP.getEfuseMac() & 0xFFFFFF; // Get the unique part of the ESP32's MAC address
String clientId = "ESP32Client-" + String(chipId, HEX) + "-" + String(randomNum, HEX);
return clientId;
}
/****************************************************************************/
void displayMessage(String msg)
{
lcd.clear(); // Clear the screen
if (msg.length() <= 16) { // Fits in one line
lcd.setCursor(0, 0); // Start at the first line
lcd.print(msg);
}
else if (msg.length() <= 32) { // Fits in two lines
lcd.setCursor(0, 0); // First line
lcd.print(msg.substring(0, 16)); // Print the first 16 characters
lcd.setCursor(0, 1); // Second line
lcd.print(msg.substring(16)); // Print the rest of the message
}
else { // Too long to fit
lcd.setCursor(0, 0);
lcd.print("Message is");
lcd.setCursor(0, 1);
lcd.print("TOO LONG");
}
}