#include "WiFi.h"
#include "LiquidCrystal_I2C.h"
#include "PubSubClient.h"
LiquidCrystal_I2C lcd (0x27, 16,2);
char clientId[50];
WiFiClient espClient;
PubSubClient client(espClient);
String name;
String sys_name;
String processor;
void setup()
{
// Define setup() function
Serial.begin(115200);
lcd.init();
lcd.backlight();
WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "");
client.setServer("broker.emqx.io", 1883);
client.setCallback(callback);
}
void mqttReconnect()
{
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
if (client.connect(clientId))
{
Serial.println(" connected");
client.subscribe("topicName/weather");
} else
{
Serial.print("failed, rc=");
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* message, unsigned int length)
{
String stMessage;
for (int i = 0; i < length; i++)
{
stMessage += (char)message[i];
}
// print the message on the console screen
Serial.println(stMessage);
// Clear the LCD screen
lcd.clear();
// Set the LCD cursor
lcd.setCursor(0,0);
// Print the message on the LCD
lcd.print(stMessage);
}
void loop()
{
if (!client.connected())
{
mqttReconnect();
}
client.loop();
}