Jump to content

Welcome, Guest!

Kayla.Jacob

HELP!!!! Raspberry Pi Temperature Sensor

Recommended Posts

We are doing a project for our intro to programming class. We are making a temperature sensor that will send our data to sen.se.  When we run our program on our raspberry pi this comes up:

 

Traceback (most recent call last) :

           File "/home/pi/Desktop/1.py", line 14, in <module>

                    device_folder = glob.glob(base_dir + '28*')[0]

IndexError: list index out of range

 

What does this mean? and how can we fix it? Thank you :)

temperature code.txt

Share this post


Link to post
Share on other sites

You're trying to access glob.glob (...) as a list.  It's a function which returns a list, but it isn't actually a list.  When glob returns, the variable device_folder will contain a list with one item, the path to the sensor.  Remove the [0] from after the call to glob.  On the next line, you'll probably need to put something like this:

device_file = device_folder[0]+ '/w1_slave'

device_file will contain a string with the complete path of the sensor.  Check out http://raspberrywebserver.com/gpio/connecting-a-temperature-sensor-to-gpio.html

Share this post


Link to post
Share on other sites

We to change that this and the same error came up..

 

Traceback (most recent call last):

       File "/home/pi/Desktop/1.py", line 15, in <module>

               device_file= device_folder[0] + '/w1_slave'

IndexError: list index out of range

Share this post


Link to post
Share on other sites

Add a line before the one above:

print device_folder

This will show whether glob.glob from the previous line returned a valid result.  If it doesn't, then you should look at the arguments passed to glob.glob.  What's the value of base_dir?  It should be something like this:

/sys/bus/w1/devices/

Check that there is a '/' at the end of the string.

Share this post


Link to post
Share on other sites

So glob.glob isn't returning a valid result.  What's the value of base_dir?  I'm not sure what environment you're using, so the simplest way to find out the value of base_dir is to add a line to print it out like you did with the device_folder variable.  Before the call to glob.glob base_dir needs to be set to this path: /sys/bus/w1/devices

 

Edit: BTW, did you enable the w1 kernel modules?  Did you check that the folder /sys/bus/w1/devices/ actually exists and contains a folder starting with '28'?

Share this post


Link to post
Share on other sites

I'm not sure how base_dir is meant to be set in your code, but when it does get set, it should be equal to "/sys/bus/w1/devices/".  You can check if base_dir is holding this string by adding 

print base_dir

on the line before glob.glob.  (If you're using a debugger, there may be some other way of checking the value of variables, but I just debug by printing variables to see what's in them.)

 

If you run your script again and base_dir has the incorrect value, you could try setting it like this:

base_dir="/sys/bus/w1/devices/"

Again, this would go on the line before glob.glob.

Share this post


Link to post
Share on other sites

We found http://www.devicehive.com/samples/python-and-raspberry-pi-temperature-sensor that showed us how to enable the w1 kernel module.  We now have the w1 folder and stil came up with the same IndexError.  

 

7.Find your sensor: ls /sys/bus/w1/devices/ it should look like 28-00000393268a

In our /sys/bus/w1/devices/ we cannot find the 28-00000393268a .  Instead the folder contains w1_bus_master1

Share this post


Link to post
Share on other sites

The fact that you can see w1_busmaster1 is a good sign - the modules appear to be loaded correctly.  Is the sensor connected at the moment?  If not, hook it up and run ls again.  The folder starting with '28' is only visible when the sensor's connected.  If it is connected, check your wiring for a bad connection.  

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

About Us

Bringing you the latest Raspberry Pi news, tutorials and user forum.

The Fruity Computer

×