Skip to content

Commit dfac77e

Browse files
committed
first commit
0 parents  commit dfac77e

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Linux Blink with UI (JavaScript)
2+
3+
The **Linux Blink** example shows a simple Linux application that changes the LED state on the board. It showcases basic event handling and UI updates through a web-based interface.
4+
5+
![Linux Blink App](assets/docs_assets/linux-blink-banner.png)
6+
7+
## Description
8+
9+
This example toggles an LED on the board using a simple web user interface. The application listens for user input through a web browser and updates the LED state accordingly. It shows how to interact with hardware from a Linux environment and provides a basis for building more complex hardware-interfacing applications.
10+
11+
The `assets` folder contains the **frontend** components of the application. Inside, you'll find the JavaScript source files along with the HTML and CSS files that make up the web user interface. The `python` folder instead includes the application **backend**.
12+
13+
The interactive toggle switch UI is generated with JavaScript, while the Arduino sketch manages the LED hardware control. The Router Bridge enables communication between the web interface and the microcontroller.
14+
15+
## Bricks Used
16+
17+
The Linux blink example uses the following Bricks:
18+
19+
- `web_ui`: Brick to create a web interface to display the LED control toggle switch.
20+
21+
## Hardware and Software Requirements
22+
23+
### Hardware
24+
25+
- Arduino UNO Q (x1)
26+
- USB-C® cable (for power and programming) (x1)
27+
28+
### Software
29+
30+
- Arduino App Lab
31+
32+
**Note:** You can run this example using your Arduino UNO Q as a Single Board Computer (SBC) using a [USB-C® hub](https://store.arduino.cc/products/usb-c-to-hdmi-multiport-adapter-with-ethernet-and-usb-hub) with a mouse, keyboard and display attached.
33+
34+
## How to Use the Example
35+
36+
1. Run the App
37+
![Arduino App Lab - Run App](assets/docs_assets/app-lab-run-app.png)
38+
2. Open the App in your browser at `<UNO-Q-IP-ADDRESS>:7000`
39+
3. Click on the circular switch to change the state of the LED
40+
41+
## How it Works
42+
43+
Once the application is running, the device performs the following operations:
44+
45+
- **Serving the web interface and handling WebSocket communication.**
46+
47+
The `web_ui` Brick provides the web server and WebSocket communication:
48+
49+
```python
50+
from arduino.app_bricks.web_ui import WebUI
51+
52+
ui = WebUI()
53+
ui.on_message('toggle_led', toggle_led_state)
54+
ui.on_message('get_initial_state', on_get_initial_state)
55+
```
56+
57+
- **Communicating LED state to the Arduino.**
58+
59+
The Router Bridge sends LED commands to the microcontroller:
60+
61+
```python
62+
Bridge.call("set_led_state", led_is_on)
63+
```
64+
65+
- **Controlling the hardware LED.**
66+
67+
The Arduino sketch handles the LED hardware control:
68+
69+
```cpp
70+
void set_led_state(bool state) {
71+
digitalWrite(LED_BUILTIN, state ? LOW : HIGH);
72+
}
73+
```
74+
75+
The high-level data flow looks like this:
76+
77+
```
78+
Web Browser Toggle → WebSocket → Python Backend → Router Bridge → Arduino LED Control
79+
```
80+
81+
## Understanding the Code
82+
83+
Here is a brief explanation of the application components:
84+
85+
### 🔧 Backend (`main.py`)
86+
87+
The Python code manages the web interface, handles user interactions, and communicates with the Arduino.
88+
89+
- **`ui = WebUI()`:** Initializes the web server that serves the HTML interface and handles WebSocket communication.
90+
91+
- **`ui.on_message('toggle_led', toggle_led_state)`:** Registers a WebSocket message handler that responds when the user clicks the toggle button in the web interface.
92+
93+
- **`ui.send_message('led_status_update', get_led_status())`:** Sends LED status updates to all connected web clients in real-time.
94+
95+
- **`Bridge.call("set_led_state", led_is_on)`:** Calls the Arduino function to physically control the LED hardware.
96+
97+
- **`get_led_status()`:** Returns the current LED state as a dictionary for the web interface.
98+
99+
### 🔧 Frontend (`index.html` + `app.js`)
100+
101+
The web interface provides a simple toggle button for LED control.
102+
103+
- **Socket.IO connection:** Establishes WebSocket communication with the Python backend through the `web_ui` Brick.
104+
105+
- **`socket.emit('toggle_led', {})`:** Sends a toggle message to the backend when the user clicks the button.
106+
107+
- **`socket.on('led_status_update', updateLedStatus)`:** Receives LED status updates and updates the button appearance accordingly.
108+
109+
- **`updateLedStatus(status)`:** Changes the button's visual state (LED IS ON/OFF) based on the received status.
110+
111+
### 🔧 Hardware (`sketch.ino`)
112+
113+
The Arduino code handles LED hardware control and sets up Bridge communication.
114+
115+
- **`pinMode(LED_BUILTIN, OUTPUT)`:** Configures the built-in LED pin as an output for controlling the LED state.
116+
117+
- **`Bridge.begin()`:** Initializes the Router Bridge communication system for receiving commands from Python.
118+
119+
- **`Bridge.provide(...)`:** Registers the `set_led_state` function to be callable from the Python web interface.
120+
121+
- **`set_led_state(bool state)`:** Controls the LED hardware by setting the pin HIGH or LOW based on the received state parameter.
122+
123+
- **Empty `loop()`:** The main loop remains empty since all LED control is event-driven through Bridge function calls.
124+
125+
# scratch-arduino-app

0 commit comments

Comments
 (0)