Introduction: Send an SMS Using an ESP8266
This instructable takes you through the steps to send an SMS messages over the internet from an ESP8266 NodeMCU module board, to a mobile phone. To be able to send the message you will need to go through the steps to get a virtual phone number from Twilio, a communications company. No credit card needed because Twilio has free Trial account option. Then, you will download and use a sample Arduino IDE program to send SMS messages.
The sample program in this instructable has function calls that are straight forward to copy, paste, edit, and use in your own projects.
I'm using a NodeMCU ESP8266 CH340G ESP-12E Wireless WIFI Internet Development Board. They sell for about $3 on eBay from Chinese suppliers.
Step 1: Test Your ESP8266 NodeMCU
You can test with or without a breadboard. Without the breadboard the onboard light will blink on and off which is effective for the test.
In my sample, I plug the NodeMCU into the Breadboard. I plug the negative side of an LED into the ground (G or GND) pin on the NodeMCU. The positive side of the LED is connected through a resister (500 to 5K ohm) to the NodeMCU pin D4. When the sample program is run, the on board LED will turn on, the external LED off, then the on board LED will turn off, the external LED on. The LED lights will alternate on and off.
Download and run the basic Arduino test program: nodeMcuTest.ino. While running the program, the onboard LED light will turn on for 1 second, turn off for 1 second, and continuously cycle. Also, messages are posted which can be viewed in the Arduino IDE Tools/Serial Monitor.
+++ Setup. + Initialized the on board LED digital pin for output. LED is off. ++ Go to loop. + Loop counter = 1 + Loop counter = 2 + Loop counter = 3 ...
Once you have confirmed that your IDE can be used to program your NodeMCU, move to the next step.
Note regarding libraries, I have installed to following for ESP8266 projects:
- Arduino WiFi library version 1.2.7.
- PubSubClient version 2.7.0, by Nick O'Leary, for MQTT messaging. Not required for this project.
- IRremoteESP8266 version 2.6.3, for infrared. Not required for this project.
If you have more information on required libraries, please make a comment. The reason I don't have specifics is that I didn't record what I installed, sorry.About the ESP8266 NodeMCU
- 80 MHz clock speed
- Operating voltage: 3.3V
- Storage flash memory: 4 MB, SRAM: 64 KB
- 9 common use digital GPIO pins labeled: D0 to D8.
- Of the 9, 4 pins can be used for SPI, and 2 pins for I2C.
- Interrupt GPIO pins D0-D8.
- Do not use the 6 pins: CLK, SD0, CMD, SD1, SD2, SD3 (GPIO 6-11), because they are in use.
- Tested: button input using D0-D02.
- Tested: blink LED using D0-D08. Out to a resister, to an LED, to ground.
- Need to test, UART1 (TX = GPIO2), Serial1 object: D4 or D7 and D8.
Label GPIO pin#
D0 16 GPIO read/write, only. May not have interrupt feature.
D1 5 Digital GPIO.
D2 4 Digital GPIO.
D3 0 Digital GPIO.
D2 4 I2C:SCL, clock DS3231, PCF8574 input modules
D3 0 I2C:SDA
D4(TX) 2 Built in, on board LED.
3V 3v output
D5 14 Digital GPIO.
D6 12 Digital GPIO.
D7(RX) 13 Works for input, for example, infrared receive.
D8(TX) 15 Doesn't work for input, for example, infrared receive.
D5 14 SD card: SPI SCK
D6 12 SD card: SPI MISO
D7(RX) 13 SD card: SPI MOSI
D8 15 SD card: CS for SPI enable/disable a device. Can use other digital pins.
RX 03 System uplod from the IDE, which cause reboot after upload.
TX 01 System uplod.
3V 3v output
Step 2: Open a Free Twilio Trial Account and Get Your Own Phone Number
Twilio is an internet communications platform company. The products being used in this instructable are Twilio Programmable Messaging and phone numbers. Twilio has a inventory of virtual phone numbers that can be used to send and receive SMS messages to any mobile phone.
In this step, you will:
- Open a Twilio account, if you don't already have one. A trial account is free and does not require a credit card. Your trial account will have a trial balance that is used to pay for phone numbers and exchanging text messages with mobile phones.
- Buy a Twilio phone number. It's free because it's paid for using your trial balance.
- Send a message from your mobile phone to your new phone number.
- Once Twilio receives your message, an automated response message is sent to your mobile phone.
- Use the Twilio Console website application to view your message logs.
Link to the Twilio Console website to open an account.
Link to the Twilio Console website to buy a Twilio phone number.
Your new Twilio SMS capable phone number has an automatic response already configured. As a test, send an SMS message from your mobile phone to your Twilio phone number. You will receive the following message:
Thanks for your message. Configure your number's SMS URL to change this message. Reply Help for Help. Reply Stop to unsubscribe.
Now use the Twilio Console to view your message logs from the above test:
More About Using Twilio
You can create your own custom auto response. Link to a Studio how-to, how to make a custom SMS reply message. Studio is our drag and drop Twilio Console tool.
You can use the Twilio Console to view your log messages and download the logs as CVS and loading them into a spreadsheet. You can select a date and time range, to and from phone numbers, and
status such as: Undelivered or Sent. You also have the option to download the logs as CVS files. Since it has a limit of 300 logs at a time, you can limit the select by date.
Link to documentation and sample programs to send messages.
Link to program samples to list message logs.
Link to program message properties. The list of properties that are sent in the HTTP request.
Link to list SMS logs for a period of time.
Step 3: Send an SMS From Your NodeMCU
Download the program: HttpTwPost.ino, and load it into your Arduino IDE. Into the program enter your WiFi network ID and password. Enter your own Twilio account SID, auth token, and phone number. The Twilio account SID and auth token are viewable from the Twilio Console dashboard. Click Auth Token "show", to view the auth token.
// Your network SSID and password
const char* ssid = "YourNetworkId"; const char* password = "YourNetworkPassword"; const char* account_sid = "YourTwilioAccountSID"; const char* auth_token = "YourAuthToken"; String from_number = "+16505551111"; // After encoding is added, add "+". String to_number = "+16505552222"; String message_body = "Hello from the NodeMCU.";
Twilio note, when using phone numbers with Twilio, it's best to format numbers as E.164 formatted phone numbers. E.164 formatted phone numbers start with "+" and the country code. The phone number does not have spaces, hyphens, or brackets. Example: +16505551111.
Run the program. When the program starts, it will send an SMS text message to your mobile phone number. Also, messages are posted which can be viewed in the Arduino IDE Tools/Serial Monitor. The monitor messages include the response from Twilio.
+ Connect to WiFi. .... + Connected to WiFi, IP address: 192.168.1.76 + Using fingerprint 'BC B0 1A 32 80 5D E6 E4 A2 29 66 2B 08 C8 E0 4C 45 29 3F D0' + Connecting to api.twilio.com + Connected. + Post an HTTP send SMS request. + Connection is closed. + Response: HTTP/1.1 201 CREATED Date: Thu, 16 Jul 2020 20:39:49 GMT Content-Type: application/xml Content-Length: 878 Connection: close Twilio-Concurrent-Requests: 1 Twilio-Request-Id: RQe4fbdd142fca4b2fab24697e74006837 Twilio-Request-Duration: 0.116 Access-Control-Allow-Origin: * Access-Control-Allow-Headers: Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since Access-Control-Allow-Methods: GET, POST, DELETE, OPTIONS Access-Control-Expose-Headers: ETag Access-Control-Allow-Credentials: true X-Powered-By: AT-5000 X-Shenanigans: none X-Home-Region: us1 X-API-Domain: api.twilio.com Strict-Transport-Security: max-age=31536000 <?xml version='1.0' encoding='UTF-8'?> <TwilioResponse> ... </TwilioResponse> + Starting the loop.
You can view the Twilio message logs, to view your sent message.
Step 4: Button to Send an SMS
The above video shows an ESP8266 NodeMCU use to send messages to a mobile phone. In the video, the NodeMCU is already started and connected the WiFi network.
When the button is pressed, the onboard LED light goes on. The SMS message request is sent to the Twilio Messaging service. The service sends an SMS to my mobile phone. The message is received. The service also replies to the NodeMCU that the message was queued to be sent. By this time, the message had already been received on the phone. After the NodeMCU receives the response from Twilio, the onboard light goes out. The circuit is ready to send another message.
Following is how to implement the circuit in the video. This step requires a breadboard, button and wires. Add a button on the breadboard. One side of the button connects to the NodeMCU D1 pin. Wire the other side of the button to the NodeMCU ground pin (pin G on my board).
Download the program: HttpTwSendSms.ino and load it into your Arduino IDE. Same as in the previous step, into the program enter your WiFi network ID and password. Enter your own Twilio account SID, auth token, and phone number.
When the program is run the NodeMCU will connect to the WiFi network. When the button is pressed, a message is sent the mobile phone number.
Step 5: SMS From a NodeMCU Is Great IoT Tool
Now you can use sample code and your Twilio account to send SMS messages over WiFi.