首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Raspberry tempdata to Thingspeak

Raspberry tempdata to Thingspeak
EN

Stack Overflow用户
提问于 2016-01-06 17:56:19
回答 2查看 1.6K关注 0票数 0

我正在尝试将我的温度测量结果从连接到树莓派的DS18B20发送到Thingspeak。我已经在Thingspeak上为这个项目创建了一个频道。

http://nergiza.com/como-hacer-un-registrador-de-temperatura-online-con-raspberry-pi/

我正在使用这个链接中的代码。

代码语言:javascript
复制
# Registrador de temperatura Nergiza.com
# python
import httplib, urllib, os, glob, time

os.system('modprobe w1-gpio')  
os.system('modprobe w1-therm')  
base_dir = '/sys/bus/w1/devices/'  
device_folder = glob.glob(base_dir + '28*')[0]  
device_file = device_folder + '/w1_slave'  

def read_temp_raw():  
    f = open(device_file, 'r')  
    lines = f.readlines()  
    f.close()  
    return lines

def read_temp():  
    lines = read_temp_raw()  
    while lines[0].strip()[-3:] != 'YES':  
        time.sleep(0.2)  
        lines = read_temp_raw()  
    equals_pos = lines[1].find('t=')  
    if equals_pos != -1:  
        temp_string = lines[1][equals_pos+2:]  
        temp_c = float(temp_string) / 1000.0  
        return temp_c  

temperatura = read_temp()  
params = urllib.urlencode({'field1': temperatura, 'key':'Pon_aquí_tu_key'})  
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":  
    "text/plain"}  
conn = httplib.HTTPConnection("api.thingspeak.com:80")  
conn.request("POST", "/update", params, headers)  
response = conn.getresponse()  
print response.status, response.reason  
data = response.read()  
conn.close()

我已经将api_key的密钥从Thingspeak更改了。当我尝试在终端中运行此命令时,它返回:

代码语言:javascript
复制
400 Bad Request  

400 Bad Request是一种糟糕的语法。

Thingspeak每15秒接收一次数据是有限制的。

但即使在底部添加time.sleep(16),这也不会改变任何事情。

我正在使用USB调制解调器/记忆棒。

有人有什么建议吗?

EN

回答 2

Stack Overflow用户

发布于 2017-04-13 17:40:05

我正在使用这个简单的代码从树莓派发送数据到thingspeak,这对我来说工作得很好,.try this…

代码语言:javascript
复制
import sys
import RPi.GPIO as GPIO
import os
from time import sleep
import urllib2

DEBUG = 1
# Setup the pins we are connect to

#CONNECT OUT PIN 
co2pin = 16

#Setup API and delay
myAPI = ""
myDelay = 16 #how many seconds between posting data

GPIO.setmode(GPIO.BCM)
GPIO.setup(co2pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


def Cotime(co2pin):
    co = 0  
    if (GPIO.input(co2pin) == True):
    co += 1
    return (str(co))


if __name__ == '__main__':
    baseURL = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
    print baseURL

    while True:
    try:
        co = Cotime(co2pin)
        f = urllib2.urlopen(baseURL +"&field3=%s" % (co))
        print f.read()
        print 'Value Detected' +str(co)
        f.close()
        sleep(int(myDelay))
    except:
        print 'exiting.'
        break
票数 2
EN

Stack Overflow用户

发布于 2016-01-06 21:36:45

这是一个新手犯的错误。

O在API_Key中更改为0。

脚本可以完美地用于DS18B20和thingspeak.com

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34630184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档