SEN0193 + ESP32

Home Assistant automation projects, questions, etc. go here.
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

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!
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

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.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

Thanks @LEDG parts arriving tomorrow, will give it a go!
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Sweet. Keep us posted.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

@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!
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

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!
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

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?
Attachments
SensorConnections.jpg
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

Needed to add

Code: Select all

attenuation: 11db
to the sensor, voltage changes now when it goes in water. Whoop Whoop :D
dirkdiggler147
LED-Curious
LED-Curious
Reactions:
Posts: 20
Joined: Fri Jun 12, 2020 9:52 pm

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 ?
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

@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.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Post Reply