|
| 1 | +LoRa |
| 2 | +==== |
| 3 | + |
| 4 | +.. include:: ../refs/hardware.lora.ref |
| 5 | + |
| 6 | +LoRa is used to control the built-in long-range wireless communication module inside the host device. Below is the detailed LoRa support for the host: |
| 7 | + |
| 8 | +.. table:: |
| 9 | + :widths: auto |
| 10 | + :align: center |
| 11 | + |
| 12 | + +-----------------+---------+ |
| 13 | + | Controllers | LoRa | |
| 14 | + +=================+=========+ |
| 15 | + | UnitC6L | |S| | |
| 16 | + +-----------------+---------+ |
| 17 | + |
| 18 | +.. |S| unicode:: U+2714 |
| 19 | + |
| 20 | +UiFlow2 Example |
| 21 | +--------------- |
| 22 | + |
| 23 | +Sender |
| 24 | +^^^^^^ |
| 25 | + |
| 26 | +Open the |unit_c6l_tx_example.m5f2| project in UiFlow2. |
| 27 | + |
| 28 | +This example sends data every second. |
| 29 | + |
| 30 | +UiFlow2 Code Block: |
| 31 | + |
| 32 | + |unit_c6l_tx_example.png| |
| 33 | + |
| 34 | +Example output: |
| 35 | + |
| 36 | + None |
| 37 | + |
| 38 | +Receiver |
| 39 | +^^^^^^^^ |
| 40 | + |
| 41 | +Open the |unit_c6l_rx_example.m5f2| project in UiFlow2. |
| 42 | + |
| 43 | +This example receives and displays data. |
| 44 | + |
| 45 | +UiFlow2 Code Block: |
| 46 | + |
| 47 | + |unit_c6l_rx_example.png| |
| 48 | + |
| 49 | +Example output: |
| 50 | + |
| 51 | + None |
| 52 | + |
| 53 | +MicroPython Example |
| 54 | +------------------- |
| 55 | + |
| 56 | +Sender |
| 57 | +^^^^^^ |
| 58 | + |
| 59 | +This example sends data every second. |
| 60 | + |
| 61 | +MicroPython Code Block: |
| 62 | + |
| 63 | + .. literalinclude:: ../../../examples/hardware/lora/unit_c6l_tx_example.py |
| 64 | + :language: python |
| 65 | + :linenos: |
| 66 | + |
| 67 | +Example output: |
| 68 | + |
| 69 | + None |
| 70 | + |
| 71 | +Receiver |
| 72 | +^^^^^^^^ |
| 73 | + |
| 74 | +This example receives and displays data. |
| 75 | + |
| 76 | +MicroPython Code Block: |
| 77 | + |
| 78 | + .. literalinclude:: ../../../examples/hardware/lora/unit_c6l_rx_example.py |
| 79 | + :language: python |
| 80 | + :linenos: |
| 81 | + |
| 82 | +Example output: |
| 83 | + |
| 84 | + None |
| 85 | + |
| 86 | +**API** |
| 87 | +------- |
| 88 | + |
| 89 | +class LoRa |
| 90 | +^^^^^^^^^^ |
| 91 | + |
| 92 | +.. class:: hardware.LoRa(freq_khz = 868000, \ |
| 93 | + bw = "250", \ |
| 94 | + sf = 8, \ |
| 95 | + coding_rate = 8, \ |
| 96 | + reamble_len = 12, \ |
| 97 | + syncword = 0x12, \ |
| 98 | + output_power = 10) |
| 99 | + |
| 100 | + Create an LoRa object. |
| 101 | + |
| 102 | + :param int freq_khz: LoRa RF frequency in KHz, with a range of 850000 KHz to 930000 KHz. |
| 103 | + :param str bw: Bandwidth, options include: |
| 104 | + |
| 105 | + - ``"7.8"``: 7.8 KHz |
| 106 | + - ``"10.4"``: 10.4 KHz |
| 107 | + - ``"15.6"``: 15.6 KHz |
| 108 | + - ``"20.8"``: 20.8 KHz |
| 109 | + - ``"31.25"``: 31.25 KHz |
| 110 | + - ``"41.7"``: 41.7 KHz |
| 111 | + - ``"62.5"``: 62.5 KHz |
| 112 | + - ``"125"``: 125 KHz |
| 113 | + - ``"250"``: 250 KHz |
| 114 | + - ``"500"``: 500 KHz |
| 115 | + :param int sf: Spreading factor, range from 7 to 12. Higher spreading factors allow reception of weaker signals but with slower data rates. |
| 116 | + :param int coding_rate: Forward Error Correction (FEC) coding rate expressed as 4/N, with a range from 5 to 8. |
| 117 | + :param int preamble_len: Length of the preamble sequence in symbols, range from 0 to 255. |
| 118 | + :param int syncword: Sync word to mark the start of the data frame, default is 0x12. |
| 119 | + :param int output_power: Output power in dBm, range from -9 to 22. |
| 120 | + |
| 121 | + UiFlow2 Code Block: |
| 122 | + |
| 123 | + |init.png| |
| 124 | + |
| 125 | + MicroPython Code Block: |
| 126 | + |
| 127 | + .. code-block:: python |
| 128 | +
|
| 129 | + from hardware import LoRa |
| 130 | +
|
| 131 | + lora_0 = LoRa(868000, '250', 8, 8, 12, 0x12, 10) |
| 132 | +
|
| 133 | + .. method:: set_freq(freq_khz) |
| 134 | + |
| 135 | + Set frequency in kHz. |
| 136 | + |
| 137 | + :param int freq_khz: Frequency in kHz (850000 ~ 930000), default is 868000. |
| 138 | + |
| 139 | + UiFlow2 Code Block: |
| 140 | + |
| 141 | + |set_freq.png| |
| 142 | + |
| 143 | + MicroPython Code Block: |
| 144 | + |
| 145 | + .. code-block:: python |
| 146 | +
|
| 147 | + lora_0.set_freq(freq_khz) |
| 148 | +
|
| 149 | + .. method:: set_sf(sf) |
| 150 | + |
| 151 | + Set spreading factor (SF). |
| 152 | + |
| 153 | + :param int sf: Spreading factor (7 ~ 12) |
| 154 | + |
| 155 | + UiFlow2 Code Block: |
| 156 | + |
| 157 | + |set_sf.png| |
| 158 | + |
| 159 | + MicroPython Code Block: |
| 160 | + |
| 161 | + .. code-block:: python |
| 162 | +
|
| 163 | + lora_0.set_sf(sf) |
| 164 | +
|
| 165 | + .. method:: set_bw(bw) |
| 166 | + |
| 167 | + Set bandwidth. |
| 168 | + |
| 169 | + :param str bw: Bandwidth in kHz as string. Must be one of: |
| 170 | + '7.8', '10.4', '15.6', '20.8', '31.25', '41.7', |
| 171 | + '62.5', '125', '250', '500'. |
| 172 | + |
| 173 | + UiFlow2 Code Block: |
| 174 | + |
| 175 | + |set_bw.png| |
| 176 | + |
| 177 | + MicroPython Code Block: |
| 178 | + |
| 179 | + .. code-block:: python |
| 180 | +
|
| 181 | + lora_0.set_bw(bw) |
| 182 | +
|
| 183 | + .. method:: set_coding_rate(coding_rate) |
| 184 | + |
| 185 | + Set coding rate. |
| 186 | + |
| 187 | + :param int coding_rate: Coding rate (5 ~ 8) |
| 188 | + |
| 189 | + UiFlow2 Code Block: |
| 190 | + |
| 191 | + |set_coding_rate.png| |
| 192 | + |
| 193 | + MicroPython Code Block: |
| 194 | + |
| 195 | + .. code-block:: python |
| 196 | +
|
| 197 | + lora_0.set_coding_rate(coding_rate) |
| 198 | +
|
| 199 | + .. method:: set_syncword(syncword) |
| 200 | + |
| 201 | + Set syncword. |
| 202 | + |
| 203 | + :param int syncword: Sync word (0 ~ 0xFF) |
| 204 | + |
| 205 | + UiFlow2 Code Block: |
| 206 | + |
| 207 | + |set_syncword.png| |
| 208 | + |
| 209 | + MicroPython Code Block: |
| 210 | + |
| 211 | + .. code-block:: python |
| 212 | +
|
| 213 | + lora_0.set_syncword(syncword) |
| 214 | +
|
| 215 | + .. method:: set_preamble_len(preamble_len) |
| 216 | + |
| 217 | + Set preamble length. |
| 218 | + |
| 219 | + :param int preamble_len: Preamble length, range: 0~255. |
| 220 | + |
| 221 | + UiFlow2 Code Block: |
| 222 | + |
| 223 | + |set_preamble_len.png| |
| 224 | + |
| 225 | + MicroPython Code Block: |
| 226 | + |
| 227 | + .. code-block:: python |
| 228 | +
|
| 229 | + lora_0.set_preamble_len(preamble_len) |
| 230 | +
|
| 231 | + .. method:: set_output_power(output_power) |
| 232 | + |
| 233 | + Set output power in dBm. |
| 234 | + |
| 235 | + :param int output_power: Output power in dBm (-9 ~ 22) |
| 236 | + |
| 237 | + UiFlow2 Code Block: |
| 238 | + |
| 239 | + |set_output_power.png| |
| 240 | + |
| 241 | + MicroPython Code Block: |
| 242 | + |
| 243 | + .. code-block:: python |
| 244 | +
|
| 245 | + lora_0.set_output_power(output_power) |
| 246 | +
|
| 247 | + .. method:: set_irq_callback(callback) |
| 248 | + |
| 249 | + Set the interrupt callback function to be executed on IRQ. |
| 250 | + |
| 251 | + :param callback: The callback function to be invoked when the interrupt is triggered. |
| 252 | + The callback should not take any arguments and should return nothing. |
| 253 | + |
| 254 | + Call `start_recv()` to begin receiving data. |
| 255 | + |
| 256 | + UiFlow2 Code Block: |
| 257 | + |
| 258 | + |set_irq_callback.png| |
| 259 | + |
| 260 | + MicroPython Code Block: |
| 261 | + |
| 262 | + .. code-block:: python |
| 263 | +
|
| 264 | + lora_0.set_irq_callback() |
| 265 | +
|
| 266 | + .. method:: start_recv() |
| 267 | + |
| 268 | + Start receive data. |
| 269 | + |
| 270 | + This method initiates the process to begin receiving data. |
| 271 | + |
| 272 | + UiFlow2 Code Block: |
| 273 | + |
| 274 | + |start_recv.png| |
| 275 | + |
| 276 | + MicroPython Code Block: |
| 277 | + |
| 278 | + .. code-block:: python |
| 279 | +
|
| 280 | + lora_0.start_recv() |
| 281 | +
|
| 282 | + .. method:: recv(self, timeout_ms, rx_length, rx_packet) |
| 283 | + |
| 284 | + Receive data. |
| 285 | + |
| 286 | + :param int timeout_ms: Timeout in milliseconds (optional). Default is None. |
| 287 | + :param int rx_length: Length of the data to be read. Default is 0xFF. |
| 288 | + :param RxPacket rx_packet: An instance of `RxPacket` (optional) to reuse. |
| 289 | + :returns: Received packet instance |
| 290 | + :rtype: RxPacket |
| 291 | + |
| 292 | + Attempt to receive a LoRa packet. Returns `None` if timeout occurs, or returns the received packet instance. |
| 293 | + |
| 294 | + UiFlow2 Code Block: |
| 295 | + |
| 296 | + |recv.png| |
| 297 | + |
| 298 | + MicroPython Code Block: |
| 299 | + |
| 300 | + .. code-block:: python |
| 301 | +
|
| 302 | + data = lora_0.recv() |
| 303 | +
|
| 304 | + .. method:: send(buf, tx_at_ms=None) |
| 305 | + |
| 306 | + Send data. |
| 307 | + |
| 308 | + :param str | list | tuple | int | bytearray packet: The data to be sent. |
| 309 | + :param int tx_at_ms: The timestamp in milliseconds when to send the data (optional). Default is None. |
| 310 | + :returns: Returns a timestamp (result of `time.ticks_ms()`) indicating when the data packet was sent. |
| 311 | + :rtype: int |
| 312 | + |
| 313 | + Send a data packet and return the timestamp after the packet is sent. |
| 314 | + |
| 315 | + UiFlow2 Code Block: |
| 316 | + |
| 317 | + |send.png| |
| 318 | + |
| 319 | + MicroPython Code Block: |
| 320 | + |
| 321 | + .. code-block:: python |
| 322 | +
|
| 323 | + lora_0.send() |
| 324 | +
|
| 325 | + .. method:: standby() |
| 326 | + |
| 327 | + Set module to standby mode. |
| 328 | + |
| 329 | + Puts the LoRa module into standby mode, consuming less power. |
| 330 | + |
| 331 | + UiFlow2 Code Block: |
| 332 | + |
| 333 | + |standby.png| |
| 334 | + |
| 335 | + MicroPython Code Block: |
| 336 | + |
| 337 | + .. code-block:: python |
| 338 | +
|
| 339 | + lora_0.standby() |
| 340 | +
|
| 341 | + .. method:: sleep() |
| 342 | + |
| 343 | + Put the module to sleep mode. |
| 344 | + |
| 345 | + Reduces the power consumption by putting the module into deep sleep mode. |
| 346 | + |
| 347 | + UiFlow2 Code Block: |
| 348 | + |
| 349 | + |sleep.png| |
| 350 | + |
| 351 | + MicroPython Code Block: |
| 352 | + |
| 353 | + .. code-block:: python |
| 354 | +
|
| 355 | + lora_0.sleep() |
| 356 | +
|
| 357 | + .. method:: irq_triggered() |
| 358 | + |
| 359 | + Check IRQ trigger. |
| 360 | + |
| 361 | + :returns: Returns `True` if an interrupt service routine (ISR) has been triggered since the last send or receive started. |
| 362 | + :rtype: bool |
| 363 | + |
| 364 | + UiFlow2 Code Block: |
| 365 | + |
| 366 | + |irq_triggered.png| |
| 367 | + |
| 368 | + MicroPython Code Block: |
| 369 | + |
| 370 | + .. code-block:: python |
| 371 | +
|
| 372 | + lora_0.irq_triggered() |
| 373 | +
|
| 374 | +Refer to :ref:`lora_rxpacket` for more details about RxPacket. |
0 commit comments