Skip to content

Commit e76b04f

Browse files
committed
updated
1 parent 046d635 commit e76b04f

2 files changed

Lines changed: 46 additions & 39 deletions

File tree

examples/Basic_Duplex/README.md

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
1-
# Basic Duplex: MH-Tiny88 ↔ ESP-M3
1+
# Basic Duplex: MH-Tiny88 ↔ ESP-M3 (Polling Style)
22

3-
This example demonstrates a *full-duplex, bidirectional* link between an *MH-Tiny88* (acting as a sensor node) and an *ESP-M3* (acting as a Wi-Fi bridge). It utilizes *COBS framing* and *Fletcher-16 checksums* to ensure data integrity over a noisy serial line.
3+
This example demonstrates a *full-duplex, bidirectional* link between an *MH-Tiny88* (Sensor Node) and an *ESP-M3* (Wi-Fi Bridge). It uses the standard *Polling* pattern to check for new data, making it easy to integrate into existing Arduino sketches.
44

5-
## 🛠 Hardware Setup
5+
## 🌟 Features
66

7-
Since the MH-Tiny88 operates at *5V* and the ESP-M3 operates at *3.3V*, you must use a voltage divider or level shifter on the Tiny88's TX line to avoid damaging the ESP.
7+
- *Polling Logic*: Uses `link.available()` and `link.peek()` for manual data handling.
8+
- *Bi-Directional*: Both devices act as peers, sending and receiving data simultaneously.
9+
- *Reliable*: Protected by *COBS* framing and *Fletcher-16 checksums*.
810

9-
### Wiring Diagram
11+
## 🛠 Hardware Setup
1012

11-
MH-Tiny88 (5V) Connection ESP-M3 (3.3V)
12-
TX (D1) ───[ 1kΩ ]──┬── RX
13-
└─[ 2kΩ ]─┤ GND
14-
RX (D0) ────────────── TX
15-
GND ────────────── GND (Common)
13+
The MH-Tiny88 (*5V*) and ESP-M3 (*3.3V*) require a voltage divider on the Tiny88's TX line to protect the ESP.
1614

17-
> Note: The 2kΩ resistor goes to Ground, creating a 3.3V signal from the 5V TX.
15+
*MH-Tiny88 (5V)* | *Connection* | *ESP-M3 (3.3V)*
16+
TX (D1) | ───[ 1kΩ ]──┬── | RX
17+
| └─[ 2kΩ ]─┤ | GND
18+
RX (D0) | ────────────── | TX
19+
GND | ────────────── | GND (Common)
1820

19-
## 📂 Example Structure
21+
## 📂 Code Overview
22+
This example uses a manual check inside the `loop()` function:
2023

21-
*SharedData.h*: Defines the struct MyData used by both devices. *Must be identical on both sides*.
22-
*Tiny88_Sensor.ino*: Collects "simulated" sensor data and sends it every 2 seconds. Uses a *callback* to handle incoming commands from the ESP.
23-
*ESPM3_Bridge.ino*: Receives sensor data and immediately sends a command response back to the Tiny88.
24+
```cpp
25+
void loop() {
26+
link.update(); // Keep the engine running
2427

25-
## 🚀 How to Run
28+
if (link.available()) {
29+
const MyData& incoming = link.peek();
30+
// Handle your data here...
31+
32+
link.flush(); // Clear the flag to allow the next packet
33+
}
34+
}
35+
```
2636

27-
Open Tiny88_Sensor.ino in the Arduino IDE and upload it to your *MH-Tiny88*.
28-
Open ESPM3_Bridge.ino and upload it to your *ESP-M3*.
29-
Open the Serial Monitor for the *ESP-M3* at *9600 Baud*.
30-
You should see the incoming packets from the Tiny88 being displayed.
37+
- *`link.available()`*: Returns `true` only when a full, verified packet has arrived.
38+
- *`link.peek()`*: Provides a constant reference to the data without copying it, saving RAM.
39+
- *`link.flush()`*: Tells the library you have finished reading the data, allowing the hardware buffer to be parsed for the next packet.
3140

32-
## 📈 Features Demonstrated
41+
## 🚀 How to Run
3342

34-
*Asynchronous Callbacks*: Using link.onReceive() for non-blocking command handling.
35-
*Telemetry*: Monitoring link.getStats() for CRC errors or timeouts.
36-
*Safety*: Automatic buffer protection via COBS delimiting.
43+
Upload `Tiny88_Sensor.ino` to the *MH-Tiny88*.
44+
Upload `ESPM3_Bridge.ino` to the *ESP-M3*.
45+
Observe the interaction via the Serial Monitor.

examples/Basic_Duplex/Tiny88_Sensor.ino

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
#include <../../src/TinyLink.h>
1+
#include <TinyLink.h>
22
#include <../../src/adapters/TinyArduinoAdapter.h>
33
#include "SharedData.h"
44

5-
// Initialize Adapter on Hardware Serial
65
TinyArduinoAdapter adapter(Serial);
76
TinyLink<MyData, TinyArduinoAdapter> link(adapter);
87

9-
void onCommandReceived(const MyData& data) {
10-
// If ESP sends a command back
11-
if (data.commandId == 1) {
12-
digitalWrite(LED_BUILTIN, HIGH);
13-
} else {
14-
digitalWrite(LED_BUILTIN, LOW);
15-
}
16-
}
17-
188
void setup() {
199
Serial.begin(9600);
20-
pinMode(LED_BUILTIN, OUTPUT);
21-
22-
// Register asynchronous callback
23-
link.onReceive(onCommandReceived);
2410
}
2511

2612
void loop() {
2713
link.update();
2814

15+
// Polling for new data
16+
if (link.available()) {
17+
const MyData& incoming = link.peek();
18+
19+
if (incoming.commandId == 1) {
20+
digitalWrite(LED_BUILTIN, HIGH);
21+
}
22+
23+
link.flush(); // Clear flag for next packet
24+
}
25+
26+
// Send sensor data every 2s
2927
static uint32_t lastSend = 0;
3028
if (millis() - lastSend > 2000) {
3129
MyData status = { millis(), 24.5f, 0 };

0 commit comments

Comments
 (0)