While working for few projects it was a bit difficult to to use the current existing SMS service which are generally used in IOT. Using NEXMO API for sending SMS as a demo is lot easier than using other SMS serivce.
In this I have demonstrated how to use NEXMO SMS API, with all details.
The code provided for sending SMS can be used in any of your IOT projects. You can integrate this piece of code on detection of certain actions, so SMS will be sent.
My code is setup to send SMS when a push button is pressed. But this can triggered when sensor value if equal to certain value.
Follow below given steps to run the program:
- Sign-up for NEXMO account here.
- Once you sign-up visit your dashboard here.
- Make note of your API key and App secret. In new dashboard, it is namely key and secret.
- In Raspberry Pi, open the terminal and install NEXMO library using following code:
pip install NEXMO
- Following code will be used in your main program to send SMS.
import nexmo
Save this code in a file and name it “send_sms.py” in your Raspberry Pi.
- Below is your main program which will call send_sms.py on the press of push button.
import RPi.GPIO as GPIO
import os
import sys
push_button = 23
led = 18
def setup():
GPIO.setmode(GPIO.BCM) # Numbers GPIOs by physical location
GPIO.setup(led,GPIO.OUT)
GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
def destroy():
GPIO.output(17, GPIO.LOW) # led off
if _name_ == "_main_": # Program start from here
setup()
try:
if(GPIO.input(push_button) == True):
GPIO.output(18,GPIO.HIGH)
print("sms sent")
os.system("python send_sms.py") #Using this line in any python program you can send sms. This call the send_sms.py program and runs it.
break
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
***NOTE for trial users in NEXMO SMS sending is only available between 10 a.m. to 10 p.m. . You can only send SMS to your registered mobile number.
FOR MORE INTERESTING PROJECTS AND TUTORIALS VISIT iotguider.in
Comments
Please log in or sign up to comment.