Magic SMS Button

by drvax in Circuits > Gadgets

813 Views, 3 Favorites, 0 Comments

Magic SMS Button

Use Python to Build a Magic SMS Button

Devices such as "Ring" doorbells have become ubiquitous in today's world. Even more exciting than the availability of new appliances that connect our world is the availability of components and tools anyone can use to build connected devices themselves. In this tutorial, I will walk you through the assembly of a "Ring" style device that will send an SMS message when you press a button. This build is easily extended to support multiple buttons per device or send text messages to multiple people.

Without any programming, you can build your first connected device that supports a single message sent to a single number by following the steps below. This would be ideal as a way for a young child or an elderly parent to send a text to you, so you know they want you to call. Think of the possibilities.

Several steps are designed to introduce you to the tools you can use in the future to write custom programs for electronic devices and to familiarize you with basic debugging capabilities.

Do not let these steps scare you off. These new tools and devices are truly open to anyone interested in learning.

Suppose you prefer to watch instead of reading, head over to the DrVAX Youtube channel, where you can watch a video covering these instructions. Here is a link this the video:

DrVAX Instruction Video

So now, let's learn something together.

P.S. I want to give a shout-out to Adafruit for excellent example programs and Circuit Python, Twilio for their Micro Python example, and the Adafruit Discord community for assistance with the Circuit Python response library.

Supplies

Feather2S ESP32 Development Board from Adafruit

The following components are available from many online suppliers, including Amazon and Adafruit.

1 Half-Size Breadboard

4 Breadboard Jumper Wires

One USB-C to USB charging cable and charger (a phone charger will work)

One Button (normally open), it is easier if this will plug into the breadboard

The feathers2_SMS_button.py code at https://github.com/ishapiro/circuitpythonexamples

A Windows, macOS, or Linux Computer (even a Raspberry Pi will work)

Learn About the FeatherS2

IMG_5359.jpeg

The FeatherS2 is a 32 bit, 2" x 7/8" single-board computer with amazing capabilities. It has Bluetooth, Wifi, and Battery charging support built-in along with 16MB of flash memory. The FeatherS2 is provided without "headers" (connectors) for a breadboard to increase flexibility.

As our first step, we need to solder headers onto the board.

Solder on the Headers

IMG_5360.jpeg
IMG_5362.jpeg

The easiest way to solder on the headers is to press them into a breadboard and then place the FeatherS2 on top. This will hold them in place while you solder each of the pins. Remember when soldering headers onto boards to use as little heat as possible to melt the solder and to heat the pin and the pad and then touch the solder to the junction of the pin and the pad. There are many tutorials available on soldering, so I will not belabor the technique here.

The pictures above demonstrate this approach.

Install the Development Software We Need for the Project

Screen Shot 2021-04-06 at 8.50.01 AM.png

The key to this project's flexibility rests on the fact that we are using a Circuit Python-supported microprocessor board. Circuit Python is a derivative of Micro Python, an open-source project designed to enable people new to software to program the explosion of new pocket-sized electronics platforms. Traditional software for embedded systems, computers hidden in another product, is written in the 'C' programming language. For example, most standard Arduino simple board computers use the Ardunio Interactive Development System and the 'C' programming language. 'C' as a programming language is generally viewed as more complex for folks new to programming than Python.

Circuit Python is the Adafruit (a company that sells electronic components to "makers") fork of Micro Python. It is open source while with the support of a rapidly growing company behind it.

In this project, we are NOT going to need to write any programs. We will need to create a configuration file for our "Magic SMS Button" and debug the system if something goes wrong. We will use the Mu editor for both of these tasks.

Head over to the Code with Mu website, download the version for your operating system, and install it.

Code with Mu

Now click on the first button on the top left, the "mode" bottom, and select Circuit Python.

Once completed, exit the editor.

You may see an error message saying you have not Circuit Python device attach, which you can safely ignore.

Plug Your FeatherS2 Into a USB Port of Your Computer

Screen Shot 2021-04-05 at 6.16.12 PM.png
Screen Shot 2021-04-06 at 10.25.36 AM.png

The FeatherS2 comes with Circuit Python built-in. Circuit Python-compatible devices have a unique capability. You plug them into a computer, and they look like a USB drive. However, when you copy a Python program with the special name "code.py" to the device, it reboots and starts running the program.

So plug in the device and look for CIRCUITPY* to appear on your desktop or in your file finder. If it does not appear, make sure the USB-C cable is plugged all the way into the device. If you still do not see it, try clicking the RST button on the board for about 3 seconds. If this does not work, head over to the Adafruit troubleshooting site at:

Adafruit Circuit Python Troubleshooting

Load a Program From the FeatherS2 Unto MU

Screen Shot 2021-04-07 at 1.41.41 PM.png

Once you see the CIRCUITPY device on your desktop, rerun MU and select "Load" to see the device's files, select the file named code.py and open it.

Do not panic. You will not need to know what this code does to complete this project. I want to use this file to show you a bit about your device. * Different Circuit Python device will show up on your machine under different names. If you are using a board with a different Circuit Python version than my FeatherS2, look for a new USB device when your plugin the microprocessor board.

Learn About Debugging Software With MU

Screen Shot 2021-04-06 at 10.30.48 AM.png

Now we will use the Mu Editor to learn about debugging software by using the program that came with the FeatherS2. This will prepare us to install our own specialized software.

In the Mu Editor, click on "Serial," which will display a new window on the bottom of the screen.

Now, let's resave our program to a FeatherS2. Remember, when you save a program to a Circuit Python device named code.py, it runs it. You should see the results of running the program in the serial display area. The Multicolor LED should also be changing colors on your FeatherS2.

Starting and Starting Programs on the FeatherS2

Screen Shot 2021-04-06 at 10.31.43 AM.png

Now, let's stop the program.

This step is essential. Click into the serial window.

Type a ctrl-c, and you may see a ">>>" display if you do not worry. The next step will fix this. Type a ctrl-d. This command causes FeatherS2 to reboot and rerun the program. Your screen should look like the picture on the top right of this step. Anytime you want to stop a running program, type a ctrl-c. After saving a program to restart it type ctrl-d.

Copy the Adafruit Library to the Board

Screen Shot 2021-04-05 at 6.17.12 PM.png

The Python program we will be using utilizes several modules from the Adafruit CircuitPython Library. We could copy individual models from the library we need to the board, and this would be most efficient, however since we have plenty of space, we will copy the whole library.

The Adafruit CircuitPython library is located at:

CircuitPython Libraries

You will need to download the zip file, unzip it, and then inside the unzipped directory, you will find a folder labeled "lib." Copy this folder to the FeatherS2. If there is a lib folder already on the device, overwrite it.

Extra Credit: If you think you will need lots and lots of space for your programs and data, you can also selectively copy just the models used in your program into the "lib" folder. The "feathers2_SMS_button.py" program I supply with this tutorial uses the following libraries:

import ssl
import wifi
import socketpool
import adafruit_requests
import time
import board
import digitalio
import feathers2
import binascii

Create a Twilio Account

twilio account.jpg

The "Magic SMS Button" program uses the Twilio service to send out SMS (text) messages. A single button sending SMS messages to a single recipient is compatible with the "trial" subscription tier on the Twilio service, completely free. You will need a credit card. However, your card is not charged unless you upgrade your subscription.

Head over to Twilio and sign up for a trial account:

Twilio Website

Once you have set up an account and select a telephone number (you can only select one with a free account), click on the "home" icon that is labeled "Dashboard." You need to copy down three key items to use the SMS service. The "account sid," the "auth token," and the "phone numbers." Make sure you put these in a safe place, and we will use them in the next step.

IMPORTANT

If you are using a Twilio Trial Account, you will need to "verify" your destination telephone number before successfully using the SMS service. Here is a link to the Twilio page to verify a phone number:

Twilio Page for Verifying a Telephone Number

You will need to log into your account before you can verify your telephone number.

Wiring the Board

Screen Shot 2021-04-06 at 11.31.43 AM.png

Let's begin this step by talking about breadboards. Our electronic breadboard has four sections.

In the picture on the top, the sections labeled "power rail" are connected horizontally. These sections are used for your power and ground connections. The sections labeled "terminal strip" are connected vertically, all the holes on an orange line are connected. The gap between the two terminal strips makes it easy to plug components into the board with two sides separated. The orange strips of holes on the top are not connected to the orange strip of holes in the bottom section. We will put both our FeatherS2 and our switch across this gap connected to terminal strips on each side.

The green lines in the above diagram are jumper wires you need to connect. The grey box and circle are components. The small grey rectangle labeled 450 ohms is a resistor. This component is optional but good practice. It ensures the switch is in an off position when it is not pressed, but it may not be necessary with this board.

Now plug your FeatherS2 into your Electronic Breadboard, add the switch on the right side spanning the two-terminal strip sections and add jumpers and a 400 to 1000 OHM resistor as indicated. This circuit has a total of only 7 components. Think about that. You are building the notification equivalent of a Ring Doorbell with just seven components selling for about $50.

Double-check your work, and then continue to test the board in the next section.

Testing the Button

IMG_5341.jpeg

Your electronic breadboard should look similar to the picture at the top. Don't worry about the color of your resistor, as these will vary.

Now we need to test your wiring and the button before we attempt to send an SMS message. Begin by going to my Circuit Python Examples Github repository.

Circuit Python Examples from DrVAX

Click on the code button on the top right and download a ZIP file of the repository. Unzip the file to create a folder.

Now, after plugging in your FeatherS2 open MU. Using MU, load the program named "feathers2_button_test.py".

Click on the MU "Serial" button to get ready to run the test program.

This next step is a bit tricky.

Double click on the tab in MU that says feathers2_button_test.py. Double-clicking on a tab allows you to save as, renaming the file and saving it somewhere else. Navigate to your CIRCUITPY device (which is the FeatherS2) and save the file as code.py.

You should see a prompt in the serial display section that says, "Press the button."

Ok now. Please press the button to try the program and see how it works.

If the program does not start on its own.

If after you save a code.py program to a Circuit Python device, it does not start. Click into the serial console area and enter a ctrl-d. This will do a soft reboot of the device, and the program should begin.

Configure the SMS Program

secrets py screenshot.jpg

You will not have to write any code to set up your "Magic SMS Button. However, you will need to create a configuration file.

In the MU Editor, click on "New" to start a new file. Cut and paste the following into your new file replacing the dummy data with the correct information you copied from the Twilio site in an earlier step.

secrets = {
'ssid' : 'DrVax XXXXXXXXt', 'password' : 'KHKHKHKHK',
'TWILIO_ACCOUNT_SID' : 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'TWILIO_AUTH_TOKEN' : 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
'TWILIO_FROM_NUMBER' : '+1AAAEEENNNN',
'NOTIFICATION_NUMBER' : '+1AAAEEENNNN',
'message' : 'Your message here. Button pressed at ',
}

A couple of things to verify. Each line without a curly brace must end with a comma, including the last line. Single space the lines. The values are all case sensitive, including the labels at the beginning of the lines.

Save this file as secrets.py on the FeatherS2 (the CIRCUITPY device).

Running the Program the First Time

magic sms button output.jpg

Now using the MU Editor, open the program in the unzipped directory named "feathers2_SMS_button.py". Using the double-clicking trick on a tab, change the name of this file to code.py and save it to the FeatherS2.

If your serial console is open, you should see the following output displayed in the picture above.

  • First, the program displays your FeatherS2 hardware address
  • Then it lists the wifi networks it can see
  • It connects to the wifi network you specified in the secrets.py file
  • Then it displays your IP address and sets up the HTTP communications
  • Finally, it displays a message you to press a button

If you see any errors, make sure your secrets.py file is formatted correctly.

Now press the button and watch your cell phone for a text message.

Debugging Problems

feathers2 wifi error.jpg

Several things can potentially go wrong. In most cases, if something goes wrong, you should start by verifying the information in the secrets.py file. Often you will see an error message in the MU serial console that will give you a hint of what is wrong.

The picture at the top of this step shows what happens if your wifi password is wrong.

If your device does not connect to your network and verified the SID and password in the secrets file, try moving closer to the wifi router. The antenna in the FeatherS2 is quite small.

After sending the request to Twilio the program will display the reply received. For many potential errors in secrets.py, you will see an error message in the reply. When the SMS is sent properly, the program will print to the console a long message that looks as follows. Do not let this complex reply scare you. Just look for obvious error messages.

Please note I have changed all of the authorization info and phone numbers in this sample:

*************************** debug output *******************************

Button Pressed: 1

Fetching JSON from http://worldtimeapi.org/api/ip

----------------------------------------

200 {'timezone': 'America/Chicago', 'utc_datetime': '2021-04-06T19:24:56.268883+00:00', 'raw_offset': -21600, 'client_ip': '73.209.1.53', 'dst_from': '2021-03-14T08:00:00+00:00', 'unixtime': 1617737096, 'utc_offset': '-05:00', 'datetime': '2021-04-06T14:24:56.268883-05:00', 'week_number': 14, 'abbreviation': 'CDT', 'day_of_year': 96, 'day_of_week': 2, 'dst': True, 'dst_offset': 3600, 'dst_until': '2021-11-07T07:00:00+00:00'}

---------------------------------------- QUNiYjZkOWVhNGJiNhkhkkhkhhkhkhkhkhkkhkhk0NWRhZjU4Y2ExODJhNTNmZA==
---
{'Authorization': 'Basic QUNiYjZkOWVhNGJiNDRkNjjkjkjkkjkkjkjkjMzlmOGY0NWRhZjU4Y2ExODJhNTNmZA=='} SMS sent with status code 201

Response: {"sid": "SM959e9198fd044cbc9e96b339f9506d37", "date_created": "Tue, 06 Apr 2021 19:24:57 +0000", "date_updated": "Tue, 06 Apr 2021 19:24:57 +0000", "date_sent": null, "account_sid": "ACbb6d9xxxxxxxxxxxxxx", "to": "+1xxxxxxxx", "from": "+1xxxxxxxx", "messaging_service_sid": null, "body": "Sent from your Twilio trial account - Son, please call me. Button pressed at : 2021-04-06T14:24:56", "status": "queued", "num_segments": "1", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": "USD", "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/ACxxxxxxxxxxxxxxx/Messages/SM959e9198fd044cbc9e96b339f9506d37.json", "subresource_uris": {"media": "/2010-04-01/Accounts/ACxxxxxxxxxxxxx5e4913939/Messages/SM959e9198fd044cbc9e96b339f9506d37/Media.json"}}

*************************** end of debug output **************************

Important

If you change something in the configy.py file, you must save it back to the FeatherS2. If the FeatherS2 does not restart the program, click into the console and type a ctrl-c followed by a ctrl-d.

Finally, you cal look at twilio.com/console/sms/logs to verify that Twilio received your message request.

Enjoy

IMG_5341.jpeg

Once your "Magic SMS Button" is working properly you can disconnect it from the computer. The program is saved on the FeatherS2 so you can just plug it into a USB power supply, a small phone charge will work fine, and you should be ready to go.

In future tutorials, I will discuss extending the capabilities of this application and how to design and 3d print a beautiful case for the "Magic SMS Button"

Let's continue to learn together.

Irv

DrVAX YouTube Channel