I’m trying to get my RPi to send temp data to Xively and I’m having a horrible time getting my code to work. This is the latest reponse I get when I run the python script…
JavaScript
x
9
1
Traceback (most recent call last):
2
File "xively1.py", line 11, in <module>
3
import xively # for Xively
4
File "/home/pi/xively/xively.py", line 11, in <module>
5
FEED_ID = os.environ["736915202"]
6
File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__
7
raise KeyError(key)
8
KeyError: '736915202'
9
Can someone explain to me what this means and what I need to do to correct? Thanks.
JavaScript
1
64
64
1
#!/usr/bin/env python
2
3
# This program reads two DS18B20 1-wire temperature sensors and sends the values to
4
# Xively https://xively.com/feeds/360495937
5
6
# tempF[0] is the reading from the outdoor tempersture sensor '28-0000059f6ece'
7
# tempF[1] is the reading from the indoor temperature sensor '28-0000059fa9ce'
8
outdoor = 0
9
indoor = 1
10
11
import xively # for Xively
12
import time
13
import datetime
14
import os # for 1-wire
15
import glob # for 1-wire
16
17
def read_temp_raw(): #a function that grabs the raw temperatures data from the sensors
18
f_1 = open(device_file[0], 'r') # first DS18B20
19
lines_1 = f_1.readlines()
20
f_1.close()
21
f_2 = open(device_file[1], 'r') # second DS18B20
22
lines_2 = f_2.readlines()
23
f_2.close()
24
return lines_1 + lines_2
25
26
def read_temp(): #a function that checks that the connections were good
27
lines = read_temp_raw()
28
while lines[0].strip()[-3:] != 'YES' or lines[2].strip()[-3:] != 'YES':
29
time.sleep(0.2)
30
lines = read_temp_raw()
31
equals_pos = lines[1].find('t='), lines[3].find('t=')
32
tempC = float(lines[1][equals_pos[0]+2:])/1000, float(lines[3] [equals_pos[1]+2:])/1000
33
tempF = round(tempC[0] * 9.0 / 5.0 + 32.0,1), round(tempC[1] * 9.0 / 5.0 + 32.0,1)
34
return tempF
35
def send_to_Xively(TempF):
36
XIVELY_FEED_ID = "736915202"
37
XIVELY_API_KEY = "QA6mqStQIqCFLOXtA4tzxiFpv8cqHNaYr1MFjRZdrphGwxYN"
38
#def send_to_Xively(TempF):
39
now = datetime.datetime.utcnow()
40
try:
41
print "Sending to Xively...",
42
xivelyapi = xively.XivelyAPIClient(XIVELY_API_KEY)
43
xivelyfeed = xivelyapi.feeds.get(XIVELY_FEED_ID)
44
xivelyfeed.datastreams = [
45
xively.Datastream(id='temp0', current_value=round(TempF[outdoor],1), at=now),
46
xively.Datastream(id='temp1', current_value=round(TempF[indoor],1), at=now)
47
]
48
xivelyfeed.update()
49
print "OK"
50
except requests.HTTPError as e:
51
print "HTTPError({0}): {1}".format(e.errno, e.strerror)
52
53
# this part for the 1-wire DS18S20
54
os.system('modprobe w1-gpio')
55
os.system('modprobe w1-therm')
56
57
#set up the locations of the sensors in the system
58
device_folder = glob.glob('/sys/bus/w1/devices/28*')
59
device_file = [device_folder[0] + '/w1_slave', device_folder[1] + '/w1_slave']
60
61
# main program
62
TempF = read_temp()
63
send_to_Xively(TempF)
64
Advertisement
Answer
The xively
module is expecting the environment variable “736915202” to be set. It’s not, so it’s throwing an exception.
That seems like a very strange environment variable to have set, though. Is /home/pi/xively/xively.py a third-party module, or did you write it? If you wrote that file, but you expected calling import xively
to import a third-party module, you should rename /home/pi/xively/xively.py to something else, and that will probably fix your problem.