Advertisement
pleasedontcode

"MQTT Potentiometer" rev_03

Sep 14th, 2024
103
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:21:30
  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. The Broker need a username and */
  22.     /* Password for authentifikation. Makte the */
  23.     /* Potentiometer available for the MQTT Server to */
  24.     /* control it from Home Assitant */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <WiFi.h>                // Include the WiFi library for ESP32
  29. #include <PubSubClient.h>        // Include the PubSubClient library for MQTT
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. // Define the MQTT callback function prototype
  36. void mqttCallback(char* topic, byte* payload, unsigned int length);
  37. void reconnect(void);
  38.  
  39. /***** DEFINITION OF ANALOG INPUT PINS *****/
  40. const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4; // Define the pin for the potentiometer
  41.  
  42. // WiFi and MQTT configuration
  43. const char* ssid = "Your_SSID";               // Your WiFi SSID
  44. const char* password = "Your_PASSWORD";       // Your WiFi Password
  45. const char* mqtt_server = "Your_MQTT_Broker"; // Your MQTT Broker address
  46. const char* mqtt_user = "Your_MQTT_Username"; // MQTT Username
  47. const char* mqtt_password = "Your_MQTT_Password"; // MQTT Password
  48. const char* mqtt_topic = "home/potentiometer"; // MQTT topic to publish potentiometer value
  49.  
  50. WiFiClient espClient;                         // Create a WiFi client
  51. PubSubClient client(espClient);                // Create a PubSubClient object
  52.  
  53. void setup(void)
  54. {
  55.     Serial.begin(115200);                      // Start the Serial communication
  56.     pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT); // Set the potentiometer pin as input
  57.  
  58.     // Connect to WiFi
  59.     WiFi.begin(ssid, password);                 // Start WiFi connection
  60.     while (WiFi.status() != WL_CONNECTED) {    // Wait for connection
  61.         delay(1000);                            // Wait 1 second
  62.         Serial.println("Connecting to WiFi...");
  63.     }
  64.     Serial.println("Connected to WiFi");
  65.  
  66.     // Set up MQTT server
  67.     client.setServer(mqtt_server, 1883);       // Set the MQTT server and port
  68.     client.setCallback(mqttCallback);           // Set the callback function for incoming messages
  69. }
  70.  
  71. void loop(void)
  72. {
  73.     // Ensure the client is connected to the MQTT broker
  74.     if (!client.connected()) {
  75.         reconnect();                            // Reconnect if not connected
  76.     }
  77.     client.loop();                              // Handle MQTT communication
  78.  
  79.     // Read the potentiometer value
  80.     int potValue = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4); // Read the potentiometer value
  81.     char potValueStr[4];                        // Buffer to hold the potentiometer value as a string
  82.     itoa(potValue, potValueStr, 10);            // Convert the integer value to a string
  83.  
  84.     // Publish the potentiometer value to the MQTT topic
  85.     client.publish(mqtt_topic, potValueStr);   // Publish the value
  86.     delay(2000);                                // Wait for 2 seconds before the next reading
  87. }
  88.  
  89. // Function to handle reconnection to the MQTT broker
  90. void reconnect() {
  91.     // Loop until we're reconnected
  92.     while (!client.connected()) {
  93.         Serial.print("Attempting MQTT connection...");
  94.         // Attempt to connect
  95.         if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
  96.             Serial.println("connected");
  97.         } else {
  98.             Serial.print("failed, rc=");
  99.             Serial.print(client.state());
  100.             Serial.println(" try again in 5 seconds");
  101.             delay(5000); // Wait 5 seconds before retrying
  102.         }
  103.     }
  104. }
  105.  
  106. // Callback function for incoming messages
  107. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  108.     // Handle incoming messages if needed
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement