Digi International Inc XB3C1 Manual Do Utilizador

Página de 146
Get started with MicroPython
Example: code a request help button
Digi XBee3 Cellular LTE Cat 1 Smart Modem User Guide
52
Send a text (SMS) when the button is pressed
After
that checks for a button press, add sending an SMS to your code. Instead of
printing Button pressed! to the screen, this code sends Button pressed to a cell phone as a text
(SMS) message.
To accomplish this, use the sms_send() method, which sends a string to a given phone number. It
takes the arguments in the order of
1. <phone number>
2. <message-to-be-sent>
Before you run this part of the example, you must create a variable that holds the phone number of
the cell phone or mobile device you want to receive the SMS.
1. To do this, at the MicroPython >>> prompt, type the following command, replacing 1123456789
with the full phone number (no dashes, spaces, or other symbols) and press Enter:
ph = 1123456789
2. After you create this ph variable with your phone number, copy the code below and enter it
into MicroPython using
paste mode
and then run it.
from machine import Pin
import network # Import network module
import time
c = network.Cellular() # initialize cellular network parameter
dio0 = Pin("D0", Pin.IN, Pin.PULL_UP)
while not c.isconnected():
# While no network connection.
print("Waiting for connection to cell network...")
time.sleep(5)
print("Connected.")
# Give feedback to inform user a button press is needed.
print("Waiting for SW2 press...")
while (True):
if (dio0.value() == 0):
# When SW2 is pressed, the module will send an SMS
# message saying "Button pressed" to the given target cell phone
number.
try:
c.sms_send(ph, 'Button Pressed')
print("Sent SMS successfully.")
except OSError:
print("ERROR- failed to send SMS.")
# Exit the WHILE loop.
break
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.
Note
If you have problems pasting the code, see
. For SMS failures, see