Created: 2014-05-18 14:39
Updated: 2017-05-05 23:06


Log data captured from AM2302 into a MySQL database using a Raspberry Pi

This script is based heavily on the Adafruit scripts found at:

Follow of the the Adafruit instructions to make your newly-minted temperature & humidity sensor work with python and Google docs spreadsheets. You will likely need to use an older version of Google speadsheets; the lateest iteration of spreadsheet does not seem to function with gspread, the python library for writing to google docs spreadsheets.

Look in the forum for more information re. the older google docs.

Once you have that working, you will have most the crucial components, including knowledge of the sensor and how it is integrated into python.

What you Need

  1. A Raspberry Pi with the latest Raspbian installation.

  2. MySQL Server. There are lots of good tutorials for installing MySQL for the Pi, so go forth and Google. Or, if you already have a MySQL Server, skip to instruction 2.

  3. A database in the MySQL Server with a database and user that you can connect to and execute INSERT statements against. You will need a table with the four following columns, which have self-explanatory names:

           capturetime, temperature, humidity, sensornum

    I decided to include a sensornum column in the event you are capturing data from more than one sensor (i.e. one indoors and one outdoors).

    A stand-alone table would have a create script like this:

           CREATE TABLE `wd_temp_humidity` (
                        `id` int(11) NOT NULL AUTO_INCREMENT,
                        `capturetime` datetime DEFAULT NULL,
                        `temperature` decimal(5,1) DEFAULT NULL,
                        `humidity` decimal(5,1) DEFAULT NULL,
                        `sensornumber` int(11) NOT NULL,
                        PRIMARY KEY (`id`)
  4. Python Library MySQLdb for connecting your python script to your database. I installed the library on my Pi using apt-get:

           sudo apt-get install python-mysqldb
  5. One (or more) of the following Temperature and Humidity sensors:

           DHT11, DHT22, AM2302

    I picked the AM2302 because it did not require any other parts such as resistors to attach it to the Pi. It also comes in a small plastic case which will be useful if you want to mount the sensor onto something.

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more