Skip to content

Commit 1742ac8

Browse files
committed
Add support PC host and add examples and shell scripts
1 parent d55432d commit 1742ac8

File tree

15 files changed

+1150
-69
lines changed

15 files changed

+1150
-69
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Suwatchai K. <suwatchai@outlook.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,38 @@
1515

1616
<p align="center">
1717
<b>The Arduino Serial bridge for TCP, UDP, and WebSocket Clients</b><br>
18-
Provides a simple way to use advanced network functionality over a serial link, enabling boards without native networking to communicate through a WiFi‑capable device.
18+
Enable advanced network functionality on non-networked boards by bridging them to a WiFi‑capable device (ESP32/ESP8266) or a PC via USB.
1919
</p>
2020

21-
<p align="center">It is designed for Arduino boards such as AVR, STM32, and Teensy that lack built‑in WiFi or Ethernet, offering a straightforward alternative to firmware‑based solutions. By bridging communication through modules like ESP32, ESP8266, Raspberry Pi Pico W, or MKR WiFi 1010, the library makes network access broadly available. With support for <b>SSL/TLS</b>, <b>WebSockets</b>, and <b>UDP</b>, SerialNetworkBridge enables secure communication without requiring firmware‑level certificate management.</p>
21+
<p align="center">It is designed for Arduino boards such as AVR, STM32, and Teensy that lack built‑in WiFi or Ethernet. By bridging communication through modules like ESP32, Raspberry Pi Pico W, or even a PC running a Python script, the library makes network access broadly available. With support for <b>SSL/TLS</b>, <b>WebSockets</b>, and <b>UDP</b>, SerialNetworkBridge enables secure communication without requiring firmware‑level certificate management.</p>
2222

23+
---
24+
25+
## 🏗 Architecture Options
26+
27+
You can deploy this library in two ways depending on your hardware:
28+
29+
### Option A: Microcontroller Bridge (e.g., ESP32)
30+
Use a WiFi-capable microcontroller as a dedicated network co-processor.
31+
<p align="center">
32+
<img src="https://raw.githubusercontent.com/mobizt/SerialNetworkBridge/refs/heads/main/assets/diagram.svg" alt="Microcontroller Bridge Architecture" width="800"/>
33+
</p>
34+
35+
### Option B: PC / USB Bridge (Python)
36+
Use your computer or Raspberry Pi as the gateway via the USB cable.
2337
<p align="center">
24-
<img src="https://raw.githubusercontent.com/mobizt/SerialNetworkBridge/refs/heads/main/assets/diagram.svg" alt="SerialNetworkBridge communication flow" width="800"/>
38+
<img src="https://raw.githubusercontent.com/mobizt/SerialNetworkBridge/refs/heads/main/assets/diagram_pc.svg" alt="PC USB Bridge Architecture" width="800"/>
2539
</p>
2640

2741
---
2842

2943
## ✨ Features
3044

3145
- **Multi-Protocol Support:** Bridge **TCP**, **UDP**, and **WebSocket** clients via serial.
32-
- **Hardware Agnostic:** Designed for any Arduino board with a HardwareSerial port.
46+
- **Universal Compatibility:** Works with any interface implementing the `Stream` class (`HardwareSerial`, `SoftwareSerial`, `USBSerial`, etc.).
47+
- **PC Host Mode:** Connect your Arduino directly to a **PC or Raspberry Pi** via USB to access the internet using the provided Python script.
3348
- **Secure:** Support for **SSL/TLS** (HTTPS/WSS) and **STARTTLS** upgrades handled by the host.
34-
- **Lightweight:** Header-only design for embedded use.
49+
- **Performance:** Supports `NeoHWSerial` for high-performance interrupt-driven communication on AVR boards (see `examples/Features/NeoHWSerial_Client`).
3550
- **Event-Driven:** WebSocket implementation supports async events and callbacks.
3651

3752
---
@@ -60,13 +75,11 @@ lib_deps =
6075
- Arduino AVR (Uno, Mega2560, Nano)
6176
- STM32 series
6277
- Teensy boards
63-
- Any board with `HardwareSerial`
78+
- Any board with a `Stream` interface (Hardware or Software Serial)
6479

6580
**Hosts (Network Bridges):**
66-
- ESP32 / ESP8266
67-
- Raspberry Pi Pico W
68-
- MKR WiFi 1010, MKR 1000 WiFi
69-
- Arduino UNO WiFi Rev2
81+
- **Microcontrollers:** ESP32, ESP8266, Raspberry Pi Pico W, MKR WiFi 1010, Arduino UNO WiFi Rev2.
82+
- **PC / Linux:** Windows, Linux, macOS, or Raspberry Pi (via USB & Python).
7083

7184
---
7285

@@ -89,20 +102,21 @@ Most Arduino AVR boards (Uno, Mega) operate at **5V**, while ESP32/ESP8266 modul
89102

90103
---
91104

92-
## 🚀 Usage
105+
## 🚀 Usage: The Client (Your Arduino)
93106

94-
See the [`examples`](/examples/) folder for full sketches:
107+
These sketches run on your non-networked board (e.g., Arduino Mega). They communicate with a "Host" (see the next section) to access the internet.
95108

96109
### 1. TCP Client Example (HTTP GET)
97110

98111
```cpp
112+
// NOTE: Comment out this line if connecting to PC Host via USB!
99113
#define ENABLE_SERIALTCP_DEBUG
100114
#include <SerialNetworkBridge.h>
101115

102116
SerialTCPClient client(Serial2, 0 /* slot */);
103117

104118
void setup() {
105-
Serial.begin(115200);
119+
Serial.begin(115200); // Debug
106120
Serial2.begin(115200); // Link to Host
107121

108122
if (client.connect("httpbin.org", 80)) {
@@ -151,16 +165,13 @@ void loop() {
151165
if (size > 0) {
152166
Serial.print("Packet received, size: ");
153167
Serial.println(size);
154-
udp.read(packet, 48); // Read response
155-
// Process NTP data...
168+
// Read response...
156169
}
157170
}
158171
```
159172

160173
### 3. WebSocket Client Example
161174

162-
Note: The corresponding Host setup for this example is located in [examples/Features/Websocket/Host](examples/Features/Websocket/Host).
163-
164175
```cpp
165176
#define ENABLE_SERIALTCP_DEBUG
166177
#include <SerialNetworkBridge.h>
@@ -170,11 +181,10 @@ SerialWebsocketClient ws(Serial2, 2 /* slot */);
170181
void onWsEvent(WSMessageType type, const uint8_t* payload, size_t len) {
171182
switch(type) {
172183
case WS_EVENT_CONNECTED:
173-
Serial.println("Connected!");
174184
ws.sendText("Hello World!");
175185
break;
176186
case WS_FRAME_TEXT:
177-
Serial.printf("Received: %.*s\n", len, payload);
187+
Serial.printf("RX: %.*s\n", len, payload);
178188
break;
179189
}
180190
}
@@ -184,73 +194,74 @@ void setup() {
184194
Serial2.begin(115200);
185195

186196
ws.onEvent(onWsEvent);
187-
// Connect SSL (WSS)
188197
ws.connect("socketsbay.com", 443, "/wss/v2/1/demo/", true);
189198
}
190199

191200
void loop() {
192-
ws.loop(); // Essential for event processing
201+
ws.loop(); // Essential
193202
}
194203
```
195204
196205
---
197206
198-
## 🧩 Host Setup (ESP32 Example)
207+
## 🧩 Host Setup (Choose One)
208+
209+
You need **one** of the following acting as the bridge.
199210
200-
The **Host** device acts as the bridge. It requires the `SerialNetworkHost` library and definitions for the native clients (TCP, UDP, WebSocket).
211+
### Option A: Microcontroller Host (ESP32/ESP8266)
212+
213+
Upload the **Host** sketch to an ESP32 or ESP8266. This device connects to WiFi and forwards traffic from the Serial port.
201214
202215
```cpp
203216
#include <WiFi.h>
204217
#include <WiFiClientSecure.h>
205218
#include <WiFiUdp.h>
206219
#include <WebSocketsClient.h> // Native WS Library
207-
208220
#define ENABLE_SERIALTCP_DEBUG
209221
#include <SerialNetworkBridge.h>
210222
211-
const char* ssid = "WIFI_SSID";
212-
const char* pass = "WIFI_PASS";
213-
214223
// Native Clients
215224
WiFiClientSecure ssl_client;
216225
WiFiUDP udp_client;
217226
WebSocketsClient ws_client;
218227
219-
// Bridge Manager
220-
SerialNetworkHost host(Serial2);
221-
222-
// Callback to translate Serial commands to Native WS calls
223-
bool onWsCommand(int slot, uint8_t cmd, const uint8_t *payload, size_t len) {
224-
if (slot != 2) return false; // We assigned Slot 2 to WS
225-
// ... Implementation details in examples/Features/Websocket/Host ...
226-
return true;
227-
}
228+
SerialNetworkHost host(Serial2); // Serial Port connected to Client
228229
229230
void setup() {
230231
Serial.begin(115200);
231232
Serial2.begin(115200, SERIAL_8N1, 16, 17);
232-
233-
WiFi.begin(ssid, pass);
234-
while (WiFi.status() != WL_CONNECTED) delay(500);
233+
WiFi.begin("SSID", "PASS");
235234
236235
ssl_client.setInsecure();
237236
238237
// Register Clients to Slots
239-
host.setTCPClient(&ssl_client, 0); // Slot 0: TCP
240-
host.setUDPClient(&udp_client, 1); // Slot 1: UDP
241-
host.setWebSocketClient(&ws_client, 2); // Slot 2: WS
238+
host.setTCPClient(&ssl_client, 0);
239+
host.setUDPClient(&udp_client, 1);
240+
host.setWebSocketClient(&ws_client, 2);
242241
243-
host.setWebSocketCallback(onWsCommand); // Register WS Handler
244-
245242
host.notifyBoot();
246243
}
247244
248245
void loop() {
249-
host.loop(); // Handle Serial Traffic
246+
host.loop(); // Handle Serial Traffic
250247
ws_client.loop(); // Handle WS Traffic
251248
}
252249
```
253250

251+
### Option B: PC / Raspberry Pi Host (Python)
252+
253+
You can use your computer as the bridge via the USB cable!
254+
255+
1. **Arduino Setup:** * Use `Serial` (USB) for the client connection.
256+
* **Disable Debugging:** Comment out `#define ENABLE_SERIALTCP_DEBUG`.
257+
2. **PC Setup:**
258+
* Install Python 3.7+.
259+
* Navigate to `examples/Features/PC_USB_Host`.
260+
* Run the installer script (`install_libs.bat` or `install_libs.sh`).
261+
* Run the bridge script (`run.bat` or `run.sh`).
262+
263+
*See [`examples/Features/PC_USB_Host`](examples/Features/PC_USB_Host) for detailed instructions.*
264+
254265
---
255266

256267
## 📚 API Highlights
@@ -292,12 +303,6 @@ See [LICENSE](LICENSE) for details.
292303

293304
---
294305

295-
## 🙌 Contributing
296-
297-
Pull requests are welcome! Please open an issue first to discuss proposed changes or enhancements.
298-
299-
---
300-
301306
## 📧 Author
302307

303308
Developed and maintained by **mobizt**.

assets/diagram_pc.svg

Lines changed: 48 additions & 0 deletions
Loading
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#ifndef INTERRUPT_STREAM_H
2+
#define INTERRUPT_STREAM_H
3+
4+
#include <Arduino.h>
5+
#include <NeoHWSerial.h>
6+
7+
// Define buffer size (Power of 2 is best for performance)
8+
#define ISR_BUF_SIZE 1024
9+
10+
class InterruptStream : public Stream {
11+
private:
12+
volatile uint8_t _buffer[ISR_BUF_SIZE];
13+
volatile uint16_t _head;
14+
volatile uint16_t _tail;
15+
NeoHWSerial *_txPort; // Pointer to the real hardware port for sending data OUT
16+
17+
public:
18+
// Constructor: Pass the NeoSerial port you want to write to (e.g., NeoSerial1)
19+
InterruptStream(NeoHWSerial &port) : _head(0), _tail(0), _txPort(&port) {}
20+
21+
// -----------------------------------------------------------------
22+
// ISR METHOD: Call this inside your NeoHWSerial attachInterrupt function
23+
// -----------------------------------------------------------------
24+
inline void push(uint8_t c) {
25+
uint16_t next = (_head + 1) % ISR_BUF_SIZE;
26+
27+
// Only write if buffer is not full
28+
if (next != _tail) {
29+
_buffer[_head] = c;
30+
_head = next;
31+
}
32+
// Else: Buffer overflow - byte dropped (increase ISR_BUF_SIZE if this happens)
33+
}
34+
35+
// -----------------------------------------------------------------
36+
// STREAM INTERFACE (Used by SerialNetworkBridge)
37+
// -----------------------------------------------------------------
38+
virtual int available() {
39+
return (ISR_BUF_SIZE + _head - _tail) % ISR_BUF_SIZE;
40+
}
41+
42+
virtual int read() {
43+
if (_head == _tail) return -1;
44+
uint8_t c = _buffer[_tail];
45+
_tail = (_tail + 1) % ISR_BUF_SIZE;
46+
return c;
47+
}
48+
49+
virtual int peek() {
50+
if (_head == _tail) return -1;
51+
return _buffer[_tail];
52+
}
53+
54+
// Pass output data directly to the hardware UART
55+
virtual size_t write(uint8_t c) {
56+
return _txPort->write(c);
57+
}
58+
59+
// Support for writing buffers
60+
virtual size_t write(const uint8_t *buffer, size_t size) {
61+
return _txPort->write(buffer, size);
62+
}
63+
64+
virtual void flush() {
65+
_txPort->flush();
66+
}
67+
};
68+
69+
#endif

0 commit comments

Comments
 (0)