This tutorial is to make the reader understanding the concepts behind sending SMS through the internet. For this we are going to use ESP8266 WiFi module which will detect the button press and send a message to the predefined number, this technique can be used in alarm systems, security alerts, SMS based embedded applications, etc. Programming part is done through LUA scripting language.
You can go through “Getting started with ESP8266” if you are new to this. Before continuing with this project you have to flash ESPs with NodeMCU firmware and go through Getting Started with the ESPlorer IDE.
Circuit Connection:
Here we are using USB to TTL converter for connecting the Module with the PC. The 3.3V output of the converter is used to power the module.
The connection details are as below:
ESP8266 |
USB-TTL
|
Vcc |
3.3v |
Gnd |
Gnd |
TX |
RX |
RX |
TX |
CH_PD |
Connected to 3.3v to enable chip firmware boot |
Don’t forget to pull up CH_PD HIGH, you won’t get a response from the module if it is not done.
You can also use Arduino for the connection. For more details refer here.
The connection details are as below:
ESP8266 |
Arduino UNO
|
Vcc |
3.3v |
Gnd |
Gnd |
TX |
TX |
RX |
RX |
CH_PD |
Connected to 3.3v to enable chip firmware boot |
Connect a Push button to GPIO 2 which is pulled down to the ground.
Disconnect the GPIO 2 from the button while powering up the module to avoid unnecessary Reset. s
Downloading ESPlorer:
It’s recommended to use the ESPlorer program to open and save Lua files into your ESP8266.
Follow these instructions:
2. Unzip that folder.
3. Run ESPlorer.jar. It’s a JAVA program, make sure that JAVA is installed on your computer.
4. Open the ESPlorer.
Steps involved in implementation:
Three steps are mandatory for the implementation of this technique.
1. A Nexmo account. https://dashboard.nexmo.com/sign-up.
2. A Thingspeak account. https://thingspeak.com/users/sign_up
3. NodeMCU SMS Messaging Code.
STEP 1:
After signing up with an account in Nexmo which provides you an SMS API. Make sure to note down the key and the secret from the API settings tab.
Fig. 1: Screenshot of Nexmo Sign Up Page
Next step is to add numbers and verifying it you can add 10 numbers in the free version of Nexmo. Now you can check the API by copying the below link and pasting it into the address box of your favourite web browser.
http://rest.nexmo.com/sms/xml?api_key=xxxxx&api_secret=xxxxxx&from=senderID&to=destination_number&text=messagebody
Replace necessary values in the URL (you can get all these from the Nexmo site). Upon hitting “Enter” you should receive a text almost immediately.
Fig. 2: Screenshot from Nexmo website showing API Key and Secret Password used for sending text messages using ESP8266 Wi-Fi Module
API Key and Secret can be found in as shown in the above picture.
Sender ID can be any, Destination number should be entered with the country code without “0” or “+” for example, India country code is “+91” you should enter the number like” 919876543210” without any space. Be sure to verify the destination number.
STEP 2:
Since ESP8266 doesn’t support HTTPS secured connection and Nexmo will not get a request in HTTP connection, we need to bridge these two by using HTTPS relay, which will receive HTTP requests ESP8266 and convert to the HTTPS request. For this, we are going to use Thingspeak.com, after signing in go to apps, then to ThingHTTP and create a new Thing HTTP.
Make the following changes in the setup page, “XXXX” are to be changed to your credentials.
1. Name:SMS
2. URL:https://rest.nexmo.com/sms/json?api_key=XXXX&api_secret=XXXX&from=XXXX&
3. Method: POST
4. Content-type : application/x-www-form-urlencoded
5. Click remove headers, and leave host blank
6. Body: to=%%number%%&text=%%message%%
7. Leave other blank
8. Click Save ThingHTTP
Fig. 3: Screenshot from Nexmo website showing convertion of https protocol supported by ESP8266 Modem on Thingspeak Server
You can now send your request to ThingHTTP and view the response using the following URL:
https://api.thingspeak.com/apps/thinghttp/send_request?api_key=XXXX &number=91XXXX&message=Hi from Esp8266
STEP 3:
NodeMCU SMS Messaging Code is written in LUA and uploaded using Esplorer. Here we are using a button to trigger the SMS sending code. For this button interface, we are using Interrupt with debounce in ESP8266.
Using interrupt mode:
If we want to know when a pin’s value change (ie. At once a button is pressed) a better solution is to use interrupt mode. That way we can create a function and set it to be called every time the pin’s value changes.
We define which callbacks are called when by using gpio.trig().
Let’s write to the console when GPIO2 changes value:
local pin = 4 –> GPIO2
function onChange ()
print(‘The pin value has changed to ‘..gpio.read(pin))
end
gpio.mode(pin, gpio.INT)
gpio.trig(pin, ‘both’, onChange)
Event name | Description |
---|---|
up | Occurs when the pin moves high. |
down | Occurs when the pin moves low. |
both | Occurs when the pin moves high or low. |
low | Occurs repeatedly while the pin is low. |
high | Occurs repeatedly while the pin is high. |
Debouncing:
When you press a button down the pin changes value, changes back again and finally settles on the new value once more. This is called contact bounce and it is common.
For avoiding this Software debouncing is implemented by the following code:
local pin = 4 –> GPIO2
function debounce (func)
local last = 0
local delay = 200000
return function (…)
local now = tmr.now()
if now – last < delay then return end
last = now
return func(…)
end
end
function onChange ()
print(‘The pin value has changed to ‘..gpio.read(pin))
end
gpio.mode(pin, gpio.INT)
gpio.trig(pin, ‘both’, debounce(onChange))
The debounce() function takes a function as its argument and wraps it in another. When the outer function is called, that then calls the inner function, but only if it hasn’t been called in the last 5000us (5ms).
The POST request to send a text message is the following:
1. POST : /apps/thinghttp/send_request?api_key=XXXX
2. Host: api.thingspeak.com
3. Connection: close
4. Content-Type: application/x-www-form-URL encoded
5. Content-Length: xxx
6. number=91XXXX&message=my message
This project consists of two codes one should be named as init.lua and this is responsible for the initialization of the connection with WiFi router it will retry until connection to the router and getting the IP address and then it will call SMS.lua which will send the SMS once the button is pressed.
Uploading Your Lua Script:
Follow these steps to send the code to your ESP8266:
1) Connect your FTDI programmer and select your FTDI COM Port.
2) Set baud rate at 9600
3) Select NodeMCU+MicroPtyhon tab
4) Copy your Lua script into ESPlorer and save your file with the name “init.lua” and SMS.luas respectively.
5) Upload to the ESP and Run on ESP.
Don’t forget to change the SSID, Password to your Home WiFi Network and Api_key in the code.
Working:
Here the ESP8266 is connected to the WiFi home network and initialized with the coding when the button is pressed the ESP8266 will POST the request to the SMS API and the message will be sent to the predefined number. This technique is very much useful in the Security systems.
Project Video
Filed Under: Featured Contributions
Filed Under: Featured Contributions
Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.