Skip to content

Instantly share code, notes, and snippets.

@bcdejp
Created February 21, 2015 03:43
Show Gist options
  • Save bcdejp/f8cf78934478273bc7af to your computer and use it in GitHub Desktop.
Save bcdejp/f8cf78934478273bc7af to your computer and use it in GitHub Desktop.
温度をMySQLデータベースに記録
#!/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