Send Text Messages With A Raspberry Pi and Nexmo - Raspberry Pi - Maker Pro

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

01/12/2021, 02:58 Send Text Messages With a Raspberry Pi and Nexmo | Raspberry Pi | Maker Pro

import nexmo # Importing the nexmo library


import RPi.GPIO as GPIO
from time import sleep

sensor_pin = 17 # Initialized GPIO 17 for motion sensor


GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(sensor_pin, GPIO.IN) # Declared GPIO 17 as input pin

# Connecting to the nexmo using API key and API secret


client = nexmo.Client(key='86af8054', secret='wVFo9rKpcYbVg')

receiver = 'Enter number' # The number at which you want to send to


message = '''Body detected '''

# Function to send message


def send_sms():
# Sending the message
response = client.send_message({'from' : 'Nexmo', 'to' : receiver, 'text' : message})
# Getting the response message
response = response['messages'][0]

# Checking whether we are successful or we got a error


if response['status'] == '0':
print 'send message', response['message-id']
else:
print 'Error:' , response['error']

while True:
try:
# Reading the motion sensor pin state
pin_state = GPIO.input(sensor_pin)
if pin_state==0: #When output from motion sensor is LOW
print "Body Not Detected",pin_state
sleep(0.1)

elif pin_state==1: #When output from motion sensor is HIGH


print "Body Detected",pin_state
send_sms()
sleep(5)
except KeyboardInterrupt:
GPIO.cleaup()

https://maker.pro/raspberry-pi/projects/how-to-send-text-messages-from-a-raspberry-pi 1/1

You might also like