/      日本語

Receiving Data from a TWELITE Child

Output acceleration data received from TWELITE CUE to the serial monitor
TWELITE SPOT enables the use of numerous small, low-power wireless tags by combining ESP32 with TWELITE.

What You Need

Setting Up the Environment

1. Install the Development Environment 🚛

If you have not set up the environment described in Basic Firmware Development Using ESP32, please do so first.

2. Configure the Board Settings ⚙️

Set the board type to ESP32 Dev Module and configure as shown below.

Settings After Configuration

Settings After Configuration

3. Install the Library 🚚

Install the MWings Library required to use TWELITE from ESP32.

Open Sketch -> Include Library -> Manage Libraries…

Location of Library Manager

Location of Library Manager

Type MWings in the search box and install MWings.

Library Manager

Library Manager

Prepare the TWELITE SPOT

1. Remove the Cover ⛏️

Remove the top cover of the TWELITE SPOT case.

2. Connect the Cables 🔌

Connect TWELITE R3 / R2 to the ESP32 7P interface labeled ESP32, then supply 5V power to the USB-C connector on the side.

Connection Example (ESP32)

Connection Example (ESP32)

Run the Sketch First

1. Open the Sample 📂

Launch Arduino IDE, then select File -> Examples -> MWings -> monitor_spot_app_cue.

monitor_spot_app_cue

2. Select the Serial Port ⚙️

From the Tools -> Port menu, select the port of the connected device (TWELITE R series).

Serial Port Selection

Serial Port Selection

3. Start the ESP32 👇

Start ESP32 in programming mode.

Press the reset switch EN(RST) and the ESP32 boot switch BOOT, then release in the order EN(RST) -> BOOT.

Button Locations

Button Locations

4. Write the Sketch ⚒️

Click the Upload button at the top of the Arduino IDE.

Upload Completion Screen

Upload Completion Screen

5. Open the Serial Monitor 🖥️

Open the Window

Click the Serial Monitor button at the top right of the Arduino IDE.

Serial Monitor Button at Top Right

Serial Monitor Button at Top Right

Configure

Set the serial monitor baud rate to 115200.

6. Restart ESP32 🚀

After writing is complete, press and release the ESP32 reset switch EN(RST) on the TWELITE SPOT to reset the ESP32.

Reset Switch Location

Reset Switch Location

7. Confirm Startup 💬

If the following string appears on the serial monitor, startup was successful.

Monitor example for TWELITE SPOT: App_CUE (CUE Mode)
Successful Startup Display

Successful Startup Display

8. Start TWELITE CUE ⚡

Insert a CR2032 coin battery into the TWELITE CUE. It will start operating immediately.

Inserting the Coin Battery

Inserting the Coin Battery

9. Confirm Reception 💬

If the following string appears on the serial monitor, data reception from TWELITE CUE was successful.

Packet Number:     #3
Source Logical ID: 0x1
LQI:               147
Supply Voltage:    3310 mV
Accel Event:       Dice (1)
Accel X Axis [0]:  72 mG
Accel Y Axis [0]:  -64 mG
Accel Z Axis [0]:  1000 mG
Magnet State:      Leaving or Not found
Successful Reception Display

Successful Reception Display

Sketch Explanation

Here is a brief explanation of the sample sketch (monitor_spot_app_cue.ino).

Including the Library

Line 4 includes the MWings library you installed earlier.

#include "MWings.h"

Defining Pin Numbers

Lines 6-8 define the pin numbers.

const int RST_PIN = 5;
const int PRG_PIN = 4;
const int LED_PIN = 18;
NameDescription
RST_PINPin number connected to the TWELITE RST pin
PRG_PINPin number connected to the TWELITE PRG pin
LED_PINPin number connected to the ESP32 LED on the board

Defining TWELITE Settings

Lines 10-11 define the settings applied to the TWELITE Parent installed in TWELITE SPOT.

const uint8_t TWE_CHANNEL = 18;
const uint32_t TWE_APP_ID = 0x67720102;
NameDescription
TWE_CHANNELTWELITE frequency channel
TWE_APP_IDTWELITE application ID

Serial Port Setup

Lines 19-21 initialize the serial ports and output the startup message to the serial monitor.

  Serial.begin(115200);
  Serial.println("Monitor example for TWELITE SPOT: App_CUE (CUE Mode)");
  Serial2.begin(115200);

Serial is used for communication with the Arduino IDE serial monitor. The baud rate is set to 115200 bps to match the serial monitor settings.

Serial2 is used for communication with the TWELITE Parent installed on TWELITE SPOT. This is also set to 115200 bps to match the Parent’s initial settings.

TWELITE Initialization

Lines 24-26 call Twelite.begin() to configure and start the TWELITE Parent on TWELITE SPOT.

    Twelite.begin(Serial2,
                      LED_PIN, RST_PIN, PRG_PIN,
                      TWE_CHANNEL, TWE_APP_ID);
ParameterTypeDescription
Serial2HardwareSerial&Serial port used for communication with TWELITE
LED_PINintPin number connected to the status LED
RST_PINintPin number connected to the TWELITE RST pin
PRG_PINintPin number connected to the TWELITE PRG pin
TWE_CHANNELuint8_tTWELITE frequency channel
TWE_APP_IDuint32_tTWELITE application ID

Registering Packet Reception Event

Lines 29-49 call Twelite.on() to register the processing to be done when data is received from the TWELITE CUE.

Here, the received packet content is output to the serial monitor.

    Twelite.on([](const ParsedAppCuePacket& packet) {
        Serial.println("");
        Serial.print("Packet Number:     #");
        Serial.println(packet.u16SequenceNumber, DEC);
        Serial.print("Source Logical ID: 0x");
        Serial.println(packet.u8SourceLogicalId, HEX);
        Serial.print("LQI:               ");
        Serial.println(packet.u8Lqi, DEC);
        Serial.print("Supply Voltage:    ");
        Serial.print(packet.u16SupplyVoltage, DEC); Serial.println(" mV");
        Serial.print("Accel Event:       ");
        printAccelEvent(packet.u8AccelEvent);
        Serial.print("Accel X Axis [0]:  ");
        Serial.print(packet.i16SamplesX[0], DEC); Serial.println(" mG");
        Serial.print("Accel Y Axis [0]:  ");
        Serial.print(packet.i16SamplesY[0], DEC); Serial.println(" mG");
        Serial.print("Accel Z Axis [0]:  ");
        Serial.print(packet.i16SamplesZ[0], DEC); Serial.println(" mG");
        Serial.print("Magnet State:      ");
        printMagnetState(packet.u8MagnetState, packet.bMagnetStateChanged);
    });

This event is called only when a packet is received from the TWELITE CUE.

The received packet content is stored in the argument packet of type ParsedAppCuePacket.

packet contains the following data (the bold data are used in the code above).

DataTypeDescription
packet.u32SourceSerialIduint32_tSource Serial ID
packet.u8SourceLogicalIduint8_tSource Logical Device ID
packet.u16SequenceNumberuint16_tSequence Number
packet.u8Lqiuint8_tLQI (Radio Signal Quality Indicator)
packet.u16SupplyVoltageuint16_tSupply Voltage (mV)
packet.i16SamplesXint16_t[10]X-axis acceleration samples (mG)
packet.i16SamplesYint16_t[10]Y-axis acceleration samples (mG)
packet.i16SamplesZint16_t[10]Z-axis acceleration samples (mG)
packet.u8SampleCountuint8_tNumber of samples
packet.bHasAccelEventbooltrue if acceleration event is present
packet.u8AccelEventuint8_tAcceleration Event ID
packet.u8MagnetStateuint8_tMagnetic Event ID
packet.bMagnetStateChangedbooltrue if magnetic sensor state changed

Updating TWELITE Data

Line 55 calls Twelite.update().

    Twelite.update();

Twelite.update() reads packet data (ModBus ASCII format) from the TWELITE Parent one byte at a time.