我在输出过程中收到ERROR:Exception:从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

最佳答案

for response in req.json:


根据the documentationjson是一个方法,必须调用,因此应为:

for response in req.json():


将来,最好包含尽可能多的代码以重现该问题,并包括完整的错误消息和回溯信息。

关于python - Instancemethod对象不可迭代(AirPi)(Python),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37234517/

10-08 22:51