# rajkhare05/smart-bin architecture

> Architecture diagram of the GitHub repository rajkhare05/smart-bin, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/rajkhare05/smart-bin
- Repository: https://github.com/rajkhare05/smart-bin

## Overview

#### Purpose
Smart Bin is an IoT waste-bin monitoring system. An ESP8266/Arduino-side sketch collects hardware signals (documented components include HC-SR04 distance sensing and SIM800L connectivity), while Node.js services expose bin data and Telegram-based user interaction.

#### Device Firmware Boundary
The embedded device program is concentrated in `esp.ino`. It is the physical-system boundary: it should read bin/sensor state and communicate it upstream over the ESP8266’s network connection. Hardware dependencies are described in `README.md`, including ESP8266, Arduino Nano, SIM800L, and HC-SR04.

#### Backend Service
The server is a Node.js application rooted at `server/index.js`, with runtime settings in `server/config.js` and dependencies declared by `server/package.json`. This service is the likely ingestion/API boundary between deployed bins and client-facing automation.

#### Persistent State
`server/dump.rdb` indicates Redis is used for persisted server state. Redis likely holds current bin readings, location/status records, or other fast-changing device data. The backend owns the boundary between incoming device updates and this datastore.

#### Telegram Bot Interface
A separate Node.js Telegram bot process is rooted at `telegram-bot/index.js`, configured by `telegram-bot/config.js`, and packaged independently in `telegram-bot/package.json`. This separation allows messaging credentials, polling/webhook behavior, and conversational handling to evolve independently from the device/API service.

#### Command and Callback Routing
Bot interactions are organized by Telegram interaction type:
- Text/basic commands: `telegram-bot/commands/basic.js`
- Inline callback handling: `telegram-bot/callbackQueries/city.js`

The city callback module suggests a user flow where users select or filter bin information by city. The bot is the human-facing control/read boundary; it should query backend-owned state rather than communicate directly with embedded devices.

#### Primary Flow
1. Sensors attached to the bin produce measurements in `esp.ino`.
2. The firmware sends measurements/status to the Node backend at `server/index.js`.
3. The backend validates or transforms updates and stores operational state in Redis, represented by `server/dump.rdb`.
4. Telegram users issue commands or select city callbacks through `telegram-bot/index.js`.
5. Bot handlers in `telegram-bot/commands/basic.js` and `telegram-bot/callbackQueries/city.js` retrieve and present relevant bin information.

#### Runtime Topology
There are three material runtimes: embedded ESP8266 firmware, the Node.js backend, and the Node.js Telegram bot. Redis is the state infrastructure, and Telegram is the external messaging platform. Configuration is split per Node process (`server/config.js` and `telegram-bot/config.js`), implying deployment and credential boundaries between API/storage concerns and bot concerns.

Main entry points: [`telegram-bot/index.js`](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/index.js) (Bot Runtime), [`server/index.js`](https://github.com/rajkhare05/smart-bin/blob/master/server/index.js) (Ingestion API), [`esp.ino`](https://github.com/rajkhare05/smart-bin/blob/master/esp.ino) (ESP8266 Firmware), [`telegram-bot/commands/basic.js`](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/commands/basic.js) (Basic Commands)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_device["Embedded Device"]
  node_hardware{{"Bin Sensors<br/>physical hardware<br/>[README.md]"}}
  node_firmware["ESP8266 Firmware<br/>Arduino sketch<br/>[esp.ino]"]
end

subgraph group_backend["Backend Service"]
  node_backend_config["Backend Settings<br/>Node config<br/>[config.js]"]
  node_backend_api["Ingestion API<br/>Node.js service<br/>[index.js]"]
  node_backend_deps["Backend Dependencies<br/>package manifest<br/>[package.json]"]
  node_redis[("Redis State<br/>Redis datastore<br/>[dump.rdb]")]
end

subgraph group_bot["Telegram Bot"]
  node_bot_runtime["Bot Runtime<br/>Node.js bot<br/>[index.js]"]
  node_bot_config["Bot Settings<br/>Node config<br/>[config.js]"]
  node_basic_commands["Basic Commands<br/>command handler<br/>[basic.js]"]
  node_city_callbacks["City Callbacks<br/>callback handler<br/>[city.js]"]
  node_bot_deps["Bot Dependencies<br/>package manifest<br/>[package.json]"]
end

node_telegram(("Telegram Platform<br/>external messaging"))

node_hardware -->|"measurements"| node_firmware
node_firmware -->|"bin updates"| node_backend_api
node_backend_config -.->|"configures"| node_backend_api
node_backend_deps -.->|"packages"| node_backend_api
node_backend_api -->|"stores state"| node_redis
node_telegram -->|"updates"| node_bot_runtime
node_bot_runtime -->|"responses"| node_telegram
node_bot_config -.->|"configures"| node_bot_runtime
node_bot_deps -.->|"packages"| node_bot_runtime
node_bot_runtime -->|"routes commands"| node_basic_commands
node_bot_runtime -->|"routes callbacks"| node_city_callbacks
node_basic_commands -->|"queries bin data"| node_backend_api
node_city_callbacks -->|"filters by city"| node_backend_api

click node_hardware "https://github.com/rajkhare05/smart-bin/blob/master/README.md"
click node_firmware "https://github.com/rajkhare05/smart-bin/blob/master/esp.ino"
click node_backend_config "https://github.com/rajkhare05/smart-bin/blob/master/server/config.js"
click node_backend_api "https://github.com/rajkhare05/smart-bin/blob/master/server/index.js"
click node_backend_deps "https://github.com/rajkhare05/smart-bin/blob/master/server/package.json"
click node_redis "https://github.com/rajkhare05/smart-bin/blob/master/server/dump.rdb"
click node_bot_runtime "https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/index.js"
click node_bot_config "https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/config.js"
click node_basic_commands "https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/commands/basic.js"
click node_city_callbacks "https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/callbackQueries/city.js"
click node_bot_deps "https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_hardware,node_firmware toneBlue
class node_backend_config,node_backend_api,node_backend_deps,node_redis toneAmber
class node_bot_runtime,node_bot_config,node_basic_commands,node_city_callbacks,node_bot_deps toneMint
class node_telegram toneNeutral
```

## Components

### Embedded Device

- [Bin Sensors](https://github.com/rajkhare05/smart-bin/blob/master/README.md): physical hardware · HC-SR04 and bin hardware provide measurements. · `README.md`
- [ESP8266 Firmware](https://github.com/rajkhare05/smart-bin/blob/master/esp.ino): Arduino sketch · Reads bin state and sends upstream updates. · `esp.ino`

### Backend Service

- [Backend Settings](https://github.com/rajkhare05/smart-bin/blob/master/server/config.js): Node config · `server/config.js`
- [Ingestion API](https://github.com/rajkhare05/smart-bin/blob/master/server/index.js): Node.js service · Validates device updates and serves bin state. · `server/index.js`
- [Backend Dependencies](https://github.com/rajkhare05/smart-bin/blob/master/server/package.json): package manifest · `server/package.json`
- [Redis State](https://github.com/rajkhare05/smart-bin/blob/master/server/dump.rdb): Redis datastore · Stores current operational bin data. · `server/dump.rdb`

### Telegram Bot

- [Bot Runtime](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/index.js): Node.js bot · Human-facing Telegram interaction boundary. · `telegram-bot/index.js`
- [Bot Settings](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/config.js): Node config · `telegram-bot/config.js`
- [Basic Commands](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/commands/basic.js): command handler · `telegram-bot/commands/basic.js`
- [City Callbacks](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/callbackQueries/city.js): callback handler · Handles city selection or filtering. · `telegram-bot/callbackQueries/city.js`
- [Bot Dependencies](https://github.com/rajkhare05/smart-bin/blob/master/telegram-bot/package.json): package manifest · `telegram-bot/package.json`

### Other components

- **Telegram Platform**: external messaging

## Connections

- Bin Sensors → ESP8266 Firmware: measurements
- ESP8266 Firmware → Ingestion API: bin updates
- Backend Settings → Ingestion API: configures
- Backend Dependencies → Ingestion API: packages
- Ingestion API → Redis State: stores state
- Telegram Platform → Bot Runtime: updates
- Bot Runtime → Telegram Platform: responses
- Bot Settings → Bot Runtime: configures
- Bot Dependencies → Bot Runtime: packages
- Bot Runtime → Basic Commands: routes commands
- Bot Runtime → City Callbacks: routes callbacks
- Basic Commands → Ingestion API: queries bin data
- City Callbacks → Ingestion API: filters by city
