Proximity Detection in NodeMCU Using Wifi
by jnamanjain2001 in Circuits > Arduino
715 Views, 1 Favorites, 0 Comments
Proximity Detection in NodeMCU Using Wifi
In this project we connect an ESP8266 to a WiFi access point. If a mobile phone with a specific MAC address is also connected to the same access point with RSSI greater than a threshold then updated its status to an IoT dashboard. We use different LED colors on ESP32 to show the status of send/delivered message
Supplies
Hardware Requirements:
- ESP8266 WiFi module
- RGB Led
- Jumper Cables
- Micro Usb Cable
Software Requirements:
- Arduino IDE
- Ubidots Account
- Node.js
Arduino Library Requirements:
AVision_ESP8266 by A-Vision Software
- PubSubClient.h (https://github.com/knolleary/pubsubclient)
PubSubClient by Nick O'Leary
- ArduinoJson.h (https://github.com/bblanchon/ArduinoJson)
ArduinoJson by Benoit Blanchon
- Ubidots.h (https://github.com/ubidots/ubidots-esp8266)
Ubidots MQTT for ESP8266 by Jose Garcia
Initial Setup
Clone the GitHub Repository - https://github.com/namanjain-iitr/Proximity-Detector
Using ssh:
git clone git@github.com:namanjain-iitr/Proximity-Detector.git
Navigate to Proximity-Detector library using:
cd Proximity-Detector
- Install Node.js to run Frontend and Backend Components - https://nodejs.org/en/download
- Download the Arduino IDE to upload and debug arduino code - https://www.arduino.cc/en/software
- Setup Arduino IDE for NodeMCU via these steps - https://www.instructables.com/Steps-to-Setup-Arduino-IDE-for-NODEMCU-ESP8266-WiF/
- Install all the libraries mentioned in previous section in Arduino IDE - https://docs.arduino.cc/software/ide-v1/tutorials/installing-libraries
- (Optionally) Download any IDE to open and edit Frontend and Backend code files.
Connect ESP 8266 to RGB lights according to given pinout diagram.
Ubidots Setup
- Create a Ubidots STEM account at - https://ubidots.com/stem
- Log In to your Ubidots STEM account - https://stem.ubidots.com/accounts/signin/
- Click on Profile Icon on Top Right side and select API Credentials.
- Copy the default token value and Save it for use in Next Section.
Arduino Code Setup
Navigate to WifiProximity Directory to access WifiProximity.ino file that contains the arduino code for our project:
cd WifiProximity
Now open the WifiProximity.ino file inside your arduino IDE and fill the following details:
First, enter your Ubidots Token string taken from the previous step:
char ubidotsToken[] = ""; // Enter Your Ubidots Token String From Ubidots
Next, Fill the SSID and Password of your Wifi-Router in respective fields. NodeMCU will use this WiFi to connect to the internet:
const char *ssid = ""; // Your Wifi Username
const char *password = ""; // Your Wifi Password
Fill the URL and port for you MQTT broker. There are several ways to get your MQTT broker:
- Download and locally run your own MQTT broker offline from - https://mosquitto.org/download/
- Create a private instance of Cloud MQTT Broker. E.g- HiveMQ.
- For testing you can also use a free public MQTT broker. E.g- https://www.hivemq.com/public-mqtt-broker/
Currently for testing purposes we are using the free public MQTT broker by HiveMQ.
const char *mqtt_broker = "broker.hivemq.com"; // MQTT Broker URL
const int mqtt_port = 1883; // MQTT Port
Now, macTopic and statusTopic are distinct strings that uniquely identify the MQTT topic for receiving and sending data. You can enter any two distinct strings of your choice. On public MQTT broker use random string to avoid clash of MQTT topic with other MQTT users.
const char *macTopic = ""; // MQTT topic for subscribing to mac address list
const char *statusTopic = ""; // MQTT topic to publish rssi of nearby devices
To learn more about MQTT protocol visit: https://mqtt.org/
Finally, Upload the updated code to ESP8266 via a Micro-USB Cable.
Backend Setup
The Backend API Services are coded in Node JS. To run the backend server make sure you have node installed.
If you are inside WifiProximity Directory, First Navigate to root directory of the project using:
cd ..
Navigate to the Backend directory :
cd Backend/
Fill the following values inside index.js file in backend folder. Make sure that all values are exactly same as values inside WifiProximity.ino file filled in previous step.
const macTopic = ''; // MQTT topic for subscribing to mac address list
const statusTopic = ''; // MQTT topic to publish rssi of nearby devices
const mqttHost = ''; // MQTT Broker URL
const mqttPort = ''; // MQTT Port
Run the following command inside Backend Folder to install all the backend dependencies:
npm install
To start the backend server, run :
npm start
After running the above command, the console should output if the server was started successfully at port:
Server started at port 4000
This will start the backend server at http://localhost:4000.
Frontend Setup
The frontend for this project is coded in React JS. To run the code, make sure you have Node Package Manager (npm) installed.
If you are inside Backend Directory, First Navigate to root directory of the project using:
cd ..
Now, Navigate to the Frontend directory :
cd Frontend/
Run the following command inside Frontend Folder to install all the frontend dependencies:
npm install --legacy-peer-deps
Now, Depending on your requirements, you either can run:
npm start
This runs the app in the development mode. Open http://localhost:3000 to view it in your browser. The page will reload when you make changes. You may also see any lint errors in the console. Or you can also run:
npm run build
This builds the app for production to the build folder. It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes. Your app is ready to be deployed! See the section about deployment for more information.
Working
- ESP 8266 takes a list of mac address from backend for proximity detection.
- It runs the Wifi Proximity Detection algorithm by Ricardo Oliveria using Wifi Promiscuous mode to check which devices are present in proximity of ESP 8266 device. Video Explanation - https://youtu.be/30Eww40s9D0
- It sends the rssi value of all the device within proximity to Backend server as well as Ubidots dashboard.
- We can add mac address of any new device for proximity detection from our react frontend.
- React Frontend also shows a list of last-seen time and best RSSI value of all devices that are being tracked.
- Ubidots Dashboard shows a live graph of device status for each mac address in our list.
RGB Led Modes:
- Blue Indicates that ESP 8266 is running Proximity Detection Algorithm
- Green indicates atleast one device from give mac-address list is present in proximity.
- Red indicates that ESP 8266 is either connecting to Wifi or sending/receiving data from Ubidots/Backend.
Notes:
- Any change in device status will take a couple minutes to reflect on Ubidots dashboard and React Frontend.
- Also note that device must be sending some network packets for Proximity detection by ESP 8266.
- It is not necessary for device and ESP 8266 to be on same network for proximity detection.
- Turn off Private Wifi-Address or mac-address randomization in device to ensure that its mac address doesn't change.