Digi International Inc XB3C1 Manuel D’Utilisation

Page de 146
Get started with MicroPython
Example: code a request help button
Digi XBee3 Cellular LTE Cat 1 Smart Modem User Guide
50
Many stores have help buttons in their aisles that a customer can press to alert the store staff that
assistance is required in that aisle. You can implement this type of system using the Digi XBee Smart
Modem, and this example provides the building blocks for such a system. This example, based on SMS
paging, can have many other uses such as alerting someone with a text to their phone if a water
sensor in a building detects water on the floor, or if a temperature sensor reports a value that is too
hot or cold relative to normal operation.
Enter MicroPython paste mode
In the following examples it is helpful to know that MicroPython supports
, where you can
copy a large block of code from this user guide and paste it instead of typing it character by character.
To use paste mode:
1. Copy the code you want to run. For example, copy the following code that is the code from the
LED example:
from machine import Pin
led = Pin("D4", Pin.OUT, value=0)
Note
You can easily copy and paste code from the online version of this Guide. Use caution with the
PDF version, as it may not maintain essential indentations.
2. In the terminal, at the MicroPython >>> prompt type Ctrl-+to enter paste mode. The terminal
displays paste mode; Ctrl-C to cancel, Ctrl-D to finish.
3. The code appears in the terminal occupying four lines, each line starts with its line number and
three symbols. For example line 1 starts with 1===.
4. If the code is correct, press Ctrl+to run the code and you should once again see the DS4 LED
turn on. If you get a Line 1 SyntaxError: invalid syntax error, see
.
(If you wish to exit paste mode without running the code, for example, or if the code did not
copy correctly, press Ctrl+to cancel and return to the normal MicroPython >>> prompt).
5. Next turn the LED off. Copy the code below:
from machine import Pin
led = Pin("D4", Pin.OUT, value=1)
print("DS4 LED now OFF!")
print("Paste Mode Successful!")
6. Press Ctrl+to enter paste mode.
7. Press Ctrl Shift or right-click in the Terminal and select Paste to paste the copied code.
8. If the code is correct, press Ctrl+to run it. The LED should turn off and you should see two
confirmation messages print to the screen.
Catch a button press
For this part of the example, you write code that responds to a button press on the XBIB-U-DEV board
that comes with the XBee Smart Modem Development Kit. The code monitors the pin connected to
the button on the board labeled SW2.