CommonGarden / Grow-IoT

Software packages for smart growing environments.
http://commongarden.org/
Other
25 stars 6 forks source link

README exampe device doesn't work. #443

Closed JakeHartnell closed 6 years ago

JakeHartnell commented 6 years ago

Documentation is out of date! Let's fix it. To do:

const Grow = require('Grow.js');

  // Create a new grow instance and connect to https://grow.commongarden.org
  var grow = new Grow({
    uuid: 'test_calvin',
    token: '123456',

    component: 'Thermostat',

    // Properties can be updated by the API
    properties: {
      state: 'off',
      growfile: {
        temperature: {
          min: 22,
          ideal: 27,
          max: 29,
          pid: {
            k_p: 300,
            k_i: 0,
            k_d: 200,
            dt: 1
          }
        },
      },
      interval: 1000,
      threshold: 0.2,
    },

    start: function () {
      var interval = this.get('interval');
      data_interval = setInterval(()=> {
        this.temp_data();
      }, interval);

      var growfile = this.get('growfile');
      this.registerTargets(growfile);

      var threshold = this.get('threshold');

      // Listen for correction events from our PID controller
      this.on('correction', (key, correction) => {
        console.log(key, correction);
        if (Math.abs(correction) > threshold) {
          if (key === 'temperature') {
            if (correction > 0) {
              this.call('turn_on');
            } else {
              this.call('turn_off');
            }
          }
        }
      });
    },

    stop: function () {
      clearInterval(data_interval);
      this.removeAllListeners();
    },

    restart: function () {
      this.stop();
      this.removeTargets();
      this.start();
    },

    // Note, there are probably more elegant ways of handling subthing methods.
    turn_on: function () {
      console.log('Heater on');
      this.set('state', 'on');
    },

    turn_off: function () {
      console.log('Heater off');
      this.set('state', 'off');
    },

    temp_data: function () {
      let temp = Math.random() * 10;
      grow.emit('temperature', temp);

      console.log('temperature: ' + temp);
    }
  }, 'data.json');

  grow.connect();