---
# System prepended metadata

title: How to make an end to end sensor-indicator model IoT device system

---

---
title: 'How to make an end to end sensor-indicator model IoT device system'
---

## Sensor-Indicator model IoT Device System

This model of IoT device system deals with two components
- Sensor module 
    > It contains sensors which can be analog or digital along with a microcontroller with some communication features like WIFI, cellular GSM, LoRa WAN or ethernet. The data obtained in transmitted from this module

    In our project, we have used LDR (light dependent resistor) or also called photoresitor to sense the intensity of light in the surrounding. It is an analog sensor. The less the light intensity, the less will be its value and more the light intensity more is the value. The microcontroller we used here is NodeMCU running on ESP8266 chip with WIFI capabilites.
    
- Indicator Model
    > The data received from the sensor will now be utilised here for representing in the form of LEDs level indicator.
    
    In this project, the data we get from the sensor module (LDR value), is mapped into 5 LEDs.

- Communication
    > Data transfer from one module to another can be done by various protocols like HTTP, TCP/UDP. 
    
    Here we used MQTT (Message Queuing Telemetry Transport). It follows the TCP/IP connection protocol with a pub/sub feature. A message is published to a broker through a topic from a client. Another client now subscribe to that topic to recieve the message. MQTT can be local or global. Local MQTT works on local network like homes, offices, etc whereas in global MQTT, messages can be published anywhere the world.
    
### How we integrated everything?

In our project, the data from the LDR sensor is published to MQTT broker (about broker we will come later) via the topic `room/ldrval`. The indicator module is subcribed to that topic from the broker. 
We added another featue to the project that we can control (on/off) a LED with on and off commands passed via `room/switch` topic.

### About the MQTT Broker

We have used global self-made MQTT broker. We have used a `droplet` or virtual machine on linux distribution specifically Ubuntu and installed `mosquitto` broker. We have configured the virtual machine to listen to port 1880 with no authentication and authorization (well that is our future scope to make it secure). Thus our global self-made MQTT broker with almost zero-latency. We have used `DigitalOcean's droplet`.

### What about the python script?

The idea behind the use of the python script is that we wanted a centralised server through which the data must get uploaded to our database for future reference and study. We have used our virtual machine to host the script in background (daemon-task). This script is subscribed to the topic `room/ldrval` and uses `MongoDB Atlas` cloud to upload the data with a timestamp with the help of `pymongo` package from `pip`. The script is available at [here](https://github.com/swagatachanda/iotProject/blob/master/iotScripts/iotmqtt.py).

### How did we represent our project?

We have created a `NodeJs` backend-service built with `Express` framework and utilized `Socket.io` for websocket protocol and `HTTP` protocol. The backend service provides the following feature 
- Fetch data from database
- Switch on/off the led
- WebSocket support to provide realtime data

The API documentation can be found at [here](https://documenter.getpostman.com/view/14983874/UVC3koRP). We have used `Postman` for testing and preparing the documentation and used `Heroku` for deploying the backend-service.

The frontend service is made in `ReactJs`. The data from the database can be viewed here in charts by using `Chart.js`. Real-time data is sent thorugh `socket.io` from backend and recieved on the front-end by `socket.io-client`. There is a button which will provide the interaction of the LED.

### Where to find the project?
Our project repos : 
- [Backend](https://github.com/swagatachanda/iotProject)
- Frontend

### Who have contributed?
Contributors : 
- Arnab Chatterjee
- Ayatika Bhowmick
- Patrali Sarkar
- Pratyay Saha
- Swagata Chanda