{"id":27772290,"url":"https://github.com/shawsuraj203/smart_home","last_synced_at":"2026-05-01T04:40:02.622Z","repository":{"id":288748165,"uuid":"965193195","full_name":"shawsuraj203/smart_home","owner":"shawsuraj203","description":"Smart Home Power Monitoring System","archived":false,"fork":false,"pushed_at":"2025-04-27T17:23:29.000Z","size":2262,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-29T23:57:19.932Z","etag":null,"topics":["acs712","ads1115","multithreading","raspberry-pi","rest-api","sqlite3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shawsuraj203.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-12T16:08:01.000Z","updated_at":"2025-04-27T17:23:33.000Z","dependencies_parsed_at":"2025-04-19T16:07:35.440Z","dependency_job_id":"f20cacfb-4bde-4fbb-b306-f87c5f84668c","html_url":"https://github.com/shawsuraj203/smart_home","commit_stats":null,"previous_names":["shawsuraj203/smart_home"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawsuraj203%2Fsmart_home","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawsuraj203%2Fsmart_home/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawsuraj203%2Fsmart_home/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shawsuraj203%2Fsmart_home/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shawsuraj203","download_url":"https://codeload.github.com/shawsuraj203/smart_home/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251602816,"owners_count":21615963,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["acs712","ads1115","multithreading","raspberry-pi","rest-api","sqlite3"],"created_at":"2025-04-29T23:58:17.350Z","updated_at":"2026-05-01T04:39:57.595Z","avatar_url":"https://github.com/shawsuraj203.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Smart Home Sensor System Documentation\n\nThis document provides an overview of the smart home sensor system, including hardware abstraction, database management, data collection, web posting functionalities, and logging.\n\n---\n\n## HW Design\n\n![](./out/design/hw_design.png)\n\n### HW Components\n1. Raspberry Pi 5 with Raspbian OS installed.\n2. ACS712 Current Sensor.\n3. ADS1115 ADC, 4 Channel 16-bit ADC.\n4. Resistors and wiring for voltage measurement setup.\n\n---\n\n## Workflow\n\n1. **Initialize the System**:\n   - Initialize the logger module using `setup_logger()`.\n   - The ADC is initialized using `sen_hal.adc_init()`.\n   - A database connection is established using `sens_db.setup_database()`.\n\n2. **Data Collection**:\n   - The `data_Collection` function collects sensor data and stores it in the database.\n\n3. **Web Posting**:\n   - The `post_sensor_data` function posts the collected data to a web interface.\n\n4. **Graceful Shutdown**:\n   - The script handles interruptions (e.g., `KeyboardInterrupt`) and ensures proper cleanup of resources.\n\n---\n\n## Dependencies\n\n- `time`, `threading`, `logging`: Standard Python libraries.\n- `sqlite3`: For database management.\n- `requests`: For HTTP requests.\n- `Adafruit_ADS1x15`: For hardware abstraction.\n\n---\n\n## Flow Chart\n\n![](./out/design/activity/activity.png)\n\n---\n\n## 1. `logger.py` - Logging Module\n\nThis module provides functionality for setting up and managing logging throughout the system.\n\n### Functions\n\n#### `setup_logger(name, log_file, level=logging.INFO)`\nSets up a logger with the specified name, log file, and logging level.\n\n##### Parameters:\n- **`name`**: The name of the logger.\n- **`log_file`**: The file where logs will be written.\n- **`level`**: The logging level (e.g., `logging.INFO`, `logging.DEBUG`).\n\n##### Example Usage:\n```python\nlog = setup_logger(\"smart_home\", \"./smart_home.log\", level=logging.DEBUG)\n```\n\n#### `log_message(logger, message, level=logging.INFO)`\nLogs a message with the specified logger and logging level.\n\n##### Parameters:\n- **`logger`**: The logger instance.\n- **`message`**: The message to log.\n- **`level`**: The logging level (e.g., `logging.INFO`, `logging.ERROR`).\n\n##### Example Usage:\n```python\nlog_message(log, \"System initialized successfully.\", level=logging.INFO)\n```\n\n#### `get_logger()`\nReturns the global logger instance.\n\n##### Example Usage:\n```python\nlog = get_logger()\nlog.info(\"This is a log message.\")\n```\n\n---\n\n## 2. `sen_hal.py` - Sensor Hardware Abstraction Layer\n\nThis module provides functionality to interface with an ADC (Analog-to-Digital Converter) and read current values using an ACS712 current sensor.\n\n### Constants\n\n- **`GAIN`**: Gain setting for the ADC. Adjust this based on your voltage range.\n- **`ACS712_SENSITIVITY`**: Sensitivity of the ACS712 current sensor in V/A (e.g., 185mV/A for a 5A module).\n- **`VREF`**: Reference voltage for the ADS1115 ADC.\n- **`ADC_RESOLUTION`**: Resolution of the ADC (16-bit, 32768).\n- **`ADC_CALIBRATION`**: Calibration value for the sensor to mitigate inaccuracies in readings.\n\n### Global Variables\n\n- **`adc`**: A global variable to hold the ADC instance.\n\n### Functions\n\n#### `adc_init()`\nInitializes the ADC by creating an instance of the ADS1115 ADC.\n\n##### Example Usage:\n```python\nadc_init()\n```\n\n#### `read_current(device_id)`\nReads the current value for a given device ID.\n\n1. Reads the raw ADC value using the `read_adc` method (10 samples).\n2. Converts the raw ADC value to voltage using the formula:\n   ```python\n   voltage = (raw_value / ADC_RESOLUTION) * VREF\n   ```\n3. Calculates the current in amperes using the formula:\n   ```python\n   current = (voltage - (VREF / 2)) / ACS712_SENSITIVITY\n   ```\n4. Returns the calculated current.\n\n##### Parameters:\n- **`device_id`**: The ID of the device to read the current for.\n\n##### Returns:\n- **`current`**: The calculated current in amperes.\n\n##### Example Usage:\n```python\ncurrent = read_current(0)\nprint(f\"Current: {current} A\")\n```\n\n![](./out/design/sens_hal/sens_hal.png)\n\n---\n\n## 3. `sens_db.py` - SQLite Database for Sensor Data\n\nThis module provides functionality to manage an SQLite database for storing and retrieving sensor data.\n\n### Database Configuration\n\nThe database file is named `sensor_data.db`. The table `SensorData` is created with the following schema:\n\n- `id`: An auto-incrementing primary key.\n- `deviceid`: A string representing the device ID.\n- `timestamp`: A string representing the timestamp of the data.\n- `power`: A real number representing the power reading.\n- `current`: A real number representing the current reading.\n\n### Functions\n\n#### `setup_database()`\nInitializes the database connection and ensures the `SensorData` table exists.\n\n##### Example Usage:\n```python\nconn, cursor = setup_database()\n```\n\n#### `store_data(cursor, conn, data)`\nInserts sensor data into the `SensorData` table.\n\n##### Parameters:\n- **`cursor`**: The SQLite cursor object.\n- **`conn`**: The SQLite database connection object.\n- **`data`**: A dictionary containing the following keys:\n  - `deviceid` (str): The ID of the device.\n  - `timestamp` (str): The timestamp of the data.\n  - `power` (float): The power value.\n  - `current` (float): The current value.\n\n##### Example Usage:\n```python\ndata = {\n    \"deviceid\": \"0\",\n    \"timestamp\": \"2025-04-18 12:00:00\",\n    \"power\": 230.0,\n    \"current\": 1.0\n}\nstore_data(cursor, conn, data)\n```\n\n#### `get_table_row_count()`\nRetrieves the total number of rows in the `SensorData` table.\n\n##### Example Usage:\n```python\nrow_count = get_table_row_count()\nprint(f\"Total rows: {row_count}\")\n```\n\n#### `read_row_by_index(row_index)`\nReads a specific row from the `SensorData` table by its index.\n\n##### Parameters:\n- **`row_index`**: The 0-based index of the row to retrieve.\n\n##### Returns:\n- **`tuple`**: The row data as a tuple, or `None` if the index is invalid.\n\n##### Example Usage:\n```python\nrow = read_row_by_index(0)\nif row:\n    print(f\"Row data: {row}\")\nelse:\n    print(\"Invalid row index.\")\n```\n\n![](./out/design/sens_db/sens_db.png)\n\n---\n\n## 3. `sen_web.py` - Sensor Data Web Posting\n\nThis module posts sensor data from the database to a remote server using HTTP requests.\n\n### Constants\n- **`URL`**: The URL of the remote server to which the sensor data will be posted.\n\n### Functions\n\n#### `post_sensor_data()`\nPosts sensor data from the database to a remote server.\n\n1. Reads rows from the database.\n2. Filters and processes data for each device.\n3. Sends the data to the server using HTTP POST requests.\n4. Implements retry logic for failed requests.\n\n##### Example Usage:\n```python\npost_sensor_data()\n```\n\n![](./out/design/sens_web/sens_web.png)\n\n---\n\n## 5. `main.py` - Smart Home Sensor Data Collection and Web Posting\n\nThis script orchestrates the entire workflow of the smart home system, including data collection, storage, and web posting.\n\n### Constants\n- **`DEVICE_IDS`**: A list of device IDs for which sensor data is collected.\n- **`VOLTAGE`**: A constant voltage value used for power calculations.\n\n### Functions\n\n#### `read_sensor_data(device_id)`\nReads current values from a sensor and calculates power.\n\n##### Example Usage:\n```python\npower, current = read_sensor_data(0)\nprint(f\"Power: {power} W, Current: {current} A\")\n```\n\n#### `data_Collection(cursor, conn)`\nCollects sensor data, creates data entries, and stores them in the database.\n\n##### Example Usage:\n```python\ndata_Collection(cursor, conn)\n```\n\n#### `main()`\nInitializes the logger, system, starts data collection and web posting threads, and manages their lifecycle.\n\n##### Example Usage:\n```python\nif __name__ == \"__main__\":\n    main()\n```\n\n![](./out/design/sequence/sequence.png)\n\n---\n\n## Notes\n\n- The system assumes a constant voltage of 230V for power calculations for Indian households.\n- Update the `URL` in `sen_web.py` to match the actual server endpoint.\n\n---\n\n## Example Usage\n\nRun the script using the following command:\n```bash\npython main.py\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshawsuraj203%2Fsmart_home","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshawsuraj203%2Fsmart_home","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshawsuraj203%2Fsmart_home/lists"}