Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: "WiFi Potentiometer"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-09-14 23:42:54
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Connect the ESP32 Dev board to an Home Assistant */
- /* MQTT Broker. The Broker need a username and */
- /* Password for authentifikation. Add 2 different */
- /* Adafruit DS3502 Potentiometer one is connected to */
- /* 0x28 the other to 0x2A. Read the MQTT topic and */
- /* the payload. */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <Adafruit_DS3502.h> // https://github.com/adafruit/Adafruit_DS3502
- #include <WiFi.h> // Include WiFi library
- #include <PubSubClient.h> // Include PubSubClient library for MQTT
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4;
- /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- Adafruit_DS3502 ds3502_1; // Create an instance of the Adafruit_DS3502 class for address 0x28
- Adafruit_DS3502 ds3502_2; // Create an instance of the Adafruit_DS3502 class for address 0x2A
- // WiFi and MQTT configuration
- const char* ssid = "YOUR_SSID"; // Replace with your WiFi SSID
- const char* password = "YOUR_PASSWORD"; // Replace with your WiFi password
- const char* mqtt_server = "YOUR_MQTT_BROKER_IP"; // Replace with your MQTT broker IP
- const char* mqtt_user = "YOUR_MQTT_USERNAME"; // Replace with your MQTT username
- const char* mqtt_password = "YOUR_MQTT_PASSWORD"; // Replace with your MQTT password
- WiFiClient espClient; // Create a WiFi client
- PubSubClient client(espClient); // Create a PubSubClient object
- void setup(void)
- {
- // Initialize serial communication at 115200 baud rate
- Serial.begin(115200);
- // Wait until the serial port is opened
- while (!Serial) { delay(1); }
- // Initialize the potentiometer pin as input
- pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT);
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("Connected to WiFi");
- // Initialize the MQTT client
- client.setServer(mqtt_server, 1883);
- // Initialize the DS3502 chips
- if (!ds3502_1.begin(0x28) || !ds3502_2.begin(0x2A)) {
- Serial.println("Couldn't find DS3502 chips");
- while (1); // Halt if the chips are not found
- }
- Serial.println("Found DS3502 chips");
- }
- void loop(void)
- {
- // Ensure the MQTT client is connected
- if (!client.connected()) {
- // Attempt to reconnect
- if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
- Serial.println("Connected to MQTT broker");
- } else {
- Serial.print("Failed to connect to MQTT broker, rc=");
- Serial.print(client.state());
- delay(2000);
- return; // Exit loop if not connected
- }
- }
- client.loop(); // Maintain MQTT connection
- // Example wiper values to set and read voltage
- for (uint8_t wiper_value : {0, 63, 127}) {
- ds3502_1.setWiper(wiper_value); // Set the wiper of the first potentiometer
- ds3502_2.setWiper(wiper_value); // Set the wiper of the second potentiometer
- // Read the voltage from the potentiometer
- float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4) * (5.0 / 1024);
- Serial.print("Wiper voltage with wiper set to ");
- Serial.print(wiper_value);
- Serial.print(": ");
- Serial.print(voltage);
- Serial.println(" V");
- // Publish the wiper value to MQTT
- String topic = "home/potentiometer/wiper";
- String payload = String(wiper_value);
- client.publish(topic.c_str(), payload.c_str());
- delay(1000); // Wait for a second before the next iteration
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement