# Reading Temperature Sensors in the Wolfram Language on the RPi

GROUPS:
4 years ago
10 Replies
 Sam Carrettie 2 Votes 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.
4 years ago
4 years ago
 Rich Points 1 Vote I just discovered Wolfram Alpha and Mathmatica yesterday and I'm excited about the possibilities.  I've been doing something very simmilar with DS18b20s on my Raspberry Pi with a LAMP server setup, you can see it here http://richpoints.x64.me  I'm currently using a service called Xively to graph the data which has some limitations, ideally I'd like to have multiple lines on one graph as you guys have detailed here.Being new to Wolfram I have some basic questions.  Wolfram scripts end in .wl? Is this how you'd execute a script from the command line? $wolfram file_name.wlCan I call Wolfram scripts from cron?How does Wolfram integrate with a LAMP server? Is a LAMP server still neccesary?How do I embed graphs into an html page?What would the code look like from Emerson's example in it's entirety? Just copy and past the code blocks?I'm finding it hard to find Wolfram Alpha 101 tutorials any suggestions?Thanks!Rich Answer 4 years ago  Emerson Willard 3 Votes (1) Yes but I used a .m file.(2)$ wolfram -script temperaturescript.m(3) Yes, or on startup using /etc/rc.local.(4+5) only the Wolfram language is used.(6) You could embed a cdf in a web page,however this would require users to have plugins, or wait for the arrival of the cloud funtionality as detailed here: http://blog.wolfram.com/2014/02/24/starting-to-demo-the-wolfram-language/(7) This is the content of the .m file which can be run as a script. The code now automatically determines the number of sensors connected. The last line of code is necessary to stop the kernel from exiting after all the commands have been read in, which is a trap for those used to working in the front end only. It is easiest to develop code interactively using a Notebook(front end) interface or the Workbench, and then run it as a script once you have it working. Currently I have three sensors connected to a Beehive to measure temperature regulation, I will post on this shortly. 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]];  upload[filesourcepath_, savedir_String] :=    Run["/home/pi/Dropbox-Uploader/dropbox_uploader.sh upload " <>     filesourcepath <> " " <>     FileNameJoin[{savedir, FileNameTake[filesourcepath]}]];    file = "/home/pi/Desktop/temperaturedata.txt"Run["sudo modprobe w1-gpio"];Run["sudo modprobe w1-therm"];Print[file];Print[devicefolders[]];task1 = CreateScheduledTask[upload[file, "temperaturexperiment"], 120,AbsoluteTime[] + 180];task2 = CreateScheduledTask[PutAppend[{DateList[AbsoluteTime[]],Sequence@@Map[temperature[devicefile[#]]&,Range[lengthdevicefolder] ]}, file], 5];StartScheduledTask[{task1, task2}];While[Length[ScheduledTasks[]]>0,Pause[1]]; (8) I would Install Mathematica on the Raspberry Pi and try any of the above code.
4 years ago
 Yoshihiro Sato 2 Votes Hello, Thank you.I have succeeded in measuring the cooling of hot water using Vernier and LM73 sensor, which I described in a bit more detail here.http://mmays.hatenablog.com/entry/2014/03/07/155351Yoshihiro Sato
4 years ago
 Ken Levasseur 1 Vote 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?
3 years ago
 Ken Levasseur 1 Vote I figured out that all I had to do is disable the Device Tree using raspi-config / Advanced Options. My only concern is that I read that this may not be a reasonable option in the future. I would hope that more guidance in connecting devices using the Device Tree will appear at some point!
 Emerson Willard 1 Vote 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"]