Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "Potentiometer MQTT"
- - Source Code compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-09-14 18:15:54
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Connect the ESP32 DEv board to an Home Assistant */
- /* MQTT Broker. Makte the Potentiometer available for */
- /* the MQTT Server to control it from Home Assitant */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <WiFi.h> // Include the WiFi library for ESP32
- #include <PubSubClient.h> // Include the PubSubClient library for MQTT
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = 4; // Use pin number 4 instead of D4
- // WiFi credentials
- const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
- const char* password = "Your_PASSWORD"; // Replace with your WiFi password
- // MQTT Broker details
- const char* mqtt_server = "broker.hivemq.com"; // Replace with your MQTT broker address
- const char* mqtt_topic = "home/potentiometer"; // Topic to publish the potentiometer value
- WiFiClient espClient; // Create a WiFi client
- PubSubClient client(espClient); // Create a PubSubClient object
- void setup(void)
- {
- // Start the Serial communication
- Serial.begin(115200);
- pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT); // Set the potentiometer pin as input
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print("."); // Print dots while connecting
- }
- Serial.println("Connected to WiFi");
- // Set the MQTT server
- client.setServer(mqtt_server, 1883); // Set the MQTT server and port
- }
- void loop(void)
- {
- // Reconnect to MQTT if not connected
- if (!client.connected()) {
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP32Client")) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000); // Wait 5 seconds before retrying
- }
- }
- }
- client.loop(); // Maintain the MQTT connection
- // Read the potentiometer value
- int potValue = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4);
- // Convert the potentiometer value to a string
- String potValueStr = String(potValue);
- // Publish the potentiometer value to the MQTT topic
- client.publish(mqtt_topic, potValueStr.c_str());
- Serial.print("Potentiometer Value: ");
- Serial.println(potValueStr); // Print the value to the Serial monitor
- delay(2000); // Wait for 2 seconds before the next reading
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement