# alexsousadev/wifi-init architecture

> Architecture diagram of the GitHub repository alexsousadev/wifi-init, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexsousadev/wifi-init
- Repository: https://github.com/alexsousadev/wifi-init

## Overview

- Pico W Hardware (Embedded Client):
– Firmware: Written in C using the Pico SDK and compiled by CMake.
– Wi‑Fi & Communication Modules:
▪ The “wifi-connection” module (from utils/wifi-connection) initializes the onboard Wi‑Fi chip (CYW43) and connects to the selected wireless network.
▪ The “send-data-to-server” module (from utils/send-data-to-server) is responsible for constructing and sending HTTP requests over TCP.
– lwIP Stack: Provides the lightweight TCP/IP protocol support.
– Utility Code (e.g., wifi-test.c) that ties the initialization and communication together.

- Local Server (Backend Server):
– Node.js/Express Server: Located under the “server” directory, which is built using TypeScript (server.ts) and uses Express to create an endpoint (/receber) that receives JSON data.
– Server Dependencies: package.json and package-lock.json, which manage libraries like Express.

- Supporting Configuration and Build Files:
– CMakeLists.txt and pico_sdk_import.cmake: Define the build configuration for the Pico firmware.
– lwipopts.h: Contains custom network stack configuration used by the Pico application.
– .gitignore, .vscode: Provide additional development environment settings.

##### Relationships and Data Flow
- The Pico W connects to a specified local Wi‑Fi network using the initialization functions from the “wifi-connection” module.
- Once connected, the Pico W continuously sends a formatted HTTP POST request to the server using a TCP connection formed by the lwIP stack. This request is assembled by the “send-data-to-server” code (with its steps of creating a TCP PCB, converting the server IP, connecting, composing the HTTP header and JSON body, and calling tcp_write and tcp_output).
- The HTTP request contains the “dado” value (the counter) in JSON format, which the server (via the Express app on route “/receber”) receives and logs to the console.
- Error handling occurs in both the embedded firmware (e.g., connection failure, tcp_callback on sent_data) and on the server side.

##### Architectural Patterns and Technologies
- Separation of Concerns: The project neatly separates the embedded code (hardware-level, network library use, and C-based build configuration) from the server logic written in Node.js/Express.
- Client-Server Architecture: The Pico W serves as a client that periodically assembles and sends its data to a server endpoint.
- Modular Design: The “utils” folder indicates a modular approach where specific functionalities (Wi‑Fi connection versus data sending) are isolated.
- Use of Standard Libraries and SDKs: Pico SDK, lwIP library, and Node.js with Express represent common technologies in embedded and full-stack development.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Raspberry Pi Pico W"
        FW_BUILD["Firmware Build Config"]:::embedded
        FW_LOGIC["Firmware Logic"]:::embedded
        WIFI["Wi‑Fi Initialization\n(wifi-connection)"]:::embedded
        SEND["Data Sending Module\n(send-data-to-server)"]:::embedded
        LWIP["lwIP TCP/IP Stack"]:::embedded
    end

    subgraph "Wi‑Fi Network Layer"
        WIFI_NET["Wi‑Fi Network"]:::network
    end

    subgraph "Node.js/Express Server"
        SERVER["Express Server (/receber)"]:::server
        DEP["Server Dependencies"]:::server
    end

    %% Relationships within Pico W
    FW_BUILD -->|"initializes"| FW_LOGIC
    FW_LOGIC -->|"configures"| WIFI
    FW_LOGIC -->|"triggers"| SEND
    WIFI -->|"ensures connectivity"| SEND
    SEND -->|"uses"| LWIP

    %% From Pico to Server via Network
    LWIP -->|"TCP/IP HTTP POST"| WIFI_NET
    WIFI_NET -->|"HTTP POST Request"| SERVER

    %% Server dependencies support
    DEP ---|"dependency"| SERVER

    %% Click Events
    click FW_BUILD "https://github.com/alexsousadev/wifi-init/blob/main/CMakeLists.txt"
    click FW_LOGIC "https://github.com/alexsousadev/wifi-init/blob/main/wifi-test.c"
    click WIFI "https://github.com/alexsousadev/wifi-init/blob/main/utils/wifi-connection/wifi-connect.c"
    click SEND "https://github.com/alexsousadev/wifi-init/blob/main/utils/send-data-to-server/send-data.c"
    click LWIP "https://github.com/alexsousadev/wifi-init/blob/main/lwipopts.h"
    click SERVER "https://github.com/alexsousadev/wifi-init/blob/main/server/server.ts"
    click DEP "https://github.com/alexsousadev/wifi-init/blob/main/server/package.json"

    %% Styles
    classDef embedded fill:#cce5ff,stroke:#005cbf,stroke-width:2px;
    classDef server fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef network fill:#fff3cd,stroke:#856404,stroke-width:2px;
```
