device.ino 4.55 KB
#include "device.h"

void insertSequence(int device, int start_value, int end_value, double duration, struct action *action) {
  struct sequence *ptr = (struct sequence*) malloc(sizeof(struct sequence));
  ptr->device = device;
  ptr->start_value = start_value;
  ptr->end_value = end_value;
  ptr->action = action;
  ptr->next = NULL;
  /* Serial.println("is0"); */

  if ( sequence_tail == NULL ) {
    /* Serial.println("is1"); */
    sequence_head = ptr;
    set_relay(device, start_value);
    ptr->end = action->start + duration;
  } else {
    /* Serial.println("is2"); */
    sequence_tail->next = ptr;
    ptr->start = sequence_tail->end;
    ptr->end = sequence_tail->end + duration;
  }
  /* Serial.println("is3"); */
  sequence_tail=ptr;
}

void loop_device() {
  if ( sequence_tail != NULL ) { // there's something to do
    monitor_sequence_duration();
    next_sequence_step();
  }
}

void next_sequence_step() {
  unsigned int ut = time(NULL);
  if ( ut >= sequence_head->end ) {
    // set relay to end values of current step and start values of next step. afterwards remove the completed step;
    if ( sequence_head->start_value != sequence_head->end_value ) {
      set_relay(sequence_head->device, sequence_head->end_value);
    }
    struct sequence *tmp = (struct sequence*) malloc(sizeof(struct sequence));
    tmp = sequence_head;
    sequence_head = tmp->next;
    if ( sequence_head != NULL ) {
      set_relay(sequence_head->device, sequence_head->start_value);
      next_sequence_step(); // in case the sequence duration is 0
    } else {
      sequence_tail = NULL;
    }
    free(tmp);
  }
}

void monitor_sequence_duration() {
  String pre = "home/compost/device/";
  int device = sequence_head->device;
  String duration(pre + device + "/duration");

  unsigned int ut = time(NULL);
  double remaining = sequence_head->end - ut;
  String remainingStr(remaining);

  /* Serial.println("--------------- Duration"); */
  /* Serial.println(duration); */
  /* Serial.println(remainingStr); */
  /* Serial.println("---------------"); */

  mqttClient.publish(duration, remainingStr );
}

int fill_start(struct action *ptr) {
  if ( sequence_head == NULL ) {
    insertSequence( dst_valve_rotate,      0, 0, 0, ptr );
    insertSequence( dst_valve_watersource, 1, 1, 0, ptr );
    insertSequence( src_valve_pump,        0, 0, 0, ptr );
    insertSequence( peristaltic_reverse,   1, 0, 10, ptr ); // fill hose for 10 seconds
    insertSequence( src_valve_pump,        1, 0, ptr->data, ptr ); // let gravity do its work
    insertSequence( peristaltic,           1, 0, 10, ptr ); // vent hose for 10 seconds
    insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
    return 1;
  } else {
    return 0;
  }
}

int rotate_start(struct action *ptr) {
  if ( sequence_head == NULL ) {
    insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
    insertSequence( dst_valve_rotate,      1, 1, 0, ptr );
    insertSequence( src_valve_pump,        1, 1, 0, ptr );
    insertSequence( dive,                  1, 0, ptr->data, ptr );
    insertSequence( src_valve_pump,        0, 0, 0, ptr );
    insertSequence( dst_valve_rotate,      0, 0, 0, ptr );
    return 1;
  } else {
    return 0;
  }
}

int rotate_start(struct button *ptr){
  return 1;
}

int fertilize_start(struct action *ptr) {
  if ( sequence_head == NULL ) {
    insertSequence( dst_valve_watersource, 1, 1, 0, ptr );
    insertSequence( dst_valve_rotate,      0, 0, 0, ptr );
    insertSequence( src_valve_pump,        1, 1, 0, ptr );
    insertSequence( dive,                  1, 0, ptr->data, ptr );
    insertSequence( src_valve_pump,        0, 0, 0, ptr );
    insertSequence( dst_valve_rotate,      0, 0, 0, ptr );
    insertSequence( dst_valve_watersource, 0, 0, 0, ptr );
    return 1;
  } else {
    return 0;
  }
}



void set_relay(int device, int value) {

  String pre = "home/compost/device/";
  /* int device = sequence_head->device; */
  String state(pre + device + "/state");
  String valueStr( value );
  mqttClient.publish(state, valueStr );

  pcf8574.digitalWrite(device,!value); // sink; 1 == relay off (nc), 0 = relay on (no)

  /* Serial.println("--------------- Relay"); */
  /* Serial.println(state); */
  /* Serial.println(valueStr); */
  /* Serial.println("---------------"); */
  /* int pin = 1; */

  /* while ( pin < 7 ) { */
  /*   if (    device      == pin */
  /*        /\* || step->src_valve == pin *\/ */
  /*        /\* || step->dst_valve == pin *\/ */
  /*        ) { */
  /*     /\* pcf8574.digitalWrite(pin,0); *\/ */
  /*   } else { */
  /*     /\* pcf8574.digitalWrite(pin,1); *\/ */
  /*   } */
  /*   pin++; */
  /* } */
}