Function takes too long and times out

For a school project, I’m connecting my spark core to a Senseo coffee machine using a relay switch and temperature sensor. The Particle sends a command to the coffee maker to turn on the device and waits a while for the coffee maker to warm up. If the coffee maker is hot enough, it will send a command to pour the coffee out of the machine.
The problem I am facing is that when I run the function, it will give a time out after 30 seconds. I still want the Particle core to send something back when your coffee is ready. How can I do this?

// This #include statement was automatically added by the Spark IDE.
#include "elapsedMillis/elapsedMillis.h"

// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"

//#define DHTPIN D2    // Digital pin D2
//#define DHTTYPE DHT11 
#define ONE_WIRE_BUS D2


OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
elapsedMillis timeElapsed;
unsigned int interval = 90000; // timer na een minuut en een half
 

int OnOff = D6; // IN4 en bruin // on and off trigger for the Senseo
int eencup = D5; //IN1 en blauw    // 1 cup trigger for the Senseo
int tweecup = D4;    // 2 cup trigger for the Senseo
//float h;      // humidity
float t;      // temperature
char h1[10];  // humidity string
char t1[10];  // temperature string
int f = 0;    // failed?
double brewTemp = 30;
double cofTemp = 0;
int tempTemp =0;

void setup() {
 
  pinMode(OnOff, OUTPUT); // set the D7 LED as output
  pinMode(eencup, OUTPUT);
  pinMode(tweecup, OUTPUT);
  Spark.variable("humidity", &h1, STRING);      // for checking the sensor values manually without the function makeCoffee.
    Spark.variable("temperature", &t1, STRING); // for checking the sensor values manually without the function makeCoffee.
    Spark.variable("status", &f, INT);          // for checking the status manually without the unction makeCoffee.
  sensor.begin();                                  // for starting the function that says what pin should be used for measuring the temp.
  Spark.function("coffee", makeCoffee);            // a POST request for "blink" will reference blinkfunc, defined below
 // Spark.function("readtemp", readTemp); 
}

// call the below function when the POST request matches it
int makeCoffee(String command) 
{
    return 1;
    // turn the coffee maker on
    // the coffee maker signal only has to be triggered once, then the trigger can be turn off.
    digitalWrite(OnOff, LOW);
    delay(500);
    digitalWrite(OnOff, HIGH);
//    delay(40000); //wait a minute for the senseo to start up.
    sensor.requestTemperatures();
    cofTemp = sensor.getTempCByIndex( 0 ); // reads the Temperature of the tempsensor.
    
    
    while (cofTemp < brewTemp)
    {
        sensor.requestTemperatures();
        cofTemp = sensor.getTempCByIndex( 0 ); // reads the Temperature of the tempsensor.
        if( cofTemp > brewTemp)
        {
            if (command =="1cups")   
            {
                digitalWrite(eencup, LOW);
                delay(500); 
                digitalWrite(eencup,HIGH);
                delay(30000);
                digitalWrite(OnOff, LOW);
                delay(500);
                digitalWrite(OnOff, HIGH);
                timeElapsed = 0;    
                return 1;
            }
        
            else if (command=="2cups")
            {
                digitalWrite(tweecup, LOW);
                delay(500);
                digitalWrite(tweecup,HIGH);
                delay(30000);
                digitalWrite(OnOff, LOW);
                delay(500);
                digitalWrite(OnOff, HIGH);
                timeElapsed = 0;    
                return 1;
            }
            else
            {
                timeElapsed = 0;    
                return -1;
            }
        }
        if (timeElapsed > interval)
        {
                digitalWrite(OnOff, LOW);
                delay(500);
                digitalWrite(OnOff, HIGH);
                timeElapsed = 0;    
                return -1;
        }
        delay(1000);
    }
    

}


    
void loop()
{
    digitalWrite(OnOff, HIGH);
    digitalWrite(eencup, HIGH);
    digitalWrite(tweecup, HIGH);
    
 sensor.requestTemperatures();
 cofTemp=sensor.getTempCByIndex( 0 );
 sprintf(t1, "%2.2f",cofTemp); 

 delay(1000);
//    } 
}

@FranzFernandes, you are putting the while (cofTemp < brewTemp) in the Spark.function() which will delay the exit of the function and timeout the connection. It is better to instead set a flag in makeCoffee() that you can sample in loop() and do the work there.

in addition to getting rid of the while() loop, you should consider getting rid of the other blocking delays. Also, try using a State Machine approach like this outline:

// This #include statement was automatically added by the Spark IDE.
#include "elapsedMillis/elapsedMillis.h"

// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"

//#define DHTPIN D2    // Digital pin D2
//#define DHTTYPE DHT11
#define ONE_WIRE_BUS D2

/*This block creates new variables for you to control the state of the coffee maker*/
typedef enum {
  STAND_BY_OFF, WARM_UP, ONE_CUP_BREW, TWO_CUP_BREW, SIGNAL_COMPLETE}
CoffeeMakerState;

CoffeeMakerState state = STAND_BY_OFF;
CoffeeMakerState lastState = STAND_BY_OFF;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensor(&oneWire);
elapsedMillis timeElapsed;
unsigned int interval = 90000; // timer na een minuut en een half

int OnOff = D6; // IN4 en bruin // on and off trigger for the Senseo
int eencup = D5; //IN1 en blauw    // 1 cup trigger for the Senseo
int tweecup = D4;    // 2 cup trigger for the Senseo
//float h;      // humidity
float t;      // temperature
char h1[10];  // humidity string
char t1[10];  // temperature string
int f = 0;    // failed?
double brewTemp = 30;
double cofTemp = 0;
int tempTemp = 0;

void setup() 
{
  pinMode(OnOff, OUTPUT); // set the D7 LED as output
  pinMode(eencup, OUTPUT);
  pinMode(tweecup, OUTPUT);
  Spark.variable("humidity", &h1, STRING);      // for checking the sensor values manually without the function makeCoffee.
  Spark.variable("temperature", &t1, STRING); // for checking the sensor values manually without the function makeCoffee.
  Spark.variable("status", &f, INT);          // for checking the status manually without the unction makeCoffee.
  sensor.begin();                                  // for starting the function that says what pin should be used for measuring the temp.
  Spark.function("coffee", makeCoffee);            // a POST request for "blink" will reference blinkfunc, defined below
  // Spark.function("readtemp", readTemp);
}

void loop()
{
  if (state == STAND_BY_OFF)
  {
    // do your off stuff here
    // and hangout for an order of state...
  }
  else if (state == WARM_UP)
  {
    // do you warm up stuff here
    // when completed change state to ONE/TWO_CUP_BREW
  }
  else if (state == ONE_CUP_BREW)
  {
    // do your one cup brewing sequence here
    // when completed, change state to SIGNAL_COMPLETE
  }
  else if (state == TWO_CUP_BREW)
  {
    // do your two cup brewing sequence here
    // when completed, change state to SIGNAL_COMPLETE
  }
  else // if (state == SIGNAL_COMPLETE)
  {
    // send your notification that brewing is complete here
    // when completed, change state to STAND_BY_OFF
  }
  lastState = state;
}

int makeCoffee(String command)
{
  if (command == "1cup")
  {
    state = WARM_UP;
    cupsToBrew =1;
    return 1;
  }
  else if (command == "2cups")
  {
    state = WARM_UP;
    cupsToBrew = 2;
    return 2;
  }
  else if (command = "cancel")
  {
    state = STAND_BY_OFF;
    return 0;
  }
  else 
  {
    return -1;  // i didn't understand the command
  }
}
1 Like