雷竞技注册
项目

用覆盆子PI传递温度

2015年8月4日经过特拉维斯船用伪造

如何将覆盆子PI接口到I2C温度传感器(TMP102),然后将数据发布到Google表格和图形。

该项目表明将Raspberry PI与I2C温度传感器(TMP102)连接,然后将数据发布到Google Sheet和图表。

推荐水平

中间的

要求

  • 覆盆子PI.
    • 用于文章:模型B修订版1.0与Raspbian(Debian GNU / Linux 7.6(Wheezy))
  • I2C温度传感器
  • 将Raspberry PI连接到Internet的方法
    • 在文章中使用:覆盆子PI直接连接到路由器
  • Google帐户创建和访问表格

设置I2C

安装驱动程序

  1. 在PI上打开终端或使用SSH
  2. 在终端上键入以下内容以安装支持工具:
    sudo apt-get安装python-smbus
    sudo apt-get安装i2c-tools
  3. 使用Raspi-Config来启用I2C驱动程序:
    sudo raspi-config
    1. 选择“高级选项”
    2. 选择“i2c”
    3. 选择“是”
    4. 选择“是”
  4. 重启pi.

连接电线

覆盆子PI. TMP102板
3v3力量 vcc.
地面 GND.
SDA. SDA.
SCL. SCL.
地面 Add0.

测试连接

  1. 在PI上打开终端或使用SSH
  2. 输入以下内容:
    sudo i2cdetect -y 0
    1. 注意,如果收到错误消息“错误:无法打开文件`/ dev / i2c-0'或`/ dev / i2c / 0':没有这样的文件或目录”,请按照下列步骤操作:
      1. 将以下行添加到/ etc / modules:
        I2C_BCM2708
        i2c_dev.
      2. 重启pi.
      3. 或者通过键入以下内容在运行时加载模块:
        sudo modprobe i2c_bcm2708.
        sudo modprobe i2c_dev.
  3. 您应该在地址0x48处看到温度传感器。
  4. 运行以下Python脚本并验证报告的温度与使用该命令的温度相同:
    sudo python tmp102_read_temp.py.
    提示:使用SFTP.或USB闪存驱动器将脚本复制到PI上的文件夹
#!/ usr / bin / python导入smbus#0 = / dev / i2c-0#1 = / dev / i2c-1 i2c_bus = 0总线= smbus.smbus(i2c_bus)#7位地址(将左移以添加读写位)device_address = 0x48 #read temp register temp_reg_12bit = bus.read_word_data(device_address,0)temp_low =(temp_reg_12bit&0xff00)>> 8 temp_high =(temp_reg_12bit&0x00ff)#convert到datasheet temp的第6页=(((temp_high * 256)+ temp_low)>> 4)#handle负temps如果temp> 0x7ff:temp = temp-4096;temp_c = float(temp)* 0.0625 temp_f = temp_c * 9/5 + 32打印“temp =%3.1f c  - %3.1f f”%(temp_c,temp_f)

temp.zip.

将数据保存到Google

创建一张表

  1. 如果您没有Google帐户,请创建一个。
  2. 创建一个新工作表,并在A的列中创建一个标题,以获取TEMP的日期和B列。
  3. 删除除标题之外的所有额外行。PI将将新行附加到表格,因此您不希望数据远远下班。
    在此演示中使用的示例:https://docs.google.com/spreadsheets/d/1drifcrx7huyiemmd2c0a6k1pncz7zqxfanhoe0rc3pm/edit?usp=sharing.

设置身份验证

  1. 按照Google下面概述的步骤获取OAuth2凭据。按照步骤1-4,您只需要JSON文件。
  2. 搜索“client_id”的JSON文件。保存“client_id”之后的文本,它将在Python脚本中使用。
    “client_email”:“284377770079-0O2PSSK1B0QJJDDI6RVAG4H7I7RSL1ON@Developer.gserviceAccount.com.“,
    *注意:下载文件中的文本将是不同的
  3. 将JSON文件复制到Raspberry PI上的文件夹。
  4. 在上一节中创建的页面中,单击“文件”>“共享”。选择“可以编辑”并将上面的“客户端电子邮件”粘贴到电子邮件中。
  5. 在PI上,通过在命令行上键入以下内容来安装必要的软件:
    sudo apt-get安装python-pip
    sudo pip安装gspread oauth2client
    sudo apt-get安装python-openssl

测试连接

  1. 从较早的步骤中从与JSON文件相同的目录运行以下脚本。脚本使用从PI的PI为第一列的时间,因此请确保使用Raspi-Config正确设置时间。
    sudo python tmp102_google_sheet.py.
导入系统导入时间导入DateTime导入GSPRead导入OAuth2Client.Client Import JSON导入SMBus #Change基于您的设置#0 = / dev / i2c-0#1 = / dev / i2c-1 i2c_bus = 0 device_address = 0x48#凭据JSON文件名JSON_FILENAME ='temp logger-68e32d47588c.json'#gegroughtople stoped to gsheet_name ='temp_logging_demo'“”将tmp102数据写入google纸张“”“#load凭据来自JSON并打开电子表格编写json_key =json.load(open(json_filename))creds = oauth2client.client.signedjwtassertioncredentials(json_key ['client_email'],json_key ['private_key'],['https://spreadsheets.google.com/feeds'])client_inst = gspread。authorize(creds) gsheet = client_inst.open(GSHEET_NAME).sheet1 #initialize the i2c bus bus = smbus.SMBus(I2C_BUS) #Read the temp register temp_reg_12bit = bus.read_word_data(DEVICE_ADDRESS , 0 ) temp_low = (temp_reg_12bit & 0xff00) >> 8 temp_high = (temp_reg_12bit & 0x00ff) #convert to temp from page 6 of datasheet temp = ((( temp_high * 256 ) + temp_low) >> 4 ) #handle negative temps if temp > 0x7FF: temp = temp-4096; temp_C = float(temp) * 0.0625 temp_F = temp_C * 9/5+32 curr_time = datetime.datetime.now() print "Writing new row to %s: %s - %3.1f" % (GSHEET_NAME,curr_time,temp_F) #write a new row to the spreadsheet with the current time and temperature gsheet.append_row((curr_time, temp_F))

temp2.​​zip.

自动运行

  1. 您可以将脚本修改为循环,或者您可以使用aCron工作因此,I2C总线在不使用时被释放。这是如何设置Cron作业。
  2. 添加您希望使用Crontab运行脚本的频率:
    crontab -e.
  3. 添加以下行以运行每10mn:
    * / 10 * * * * cd / path / to / script && python / path / to / script >> / path / to / log 2>&1

在网页上绘制

  1. 通过单击文件>共享>获取Google表中的共享链接>获取可共享链接
  2. 下载.zip文件下面
  3. 将链接粘贴到下面的代码中“https://docs.google.com/spreadsheets/d/1drifcrx7huyiemmd2c0a6k1pncz7zqxfanhoe0rc3pm.“ 是。
  4. 打开网页,它应该如下所示。您也可以将JavaScript粘贴到现有的网页中。

key_pad_with_arduino.zip.zip.zip.zip.

为自己提供这个项目!得到bom。

2评论
  • F
    抚慰 2016年2月16日

    你好,

    我对Google电子表格有关JSON文件的问题。

    如何获得它并阅读它?

    亲切的问候。

    像。 回复
  • 丹尼尔UDC 2017年11月22日

    不行

    sudo python tmp102_google_sheet.py.

    回溯(最后最新呼叫):
    文件“tmp102_google_sheet.py”,第26行,在
    creds = oauth2client.client.signedjwtassertioncrentientials(json_key ['client_email'],
    AttributeError:'module'对象没有属性'signedjwtassertioncrientials'

    像。 回复