Advertisement
pleasedontcode

"MQTT Potentiometer" rev_06

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