Back to Parent

int lightPin = D2; 
int temperatureThreshold = 35;  

void setup() {
  Particle.subscribe("hook-response/TemperatureWebhook", myHandler, MY_DEVICES);

  pinMode(lightPin, OUTPUT);
}

void loop() {
  
  String data = String(10);

  Particle.publish("TemperatureWebhook", data, PRIVATE);

  delay(60000);
}


void myHandler(const char *event, const char *data) {

  String receivedStr = String(data);
  int loc1 = 0;
  int loc2 = 0;

  loc1 = receivedStr.indexOf("~");

 
  double temperature = receivedStr.substring(0, loc1).toFloat();

 
  adjustLightColor(temperature);
}


void adjustLightColor(double temperature) {
  
  if (temperature < temperatureThreshold) {
    analogWrite(lightPin, 0);  
  } else {
    analogWrite(lightPin, 255);  
  }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0