#lang racket ;; Franco 20131029 ;; ***** WARNING WARNING WARNING ****** ;; The procedure (parse-packet) is not correct. ;; It should take into account the escape sequences before converting the data. ;; to set baudrate (require racket/system) ;; to print the date and time (require racket/date) (require racket/format) ;; Serial tinyOS: http://www.tinyos.net/tinyos-2.x/doc/html/tep113.html (define sop #x7e) ;; start of packet (define eop #x7e) ;; end of packet ;; #x7e is escaped as #x7d #x5e ;; #x7d is escaped as #x7d #x5d (define esc1 #x5d) ;; first escape (define esc2 #x5e) ;; second escape (define eop-rec 0) ;; end-of-packet received (define packet null) (define in null) (define (parse-packet packet) ;; Very easy: ID is at position 10 and 11 ;; Humidity position 12 and 13 ;; Temperature position 14 and 15 (define id (+ (* (list-ref packet 10) 256) (list-ref packet 11))) (define rh (+ (* (list-ref packet 12) 256) (list-ref packet 13))) (define temp (+ (* (list-ref packet 14) 256) (list-ref packet 15))) (define cur_date (current-date)) ;; Converting RH and temp according to datasheet for sensirion sht11 (set! rh (+ (* rh 0.0367) -2.0468 (- (* 0.00000159 rh rh)))) (set! temp (+ (* temp 0.01) -39.7)) ;; Just print the date leaving a space at the end (define month (~r (date-month cur_date) #:base 10 #:min-width 2 #:pad-string "0")) (define day (~r (date-day cur_date) #:base 10 #:min-width 2 #:pad-string "0")) (define hour (~r (date-hour cur_date) #:base 10 #:min-width 2 #:pad-string "0")) (define minute (~r (date-minute cur_date) #:base 10 #:min-width 2 #:pad-string "0")) (define second (~r (date-second cur_date) #:base 10 #:min-width 2 #:pad-string "0")) (printf "~a/~a/~a ~a:~a:~a " (date-year cur_date) month day hour minute second) (printf "~a ~a ~a\n" id rh temp) ;; (print-packet packet) (flush-output) ) (define (read-loop) (process-input (read-byte in)) (read-loop)) (define (process-input data) (set! packet (cons data packet)) (cond ( (and (= eop-rec 0) (= data eop)) (set! eop-rec 1) ) ( (and (= eop-rec 1) (= data sop)) (parse-packet (reverse packet)) (set! eop-rec 0) (set! packet null) ) ) ) (define (open-port port-name) (printf "Racket-TinyOS starting... please wait\n") (set! in (open-input-file port-name #:mode 'binary)) (sleep 3) (printf "Everything ready, your packets should be coming soon...\n") (if (system (string-append "stty -F " port-name " 115200 cs8 cread clocal")) (begin (read-loop) ) (error "Could not connect to " port-name))) (define (close-connection) ;; additional cleanup here! (printf "Thank you for using Racket-tinyOS") ) (open-port "/dev/ttyUSB0")