#include <Arduino.h>
#include <WiFi.h>
#include <PubSubClient.h>
#if CONFIG_FREERTOS_UNICORE
#define TASK_RUNNING_CORE 0
#else
#define TASK_RUNNING_CORE 1
#endif
#define ANALOG_INPUT_PIN A0
#ifndef LED_BUILTIN
#define LED_BUILTIN 13 // Specify the pin your LED is connected to
#endif
// Wi-Fi credentials
const char* ssid = "Wokwi-GUEST"; // Replace with your Wi-Fi SSID
const char* password = ""; // Replace with your Wi-Fi Password
// MQTT broker credentials
const char* mqtt_server = "localhost"; // Replace with your MQTT broker address
const int mqtt_port = 1883;
const char* topic = "potentiometer";
WiFiClient espClient;
PubSubClient client(espClient);
// Function prototypes
void reconnect();
void mqttCallback(char* topic, byte* payload, unsigned int length);
void connectWiFi();
void TaskBlink(void *pvParameters);
void TaskAnalogRead(void *pvParameters);
TaskHandle_t analog_read_task_handle;
// Wi-Fi connection function
void connectWiFi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
// MQTT connection function
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
Serial.println("connected");
client.subscribe(topic); // Subscribe to the topic
Serial.printf("Subscribed to topic: %s\n", topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// MQTT message callback
void mqttCallback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message received on topic ");
Serial.print(topic);
Serial.print(": ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
// Setup function
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
connectWiFi();
// Configure MQTT client
client.setServer(mqtt_server, mqtt_port);
client.setCallback(mqttCallback);
// Create tasks
xTaskCreate(TaskBlink, "Task Blink", 2048, NULL, 1, NULL);
xTaskCreatePinnedToCore(TaskAnalogRead, "Analog Read", 2048, NULL, 5, &analog_read_task_handle, 0);
Serial.println("Basic Multi Threading with MQTT");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
/--------------------------------------------------/
/---------------------- Tasks ---------------------/
/--------------------------------------------------/
void TaskBlink(void *pvParameters) {
pinMode(LED_BUILTIN, OUTPUT);
for (;;) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
void TaskAnalogRead(void *pvParameters) {
if (digitalPinToAnalogChannel(ANALOG_INPUT_PIN) == -1) {
analog_read_task_handle = NULL;
vTaskDelete(NULL);
}
for (;;) {
int sensorValue = analogRead(ANALOG_INPUT_PIN);
char message[50];
snprintf(message, sizeof(message), "Potentiometer value: %d", sensorValue);
client.publish(topic, message);
Serial.println(message);
delay(1000);
}
}