Skip to main content

PMS9103M Air Quality Monitor with Multi-Channel Notifications

Overview​

This example demonstrates how to parse data from a PMS9103M particulate matter sensor and send alerts via multiple notification channels when PM2.5 levels exceed safe thresholds. It showcases CycBox's capabilities for:

  • Frame-based protocol decoding using the built-in frame codec with prefix, length field, and checksum validation
  • Binary payload parsing — extracting multiple uint16 big-endian values from raw sensor data
  • Multi-channel notifications — sending alerts simultaneously via Discord webhook, ntfy push notification, and SMTP email
  • Hysteresis-based alerting — using separate high/low thresholds to avoid notification flapping

This is a common pattern in environmental monitoring where air quality data needs to trigger real-time alerts across multiple communication channels.

Scenario​

A PMS9103M particulate matter sensor is connected via serial port (e.g., /dev/ttyUSB0) and continuously streams data frames at 9600 baud. The requirements are:

  1. Receive and validate sensor data frames using prefix detection, length field parsing, and checksum verification
  2. Parse all PM concentration values (PM1.0, PM2.5, PM10) and particle counts from the binary payload
  3. Send alerts via Discord, ntfy, and email when PM2.5 exceeds 75 ug/m3
  4. Send recovery notifications when PM2.5 drops below 35 ug/m3
  5. Use hysteresis to prevent repeated notifications when values fluctuate near a threshold

PMS9103M Frame Format​

FieldSizeDescription
Prefix2 bytes0x42 0x4D ("BM")
Length2 bytesuint16 big-endian, length of payload + checksum
Payload26 bytesSensor data (13 x uint16 big-endian values)
Checksum2 bytesSum16 big-endian over prefix + length + payload

PMS9103M Payload Structure​

All values are uint16 big-endian:

Byte OffsetDescription
0-1PM1.0 concentration (CF=1) in ug/m3
2-3PM2.5 concentration (CF=1) in ug/m3
4-5PM10 concentration (CF=1) in ug/m3
6-7PM1.0 concentration (atmospheric) in ug/m3
8-9PM2.5 concentration (atmospheric) in ug/m3
10-11PM10 concentration (atmospheric) in ug/m3
12-13Particles >0.3um per 0.1L air
14-15Particles >0.5um per 0.1L air
16-17Particles >1.0um per 0.1L air
18-19Particles >2.5um per 0.1L air
20-21Particles >5.0um per 0.1L air
22-23Particles >10um per 0.1L air

CF=1 values are factory-calibrated standard particle concentrations. Atmospheric values are corrected for ambient conditions and are typically used for air quality assessment.

Configuration​

CycBox is configured with a single serial connection using the frame codec to handle the PMS9103M binary protocol. Only values that differ from the defaults are shown; to try it, load the full example file with Load on the configuration page.

[
{
"app": {
"app_transport": "serial_port_transport",
"app_codec": "frame_codec"
},
"serial_port_transport": {
"serial_port_transport_port": "/dev/ttyUSB0"
},
"frame_codec": {
"frame_codec_length_mode": "u16_be",
"frame_codec_length_meaning": "payload_checksum",
"frame_codec_checksum_algo": "sum16_be",
"frame_codec_checksum_scope": "prefix_header_length_payload"
}
}
]

Frame Codec Settings​

ParameterValueDescription
frame_codec_prefix42 4dPMS "BM" start bytes
frame_codec_length_modeu16_be2-byte big-endian length field
frame_codec_length_meaningpayload_checksumLength covers payload + checksum bytes
frame_codec_checksum_algosum16_beSum16 big-endian checksum
frame_codec_checksum_scopeprefix_header_length_payloadChecksum covers entire frame except checksum itself

The frame codec handles all framing automatically — prefix detection, length-based extraction, and checksum validation. The Lua script receives only the validated 26-byte payload. See the Frame codec reference for every setting.

Lua Script Logic​

The script parses sensor data in on_receive() and uses hysteresis-based threshold checking to trigger notifications.

Notification Configuration​

The constants at the top of the full script set the PM2.5 thresholds (alert above 75 μg/m³, recover below 35 μg/m³) and the notification targets. Replace the placeholders before running it:

  • DISCORD_WEBHOOK_URL and DISCORD_USERNAME for the Discord webhook
  • NTFY_TOPIC: use a unique topic name, since ntfy.sh topics are public
  • SMTP_CONFIG: server, port, TLS mode, credentials, sender and recipient

How It Works​

1. Payload Parsing, Charting and Alerting (on_receive)

function on_receive()
local payload = message.payload

-- PMS9103M payload should be 26 bytes
if #payload ~= 26 then
log("warn", string.format("PMS9103M payload should be 26 bytes, got %d", #payload))
return false
end

-- Parse PM concentrations (CF=1, standard particles) in μg/m³
local pm1_0_cf1 = read_u16_be(payload, 1)
local pm2_5_cf1 = read_u16_be(payload, 3)
local pm10_cf1 = read_u16_be(payload, 5)

-- Parse PM concentrations (atmospheric environment) in μg/m³
local pm1_0_atm = read_u16_be(payload, 7)
local pm2_5_atm = read_u16_be(payload, 9)
local pm10_atm = read_u16_be(payload, 11)

-- Parse particle counts (number of particles per 0.1L air)
local particles_0_3um = read_u16_be(payload, 13)
local particles_0_5um = read_u16_be(payload, 15)
local particles_1_0um = read_u16_be(payload, 17)
local particles_2_5um = read_u16_be(payload, 19)
local particles_5_0um = read_u16_be(payload, 21)
local particles_10um = read_u16_be(payload, 23)

-- Add PM values to chart
message:add_int_value("PM1.0-CF1", pm1_0_cf1)
message:add_int_value("PM1.0-ATM", pm1_0_atm)
message:add_int_value("PM2.5-CF1", pm2_5_cf1)
message:add_int_value("PM2.5-ATM", pm2_5_atm)
message:add_int_value("PM10-CF1", pm10_cf1)
message:add_int_value("PM10-ATM", pm10_atm)

-- Add particle counts to chart
message:add_int_value("Particles >0.3μm", particles_0_3um)
message:add_int_value("Particles >0.5μm", particles_0_5um)
message:add_int_value("Particles >1.0μm", particles_1_0um)
message:add_int_value("Particles >2.5μm", particles_2_5um)
message:add_int_value("Particles >5.0μm", particles_5_0um)
message:add_int_value("Particles >10μm", particles_10um)

-- Check PM2.5 thresholds and send notifications
if alert_state == "normal" and pm2_5_atm > PM25_HIGH_THRESHOLD then
alert_state = "alert"
send_high_alert(pm2_5_atm)
elseif alert_state == "alert" and pm2_5_atm < PM25_LOW_THRESHOLD then
alert_state = "normal"
send_recovery_alert(pm2_5_atm)
end
local values_json = message.values_json
log("info", values_json)

return true
end
  • Validates payload length (26 bytes expected after frame codec strips prefix, length, and checksum)
  • Extracts the 12 PM and particle-count uint16 big-endian values using read_u16_be(payload, offset)
  • CF=1 values are factory-calibrated; atmospheric values are environment-corrected
  • Adds every value to the message via message:add_int_value() for real-time UI charting

Values page with PMS9103M readings: PM mass concentrations and particle counts

Chart of PM2.5 and PM10 readings from the PMS9103M

The last block is a hysteresis check on the atmospheric PM2.5 value, with alert_state remembering whether an alert is active:

  • Uses two thresholds (high=75, low=35) to create a hysteresis band
  • Alert triggers only once when PM2.5 rises above 75 μg/m³
  • Recovery triggers only once when PM2.5 drops below 35 μg/m³
  • No repeated notifications while the value stays between the two thresholds

2. Multi-Channel Notifications

Each alert is sent simultaneously via three channels. send_recovery_alert has the same shape, with a recovery message, title and tags:

local function send_high_alert(pm2_5_value)
local msg = string.format(
"PM2.5 ALERT: %d μg/m³ (threshold: %d μg/m³). Air quality is unhealthy!",
pm2_5_value, PM25_HIGH_THRESHOLD
)

-- Discord async notification
discord_send_async(DISCORD_WEBHOOK_URL, msg, DISCORD_USERNAME, nil)

-- ntfy async notification
ntfy_send_async({
topic = NTFY_TOPIC,
message = msg,
title = "PM2.5 High Alert",
priority = "high",
tags = "warning,skull",
})

-- SMTP async notification
smtp_send_async({
server = SMTP_CONFIG.server,
port = SMTP_CONFIG.port,
tls = SMTP_CONFIG.tls,
username = SMTP_CONFIG.username,
password = SMTP_CONFIG.password,
from = SMTP_CONFIG.from,
to = SMTP_CONFIG.to,
subject = string.format("PM2.5 Alert: %d μg/m³", pm2_5_value),
text = msg,
})

log("warn", msg)
end
  • All three notifications are sent asynchronously (_async suffix) so they don't block sensor data processing
  • Discord uses a webhook URL with a custom username
  • ntfy supports priority levels and emoji tags
  • SMTP supports STARTTLS for secure email delivery

Alert Flow Diagram​

PM2.5 value
|
| ──── 75 μg/m³ ──── HIGH THRESHOLD ──── trigger alert ────→ "alert" state
| │
| (no notifications in this band) │
| │
| ──── 35 μg/m³ ──── LOW THRESHOLD ───── trigger recovery ──→ "normal" state
|

Notification Examples​

High alert message:

PM2.5 ALERT: 82 μg/m³ (threshold: 75 μg/m³). Air quality is unhealthy!

Recovery message:

PM2.5 RECOVERY: 28 μg/m³ (threshold: 35 μg/m³). Air quality restored to normal.

Full Script​

serial_frame_codec_discord_ntfy_smtp_notify_pms9103m.lua
-- PMS9103M Air Quality Monitor with Multi-Channel Notifications
-- Parses PMS9103M particulate matter sensor data and sends alerts via
-- Discord webhook, ntfy push notification, and SMTP email when PM2.5
-- levels exceed a high threshold or recover below a low threshold.
--
-- Frame format:
-- Prefix: 0x42 0x4D ("BM")
-- Length: 2 bytes (big-endian, length of payload + checksum)
-- Payload: 26 bytes of sensor data
-- Checksum: 2 bytes (Sum16 big-endian)
--
-- Payload structure (all u16 big-endian):
-- Bytes 0-1: PM1.0 concentration (CF=1) in μg/m³
-- Bytes 2-3: PM2.5 concentration (CF=1) in μg/m³
-- Bytes 4-5: PM10 concentration (CF=1) in μg/m³
-- Bytes 6-7: PM1.0 concentration (atmospheric) in μg/m³
-- Bytes 8-9: PM2.5 concentration (atmospheric) in μg/m³
-- Bytes 10-11: PM10 concentration (atmospheric) in μg/m³
-- Bytes 12-13: Particles >0.3μm per 0.1L air
-- Bytes 14-15: Particles >0.5μm per 0.1L air
-- Bytes 16-17: Particles >1.0μm per 0.1L air
-- Bytes 18-19: Particles >2.5μm per 0.1L air
-- Bytes 20-21: Particles >5.0μm per 0.1L air
-- Bytes 22-23: Particles >10μm per 0.1L air

-- ========== Configuration ==========

-- PM2.5 thresholds (μg/m³)
local PM25_HIGH_THRESHOLD = 75 -- Alert when PM2.5 exceeds this value
local PM25_LOW_THRESHOLD = 35 -- Recovery when PM2.5 drops below this value

-- Discord webhook configuration
local DISCORD_WEBHOOK_URL = "https://discord.com/api/webhooks/YOUR_WEBHOOK_ID/YOUR_WEBHOOK_TOKEN"
local DISCORD_USERNAME = "CycBox Air Monitor"

-- ntfy configuration
local NTFY_TOPIC = "cycbox_air_quality" -- Replace with your unique topic name

-- SMTP configuration
local SMTP_CONFIG = {
server = "smtp.gmail.com",
port = 587,
tls = "starttls",
username = "your-email@gmail.com",
password = "your-app-password",
from = "your-email@gmail.com",
to = "recipient@example.com",
}

-- ========== State ==========

-- Track alert state to avoid repeated notifications
-- "normal": PM2.5 is below low threshold
-- "alert": PM2.5 is above high threshold
local alert_state = "normal"

-- ========== Notification Helpers ==========

local function send_high_alert(pm2_5_value)
local msg = string.format(
"PM2.5 ALERT: %d μg/m³ (threshold: %d μg/m³). Air quality is unhealthy!",
pm2_5_value, PM25_HIGH_THRESHOLD
)

-- Discord async notification
discord_send_async(DISCORD_WEBHOOK_URL, msg, DISCORD_USERNAME, nil)

-- ntfy async notification
ntfy_send_async({
topic = NTFY_TOPIC,
message = msg,
title = "PM2.5 High Alert",
priority = "high",
tags = "warning,skull",
})

-- SMTP async notification
smtp_send_async({
server = SMTP_CONFIG.server,
port = SMTP_CONFIG.port,
tls = SMTP_CONFIG.tls,
username = SMTP_CONFIG.username,
password = SMTP_CONFIG.password,
from = SMTP_CONFIG.from,
to = SMTP_CONFIG.to,
subject = string.format("PM2.5 Alert: %d μg/m³", pm2_5_value),
text = msg,
})

log("warn", msg)
end

local function send_recovery_alert(pm2_5_value)
local msg = string.format(
"PM2.5 RECOVERY: %d μg/m³ (threshold: %d μg/m³). Air quality restored to normal.",
pm2_5_value, PM25_LOW_THRESHOLD
)

-- Discord async notification
discord_send_async(DISCORD_WEBHOOK_URL, msg, DISCORD_USERNAME, nil)

-- ntfy async notification
ntfy_send_async({
topic = NTFY_TOPIC,
message = msg,
title = "PM2.5 Restored to Normal",
priority = "default",
tags = "white_check_mark,herb",
})

-- SMTP async notification
smtp_send_async({
server = SMTP_CONFIG.server,
port = SMTP_CONFIG.port,
tls = SMTP_CONFIG.tls,
username = SMTP_CONFIG.username,
password = SMTP_CONFIG.password,
from = SMTP_CONFIG.from,
to = SMTP_CONFIG.to,
subject = string.format("PM2.5 Restored: %d μg/m³", pm2_5_value),
text = msg,
})

log("info", msg)
end

-- ========== Hook Functions ==========

function on_receive()
local payload = message.payload

-- PMS9103M payload should be 26 bytes
if #payload ~= 26 then
log("warn", string.format("PMS9103M payload should be 26 bytes, got %d", #payload))
return false
end

-- Parse PM concentrations (CF=1, standard particles) in μg/m³
local pm1_0_cf1 = read_u16_be(payload, 1)
local pm2_5_cf1 = read_u16_be(payload, 3)
local pm10_cf1 = read_u16_be(payload, 5)

-- Parse PM concentrations (atmospheric environment) in μg/m³
local pm1_0_atm = read_u16_be(payload, 7)
local pm2_5_atm = read_u16_be(payload, 9)
local pm10_atm = read_u16_be(payload, 11)

-- Parse particle counts (number of particles per 0.1L air)
local particles_0_3um = read_u16_be(payload, 13)
local particles_0_5um = read_u16_be(payload, 15)
local particles_1_0um = read_u16_be(payload, 17)
local particles_2_5um = read_u16_be(payload, 19)
local particles_5_0um = read_u16_be(payload, 21)
local particles_10um = read_u16_be(payload, 23)

-- Add PM values to chart
message:add_int_value("PM1.0-CF1", pm1_0_cf1)
message:add_int_value("PM1.0-ATM", pm1_0_atm)
message:add_int_value("PM2.5-CF1", pm2_5_cf1)
message:add_int_value("PM2.5-ATM", pm2_5_atm)
message:add_int_value("PM10-CF1", pm10_cf1)
message:add_int_value("PM10-ATM", pm10_atm)

-- Add particle counts to chart
message:add_int_value("Particles >0.3μm", particles_0_3um)
message:add_int_value("Particles >0.5μm", particles_0_5um)
message:add_int_value("Particles >1.0μm", particles_1_0um)
message:add_int_value("Particles >2.5μm", particles_2_5um)
message:add_int_value("Particles >5.0μm", particles_5_0um)
message:add_int_value("Particles >10μm", particles_10um)

-- Check PM2.5 thresholds and send notifications
if alert_state == "normal" and pm2_5_atm > PM25_HIGH_THRESHOLD then
alert_state = "alert"
send_high_alert(pm2_5_atm)
elseif alert_state == "alert" and pm2_5_atm < PM25_LOW_THRESHOLD then
alert_state = "normal"
send_recovery_alert(pm2_5_atm)
end
local values_json = message.values_json
log("info", values_json)

return true
end

Full example: cycbox/examples/serial_frame_codec_discord_ntfy_smtp_notify_pms9103m.lua