Page 1 of 2

SEN0193 + ESP32

Posted: Mon Oct 19, 2020 11:42 am
by dirkdiggler147
I am trying to setup a soil sensor (SEN0193) and have the information relayed to HA with the idea being that if the soil if not moist enough to fire an event to activate a pump.

I had imagined the setup something like: SEN0193>Arduino>ESP32>HA

I was planning on using @LEDG method of MQTT for the communication between ESP32 and HA. I am not sure what I need running on the ESP32 or the Arduino though, can anyone point me in the right direction?

Thanks!

Re: SEN0193 + ESP32

Posted: Tue Oct 20, 2020 7:53 pm
by LEDG
You could do this without the Arduino if you don't need the extra I/O - the ESP32 alone would work.

You'll need to install the MQTT broker add-on in HA and use the Pub Sub Client library on your ESP32. Wire the sensor to GPIO 32, 33, 34, or 35 on the ESP and analogRead() that pin. Publish this value out via MQTT.

Re: SEN0193 + ESP32

Posted: Tue Oct 20, 2020 9:28 pm
by dirkdiggler147
Thanks @LEDG parts arriving tomorrow, will give it a go!

Re: SEN0193 + ESP32

Posted: Tue Oct 20, 2020 10:01 pm
by LEDG
Sweet. Keep us posted.

Re: SEN0193 + ESP32

Posted: Wed Nov 04, 2020 5:55 pm
by dirkdiggler147
@LEDG I have everything connected and ESPhome and MQTT installed on HA.

I have previously played around with the ESP32 so I can now update over air.

Couple of questions though, how do I add the Pub/Sub client to the ESP32 and once that is running how do I analogueread()

Thanks!

Re: SEN0193 + ESP32

Posted: Thu Nov 05, 2020 12:32 am
by dirkdiggler147
I think I got the ESP to read the value from the soil sensor, does this look right?

Code: Select all

sensor:
  - platform: adc
    pin: GPIO32
    name: "Soil Sensor #1"
    update_interval: 30s
    filters: []
    
I added that using ESPHome and I can see the entity in HA and it has a value.

I think I now might need to use mqtt.publish to publish that value to a topic for MQTT to pick up, I am not sure of the syntax to add it though.

Also once the message has been read by MQTT how can I do anything useful with it, as in can I then configure a trigger or something similar?

Sorry for all the questions!

Re: SEN0193 + ESP32

Posted: Thu Nov 05, 2020 12:31 pm
by dirkdiggler147
I think I have found something that I will be able to use to fire the MQTT.Publish:

Code: Select all

filters:
      - lambda: |-
          if (x > 0.7) {
            #Call MQTT.Publish here passing 0;
          } else if (x < 0.26) {
            #Call MQTT.Publish here passing 100;
          } else {
            #Call MQTT.Publish here passing  (0.7-x) / (0.7-0.26) * 100.0;
          }
The above code uses 0.7V for dry and 0.26V for wet so it may be different for my setup.

I have another issue now though :)

The voltage being reported in HA is constantly 1.13V regardless of where the sensor is (in air, water etc).

I have the analog cable going from the sensor to GPIO32, the + cable from the sensor connecting to the 5V pin and the - cable connecting to gnd like this:

Image

Any ideas what I have wrong?

Re: SEN0193 + ESP32

Posted: Thu Nov 05, 2020 2:12 pm
by dirkdiggler147
Needed to add

Code: Select all

attenuation: 11db
to the sensor, voltage changes now when it goes in water. Whoop Whoop :D

Re: SEN0193 + ESP32

Posted: Thu Nov 05, 2020 3:51 pm
by dirkdiggler147
Ok all working, sorry about all the ramblings and spam but it seemed to help work through the problem.

I changed the sensor on the ESP to this:

Code: Select all

sensor:
  - platform: adc
    pin: GPIO33
    name: "Soil Sensor #1"
    unit_of_measurement: "%"
    update_interval: 1s
    attenuation: 11db
    filters: []
    on_value:
      then:
        - mqtt.publish:
            topic: NoTill/Soil/Moisture
            payload: !lambda |-
              if (x > 2.86) {
                return "0";
              } else if (x < 1.50) {
                return "100";
              } else {
                std::string val = to_string((int)((2.86-x) / (2.86-1.50) * 100.0));
                return val;
              }
If I subscribe using MQTTBox I can see the values coming through.

Although thinking about it I guess I don't really need the MQTT as the values are available in HA via the sensor.

What do you think @LEDG ?

Re: SEN0193 + ESP32

Posted: Fri Nov 06, 2020 3:37 pm
by LEDG
@dirkdiggler147 Not sure if I'm understanding your question properly but ESPhome will handle the transmission of data from the device to home assistant, so you don't need any MQTT stuff in there.