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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退