Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
/********* 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
// Include the WiFi library for ESP32 #include
// 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 */
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)