Close

Example application: a Basic Incubator Thermostat

A project log for W1209 Data Logging Thermostat

The W1209 thermostat is cheap, but it's about time that it learns new tricks!

thomasThomas 08/06/2017 at 16:410 Comments

The repository on GitHub now contains a very simple thermostat for a chicken incubator (apparently that's a very popular application for the W1209).

The core of the program is really simple:

\ background task with temperature control
: btask ( -- )
  measure            \ measure temperature
  DUP .0 CR          \ print it
  TEMPLIMIT < OUT!   \ keep temperature stable
;

The word btask runs in the 5ms background cycle. Forth transfers most data through the data stack (just like a RPN calculator), which is great for concatenating commands! measure performs sensor reading and linearization, leaving a temperature value on the stack (in units of 0.1 °C). DUP duplicates the value for .0 which prints it as ##.#, ###, or -##. CR is just a newline. For the thermostat function the remaining value on stack needs to be compared with TEMPLIMIT (e.g. 375 for 37.5°C). If the 1st value on the stack is smaller than the 2nd, < results in -1 (0b1111111111111111), or else 0. OUT! copies "all ones" or "all zeros" to all the available outputs (which happens to be just one relay). That's all, no condition structure (e.g. IF..THEN) was required. Check out incubator.fs, measure.fs, and inter.fs on GitHub. The whole program consists of just 113 lines of code including linearization, two level filtering, and formatted output.

Of course, one would should probably add some kind hysteresis. But then again, why not go for a PID controller? Yes, that's possible with a relay as the control output.

As for the "definition of done" of the initial user story:

Data logging will be next. Of course without a RTC "10 minutes" will be "about 10 minutes", but I'm sure that it won't be too difficult to work around this on the PC side.

Discussions