Advertisement
pleasedontcode

"MQTT Potentiometer" rev_07

Sep 14th, 2024
97
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 compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-14 22:53:45
  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. Add the Adafruit */
  23.     /* DS3502 Potentiometer. Read the MQTT topic and the */
  24.     /* payload. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Adafruit_DS3502.h>  // https://github.com/adafruit/Adafruit_DS3502
  29. #include <WiFi.h>             // Library for WiFi connectivity
  30. #include <PubSubClient.h>     // Library for MQTT
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void mqttCallback(char* topic, byte* payload, unsigned int length);
  36.  
  37. /***** DEFINITION OF ANALOG INPUT PINS *****/
  38. // Define the pin for the potentiometer output. Using pin number directly for ESP32.
  39. const uint8_t Potentiometer_Potentiometer_Vout_PIN = 4; // Changed from D4 to 4
  40.  
  41. /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  42. Adafruit_DS3502 ds3502;  // Instantiate the DS3502 object
  43. WiFiClient wifiClient;    // Create a WiFi client
  44. PubSubClient mqttClient(wifiClient); // Create an MQTT client
  45.  
  46. // WiFi and MQTT credentials
  47. const char* ssid = "Your_SSID";           // Replace with your WiFi SSID
  48. const char* password = "Your_PASSWORD";   // Replace with your WiFi password
  49. const char* mqttServer = "mqtt.example.com"; // Replace with your MQTT broker address
  50. const int mqttPort = 1883;                 // Replace with your MQTT broker port
  51. const char* mqttUser = "Your_MQTT_User";  // Replace with your MQTT username
  52. const char* mqttPassword = "Your_MQTT_Password"; // Replace with your MQTT password
  53. const char* mqttTopic = "potentiometer/set"; // MQTT topic to subscribe to
  54.  
  55. void setup(void)
  56. {
  57.     // Initialize serial communication for debugging
  58.     Serial.begin(115200);
  59.     // Wait until serial port is opened
  60.     while (!Serial) { delay(1); }
  61.  
  62.     // Set the pin mode for the potentiometer output
  63.     pinMode(Potentiometer_Potentiometer_Vout_PIN, INPUT); // Updated pin reference
  64.  
  65.     // Initialize the DS3502 chip
  66.     if (!ds3502.begin()) {
  67.         Serial.println("Couldn't find DS3502 chip");
  68.         while (1);  // Halt execution if the chip is not found
  69.     }
  70.     Serial.println("Found DS3502 chip");
  71.  
  72.     // Connect to WiFi
  73.     WiFi.begin(ssid, password);
  74.     while (WiFi.status() != WL_CONNECTED) {
  75.         delay(500);
  76.         Serial.print(".");
  77.     }
  78.     Serial.println("Connected to WiFi");
  79.  
  80.     // Set up MQTT client
  81.     mqttClient.setServer(mqttServer, mqttPort);
  82.     mqttClient.setCallback(mqttCallback);
  83.  
  84.     // Connect to MQTT broker
  85.     while (!mqttClient.connected()) {
  86.         Serial.print("Connecting to MQTT...");
  87.         if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) {
  88.             Serial.println("connected");
  89.             mqttClient.subscribe(mqttTopic); // Subscribe to the topic
  90.         } else {
  91.             Serial.print("failed, rc=");
  92.             Serial.print(mqttClient.state());
  93.             delay(2000);
  94.         }
  95.     }
  96. }
  97.  
  98. void loop(void)
  99. {
  100.     mqttClient.loop(); // Maintain MQTT connection
  101.  
  102.     // Example of reading the wiper value and sending it to MQTT
  103.     uint8_t currentWiperValue = ds3502.getWiper(); // Get the current wiper value
  104.     float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN) * (5.0 / 1024); // Read voltage
  105.     Serial.print("Current wiper voltage: ");
  106.     Serial.print(voltage);
  107.     Serial.println(" V");
  108.  
  109.     // Publish the current wiper value to MQTT
  110.     mqttClient.publish("potentiometer/status", String(currentWiperValue).c_str());
  111.     delay(5000); // Delay for readability
  112. }
  113.  
  114. // Callback function to handle incoming MQTT messages
  115. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  116.     // Convert payload to string
  117.     String message;
  118.     for (int i = 0; i < length; i++) {
  119.         message += (char)payload[i];
  120.     }
  121.  
  122.     // Set the wiper based on the received message
  123.     int newWiperValue = message.toInt(); // Convert the message to an integer
  124.     if (newWiperValue >= 0 && newWiperValue <= 127) {
  125.         ds3502.setWiper(newWiperValue); // Set the wiper value
  126.         Serial.print("Wiper set to: ");
  127.         Serial.println(newWiperValue);
  128.     } else {
  129.         Serial.println("Invalid wiper value received");
  130.     }
  131. }
  132.  
  133. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement