Advertisement
pleasedontcode

"WiFi Potentiometer" rev_08

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: "WiFi Potentiometer"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-09-14 23:42:54
  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 2 different */
  23.     /* Adafruit DS3502 Potentiometer one is connected to */
  24.     /* 0x28 the other to 0x2A. Read the MQTT topic and */
  25.     /* the payload. */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Adafruit_DS3502.h>  // https://github.com/adafruit/Adafruit_DS3502
  30. #include <WiFi.h>             // Include WiFi library
  31. #include <PubSubClient.h>     // Include PubSubClient library for MQTT
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  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_1; // Create an instance of the Adafruit_DS3502 class for address 0x28
  42. Adafruit_DS3502 ds3502_2; // Create an instance of the Adafruit_DS3502 class for address 0x2A
  43.  
  44. // WiFi and MQTT configuration
  45. const char* ssid = "YOUR_SSID";          // Replace with your WiFi SSID
  46. const char* password = "YOUR_PASSWORD";  // Replace with your WiFi password
  47. const char* mqtt_server = "YOUR_MQTT_BROKER_IP"; // Replace with your MQTT broker IP
  48. const char* mqtt_user = "YOUR_MQTT_USERNAME";    // Replace with your MQTT username
  49. const char* mqtt_password = "YOUR_MQTT_PASSWORD"; // Replace with your MQTT password
  50.  
  51. WiFiClient espClient;             // Create a WiFi client
  52. PubSubClient client(espClient);   // Create a PubSubClient object
  53.  
  54. void setup(void)
  55. {
  56.     // Initialize serial communication at 115200 baud rate
  57.     Serial.begin(115200);
  58.     // Wait until the serial port is opened
  59.     while (!Serial) { delay(1); }
  60.  
  61.     // Initialize the potentiometer pin as input
  62.     pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT);
  63.  
  64.     // Connect to WiFi
  65.     WiFi.begin(ssid, password);
  66.     while (WiFi.status() != WL_CONNECTED) {
  67.         delay(500);
  68.         Serial.print(".");
  69.     }
  70.     Serial.println("Connected to WiFi");
  71.  
  72.     // Initialize the MQTT client
  73.     client.setServer(mqtt_server, 1883);
  74.    
  75.     // Initialize the DS3502 chips
  76.     if (!ds3502_1.begin(0x28) || !ds3502_2.begin(0x2A)) {
  77.         Serial.println("Couldn't find DS3502 chips");
  78.         while (1); // Halt if the chips are not found
  79.     }
  80.     Serial.println("Found DS3502 chips");
  81. }
  82.  
  83. void loop(void)
  84. {
  85.     // Ensure the MQTT client is connected
  86.     if (!client.connected()) {
  87.         // Attempt to reconnect
  88.         if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
  89.             Serial.println("Connected to MQTT broker");
  90.         } else {
  91.             Serial.print("Failed to connect to MQTT broker, rc=");
  92.             Serial.print(client.state());
  93.             delay(2000);
  94.             return; // Exit loop if not connected
  95.         }
  96.     }
  97.     client.loop(); // Maintain MQTT connection
  98.  
  99.     // Example wiper values to set and read voltage
  100.     for (uint8_t wiper_value : {0, 63, 127}) {
  101.         ds3502_1.setWiper(wiper_value); // Set the wiper of the first potentiometer
  102.         ds3502_2.setWiper(wiper_value); // Set the wiper of the second potentiometer
  103.  
  104.         // Read the voltage from the potentiometer
  105.         float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4) * (5.0 / 1024);
  106.         Serial.print("Wiper voltage with wiper set to ");
  107.         Serial.print(wiper_value);
  108.         Serial.print(": ");
  109.         Serial.print(voltage);
  110.         Serial.println(" V");
  111.  
  112.         // Publish the wiper value to MQTT
  113.         String topic = "home/potentiometer/wiper";
  114.         String payload = String(wiper_value);
  115.         client.publish(topic.c_str(), payload.c_str());
  116.  
  117.         delay(1000); // Wait for a second before the next iteration
  118.     }
  119. }
  120.  
  121. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement