// Adafruit_MQTT_noTLS.ino
// https://wokwi.com/projects/399507534924937217
#include <WiFi.h>
#include <PubSubClient.h>
// #include <WiFiClientSecure.h>
// Configure WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Configure MQTT
const char* mqtt_server = "io.adafruit.com";
const int mqtt_port = 1883;
const char* mqtt_username = "kelvinw"; // Username MQTT (if needed)
const char* mqtt_password = "aio_ChRX18oS4MyxhPGBDg6Oe61iNOUF"; // Password MQTT (if needed)
const char* mqtt_client_id = "ESP32_LED_Client";
const int retry_interval = 5000;
const int publish_interval = 2000;
WiFiClient espClient;
PubSubClient mqttClient(espClient);
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
// MQTT topics for sensors and LED lights
const char* sensor1_topic = "kelvinw/feeds/test.temp";
// const char* sensor2_topic = "user/kelvinw/humd";
const char* command1_topic = "kelvinw/feeds/test.led1";
// const char* command2_topic = "user/kelvinw/led2";
// GPIO pins for LED light
#define LED1 12
// #define LED2 13
void setup_wifi() {
// delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.print(" ");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
}
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection ... ");
Serial.print(mqtt_client_id);
Serial.print(" ... ");
// Attempt to connect
if (mqttClient.connect(mqtt_client_id, mqtt_username, mqtt_password)) {
Serial.println("MQTT connected!");
// Once connected, subscribe to the topic
mqttClient.subscribe(sensor1_topic); // subscribe the topics here
// mqttClient.subscribe(sensor2_topic); // subscribe the topics here
mqttClient.subscribe(command1_topic); // subscribe the topics here
// mqttClient.subscribe(command2_topic); // subscribe the topics here
} else {
Serial.print("failed, rc = ");
Serial.print(mqttClient.state());
Serial.print("; try again in ");
Serial.print(retry_interval);
Serial.println(" ms");
// Wait 5 seconds before retrying
delay(retry_interval);
}
}
}
void setup() {
Serial.begin(9600);
setup_wifi();
mqttClient.setServer(mqtt_server, mqtt_port);
mqttClient.setCallback(callback);
pinMode(LED1, OUTPUT);
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]: ");
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.println(message);
// If the message is “on”, turn on the LED light
if (message == "on") {
digitalWrite(LED1, HIGH);
}
// If the message is “off”, turn off the LED light
else if (message == "off") {
digitalWrite(LED1, LOW);
}
}
void loop() {
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
unsigned long now = millis();
if (now - lastMsg >= publish_interval) {
lastMsg = now;
++value;
// snprintf(msg, MSG_BUFFER_SIZE, "Hello World! #%ld", value);
// Serial.print("Publish message: ");
// Serial.println(msg);
// mqttClient.publish(mqtt_topic, msg);
// publishMessage(sensor1_topic, msg, false);
// publishMessage(sensor2_topic, msg, false);
publishMessage(sensor1_topic, String(value) + String("°C"), false);
// publishMessage(sensor2_topic, String(value) + String("%"), false);
// publishMessage(command1_topic, String("13579"), true); // Send a retained message
// publishMessage(command1_topic, String(""), true); // Delete a retained message
}
}
void publishMessage(const char* topic, String payload, boolean retained) {
// if (mqttClient.publish(topic, payload.c_str(), true))
if (mqttClient.publish(topic, payload.c_str(), retained))
Serial.println("Message published [" + String(topic) + "]: " + payload);
}