Safe Sprinkler
Master/slave irrigation system (ESP-NOW + LoRaWAN)
Loading...
Searching...
No Matches
lora_msg_protocol.h
Go to the documentation of this file.
1#ifndef LORA_MSG_PROTOCOL
2#define LORA_MSG_PROTOCOL
3
4#include "general/node_cfg.h"
5
6#include <stdint.h>
7
8// ============================================================================
9// LoRa Message Types
10// ============================================================================
11// Message type IDs - use uint8_t for compact serialization
12typedef enum
13{
14 LORA_REGISTRATION = 0x00, // Registration of a new slave
15 LORA_PERIODIC_INFO = 0x01, // Periodic status update (every 24h)
16 LORA_ALARM = 0x02 // Alarm/tampering detected
18
19// ============================================================================
20// Wire Format Documentation (for TTN decoder)
21// ============================================================================
22/*
23 * Wire format for LoRa messages. All values are little-endian.
24 *
25 * REGISTRATION MESSAGE (42 bytes total):
26 * Byte 0: msg_type = 0x00
27 * Byte 1: slave_id (0-15)
28 * Bytes 2-33: device name as null-terminated string (32 bytes max)
29 * Bytes 34-37: longitude as int32 (multiply by 1e6 for actual value)
30 * Bytes 38-41: latitude as int32 (multiply by 1e6 for actual value)
31 *
32 * PERIODIC MESSAGE (2 + N*3 bytes, where N is the number of slaves):
33 * Byte 0: msg_type = 0x01
34 * Byte 1: slave_count (how many slaves follow)
35 * Then for each slave (i = 0 to slave_count-1):
36 * Byte 2+i*3: slave_id
37 * Byte 3+i*3: battery percentage (0-100)
38 * Byte 4+i*3: moisture percentage (0-100)
39 * Max size is 50 bytes (for 16 slaves: 2 + 16*3 = 50)
40 *
41 * ALARM MESSAGE (2 bytes total):
42 * Byte 0: msg_type = 0x02
43 * Byte 1: slave_id of the device that triggered the alarm
44 */
45
46// ============================================================================
47// C Struct Definitions (for internal use, not wire format)
48// ============================================================================
49
50// Status data for a single slave in periodic messages
51typedef struct
52{
53 uint8_t slave_id;
55 uint8_t moisture;
57
58// Registration message (internal representation)
59typedef struct
60{
61 uint8_t msg_type; // LORA_REGISTRATION
62 uint8_t slave_id;
63 char name[32];
64 int32_t longitude; // Fixed-point, multiply by 1e6
65 int32_t latitude; // Fixed-point, multiply by 1e6
67
68// Periodic status message (internal representation)
69typedef struct
70{
71 uint8_t msg_type; // LORA_PERIODIC_INFO
72 uint8_t slave_count;
75
76// Alarm message (internal representation)
77typedef struct
78{
79 uint8_t msg_type; // LORA_ALARM
80 uint8_t slave_id;
82
83// ============================================================================
84// FIFO Packet for queuing LoRa messages
85// ============================================================================
86// This is written to lora_fifo by state machine / handlers
87// and read by send_lora() to send the appropriate message
88
89typedef struct
90{
91 uint8_t msg_type; // LORA_REGISTRATION, LORA_PERIODIC_INFO, or LORA_ALARM
92 uint8_t slave_id; // Relevant for registration and alarm (ignored for periodic)
94
95#endif // LORA_MSG_PROTOCOL
lora_msg_id_t
Definition lora_msg_protocol.h:13
@ LORA_ALARM
Definition lora_msg_protocol.h:16
@ LORA_REGISTRATION
Definition lora_msg_protocol.h:14
@ LORA_PERIODIC_INFO
Definition lora_msg_protocol.h:15
#define MAX_SLAVE_NUM
Definition node_cfg.h:10
Definition lora_msg_protocol.h:78
uint8_t msg_type
Definition lora_msg_protocol.h:79
uint8_t slave_id
Definition lora_msg_protocol.h:80
Definition lora_msg_protocol.h:90
uint8_t slave_id
Definition lora_msg_protocol.h:92
uint8_t msg_type
Definition lora_msg_protocol.h:91
Definition lora_msg_protocol.h:70
uint8_t msg_type
Definition lora_msg_protocol.h:71
uint8_t slave_count
Definition lora_msg_protocol.h:72
Definition lora_msg_protocol.h:60
uint8_t msg_type
Definition lora_msg_protocol.h:61
int32_t longitude
Definition lora_msg_protocol.h:64
uint8_t slave_id
Definition lora_msg_protocol.h:62
int32_t latitude
Definition lora_msg_protocol.h:65
Definition lora_msg_protocol.h:52
uint8_t moisture
Definition lora_msg_protocol.h:55
uint8_t slave_id
Definition lora_msg_protocol.h:53
uint8_t battery_percentage
Definition lora_msg_protocol.h:54