将多个DS18B20值上传到Xively Newbie Alert

时间:2013-08-11 09:10:46

标签: xively

我在使用此代码将频道ID和值上传到xively时遇到问题,有人可以就我出错的地方向我提供一些指导吗?

 temp = read_temp()
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"SolarFlow","current_value":temp_0}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"SolarReturn","current_value":temp_1}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"BufferIn","current_value":temp_2}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"BufferOut","current_value":temp_3}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"UFH_Flow","current_value":temp_4}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"GSHP_Flow","current_value":temp_5}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"GSHP_WellReturn","current_value":temp_6}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"GSHP_LoopsReturn","current_value":temp_7}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"HotWaterTank","current_value":temp_8}]})
    data = json.dumps({"version":"1.0.0", "datastreams":[{"id":"HotWaterPreHeatTank","current_value":temp_9}]})

    with open("temp.tmp", "w") as f:
        f.write(data)

    subprocess.call(['curl --request PUT --data-binary @temp.tmp --header "X-ApiKey: AR4soQUW2uG3t2pTVl3FVPPKZPfH66tJ0Z8SjBlEzhvFXpIK" http://api.cosm.com/v2/feeds/2002766727'], shell=True)

    os.remove("temp.tmp")

干杯

史蒂夫

1 个答案:

答案 0 :(得分:0)

实现此目标的最佳方法是使用Xively提供的official Python module

以下几个原因没有按照你刚才描述的方式进行:

  1. 官方库提供了一个简单的API
  2. 您无需关心数据格式究竟是什么
  3. 调用curl命令每隔一次发出一个HTTP请求是绝对低效的,因为操作系统需要时间来生成和新进程