Send A Text (Sms) When The Button Is Pressed - Digi XB3-C-A2-UT series User Manual

Smart modem
Table of Contents

Advertisement

Get started with MicroPython

Send a text (SMS) when the button is pressed

After
creating a while loop
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
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():
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
Failed to send
SMS.
Digi XBee3 Cellular LTE-M Global Smart Modem User Guide
that checks for a button press, add sending an SMS to your code. Instead of
paste mode
and then run it.
# While no network connection.
Syntax error at line
Example: code a request help button
1. For SMS failures, see
Error
52

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the XB3-C-A2-UT series and is the answer not in the manual?

This manual is also suitable for:

Xbee3 cellular lte-m

Table of Contents