Instance方法对象不可迭代(AirPi)(Python)

时间:2016-05-15 04:18:52

标签: python raspberry-pi

我得到错误:输出时出现异常:从https://github.com/haydnw/AirPi/blob/master/outputs/ubidots.py调试此AirPi代码时,'instancemethod'对象无法迭代

这假设将我的传感器数据上传到Ubidots服务器。

*我将正确的令牌和变量ID放在此AirPi的配置文件中。

requiredSpecificParams = ["token"]
optionalSpecificParams = ["showcost",
                  "ID-BMP085-temp",
                  "ID-BMP085-pres",
                  "ID-DHT22-hum",
                  "ID-DHT22-temp",
                  "ID-LDR",
                  "ID-TGS2600",
                  "ID-MiCS-2710",
                  "ID-MiCS-5525",
                  "ID-Microphone",
                  "ID-Raingauge"
                  ]

def __init__(self, config):
    super(Ubidots, self).__init__(config)        
    self.token = self.params["token"]
    if "showcost" in self.params:
        self.showcost = self.params["showcost"]
    else:
        self.showcost = False
    self.ubivariables = {}
    for key, value in self.params.iteritems():
        if key[:3] == "ID-":
            if value:
                self.ubivariables[key[3:]] = value

def output_data(self, datapoints, dummy):
    """Output data.
    Output data in the format stipulated by the plugin. Calibration
    is carried out first if required.
    Because this particular plugin (ubidots) does not show time, the
    third argument (normally called 'sampletime') is called 'dummy'
    to facilitate compliance with pylint.
    Args:
        self: self.
        datapoints: A dict containing the data to be output.
        dummy: datetime representing the time the sample was taken.
    Returns:
        boolean True if data successfully output to Ubidots; False if
            not
    """
    if self.params["calibration"]:
        datapoints = self.cal.calibrate(datapoints)
    payload = []
    for point in datapoints:
        for ubivariablename, ubivariableid in self.ubivariables.iteritems():
            if point["sensor"] == ubivariablename:
                if point["value"] is not None:
                    thisvalue = {}
                    thisvalue["variable"] = ubivariableid
                    thisvalue["value"] = point["value"]
                    payload.append(thisvalue)
                    break
    headers = {'Accept': 'application/json; indent=4', 'Content-Type': 'application/json', 'X-Auth-Token': self.token}
    url = "http://things.ubidots.com/api/v1.6/collections/values"
    req = None
    cost = 0
    try:
        req = requests.post(url, data=json.dumps(payload), headers=headers)
    except Exception, e:
        print("ERROR: Failed to contact the Ubidots service.")
        print("ERROR: " + str(e))
        return False
    for response in req.json:
        if response["status_code"] is not 201:
            print("ERROR: Ubidots responded with an error for one of the values.")
            return False
        else:
            cost += 1
    if self.showcost:
        print("Ubidots upload cost " + str(cost) + " dots.")
    return True

1 个答案:

答案 0 :(得分:1)

for response in req.json:

根据the documentationjson是一种方法,必须调用,所以这应该是:

for response in req.json():

将来,尽可能多地包含重现问题所需的代码,并包含带有回溯的完整错误消息是很有帮助的。