/* 
  The following variables are automatically generated and updated when changes are made to the Thing properties

  int ldr;
  int pot;

  Properties which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

int sendCount = 0;

#define potPin 1
#define ldrPin 2
#define ledPin 6

#include "thingProperties.h"

void setup() {
  
  Serial.begin(9600);
  delay(1500);
  pinMode(6, OUTPUT);
  
  // Defined in thingProperties.h
  initProperties();
  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();

}

void loop() {
  
  ArduinoCloud.update();
  
  pot = analogRead(1);
  ldr = analogRead(2);

  Serial.println("pot = " + String(pot) + " ldr = " + String(ldr));
  
  if (pot < 100) {
    digitalWrite(ledPin, HIGH);
    if (sendCount == 0) {
      iftttSend(pot);
      sendCount = 1;
    }
  }
  
  else {
    digitalWrite(ledPin, LOW);
    sendCount = 0;
  }

  delay(1000);
  
}



void iftttSend(int val) {
  
  String str_val = String(val);
  Serial.println("Alarm triggered ! Potentiometer value is: " + str_val);
  
}