duanhuang4306 2013-12-02 00:55
浏览 68
已采纳

使用url包更新ThingSpeak上的频道

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条回答 默认 最新

  • doubingjian2006 2013-12-02 18:52
    关注

    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:)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条