#!/usr/bin/env python
# -*- coding: latin-1 -*-

import serial
import MySQLdb
import httplib, urllib
from time import sleep, gmtime, strftime


    conn = MySQLdb.connect( host="localhost",
                            user="root",
                            passwd="password",
                            db="meteo")
    cur = db.cursor()

    print (strftime("%a, %d %b %Y %H:%M:%S: Début réception\n", gmtime()))

    ser = serial.Serial('/dev/ttyACM0', baudrate=9600, xonxoff=False, rtscts=False, dsrdtr=False, timeout=None)
    ser.flushInput()

def doit():
        pressue = 0 
        humidity = 0
        pressure = 0
        result = 0
        ser.flushInput()
        result = ser.readline()
        pressure, temperature, humidity= result.split('/')
        sql = "INSERT INTO 'meteo'.'data_int' ('time', 'temp', 'press', 'hum') VALUES( now(), 'temperature', 'pressure', 'humidity')";

    try: 
        conn.request("POST", "/update", sql)
        response = conn.getresponse()
        data = response.read()
        conn.close()

    except: 
        print "connection failed"
        pressure = 0
        temperature = 0
        humidity = 0 
        result = 0 
        ser.flushInput()
        doit()

    while True: 
        doit()
        ser.flushInput()
        time.sleep(60)
