I would like to connect arduino which has 3 potentiometers connected with this code
void setup() {
Serial.begin(9600);
}
void loop() {
int potValue1 = analogRead(A0);
int potValue2 = analogRead(A1);
int potValue3 = analogRead(A2);
Serial.print("Pot1:");
Serial.println(potValue1);
Serial.print("Pot2:");
Serial.println(potValue2);
Serial.print("Pot3:");
Serial.println(potValue3);
delay(1000);
}
Does anyone know how can I send that information to HA using ESP-01S? I tried searching and I found something using MQTT but the code is not working for me
Why not just use an esp board? I never understand why try to incorporate 2 microcontrollers. A d1 mini is slightly larger than the esp01, has plenty of gpios. Having 1 board read sensors then send that to another board(which could read the sensors by itself) it seems counter productive.
If you dont know what your talking about, best just keep quiet. You obviously don’t realize the esp32 comes in a d1 mini form and has the same number of ADC pins as the NodeMCU but it’s half the size. Since it’s not common sense to you, let me explain to you. When someone is using an esp01, size is obviously a factor to them and therfore you want to reccomend a board that takes size into consideration, like a d1 mini and NOT a larger NodeMCU that’s nearly twice as big. Anything else you want to explain to me?
Thank you, only reason I wanted to use 2 microcontrollers is that I dont know a lot about this stuff and I had both at home from another project I already ordered this V2 CP2102 With Cable but based on what you both wrote that is not right one and I found this D1 mini and also this ESP32, and I would manage to fit any of them there, I want it to be place of light switch and I am printing case so I can make it slightly bigger if needed so what so you think would be the best one? I just need to connect 3 potentiometers from which it will read values and send them to HA running on Rpi 4b. I apologize if something doesnt make sense or sounds stupid, I am begginer and this is my firs real project making something myself for HA
There was nothing in the OP that indicated that size was an issue. I also did NOT say that the NodeMCU was the only ESP32 development board. It was just an example of one of several forms of the ESP32 boards.
You could use MQTT to communicate between any processors that can run the MQTT client code. It is probably the easiest way to pass data between an Arduino and an ESP. But as said before, why? Is there a reason that the analog sensors have to be on an arduino?
Only reason was that I had it at home as I mentioned in this response
So If you both say that it is unnecessarily dificult I am just going to buy something else, I cant finish it by myslef with this setup I tried everything I found and no success
So you thought you needed to explain to me what the nodemcu is, even though I just brought up the d1 mini which is essentially the same damn thing? Smells like back peddling to me.
It should work. But I think it is a complicated solution. If you want to continue with this combination just for learning, then post your code and wiring diagram.
The NodeMCU board you ordered uses the same processor as the Wemos D1, and has just one analog input port. The ESP32 board in your post will likely work just fine for what you want to do- input three potentiometers into Home Assistant. I’ve only used an ADC chip to get input from an analog voltage, but the Analog to Digital Sensor Component might be the solution you need.
While you’re waiting for the ESP32 board, experiment using the NodeMCU or Wemos board and the Analog to Digital Sensor Component and one potentiometer.
It just depends on how much work and space you have to work with. I’m not exactly sure what your doing but you could even make things cleaner looking and use a single rotary encoder instead… For example by default the rotary encoder will dim light 1. 1 button push and the rotary encoder will then control the dim on light 2 and a double push of the switch will use the same rotary encoder to dim light 3. You could go even further and do a push and hold will toggle the On/Off of all 3 lights. You have tons of options.
Ok, right now I am using this code in the ESP module just connected to computer trying to create working connection before adding the arduino itself to the mix
ESP-01S code:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "myWiFi";
const char* password = "myPass";
const char* mqtt_server = "homeassistant.local";
const char* mqtt_topic = "home/esp01s/potentiometer_A0";
const char* mqtt_username = "addons";
const char* mqtt_password = "myPass";
WiFiClient espClient;
PubSubClient client(espClient);
int previousPotValue_A0 = -1; // Initialize with a value that's unlikely to occur
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Connect to Wi-Fi
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to Wi-Fi...");
}
Serial.println("Connected to Wi-Fi");
// Connect to MQTT with username and password
Serial.println("Connecting to MQTT...");
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// Attempt to connect with username and password
if (client.connect("ESP01SClient", mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.println("Failed to connect to MQTT broker");
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
// Read value from the potentiometer connected to A0
int potValue_A0 = analogRead(A0);
// Check if the potentiometer value has changed
if (potValue_A0 != previousPotValue_A0) {
// Publish the value to the MQTT topic
Serial.print("Publishing data to MQTT: ");
Serial.println(potValue_A0);
client.publish(mqtt_topic, String(potValue_A0).c_str());
// Update the previous value
previousPotValue_A0 = potValue_A0;
}
delay(100); // Delay between readings (adjust as needed)
}
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect("Potenciometr1", mqtt_username, mqtt_password)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed to connect to MQTT broker, retrying in 5 seconds...");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle incoming MQTT messages here if needed
}
this is what I get from the serial prints
Connecting to Wi-Fi...
Connecting to Wi-Fi...
Connecting to Wi-Fi...
Connecting to Wi-Fi...
Connecting to Wi-Fi...
Connected to Wi-Fi
Connecting to MQTT...
Connected to MQTT broker
Publishing data to MQTT: 151
Publishing data to MQTT: 97
Publishing data to MQTT: 99
and it goes on and on so should be working
and over here is the log from MQTT Mosquitto broker
2023-09-02 17:52:32: New client connected from 192.168.1.132:52766 as ESP01SClient (p2, c1, k15, u'addons').
so it seems that the connection should be ok but I have no idea how to work with the data in HA, I tried adding it using configuration.yaml but It returned with error, so I figured that I will add arduino after I have successfull connection
Size isnt really an issue, the part in wall is 70mm in diameter and around 40mm deep but I can make it thicker on the outside since I am printing it myself and I thought about using some sort off button but I would like to have 3 individual knobs for each light, I would maybe add button for switching the knob to mode that controlls light temperature but that is not important
So if your just using the esp board then just use esphome. It will make things much simpler. Here’s the 4 lines of code it takes to get the same result your getting now.esphome integrates right with HA and it’s much easier. Analog To Digital Sensor — ESPHome
So I could connect potentiometer directly to the ESP-01S? Well in my case I would need 3 because it has only one analog input and I would also need to put voltage divider there because the input should take only 0-1V and itself is powered by 3,3V and I only have 5V700mA power supply, also which pin would that be? I found that there is one but I am not sure which one it is
Oh, no sorry I thought you had an esp with an adc on it. I dont believe those have an ADC pin. Those things are really only for things that are a HIGH/LOW or ON/OFF.Theyre not really a “general purpose” board like a regular esp8266 or esp32 is. I. Would just get yourself some more versatile boards then start using esphome. It will change your life and it’s much easier to do stuff like this with Home Assistant . Those 4 lines of code and HA would have discovered the board and it would automatically make the sensor for you,O you turn that pot and the values are displayed in HA in real time.
The chip on the board is the ESP8266. The chip does have one analog input, but that pin is not brought out for connections on this board.
This board is great for turning things on or off. I use them in my holiday props to control addressable LEDs or servo motors. They also are used a lot in sensors. For example I have a few scattered around the house with DS18B20 Temperature Sensors. You will find the ESP8266 chip in a ton of WiFi connected things like smart outlets. It’s not a bad thing to have in your toolbox.
For experimenting, if you have the Wemos D1 Mini, it does have the Analog-In pin on the board.
Your Arduino code on the ESP looks like it’s working and publishing the pot value to the MQTT broker.
A tool that a lot of us use to monitor what’s going on in the broker is MQTT Explorer. It is available as a Home Assistant add-on, but I prefer the stand-alone version on my PC so that I can have ESPHome open at the same time as MQTT Explorer.
In MQTT Explorer put your topic into the search bar, then all you will see is whatever the ESP published to that topic.
You can use MQTT on the ESPHome device to receive the data published by the ESP01.