Created
February 21, 2015 03:43
-
-
Save bcdejp/f8cf78934478273bc7af to your computer and use it in GitHub Desktop.
温度をMySQLデータベースに記録
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
import smbus | |
import datetime | |
import MySQLdb | |
if __name__ == "__main__": | |
i2c = smbus.SMBus(1) | |
address = 0x48 | |
block = i2c.read_i2c_block_data(address, 0x00, 12) | |
temp = (block[0] << 8 | block[1]) >> 3 | |
if(temp >= 4096): | |
temp -= 8192 | |
connector = MySQLdb.connect(host="localhost", db="logging", user=“user", passwd=“passed", charset="utf8") | |
cursor = connector.cursor() | |
str_tmp = "%6.2f" % (temp / 16.0) | |
sql = u"insert into temperature values(now(), %s)" % str_tmp | |
cursor.execute(sql) | |
connector.commit() | |
cursor.close() | |
connector.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment