WiFi/MQTT car presence sensor for garage door automation

@jmart518
Now I’ve got this “working” when i power cycle the WeMos, I’m finding the time it takes for a wifi connection to be established to be highly variable. Does anyone else have this issue? I’m getting this range of times having tested it multiple times, the extremes are outliers. 6 seconds at the fastest and over 2 minutes for the slowest.

I’m using the PCB antenna at the moment until I can do the switcheroo with the surface mount connection. I’m also using a fairly underpowered modem (SAGEMCOM F@st 5355-A) from Telstra, my provider in Australia. I have about 25 devices on my LAN.

I have tried assigning a reserved IP address from the DHCP pool but this hasn’t made the speed of connection any better.

Would hard coding the IP address for the WeMos as a static IP improve things?

Is there anything in the WiFi methods in the Arduino Code that would help? Use WiFi.config(IP) perhaps?

Will changing to the external antenna help?
edit: I’ve changed to external antenna and it doesn’t help!

Is there anything I can do networking wise to identify where the problem is?

:confused:

Already try with all diferent arduino versions. Include 1.9. Use a d1 mini and a d1 mini pro. The same, check pubsubclient to 512 and dont get mqtt sensor create.

Could you add the following?

keep_alive: 60

I’m aware this is the default and it should be optional but I commented this out while I was fiddling with my config file and got some strange behaviour. Worth a shot.

same…:frowning:

Im having the same issue. I did delete the libraries from adruino ide and change the PubSubClient 512 and to 1024. after a fresh install of arduino ide and new libraries. how would i go about adding the sensor manually to the config.yaml . i have tried the MQTT Binary Sensor Doc’s but i guess im missing something. any help would be much appreciated

after running this

i get this

homeassistant/sensor/CarPresence-uptime/config {“name”: “CarPresence-uptime”, “state_topic”: “homeassistant/sensor/CarPresence-uptime/state”, “unit_of_measurement”: “msec”, “value_template”: “{{ value }}”}
homeassistant/sensor/CarPresence-uptime/state 7469
homeassistant/binary_sensor/CarPresence/config {“name”: “CarPresence”, “device_class”: “connectivity”, “state_topic”: “homeassistant/binary_sensor/CarPresence/state”}
homeassistant/binary_sensor/CarPresence/state ON
homeassistant/sensor/CarPresence-signal/config {“name”: “CarPresence-signal”, “state_topic”: “homeassistant/sensor/CarPresence-signal/state”, “unit_of_measurement”: “dBm”, “value_template”: “{{ value }}”}
homeassistant/sensor/CarPresence-signal/state -55
homeassistant/sensor/CarPresence-signal/state -55
homeassistant/sensor/CarPresence-uptime/state 12469
homeassistant/sensor/CarPresence-signal/state -55
homeassistant/sensor/CarPresence-uptime/state 17471
homeassistant/sensor/CarPresence-signal/state -52
homeassistant/sensor/CarPresence-uptime/state 22473
homeassistant/binary_sensor/CarPresence/state OFF

my config.yaml looks like this
mqtt:
discovery: true
discovery_prefix: homeassistant
broker: 192.168.1.23
username: “user”
password: “password”
keep_alive: 60

What helped me figure out what was wrong with mine was to go to the “states” tab in the developer panel and look for the binary_sensor called CarPresence. Keep this page open and power on the device that would be running the code in your car.

If the code is running properly you should see the state of the device change from ‘off’ to ‘on’. The case of your payload (as I discovered) in your automation is important. ON and on are treated as being different by the MQTT broker.

It seems your binary_sensor is being created and the state is being changed according to mosquitto log. Check this in ‘states’ in the developer panel.

Hope this helps!

1 Like

What pubsubclient you use?

PubSubClient 2.7 Nicholas O’Leary

I note in the limitations section in the ReadMe.md it says:

Limitations

  • It can only publish QoS 0 messages. It can subscribe at QoS 0 or QoS 1.
  • The maximum message size, including header, is 128 bytes by default. This
    is configurable via MQTT_MAX_PACKET_SIZE in PubSubClient.h.
  • The keepalive interval is set to 15 seconds by default. This is configurable
    via MQTT_KEEPALIVE in PubSubClient.h.
  • The client uses MQTT 3.1.1 by default. It can be changed to use MQTT 3.1 by
    changing value of MQTT_VERSION in PubSubClient.h.

so you may want to check PubSubClient is v.2.7 and which MQTT you are using.

I done all that and still nothing…can’t receive:
homeassistant/sensor/CarPresence-uptime/config {“name”: “CarPresence-uptime”, “state_topic”: “homeassistant/sensor/CarPresence-uptime/state”, “unit_of_measurement”: “msec”, “value_template”: “{{ value }}”}
homeassistant/sensor/CarPresence-uptime/state 7469
homeassistant/binary_sensor/CarPresence/config {“name”: “CarPresence”, “device_class”: “connectivity”, “state_topic”: “homeassistant/binary_sensor/CarPresence/state”}
homeassistant/binary_sensor/CarPresence/state ON
homeassistant/sensor/CarPresence-signal/config {“name”: “CarPresence-signal”, “state_topic”: “homeassistant/sensor/CarPresence-signal/state”, “unit_of_measurement”: “dBm”, “value_template”: “{{ value }}”}

@luma Just found this, looks pretty cool. I need to digest it some.
Now you know why I never get any projects finished. squirrel , :slight_smile:

What’s the current status? Have you seen the “Open Garage” project using HC-SR04 for car presence detection? I don’t need it to open the door, I already have a cover entity for that. Just to know which car left the building.

This project won’t tell you where your car is, outside of the fact that it is either connected to your WiFi or it isn’t. The intent is to eliminate the need to press a button to open and close your garage. You mention you have a cover entity already setup, which is a requirement. This project is used to intelligently notify your existing cover entity that it needs to open when you’re coming home or leaving, and close when you’re done. I don’t see a means for the HC-SR04 to do that, so this project is something of a different animal.

Simplified workflow:

  • You start your car in the garage to leave the house, your garage door opens automatically in response.
  • You drive away, the door closes
  • You come back home, the door opens as you approach
  • You turn off your car and go into your home, the door closes again

I hear you, Pretty neat way of using the LWT. Your two projects has made me learn more about MQTT than anything else. I just mention the “Open Garage” as point of reference. I was trying to think through the state changes to see if weather the car was arriving or departing could be gleaned. As long as you never got out of sequence (AR, DP, AR, DP, etc). But if you ever did, then there is no recovery.
The reason I wanted to know was if the door was open and the car left, then shut the door, you idea takes care of that. The LWT is really elegant and simple. Now my only excuse to use HC-SR)4 would be to create a feature that would let you know when the car is in the right spot. :slight_smile:
EDIT: I forgot, squirrel.

Question, why did you use an antenna?
Does it have something to do with your home layout? Had to get the car far enough away before the door started to close? Needed it for enough lead time on arriving for the door to open?
Right now my guess is the last one.

You raise a good point - the workflow that I stated is the only thing this does. It tells you nothing about where the car actually is at any given time, just that it’s powered on and nearby or not. If you want to know where it is (home or away, or in a given spot, or GPS coords, etc), you’ll need to add something else to the mix.

Nailed it! For whatever reason the signal without the antenna in my last car (Dodge Challenger) resulted in my having to get halfway up the drive, adding an appropriate antenna gave me a little more reach so the door was fully up by the time I got to the garage. I recently went all suburbanite American and bought an F-150 that for whatever reason (aluminum body?) doesn’t have as much problem with signal attenuation. Like most things RF… it’s a mystery to me.

Hello! I get a terrible delay connecting to my network. I have done a hot-spot on my phone and it connects almost instantly, but for some reason it takes 30s to minutes before it connects to my local WiFi. Any ideas?

Using DD-WRT on my Netgear R6700. I tried setting it up as a static IP but that doesn’t seem to help.

30sec is waaaay too long, something funky is going on, and if it’s connecting OK to your hotspot, that leaves your Netgear.

I posted this a while back trying to knock down the connection time below 2.9s. A bunch of ideas were posted, none of them helped, and others confirmed that timing. So you should be able to do 3sec, just not much faster than that.

how add binary sensor manualy? dont work mqtt discovery

just used a MI door sensor , connected to HA via a hub… not MQTT involved.

Excellent work! You inspired me to create something similar.

If you happen to have a car with a WiFi access point you could do a similar thing like I have:

@luma what would be required to modify the arduino code for a D1 Mini ESP32 based board? I have some of those lying around, but I am new to the game and pretty much have only worked with flashing WLED and ESPHome to them, never used Arduino IDE. Thanks!