samedi 2 mai 2015

Wireless alarm with a Raspberry Pi with python

Part 4 : Send Alarm

The best way to alert someone is to phone it and let him a record. But i search an easy way to do it, finally i decided to send a sms and a mail. Most of the time, where i am not at home, i am at work and i can read my mails, or i keep a mobile phone on me.

Send mail

We need to install SSMTP :

  sudo apt-get install ssmtp  

next we have to create a piece of code to in ptyhon to send mail using this lib :

 import smtplib  
 from email.mime.text import MIMEText   
 def Send(Destinataire,Msg):  
      s =smtplib.SMTP('smtp.gmail.com:587')  
      s.starttls()  
      s.login('XXXXXXXX@gmail.com','XXXXXXXX')  
      s.sendmail('XXXXXXXXX@gmail.com',Destinataire ,Msg)  
      s.quit()  


Send SMS

The code can change according your phone operator. In my case i use french operator "Free Mobile". This operator offer a free web service to send a sms to myself, very useful for home automation .

The code below :

 #!/usr/bin/python  
 # -*- coding: utf-8 -*-  
 import urllib,urllib2  
 def SendSMS(Message):  
      #remplace les espaces du message par des %20  
      Message=Message.replace(' ','%20')  
      # URL du formulaire  
      url = 'https://smsapi.free-mobile.fr/sendmsg'  
      #Champ et valeur du formulaire  
      params = urllib.urlencode({'user': 'XXXXXXX','pass':'XXXXXXXX'})  
      #Message espace remplace par des %20  
      #params =params + '&msg=Alarme%20Intrusion'  
      params =params + '&msg=' + Message  
      #Envoi de la requête  
      req = urllib2.Request(url+'?'+ params)  
      response=urllib2.urlopen(req)  

Nice, now we made the sensors communication, Daemon service to start alarm and the way to send message. It remains two tasks :
  • the database to remeber the alarm state,
  • web site.
Part 5 Database (mysql)

Aucun commentaire:

Enregistrer un commentaire