Message Boards Message Boards

GROUPS:

Reading Temperature Sensors in the Wolfram Language on the RPi

Posted 5 years ago
30389 Views
|
10 Replies
|
30 Total Likes
|
These sensors are pretty cool--they are cheap to buy and surprisingly sensitive to small changes in temperature. Here's a first attempt I made to interact with the sensors in the Wolfram Language.

For this setup I used DS18B20 temperature sensors and hooked them up to the Raspberry Pi breadboard according to Adafruit's setup guide. The board should look like the following diagram (make sure the sensor is hooked up to a 3.3V pin--not a 5V pin or you could fry the sensor):


Once hooked up and connected to your Pi, run the following commands in the terminal:

sudo modprobe w1-gpio
sudo modprobe w1-therm

The temperatures are read from the sensor by "reading" the file that's created in the devices directory. You can locate the file with the following commands:

cd /sys/bus/w1/devices
ls

This will show you the contents of your devices folder, where there should be a file titled 28-xxxx, where the xxxx is the serial number unique to your sensor. Once you've got that number, enter:

cd 28-xxxx (the xxxx should be replaced with the serial number unique to your sensor)
cat w1_slave

Two lines of data should return back to you--if the first line ends with "YES" then the 5-digit number at the end of the second line is the temperature, to be read as xx.xxx degrees Celsius.

And now that we know that the temperature sensor is working, and we know how to find it, we can copy the file path and import it using the Wolfram Language.

Import["/sys/bus/w1/devices/28-000004fe0343/w1_slave"]

Since this still returns a really long string of data that we don't need, we can single out the temperature and then convert the string into a computable expression.

temp:=N[ToExpression[StringTake[Import["/sys/bus/w1/devices/28-000004fe0343/w1_slave"],-5]]/1000]

So now when we read the file, we just get the temperature back!

temp
(*22.312*)

For kicks, I set up a scheduled task to plot the ambient temperature of my office every 60 seconds for 6 hours. Unsurprisingly, the temperature only fluctuated a few tenths of a degree...!

t={}
RunScheduledTask[(deg=temp;AppendTo[t,deg]),{60,360}];
Dynamic[ListLinePlot[t,Joined->True,PlotRange->Automatic]]

And here's what the graph looked like after a little bit of time--it truly is a sensitive device (the "large" dip down to 22.1 was me touching the sensor with my cold hands!):

Any suggestions for what to do next?
10 Replies
This pretty cool start, Allison, thanks for sharing! Do you mean "what to do next" with Temperature Sensors or in general with R-Pi? I can give an idea for the former. If some folks here are physicists (and who isn't honestly at least a little bit?) and they are not afraid to get their feet wet in literal and figurative senses, there is famous simple experiment:

Calorimetry: Specific Heat Capacity of Copper

*Copper* could be any given metal. One would need some basic lab equipment and will use R-Pi as a thermometer in that setup. One surely needs some waterproof coating for sensor - simple nail polish will do I think.

The advantage of the R-Pi thermometer is that it shows record of temperature and makes it easier to judge when thermodynamic equilibrium is reached. Usually kids just have to eye-estimate when the temperature stopped changing on thermometer.

But it is not clear if one can run RunScheduledTask fast enough for this experiment.

I have been using the DS18B20 temperature sensor on a Raspberry Pi B for a while, but tried to do the same on a Raspberry Pi 2 and the 28-xxxx device file doesn't appear. I think I've tracked down the problem. It seems that there is a Device Tree described at https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README that allows you to enable the hardware to read the temp. sensor. I'm not certain what the setting would be. Any idea what it is?

Posted 3 years ago

Hi, I am trying to upload temperature and humidity data from DHT 22 sensor connected to Raspberry pi into the Wolfram Datadrop. I read the GPIO documentation. I understand GPIO DeviceRead command gives the value one or zero.I used the following

DeviceRead["GPIO",4]

I am getting 4-> -1 i want to read the temperature and humidity value from DHT22 sensor.I am not sure how to achieve it. Please guide me.

The following will work with the DS18B20 but you will have to consult the DHT22 specifications in order to get your digital signal. This code saves the temperature along with the memory in use to a new Databin. You will need to enter your password and appropriate Wolfram ID.

$HistoryLength=0
$pollinginterval=60;
initialmemory=MemoryInUse[];
CloudConnect[$WolframID, "yourpassword"];
bin=CreateDatabin[];

devicefolders[]:=FileNames["28-*", {"/sys/bus/w1/devices"}];
devicefolder[i_] := devicefolders[][[i]];
lengthdevicefolder:=Length[devicefolders[]];
devicefile[i_] := FileNameJoin[{devicefolder[i], "w1_slave"}];
read[i_] := ReadList[devicefile[i], String];
temperature[devicefile_String] := Flatten[StringCases[ReadList[devicefile, String], 
    "t=" ~~ x___ :> ToExpression[x]/1000., 1]][[1]];

Run["sudo modprobe w1-gpio"];
Run["sudo modprobe w1-therm"];
Print[devicefolders[]];
task = CreateScheduledTask[DatabinAdd[bin,{
Sequence@@Join[{"Memory"->MemoryInUse[]-initialmemory},
Map[(StringJoin["Temperature",ToString@#]->temperature[devicefile[#]])&,Range[lengthdevicefolder] ]
]
}], $pollinginterval];
StartScheduledTask[{task}];
While[Length[ScheduledTasks[]]>0,Pause[1]];

If you are using a single sensor then you could also explicitly define the data semantics of your Databin in the following way.

  SetOptions[bin,  "Interpretation" -> {"Memory" ->  Restricted["StructuredQuantity", "Bytes"], 
  "Temperature1" -> Restricted["StructuredQuantity", "DegreesCelsius"]}]

You can then deploy a report.

CloudDeploy[
 FormFunction[{{"initialchoice", "Databin Key"} -> Keys@Databin["42ffdT3r"]},
  Column[{StringTemplate[ "The choice for the Databin Key was `choice`"][<| "choice" -> #initialchoice|>],
     data = Databin["42ffdT3r"];
     DateListPlot@TimeSeries@data["Values"][#initialchoice]}] &,  "CloudCDF"]
 , Permissions -> "Public"]

Is it possible to get the readings of the DS18B20 through the Mathematica custom Arduino Sketch?

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract