使用url包更新ThingSpeak上的通道

huangapple go评论98阅读模式
英文:

Using the url package to update a channel on ThingSpeak

问题

我正在使用以下代码部分在树莓派上连续上传采样的[温度和湿度]值到ThingSpeak的一个通道上[https://www.thingspeak.com/]。问题是只有第一个值被上传,而其余的值被忽略了。我做错了什么?values.Set创建了一个之前未创建的键,并为其分配了一个初始值,并且可以替换每个下一个值而没有任何问题。为什么它们没有被上传?http.PostForm有什么问题吗?

//导入包

type Data struct {
    Temperature int
    Humidity    int
}

//....

var data Data

func httpPost(values url.Values) {
    values.Set("field1", fmt.Sprint(data.Temperature))
    values.Set("field2", fmt.Sprint(data.Humidity))
    log.Println(values)

    _, err := http.PostForm("http://api.thingspeak.com/update", values)
    if err != nil {
        log.Printf("error posting values to thingspeak: %s", err)
    }
    return
}

//....

func main() {
    dataPool := []Data{{28, 41}, {24, 43}, {27, 42}, {21, 40}}
    values := make(url.Values)
    values.Set("key", "Write API Key")

    for _, value := range dataPool {
        data = value

        //ThingSpeak更新
        httpPost(values)

        time.Sleep(2 * time.Second)
    } 
}

我对网络了解不多,但现在我通过一个连接到基于集线器的卫星互联网连接的以太网端口访问互联网[我猜不是普通的路由器连接?],所以可能是一个管理问题[宿舍,呃]。我应该向我的网络管理员咨询,但无论如何,我在这里提出这个问题。欢迎任何反馈。

英文:

I am using the following part of code on a Raspberry Pi in order to continuously upload sampled [temperature & humidity] values onto a channel at ThingSpeak [https://www.thingspeak.com/]. The problem is that only the first value gets uploaded while the rest are ignored. What am i doing wrong? values.Set creates a not-previously created key, assigns to it a first value and replaces every next value without any problem. Why don't they get uploaded? Is there anything wrong with http.PostForm?

//imports

type Data struct {
    Temperature int
    Humidity    int
}

//....

var data Data

func httpPost(values url.Values) {
    values.Set("field1", fmt.Sprint(data.Temperature))
    values.Set("field2", fmt.Sprint(data.Humidity))
    log.Println(values)

    _, err := http.PostForm("http://api.thingspeak.com/update", values)
    if err != nil {
        log.Printf("error posting values to thingspeak: %s", err)
    }
    return
}

//....

func main() {
    dataPool := []Data{{28, 41}, {24, 43}, {27, 42}, {21, 40}}
    values := make(url.Values)
    values.Set("key", "Write API Key")

    for _, value := range dataPool {
        data = value

        //ThingSpeak update
        httpPost(values)

        time.Sleep(2 * time.Second)
    } 
}

I don't know much off networks but right now i am accessing internet via an Ethernet port that connects on a hub-based satellite internet connection [i guess not a normal router connection?] so maybe it's a administrative problem [dormitories, duh]. I should ask my network administrator about that but in any case i am putting this out here. Any feedback are welcome.

答案1

得分: 2

好的,我明白了。以下是翻译好的内容:

好的,我找到了问题所在。显然,ThingSpeak 的 API 速率限制是每 15 秒一次(http://community.thingspeak.com/documentation/api/),而我尝试每 2 秒向通道发送数据。我将 2 改为 20,现在一切都正常工作了。感谢您的评论。

教训是:下次要仔细阅读文档 使用url包更新ThingSpeak上的通道

英文:

Ok, i found what the problem was. Apparently ThingSpeak has an API Rate Limit of 15 seconds (http://community.thingspeak.com/documentation/api/) while i was trying to post on the channel every 2 seconds. I set 2 to 20 and everything works like a charm now. Thanks for your comments.

Moral lesson: next time read the documentation thoroughly:)

huangapple
  • 本文由 发表于 2013年12月2日 08:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/20319246.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定