Weather station Raspberry Pi – The Python capture and storage with SQLite

The sequel to the manufacture of our weather station with Raspberry Pi. After the configuration of the transmitters and Assembly of housings, need to retrieve measurements and storing them somewhere.


  1. The database. To save the received data we will have to define a SQLite database.In this database we have two tables :
    A table for all the emitters. We will store the ID of the issuer, its voltage with the date and time of last update, the position of the transmitter (inside or outside for example), the frequency of data collection.
  2. Install sqlite3 to control your database
    apt - get install sqlite3
  3. Install ntpdate to have the exact time
    apt - get install ntpdate

    Then start manually :


    By crontab, Add the line for an update every hour

    0 */1 * * * ntpdate

    using the command

  4. Install python for scripting of recovery on the serial port
    apt - get install python
  5. Now you can create your database :
    mkdir/var/www/var/www sqlite3 meteoPi.db cd

    You now send the following commands to create the different tables

    DROP TABLE IF EXISTS transmitters;
    CREATE TABLE transmitters(ID TEXT, REAL Volt, VoltUpdate TEXT, Location TEXT, DataFreq TEXT, Type TEXT);
    DROP TABLE IF EXISTS measures;
    CREATE TABLE measures(ID TEXT, REAL measurement, DateMesure TEXT);
  6. In this base it will not forget to create a record by issuer :
    INSERT INTO VALUES transmitters('ZZ', 0.0, ' 2013-01-01 00:00:00.000', 'Garden', ' 0005 ME, 'Temp');

    For a second transmitter for example :

    INSERT INTO VALUES transmitters('ZY', 0.0, ' 2013-01-01 00:00:00.000', 'Ground floor', ' 0005 ME, 'Temp');
  7. Your database is now ready ! Here is the Python script that will store catches of probes :
    #!/usr/bin/env python
    # -*- coding: Latin-1 -*-
    import serial import sqlite3 from time import sleep, gmtime, strftime DEVICE = '/ dev/ttyAMA0' BAUD = 9600
    con = None try:
        Conn = sqlite3.connect('/var/ www/meteoPi.db')
        print (strftime("%a, %d %b %y %h:%M:%S: Start receptionn", gmtime()))
        SER = serial.Serial(DEVICE, BAUD)
        While True:
            print("%s: Checking..." % strftime("%a, %d %b %y %h:%M:%S", gmtime()))
            n = ser.inWaiting()
            If n != 0:
                data =
                nb_msg = len(data) / 12
                for i in range (0, nb_msg):
                    msg = data[i*12:(i 1)*12]
                    device = msg[1:3]
                    date_mesure = strftime("%Y-%m-%d %H:%M:%S.000", gmtime())
                    if msg[3:7] == "TMPA":
                        temp = msg[7:]
                        # INSERT de la mesure                     sql_command = "INSERT INTO Mesures VALUES('"+device "', "+temp ", '"+date_mesure "');"
                        print("%s" % (sql_command))
                    if msg[3:7] == "BATT":
                        voltage = msg[7:11]
                        if voltage == "LOW":
                            voltage = 0
                        # UPDATE du composant                     sql_command = "UPDATE Emetteurs SET Volt="+voltage ", VoltUpdate='"+date_mesure "' WHERE Id='"+device "';"
                        print("%s" % (sql_command))
                    print "Lignes misent à jour :", conn.total_changes         sleep(1 * 30)
    except sqlite.Error, e:
        print "Error %s:" % e.args[0]
        if conn:

    The observer will have noticed the message test “BATT”. All the 10 items you will receive the level of the battery by message as the temperature, for example 2.74 (on the 3V battery supplied with the transmitter).
    If you receive the LOW value it is time to change the battery !

You now have a tool to store all the measures that you capture !

Last step : the display in a web site (Beau preferably) !

You may also like...