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: "MQTT Potentiometer"
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2024-09-14 22:34:23
- ********* 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 the Adafruit */
- /* DS3502 Potentiometer. 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> // Library for WiFi connectivity
- #include <PubSubClient.h> // Library for MQTT
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void mqttCallback(char* topic, byte* payload, unsigned int length);
- /***** DEFINITION OF ANALOG INPUT PINS *****/
- const uint8_t Potentiometer_Potentiometer_Vout_PIN_D4 = D4;
- /***** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
- Adafruit_DS3502 ds3502; // Instantiate the DS3502 object
- WiFiClient wifiClient; // Create a WiFi client
- PubSubClient mqttClient(wifiClient); // Create an MQTT client
- // WiFi and MQTT credentials
- const char* ssid = "Your_SSID"; // Replace with your WiFi SSID
- const char* password = "Your_PASSWORD"; // Replace with your WiFi password
- const char* mqttServer = "mqtt.example.com"; // Replace with your MQTT broker address
- const int mqttPort = 1883; // Replace with your MQTT broker port
- const char* mqttUser = "Your_MQTT_User"; // Replace with your MQTT username
- const char* mqttPassword = "Your_MQTT_Password"; // Replace with your MQTT password
- const char* mqttTopic = "potentiometer/set"; // MQTT topic to subscribe to
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Wait until serial port is opened
- while (!Serial) { delay(1); }
- // Set the pin mode for the potentiometer output
- pinMode(Potentiometer_Potentiometer_Vout_PIN_D4, INPUT);
- // Initialize the DS3502 chip
- if (!ds3502.begin()) {
- Serial.println("Couldn't find DS3502 chip");
- while (1); // Halt execution if the chip is not found
- }
- Serial.println("Found DS3502 chip");
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("Connected to WiFi");
- // Set up MQTT client
- mqttClient.setServer(mqttServer, mqttPort);
- mqttClient.setCallback(mqttCallback);
- // Connect to MQTT broker
- while (!mqttClient.connected()) {
- Serial.print("Connecting to MQTT...");
- if (mqttClient.connect("ESP32Client", mqttUser, mqttPassword)) {
- Serial.println("connected");
- mqttClient.subscribe(mqttTopic); // Subscribe to the topic
- } else {
- Serial.print("failed, rc=");
- Serial.print(mqttClient.state());
- delay(2000);
- }
- }
- }
- void loop(void)
- {
- mqttClient.loop(); // Maintain MQTT connection
- // Example of reading the wiper value and sending it to MQTT
- uint8_t currentWiperValue = ds3502.getWiper(); // Get the current wiper value
- float voltage = analogRead(Potentiometer_Potentiometer_Vout_PIN_D4) * (5.0 / 1024); // Read voltage
- Serial.print("Current wiper voltage: ");
- Serial.print(voltage);
- Serial.println(" V");
- // Publish the current wiper value to MQTT
- mqttClient.publish("potentiometer/status", String(currentWiperValue).c_str());
- delay(5000); // Delay for readability
- }
- // Callback function to handle incoming MQTT messages
- void mqttCallback(char* topic, byte* payload, unsigned int length) {
- // Convert payload to string
- String message;
- for (int i = 0; i < length; i++) {
- message += (char)payload[i];
- }
- // Set the wiper based on the received message
- int newWiperValue = message.toInt(); // Convert the message to an integer
- if (newWiperValue >= 0 && newWiperValue <= 127) {
- ds3502.setWiper(newWiperValue); // Set the wiper value
- Serial.print("Wiper set to: ");
- Serial.println(newWiperValue);
- } else {
- Serial.println("Invalid wiper value received");
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement