; Arc data logger ; Copyright 2009 Ken Shirriff ; http://arcfn.com ; ; logs temperature / light data from Arduino, provided over serial port ; Flush output automatically (declare 'explicit-flush nil) ; Timzeone for use by gettimestamp (= tz "America/Los_Angeles") ; return a timestamp of the form 2009-08-20 19:22 (def gettimestamp () (trim (tostring (system (string "TZ=" tz " date +'%m-%d-%Y %H:%M'"))))) ; Fetches time / sun data from serial port, formats, and writes to /tmp/data (def logdata () (w/appendfile outf "/tmp/data" (w/stdout outf (w/infile serial "/dev/ttyUSB0" (let oldtimestamp nil (while 1 (with ((degc sun) (tokens (readline serial)) timestamp (gettimestamp)) (when (isnt timestamp oldtimestamp) (let degf (+ 32 (* 9. (/ (coerce degc 'num) 5.))) (prn timestamp " " (num degf 2) " " sun) (= oldtimestamp timestamp)))))))))) ; Web page depends on macros in plug.arc ; Temperature page (defop temperature req (system "gnuplot < gnuplotcmd") (page (tag h1 (prn "Temperature and light: Arc + Arduino + ARM")) (gentag img src "/graph.png") (tag br) (prn "This web page is being served by the") (link "Arc language" "http://arcfn.com/doc/index.html") (pr " web server running on a ") (link "SheevaPlug" "http://arcfn.com/2009/06/arduino-sheevaplug-cool-hardware.html") (pr " plug computer.") (pr " For more details see ") (link "arcfn.com" "http://arcfn.com") (pr ".") (para) (link "View source code for this page" "/source-t"))) ; Start a background thread to log the data (new-bgthread 'logdata (fn () (logdata)) 0) ; /source-t will view the source (defop source-t req (page (tag h1 (prn "A very small Arc server")) (tag h2 (prn "Source of temperature.arc")) (link "Home" "/") (tag pre (w/infile inf "temperature.arc" (whilet ch (readc inf) (writec ch)))))) ; gnuplotcmd is the input to gnuplot to generate the graph. ; It is the following: ; set xdata time ; set format x '%m/%d' ; set xtic 86400 ; set timefmt '%m/%d/%Y %H:%M' ; set terminal png xeeeeee x000000 size 500, 500 ; set output 'static/graph.png' ; set xlabel 'Date' ; set ylabel 'deg F' ; set y2label 'Light (arbitrary units)' ; set title 'Temperature and Illumination' ; plot '/tmp/data' using 1:3 title 'Temperature' with lines, '/tmp/data' using 1:4 title 'Light' with lines axes x1y2