Category Archives: python

Un petit script python qui m’envoie des mails quand le RER est malade

Un petit script tout moche python qui m’envoie des mails quand le RER est malade, lancé avec une crontab aux horaires qui m’intéressent, et voilà!

#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
now = time.ctime()


# RATP API part
## c'est la partie magique, merci Pierre!
import requests
r = requests.get("http://api-ratp.pierre-grimaud.fr/data/trafic/rer")
data = r.text
print(data)

import json
parsed_json = json.loads(data)
if parsed_json["trafic"] == "perturbation":
  print(parsed_json["perturbations"])
  mailbody = parsed_json["perturbations"]

  ## mail part
  import subprocess
  import smtplib
  import email.utils
  from email.mime.text import MIMEText

  recipients = "qqn@mail.com,joe@example.net"
  # Create the message
  msg = MIMEText(str(mailbody))
  msg['To'] = recipients
  msg['From'] = "noreply-rapt@whateversnmpworkshere.fr"
  msg['Subject'] = "RERB status -- " + now

  server = smtplib.SMTP('smtp.example.net')
  log = server.set_debuglevel(True) # show communication with the server
  try:
      server.sendmail(msg["From"], msg["To"].split(","), msg.as_string())
  finally:
      server.quit()
  #print(log)

automating #Twitter with #raspberrypi

I’ve been tweeting pictures and re-tweeting twits automatically for a few years now using two python tools.

  1. Twython
  2. on github
    Here’s an example script for Twython

  3. TwitterFollowBot
  4. on github
    Here’s an example script for TwitterFollowBot

Both require to have an already created Twitter app (https://apps.twitter.com/) and a little bit of python.

Temperature and humidity graph with raspberry pi

Top posted update:
Now it looks like this
pre-requisites:

  • a pinch of pygal
  • 1 raspberry pi (I used model B)
  • breadboard + accessories
  • DHT22 (or equivalent)

I just followed Adafruit’s tutorial for the wiring and the software
Then I did a slight modification to the script provided by Adafruit to write the data to a log file (temp-hum.log) instead of google drive spreadsheet.

Actual work
Updated code on Github
To generate the log file that will be parsed by pygal on github
Adafruit’s code for the sensor forked on github

A short python script to generate the graphic using Pygal:

#!/usr/bin/env python

import pygal

temp = []
hum  = []

logdata = open('temp-hum.log','r')
for line in logdata.readlines():
  line = line.split()[1:]
  temp.append(float(line[0]))
  hum.append(float(line[1]))

print(temp)
print(hum)

line_chart = pygal.Line(width=1200)
line_chart.title = 'Temperature Humidity'
line_chart.add('Temperature *C', temp)
line_chart.add('Humidity %', hum) 
line_chart.render_to_file('temp-hum.svg')
breadboard and flat cable
breadboard and flat cable
DHT22 and cables
DHT22 and cables
r-pi and all
r-pi and all
Adafruit_DHT
Adafruit_DHT
DHT22 sensor
DHT22 sensor
breadboard
breadboard
r-pi with sensor
r-pi with sensor

And the graphic looks like:
this

Sources: Adafruit DHT22 tutorial

installing inotool on the Raspberry Pi

For the basic install and configuration example, see my previous post inotool for busy people

When installing on a R-pi, few tools will not be installed by default on Raspbian, proceed to install all of them *before* attempting to clone the git repository.


apt-get install python-configobj python-setuptools git python-jinja2 python-serial

Enjoy!

raspberry pi GPIO project

The idea is to plot temperature, light an humidity inside my house.

2013-02-24 00.51.50

source of inspiration

installing mp3_scanner on mac os

install python 2.7
python-2.7 32-bit
python-2.7 64bit

install mutagen
download


tar zxvf mutagen-1.20.tar.gz
cd mutagen-1.20
python setup.py build
sudo python setup.py install

get mp3_scanner
https://github.com/tatic0/mp3_scanner