Experiment with Location, geofencing,  and a MQTT button

Experiment with Location, geofencing, and a MQTT button

TL;DR, This Saturday I did experiment with my Node-RED server and some geo fencing APIs.

I create a location-sensitive button; when a user's mobile is inside the geo-fencing (red circle), the lamp glows up, and once the user's mobile goes out, the lamp glows down.

glows up lamp
glows down lamp

I used one open-source library to achieve this: node-red-contrib-world-map.

The node-red-contrib-web-worldmap 5.1.2 is a popular Node-RED node that allows you to display geographical data on an interactive world map within your Node-RED flows. and some JavaScript code snippets. Like

// Function node code

var circleData = flow.get("circleData");

var circleLat = circleData.lat;
var circleLon = circleData.lon;
var circleRadius = circleData.radius;

var currentLat = msg.payload.lat;
var currentLon = msg.payload.lon;

if (circleData && msg.payload) {


function calculateDistance(lat1, lon1, lat2, lon2) {
    // Haversine formula (approximate distance)
    var R = 6371e3; // Earth radius in meters
    var dLat = (lat2 - lat1) * Math.PI / 180;
    var dLon = (lon2 - lon1) * Math.PI / 180;
    var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
        Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) *
        Math.sin(dLon / 2) * Math.sin(dLon / 2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    return R * c;
}

var distance = calculateDistance(circleLat, circleLon, currentLat, currentLon);

if (distance <= circleRadius) {
    msg.payload = { inside: true, distance: distance , radius: circleRadius};
} else {
    msg.payload = { inside: false, distance: distance ,radius: circleRadius };
}
}else{
    node.error = "somthing went wrong"
}
return msg;
        

The idea is completely based on the Haversine formula , with this help, I create the above method, which returns an object containing a boolean (which tells if the mobile is inside the geofencing or outside), the distance of the mobile (GPS device) from the center (geofencing), and the radius of geofencing.

If the mobile is inside the fencing, then the lamp glows up and vice versa.

Use case:

  1. Home automation: We can use this setup in home automation. When some person comes inside the house, the garden light turns on and turns off in a vice versa situation.
  2. Lock and unlock: The same setup can be used to lock and unlock the home doors.


If you are an IoT hobbyist and you have the knowledge of Node-RED, then you can use my work by downloading my workflow in the given link.

https://gist.github.com/iamrajendraverma/bc6d1bb4252bf8e299396f57f84ec74c

If you want to lean IOT with me, then you can message me at linkedin






要查看或添加评论,请登录

Rajendra Verma的更多文章

  • My First AI Model

    My First AI Model

    If you will check the below table, then you will get to know that Y is dependent on X and the relationship is. y = 2x -…

  • Evolution of Neural Network

    Evolution of Neural Network

    This is a very intersecting topic, just like an Netflix webseries. Now a days everyone is talking about AI, but no one…

  • What is Deep Learning?

    What is Deep Learning?

    A subset of machine learning: Deep learning is a branch of machine learning that focuses on using artificial neural…

  • Very Good CLI vs Flutter CLI: A Clear Distinction

    Very Good CLI vs Flutter CLI: A Clear Distinction

    We all know about Flutter cli, that it is known to develop the project and that it works to update and upgrade the…

  • Flutter Internationalization

    Flutter Internationalization

    When you create a mobile app and it hits the international market, you have to internationalize the app which is an…

  • Mixin in dart

    Mixin in dart

    Flutter is becoming the most popular language now for mobile development. When developers write code, they encounter…

  • Types of provider in Flutter

    Types of provider in Flutter

    I was working on one of my Flutter projects, and suddenly I thought that it is a good idea to explain the difference…

  • Flutter

    Flutter

    A couple of days ago, I was thinking of learning to flutter because of market requirements. But I find it too easy and…

  • My Experiment with the Matter Protocol

    My Experiment with the Matter Protocol

    Exploring the Future of IoT: An In-Depth Look at the Matter Protocol As a software engineer specializing in IoT…

  • How to filter array of a positive integer and negative integer in kotlin

    How to filter array of a positive integer and negative integer in kotlin

    Interviews are asking a general question nowadays from Kotlin developers so that I like to give its answer fun main(){…