Experimenting with HTML Geolocation API

I don't use template nodes myself, but you could try to create a code snippet similar to this one:

<md-button class="md-raised" ng-click="clicked(); sendMsg(msg)"

function sendMsg(msg) {
   if (navigator.geolocation) {
      var position = navigator.geolocation.getCurrentPosition();
      msg.latitude = position.coords.latitude;
      msg.longitude = position.coords.longitude;
      $scope.send(msg);
   } 
   else {
      alert("Geolocation is not supported by this browser.");
   }
}

Caution: I haven't tested this code !!!

That's great thankyou!, I haven't used template nodes either.

I'll give it a go and report back.

Appreciate the help.

1 Like

Here it is..


<!DOCTYPE html>
<html>
<script type="text/javascript">
var theScope = scope;
var lat = "not-set";
var long = "not-set";


// Watch the payload and update
(function(scope) {
    scope.$watch('msg.payload', function(data) {
    update(data);
    });
})(scope);

function showLocation(position) {
    var gps_obj = {};
    lat = position.coords.latitude;
    long = position.coords.longitude;
    console.log("lat = "+lat);
    console.log("long = "+long);
    gps_obj.lat = lat;
    gps_obj.long = long;
    theScope.send({payload:gps_obj});

}


function update(dta) {
    
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showLocation);
    }

/*
      dta.name = lat ;//"other";
      dta.info = long; //"stuff";
      theScope.send({payload:dta});
*/      
}


</script>
</html>


1 Like

If this was me (at this moment), I would build an ESP32 node in the car using my mobile phone as a 4G hotspot and have it send geo location info to a server running Node-RED and when in the predetermined range, have a Telegram bot in Node-RED send a message to your mobile in the car asking if you want to open the garage door. In addition, you can also just override (anytime) and send a Telegram message from your mobile and have it open the garage door (without using any geo location stuff, since it just takes a second to send a Telegram directive to NR).

I have tested Lora and NB-IoT for tracking, and both are "spotty at best" compared to 4G mobile services and since the big commercial telcos put huge money in their standard 4G and 5G nets, it is easier to have a 4G hotspot in your car if you want reliable ESP comms back to a server.

You could easily build your own Lora service at your home, but this is a lot of extra work to do which can be easily done with a Lora node (but I prefer ESP and a hotspot) in your car (unless you are a "Lora person" already). But if you have a Lora G/W setup already, you can use that, or use someone's service (if available like Things) but when I test these extensively, they not very reliable (its like a ham radio toy, not a reliable service like 4G cellular coverage, where I am). But maybe you like tinkering with Lora and/or NB-Iot?

So, if you are like me, you want it to be very reliable and easy and fun, so I recommend an ESP32 (or ESP8266) node in your car connected to a mobile hotspot in your car, and use Node-RED and Telegram to manage command and control. Telegram bots are really fun and I think, overlooked by many makers.

Honestly, if it was me I would drop the "geo location" part, and just use Telegram to send a "open the darn garage door" message when I was close to home. On rainy days, I might want to wait until parked and all "ready" so the rain will not blow into the garage! That's how I would do it..... Node-RED, a Telegram bot and Telegram on mobile (and you have the added benefit of Telegram on desktop as well when you want to open the door for someone else while working on the computer, LOL)

Hope this helps

Hi @unixneo,
Then indeed the battery problem is solved. Will have a look at your proposal in the near future!

Absolutely :wink:

Hmm that won't be easy. I'm a developer, so I will keep looking for a software solution, without extra hardware or external tools. That is the 'fun' part for me ...

Thanks for sharing your solution!

Hmm, I think that timing may be an issue with that solution. While Telegram is good, it does occasionally take a few seconds to send you an update.

What type of phone are you using?

I wonder how far your WiFi extends into the street? Potentially, you could use your phone joining the WiFi as an event to trigger the door.

Another option would be to use ESP32 or some other device with a Bluetooth module on it in the garage. Your phone could connect to it. Trouble is, you only get 100m at best from BT and it can be flaky on connections.

Using an extra ESP32 in the car seems to go against your original desire not to use more tools! In fact, OwnTracks would probably be simpler :slight_smile:

Depending on your phone though, it is possible that the phone itself is able to track its location and then you could trigger something via phone automation. Exactly what and how depends on the phone platform. As a minimum, I think that IFTTT can be triggered from both iOS and Android based on location. IFTTT can trigger Node-RED (though you will need to expose an endpoint to the Internet - can be restricted to just this use though so not too much of an issue). Android will be generally easier to do this type of automation from than iOS.

Actually, if you are going to expose a special endpoint, you might as well do it directly rather than via IFTTT which can also be slow to react. Add a security parameter to the endpoint, and make it https of course, various ways you can do that without too much trouble.

1 Like