{"id":21009705,"url":"https://github.com/lily-osp/smart-relays","last_synced_at":"2026-04-12T14:40:02.266Z","repository":{"id":112728782,"uuid":"535781931","full_name":"lily-osp/smart-relays","owner":"lily-osp","description":"This is a code that can be used to control relays with push buttons and MQTT. It gives the user a lot of flexibility to customize the code to fit their own needs. I think it would be a great tool for anyone looking to build their own solutions for controlling relays.","archived":false,"fork":false,"pushed_at":"2023-09-18T10:18:52.000Z","size":80,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"single","last_synced_at":"2025-01-20T11:11:53.437Z","etag":null,"topics":["adafruit","adafruit-io","arduino","automation","blynk","blynk-iot-platform","esp8266","iot","iot-application","mqtt","relays","web-ui"],"latest_commit_sha":null,"homepage":"","language":"C++","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/lily-osp.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}},"created_at":"2022-09-12T17:39:38.000Z","updated_at":"2023-11-26T12:39:22.000Z","dependencies_parsed_at":"2024-11-30T07:31:38.226Z","dependency_job_id":null,"html_url":"https://github.com/lily-osp/smart-relays","commit_stats":null,"previous_names":["lily-osp/smart-relays"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lily-osp%2Fsmart-relays","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lily-osp%2Fsmart-relays/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lily-osp%2Fsmart-relays/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lily-osp%2Fsmart-relays/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lily-osp","download_url":"https://codeload.github.com/lily-osp/smart-relays/tar.gz/refs/heads/single","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243431460,"owners_count":20289895,"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":["adafruit","adafruit-io","arduino","automation","blynk","blynk-iot-platform","esp8266","iot","iot-application","mqtt","relays","web-ui"],"created_at":"2024-11-19T09:17:40.333Z","updated_at":"2025-12-28T14:57:52.478Z","avatar_url":"https://github.com/lily-osp.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MQTT Relay Control\n\n[![CodeFactor](https://www.codefactor.io/repository/github/1999azzar/smart-relays/badge)](https://www.codefactor.io/repository/github/1999azzar/smart-relays)\n\nThis code provides a simple implementation of an MQTT-based relay control system using an ESP8266 board (such as NodeMCU). It connects to the Adafruit IO MQTT server and allows you to control a relay remotely through the Adafruit IO platform or a physical button connected to the board.\n\ntranslation:\n\n- [indonesian](https://github.com/1999AZZAR/smart-relays/blob/single/readme_id.md)\n\n## Prerequisites\n\nBefore running this code, make sure you have the following:\n\n- An ESP8266-based board (e.g., NodeMCU) with the necessary firmware installed.\n- Arduino IDE with the ESP8266 board package installed.\n- An Adafruit IO account.\n- Wi-Fi network credentials (SSID and password).\n\n## Hardware Setup\n\nConnect the following components to your ESP8266 board:\n\n- Connect a push button to pin D2 (GPIO 2) of the board. This button will be used to control the relay locally.\n- Connect a relay module to pin D3 (GPIO 3) of the board. This relay will control the electrical device you want to switch on/off.\n\n## Software Setup\n\n1. Install the necessary libraries:\n   - Adafruit MQTT Library: Install it from the Arduino Library Manager (Sketch -\u003e Include Library -\u003e Manage Libraries) by searching for \"Adafruit MQTT Library\" and clicking \"Install\".\n   - Adafruit MQTT Client Library: Install it from the Arduino Library Manager by searching for \"Adafruit MQTT Client\" and clicking \"Install\".\n   - ESP8266WiFi Library: Install it from the Arduino Library Manager by searching for \"ESP8266WiFi\" and clicking \"Install\".\n\n2. Open the Arduino IDE and create a new sketch.\n\n3. Copy and paste the provided code into the sketch.\n\n4. Update the following constants with your Wi-Fi and Adafruit IO credentials:\n   - `ssid`: Set it to your Wi-Fi network SSID.\n   - `password`: Set it to your Wi-Fi network password.\n   - `AIO_USERNAME`: Set it to your Adafruit IO username.\n   - `AIO_KEY`: Set it to your Adafruit IO key.\n\n5. Upload the sketch to your ESP8266 board.\n\n6. Open the serial monitor (Tools -\u003e Serial Monitor) to monitor the board's output.\n\n## Usage\n\nOnce the code is uploaded to your ESP8266 board, it will connect to your Wi-Fi network and Adafruit IO MQTT server.\n\n### Remote Control via Adafruit IO\n\n1. Go to the [Adafruit IO](https://io.adafruit.com) website  and log in to your account.\n\n2. Create a new feed called \"relay\" in your Adafruit IO dashboard.\n\n3. To control the relay remotely, navigate to your \"relay\" feed and send MQTT messages with the payload `1` to turn the relay on or `0` to turn it off.\n\n### Local Control via Push Button\n\nPress the push button connected to the D2 pin of the ESP8266 board to toggle the state of the relay.\n\n### Monitoring\n\nThe serial monitor will display the current status of the relay (ON or OFF) and indicate successful or failed publishing to Adafruit IO.\n\n## Flowchart\n\n```mermaid\ngraph TD\nA[Start] --\u003e B[Initialize libraries and credentials]\nB --\u003e C[Set up button and relay pins]\nC --\u003e D[Connect to Wi-Fi and Adafruit IO MQTT server]\nD --\u003e E[Set initial relay state to off]\nE --\u003e F[Enter main loop]\nF --\u003e G[Process incoming MQTT packets]\nG --\u003e H[Read button state]\nH --\u003e I[Publish button state to Adafruit IO]\nI --\u003e F\nG --\u003e J[If new MQTT message received for relay feed]\nJ --\u003e K[Update relay state]\nK --\u003e L[If relay state is '1']\nL --\u003e M[Turn on relay]\nM --\u003e N[Print 'Relay ON']\nL --\u003e O[If relay state is '0']\nO --\u003e P[Turn off relay]\nP --\u003e Q[Print 'Relay OFF']\nJ --\u003e R[Publish current relay state to Adafruit IO]\nR --\u003e S[If publishing successful]\nS --\u003e T[Print 'Published to Adafruit IO']\nR --\u003e U[If publishing fails]\nU --\u003e V[Print 'Publishing failed']\nR --\u003e F\n```\n\n## Troubleshooting\n\n- If the ESP8266 fails to connect to your Wi-Fi network, ensure that you have entered the correct SSID and password.\n- Check your Adafruit IO credentials (username and key) to ensure they are correct.\n- Verify the wiring connections between the ESP8266 board, button, and relay.\n- Make sure you have a stable internet connection.\n\n## Note\n\nThis code serves as a basic example and may require modifications to suit your specific requirements. Refer to the official documentation of the libraries used for more advanced usage and functionality.\n\n## Credits\n\nThis code is based on the Adafruit IO Arduino library examples and modified for this specific relay control application.\n\n- [Adafruit IO Arduino Library](https://github.com/adafruit/Adafruit_IO_Arduino)\n- [Adafruit MQTT Library](https://github.com/adafruit/Adafruit_MQTT_Library)\n- [ESP8266WiFi Library](https://github.com/esp8266/Arduino)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flily-osp%2Fsmart-relays","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flily-osp%2Fsmart-relays","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flily-osp%2Fsmart-relays/lists"}