Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

sending alerts to many one or more people

Wed Dec 05, 2018 9:47 am

I am using Raspberry Pi to monitor events and want to send SMS to one or more people on mobile

I have created following GUI to enter mobile number

Code: Select all

from tkinter import *

def callback():
    print ("You clicked the button!")

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
mainloop()
Python API code to send text sms link http://api.textlocal.in/docs/sendsms

Code: Select all

#!/usr/bin/env python
 
import urllib.request
import urllib.parse
 
def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)
 
resp =  sendSMS('apikey', '918123456789',
    'Jims Autos', 'This is your message')
print (resp)
How to integrate python code with GUI

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Wed Dec 05, 2018 9:55 am

Its not difficult, just call the sendSMS function from your callback function.

You also need to get the value of the text box and pass that to the sendSMS function.

Something similar to this should probably work

Code: Select all

#!/usr/bin/env python
#https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=228355
from tkinter import *
import urllib.request
import urllib.parse

def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('apikey', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
mainloop()
(Code untested since I don't have an API key)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Wed Dec 05, 2018 10:44 am

scotty101 wrote:
Wed Dec 05, 2018 9:55 am
Its not difficult, just call the sendSMS function from your callback function
(Code untested since I don't have an API key)
Thanks scotty101, I am outside from home. This is the untested code

if the sensor triggered send sms to many people

Code: Select all

from tkinter import *
import RPi.GPIO as GPIO
import urllib.request
import urllib.parse

"""Setup the GPIO Pins"""
GPIO.setmode(GPIO.BCM)

GPIO.setup(21,GPIO.IN) # button from pin 40 to gnd

SensorHigh = 0
TOTAL = 0


def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('apikey', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    Label(text= SensorHigh , relief=RIDGE,width=10).grid(row=1,column=1)
    TOTAL = TOTAL + 1
    Label(text=TOTAL, relief=RIDGE,width=10 ).grid(row=2,column=1)


GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

Label(text='Sensor Activate',  relief=RIDGE,width=10 , height= 5 ).grid(row=1,column=0)

Label(text=SensorHigh , relief=RIDGE, width=10 , height= 5 ).grid(row=1,column=1)

Label(text='TOTAL', relief=RIDGE,width=10, height= 5 ).grid(row=2,column=0)
Label(text=TOTAL, relief=RIDGE, width=10, height= 5).grid(row=2,column=1)

mainloop()
If you see mistake in code please let me know

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Thu Dec 06, 2018 5:48 am

scotty101 wrote:
Wed Dec 05, 2018 9:55 am
Its not difficult, just call the sendSMS function from your callback function.

You also need to get the value of the text box and pass that to the sendSMS function.

Something similar to this should probably work
I tried your code but getting error
may be I am making mistake somewhere so I am posting code with error

Code: Select all

#!/usr/bin/env python
#https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=228355
from tkinter import *
import urllib.request
import urllib.parse

def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': ***********, 'numbers': number,
        'message' : message, 'sender': VJRTC})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)
    
def callback():
    number = E1.get()
    resp =  sendSMS('apikey', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
mainloop()

When I submit number it show following message
error message
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1562, in __call__
return self.func(*args)
File "test10.py", line 18, in callback
resp = sendSMS('apikey', number,'Jims Autos', 'This is your message')
File "test10.py", line 8, in sendSMS
data = urllib.parse.urlencode({'apikey': , 'numbers': number,
NameError: name '***********' is not defined

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Thu Dec 06, 2018 9:37 am

Change

Code: Select all

def sendSMS(apikey, numbers, sender, message):
to

Code: Select all

def sendSMS(apikey, number, sender, message):
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Thu Dec 06, 2018 9:42 am

Fan174 wrote:
Thu Dec 06, 2018 5:48 am
NameError: name '***********' is not defined
If that is exactly the error you are getting, its because ************** isn't valid. If *********** is supposed to be your API key try putting it inside quotes like '*****************' e.g. '123456789'
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Thu Dec 06, 2018 3:48 pm

scotty101 wrote:
Thu Dec 06, 2018 9:42 am
Fan174 wrote:
Thu Dec 06, 2018 5:48 am
NameError: name '***********' is not defined
I know I tried with original API key I think I am mistaking somewhere
Can you guess what may be wrong in code

Code: Select all

#!/usr/bin/env python
#https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=228355
from tkinter import *
import urllib.request
import urllib.parse

def sendSMS(apikey, number, sender, message):
    data =  urllib.parse.urlencode({'gjhfyiyiyiy5999jghjgugugyfyf': apikey, 'numbers': numbers,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
mainloop()
This code is untested also This is not oruginal APIKEY

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Thu Dec 06, 2018 4:48 pm

Does this work? I've tested as much as i can without actually making the urllib request.

Code: Select all

#!/usr/bin/env python
#https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=228355
from tkinter import *
import urllib.request
import urllib.parse

def sendSMS(apikey, number, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': number,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
mainloop()
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Thu Dec 06, 2018 5:20 pm

scotty101 wrote:
Thu Dec 06, 2018 4:48 pm
Does this work? I've tested as much as i can without actually making the urllib request.
I don't have pi in hand I will test it at home

you did a lot help, please take look at this code and please tell me If I am making something wrong

Code: Select all

from tkinter import *
import RPi.GPIO as GPIO
import urllib.request
import urllib.parse

"""Setup the GPIO Pins"""
GPIO.setmode(GPIO.BCM)

GPIO.setup(21,GPIO.IN) # button from pin 40 to gnd

SensorHigh = 0
TOTAL = 0

def sendSMS(apikey, number, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': number,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    Label(text= SensorHigh , relief=RIDGE,width=10).grid(row=1,column=1)
    TOTAL = TOTAL + 1
    Label(text=TOTAL, relief=RIDGE,width=10 ).grid(row=2,column=1)


GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

Label(text='Sensor Activate',  relief=RIDGE,width=10 , height= 5 ).grid(row=1,column=0)

Label(text=SensorHigh , relief=RIDGE, width=10 , height= 5 ).grid(row=1,column=1)

Label(text='TOTAL', relief=RIDGE,width=10, height= 5 ).grid(row=2,column=0)
Label(text=TOTAL, relief=RIDGE, width=10, height= 5).grid(row=2,column=1)

mainloop()
untested code, thinking final code should be look like above code

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Thu Dec 06, 2018 5:31 pm

I'm not a fan of what you are doing here

Code: Select all

def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    Label(text= SensorHigh , relief=RIDGE,width=10).grid(row=1,column=1)
    TOTAL = TOTAL + 1
    Label(text=TOTAL, relief=RIDGE,width=10 ).grid(row=2,column=1)
You'll create new labels each time there is a falling edge on GPIO 21. I'd suggest creating the labels when you create the "Sensor Activate" and "TOTAL" caption labels and the function that is called when GPIO 21 callback is triggered should just update the count and change the text to display the newly incremented number.

I'll create an example and update this post.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Thu Dec 06, 2018 5:36 pm

Ok. I'd still rewrite it to improve the code but this does at least show how it might work. I've added an extra button and function to simulate the GPIO falling edge but you can comment these out.

Code has been modified to update the labels that contain the SensorHigh and TOTAL counts rather than create a new label each time.

Code: Select all

from tkinter import *
#import RPi.GPIO as GPIO
import urllib.request
import urllib.parse

"""Setup the GPIO Pins"""
GPIO.setmode(GPIO.BCM)

GPIO.setup(21,GPIO.IN) # button from pin 40 to gnd

SensorHigh = 0
TOTAL = 0

def sendSMS(apikey, number, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': number,
        'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback():
    number = E1.get()
    resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', number,'Jims Autos', 'This is your message')
    print ("You tried to send a message\n", resp)

top = Tk()
L1 = Label(top, text="Mobile")
L1.grid(row=4, column=0)
E1 = Entry(top)
E1.grid(row=4, column=1)

MyButton1 = Button(text="Submit", width=0, command=callback)
MyButton1.grid(row=4, column=3)
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    TOTAL = TOTAL + 1
    lblSensorHigh['text'] = SensorHigh
    lblTotal['text'] = TOTAL

#Comment this out for your version
def trigger():
    FAULTbuttonPressed(21)


GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

Label(top, text='Sensor Activate',  relief=RIDGE,width=10 , height= 5 ).grid(row=1,column=0)
lblSensorHigh = Label(top, text= SensorHigh , relief=RIDGE,width=10)
lblSensorHigh.grid(row=1,column=1)



Label(top, text='TOTAL', relief=RIDGE,width=10, height= 5 ).grid(row=2,column=0)
lblTotal = Label(top, text=TOTAL, relief=RIDGE,width=10 )
lblTotal.grid(row=2,column=1)


#Comment this out for your version
Button(text="GPIO Dummy",command=trigger).grid(row=5,column=0)

top.mainloop()


(Not sure why would have two counts SensorHigh and TOTAL given that they are both updated at the same time...)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Sat Dec 08, 2018 9:47 am

scotty101 wrote:
Thu Dec 06, 2018 4:48 pm
Does this work? I've tested as much as i can without actually making the urllib request.
I testd,It gives this error
{"errors":[{"code":80,"message":"invalid template"}],"status":"failure"}

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Sun Dec 09, 2018 10:01 pm

Can't help with that. Looks like something wrong with the call to the website to send the text.
As I don't have an API key, I can't help you.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Mon Dec 10, 2018 10:42 am

scotty101 wrote:
Thu Dec 06, 2018 5:36 pm
.(Not sure why would have two counts SensorHigh and TOTAL given that they are both updated at the same time...)
When I run following program I am getting message on mobile

Code: Select all

#!/usr/bin/env python
from tkinter import *
import RPi.GPIO as GPIO
import urllib.request
import urllib.parse
 
def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers, 'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)
 
 
resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', '919923456789', 'Dejn', 'This is your message')
print (resp)

Now I don't want to create a GUI to submit mobile number.

Now what I want if faulty product detect on conveyer belt , send message to person and count how many faulty product detected

one sensor is connected to raspberry pi (pin 21) . This sensor detects faulty product

create one GUI to show faulty lable detect
create another GUI to show how many faulty product detected

Code: Select all

#!/usr/bin/env python
from tkinter import *
import RPi.GPIO as GPIO
import urllib.request
import urllib.parse
 
def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers, 'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)
 
resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', '919923456789', 'Dejn', 'This is your message')
print (resp)


"""Setup the GPIO Pins"""
GPIO.setmode(GPIO.BCM)

GPIO.setup(21,GPIO.IN) # button from pin 40 to gnd

SensorHigh = 0
TOTAL = 0

top = Tk()
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    TOTAL = TOTAL + 1
    lblSensorHigh['text'] = SensorHigh
    lblTotal['text'] = TOTAL

GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

Label(top, text='Sensor Activate',  relief=RIDGE,width=10 , height= 5 ).grid(row=1,column=0)
lblSensorHigh = Label(top, text= SensorHigh , relief=RIDGE,width=10)
lblSensorHigh.grid(row=1,column=1)

Label(top, text='TOTAL', relief=RIDGE,width=10, height= 5 ).grid(row=2,column=0)
lblTotal = Label(top, text=TOTAL, relief=RIDGE,width=10 )
lblTotal.grid(row=2,column=1)

top.mainloop()
problem is that intially i am getting message but when sensor detects faulty product I am not getting the message

I have doubt in program because there is no If condition to check sensor Please help me in program

Code: Select all

import  RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_UP)


while True:
    inputValue = GPIO.input(21)
    if (inputValue == True):
        print("Product detected ")
    

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Mon Dec 10, 2018 11:02 am

If you want something to happen (send an SMS) based on the GPIO pin, you'd need to add code to the "FAULTbuttonPressed" function which is called by the GPIO callback. You don't need an "if" when you are using the "add_event_detect" method of the GPIO library.

At the moment your code just sends the SMS every time you run the code and not based on the GPIO pin.

Copy and Pasting code from the internet is one way to learn but it seems like you don't understand what the code does. Perhaps you would benefit from taking an online python tutorial course?
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Fan174
Posts: 171
Joined: Tue Mar 20, 2018 6:10 am

Re: sending alerts to many one or more people

Mon Dec 10, 2018 11:33 am

scotty101 wrote:
Mon Dec 10, 2018 11:02 am
If you want something to happen (send an SMS) based on the GPIO pin, you'd need to add code to the "FAULTbuttonPressed" function which is called by the GPIO callback. You don't need an "if" when you are using the "add_event_detect" method of the GPIO library.

At the moment your code just sends the SMS every time you run the code and not based on the GPIO pin.

Copy and Pasting code from the internet is one way to learn but it seems like you don't understand what the code does. Perhaps you would benefit from taking an online python tutorial course?
I am not good in python but trying to learn. I never worked with GUI and SMS service that's why I am confused
I am thinking like this

Code: Select all

#!/usr/bin/env python
from tkinter import *
import RPi.GPIO as GPIO
import urllib.request
import urllib.parse
 
def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers, 'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

def callback(): 
resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', '919923456789', 'Dejn', 'This is your message')
print (resp)


"""Setup the GPIO Pins"""
GPIO.setmode(GPIO.BCM)

GPIO.setup(21,GPIO.IN) # button from pin 40 to gnd

SensorHigh = 0
TOTAL = 0

top = Tk()
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    TOTAL = TOTAL + 1
    lblSensorHigh['text'] = SensorHigh
    lblTotal['text'] = TOTAL
	
GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

detect = callback()

Label(top, text='Sensor Activate',  relief=RIDGE,width=10 , height= 5 ).grid(row=1,column=0)
lblSensorHigh = Label(top, text= SensorHigh , relief=RIDGE,width=10)
lblSensorHigh.grid(row=1,column=1)

Label(top, text='TOTAL', relief=RIDGE,width=10, height= 5 ).grid(row=2,column=0)
lblTotal = Label(top, text=TOTAL, relief=RIDGE,width=10 )
lblTotal.grid(row=2,column=1)

top.mainloop()
code untested

scotty101
Posts: 3562
Joined: Fri Jun 08, 2012 6:03 pm

Re: sending alerts to many one or more people

Mon Dec 10, 2018 12:01 pm

Why not just test your code and find your own bugs?

Try something like this instead (Most of the code removed so you actually have to understand what I've done)

Code: Select all

..............
 
def sendSMS(apikey, numbers, sender, message):
    data =  urllib.parse.urlencode({'apikey': apikey, 'numbers': numbers, 'message' : message, 'sender': sender})
    data = data.encode('utf-8')
    request = urllib.request.Request("https://api.textlocal.in/send/?")
    f = urllib.request.urlopen(request, data)
    fr = f.read()
    return(fr)

..............
                   
def FAULTbuttonPressed(channel):
    global SensorHigh , TOTAL
    SensorHigh  = SensorHigh  + 1
    TOTAL = TOTAL + 1
    lblSensorHigh['text'] = SensorHigh
    lblTotal['text'] = TOTAL
    resp =  sendSMS('gjhfyiyiyiy5999jghjgugugyfyf', '919923456789', 'Dejn', 'This is your message')
    print (resp)
	
GPIO.add_event_detect(21, GPIO.FALLING, callback=FAULTbuttonPressed, bouncetime=250)

.........

top.mainloop()
Note: In the future you may wish to add some code so messages can only be sent every 5 minutes (for example). Unless you want a huge bill for sending too many messages.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “General discussion”