NIshit Patel
Published © GPL3+

Sending SMS Using Raspberry Pi On Event Detection

This project will show you how to send SMS using Raspberry Pi when a certain event is detected. For SMS, I have used NEXMO API.

BeginnerProtip1 hour7,418
Sending SMS Using Raspberry Pi On Event Detection

Things used in this project

Hardware components

Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
LED (generic)
LED (generic)
optional
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
Resistor 10k ohm
Resistor 10k ohm
for the push button
×1
Resistor 221 ohm
Resistor 221 ohm
for the led
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

nexmo

Story

Read more

Custom parts and enclosures

Circuit diagram

This is circuit diagram for connecting pushbutton and led. LED is optional in this case, it will light up only when sms is sent

Code

Main

Python
Save this code with whatever name you want. This will trigger SMS API when push button is pressed
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.
                destroy()

send_sms

Python
This is code for sending sms.Replace "API key", "APP secret" and "your phone number" with your details.
import nexmo
client = nexmo.Client(key='YOUR-API-KEY',secret='YOUR-API-SECRET')
client.send_message({'from': 'Nexmo', 'to': 'YOUR-PHONE-NUMBER', 'text': 'Hello world'})

Credits

NIshit Patel

NIshit Patel

4 projects • 16 followers

Comments