Raspberry Pi Temperature Sensor – V2

In my previous post I showed you how incapable I was (still am) at programming in Python. I created a simple programme to read the temperature from a set of DS18B20 sensors and to store it away in a file.

The programme worked (after a fashion) but only when running under the emulator (IDLE) and not when called directly from the command line. The reason I wanted to be able to call it from the command line was because I think its the only way that the programme can automatically start on power-up which is something I want it to be able to do.

The issue (as far as I can make out) is the fact that I was trying to use multi-threaded code without fully understanding the interaction between different threads. Its not fully clear what was wrong, but as far as I can tell printing from two different threads requires that the threads are not both trying to print at the same time.

Anyway, multi-threaded code assumes that there is some benefit and (as my eventual target will be the Pi Zero) there’s no benefit in multi-threading.

My new code therefore, is single threaded. I’ve also modified it so that I don’t need to hard code the identities of the DS18B20 sensors, the code now loops around for all of the sensors.

I’ve maintained my file writing process to store the information and only stored the temperature when it has changed. However, the way in which I store the information is now different. I’ve used a Python List with each element representing one of the active sensors. The temperature is read using the normal process and then if it is different to the temperature stored, the list is updated and then printed and saved to the file. Obviously this means that the stored data will be larger than if I used a separate file for each sensor as the temperature for each sensor will be stored every time one of them changes. However, it makes it a lot simpler to handle the resulting data as a simple X-Y scatter plot can be created.

My new code is here sttemp and the temperatures measured are 160509 – Temperature Output. The two graphs show the temperature outside (the blue line) and the temperature inside (the green line). The spike in the temperature inside is caused by the fact that the inside sensor was on my desk in the sunshine so the temperature rose very quickly. However, I moved it out of the sun and it settled back down.

What else to do?

There are yet more modifications I plan to make:

  1. To add more sensors. At the moment the two sensors are about 3 ft apart. I’ve got more sensors and I’m going to put those out in my polytunnel which means I need a few meters of wire.
  2. Rather than having a fixed size list to store the temperatures, I will dynamically create the list based upon the number of sensors detected;
  3. To record a daily max and min for each of the sensors;
  4. Perhaps to be able to select how accurately the temperature is recorded, at the moment its fixed at .1C and my experiment is to see whether a butt of water in the polytunnel smooths the temperature by absorbing heat during the day and releasing it at night. This I will do be finding out if the temperature of the water in the butt increases and decreases during the day and night.

So, I’m still learning, if you’re interested please comment.

About PelicanPlants

Growing tomatoes and other vegetables in a greenhouse and at an allotment.
This entry was posted in raspberry pi and tagged , , , . Bookmark the permalink.