Advertisement
pleasedontcode

"MQTT Potentiometer" rev_01

Sep 14th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "MQTT Potentiometer"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-14 18:07:39
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect the ESP32 DEv board to an Home Assistant */
  21.     /* MQTT Broker. Makte the Potentiometer available for */
  22.     /* the MQTT Server to control it from Home Assitant */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <WiFi.h> // Include the WiFi library for ESP32
  27. #include <PubSubClient.h> // Include the PubSubClient library for MQTT
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF ANALOG INPUT PINS *****/
  34. const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4;
  35.  
  36. // WiFi credentials
  37. const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
  38. const char* password = "Your_PASSWORD"; // Replace with your WiFi password
  39.  
  40. // MQTT Broker details
  41. const char* mqtt_server = "broker.hivemq.com"; // Replace with your MQTT broker address
  42. const char* mqtt_topic = "home/potentiometer"; // Topic to publish the potentiometer value
  43.  
  44. WiFiClient espClient; // Create a WiFi client
  45. PubSubClient client(espClient); // Create a PubSubClient object
  46.  
  47. void setup(void)
  48. {
  49.     // Start the Serial communication
  50.     Serial.begin(115200);
  51.     pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT); // Set the potentiometer pin as input
  52.  
  53.     // Connect to WiFi
  54.     WiFi.begin(ssid, password);
  55.     while (WiFi.status() != WL_CONNECTED) {
  56.         delay(500);
  57.         Serial.print("."); // Print dots while connecting
  58.     }
  59.     Serial.println("Connected to WiFi");
  60.  
  61.     // Set the MQTT server
  62.     client.setServer(mqtt_server, 1883); // Set the MQTT server and port
  63. }
  64.  
  65. void loop(void)
  66. {
  67.     // Reconnect to MQTT if not connected
  68.     if (!client.connected()) {
  69.         while (!client.connected()) {
  70.             Serial.print("Attempting MQTT connection...");
  71.             // Attempt to connect
  72.             if (client.connect("ESP32Client")) {
  73.                 Serial.println("connected");
  74.             } else {
  75.                 Serial.print("failed, rc=");
  76.                 Serial.print(client.state());
  77.                 Serial.println(" try again in 5 seconds");
  78.                 delay(5000); // Wait 5 seconds before retrying
  79.             }
  80.         }
  81.     }
  82.     client.loop(); // Maintain the MQTT connection
  83.  
  84.     // Read the potentiometer value
  85.     int potValue = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4);
  86.     // Convert the potentiometer value to a string
  87.     String potValueStr = String(potValue);
  88.     // Publish the potentiometer value to the MQTT topic
  89.     client.publish(mqtt_topic, potValueStr.c_str());
  90.     Serial.print("Potentiometer Value: ");
  91.     Serial.println(potValueStr); // Print the value to the Serial monitor
  92.  
  93.     delay(2000); // Wait for 2 seconds before the next reading
  94. }
  95.  
  96. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement