/*
   -- DOMOTICA_3sensores_actuadores --
   
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 3.1.13 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.13.13 or later version;
     - for iOS 1.10.3 or later version;
    
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// you can enable debug logging to Serial at 115200
//#define REMOTEXY__DEBUGLOG    

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__WIFI_POINT

#include <WiFi.h>
#include <ESP32Servo.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377


#include <RemoteXY.h>

// RemoteXY GUI configuration  
#pragma pack(push, 1)  
uint8_t RemoteXY_CONF[] =   // 200 bytes
  { 255,2,0,11,0,193,0,17,0,0,0,16,1,106,200,1,1,12,0,2,
  50,112,31,13,0,2,26,31,31,79,78,0,79,70,70,0,68,10,16,86,
  33,1,8,36,129,14,115,18,10,24,76,69,68,0,129,15,6,76,7,24,
  78,73,86,69,76,32,68,69,32,73,76,85,77,73,78,65,67,73,79,78,
  0,71,46,62,51,51,56,0,2,24,135,0,0,0,0,0,0,200,66,0,
  0,160,65,0,0,32,65,0,0,0,64,24,0,129,8,54,15,8,8,76,
  69,68,0,4,10,147,85,11,128,2,26,129,14,134,72,8,24,86,69,76,
  79,67,73,68,65,68,32,77,79,84,79,82,0,70,5,68,18,18,16,26,
  37,0,129,45,55,56,8,24,84,69,77,80,69,82,65,84,85,82,65,0,
  69,59,169,20,20,0,1,129,12,175,33,9,24,83,79,78,73,68,79,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t switch_01; // =1 if switch ON and =0 if OFF
  int8_t slider_01; // from 0 to 100

    // output variables
  float onlineGraph_01_var1;
  float instrument_01; // from 0 to 100
  uint8_t led_01; // from 0 to 1
  //int16_t sound_01; // =0 no sound, else ID of sound, =1001 for example, look sound list in app
  uint8_t button_01; // =1 if button pressed, else =0
    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0

} RemoteXY;   
#pragma pack(pop)
 
/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

//PINES DE ENTRADA
static int LDR=34;
static int pulsador=35;
static int temperatura=27;
//PINES DE SALIDA
static int led=19;
static int servo_pin=18;
static int buzzer_pin=5;
//variables del proceso
Servo servo;
Servo buzzer;
int valor_temp=0;
float valor_iluminacion;


void setup() 
{
  RemoteXY_Init (); 
  Serial.begin(115200);
  delay(100);
  Serial.println("*********Inicio de Programa: PIO Domotica********");
  Serial.println("https://wokwi.com/projects/404536977995357185");
  Serial.println("https://remotexy.com/en/editor/open/?id=153083");
  
  //configuracion de pines entrada salida
  pinMode(led, OUTPUT);
  pinMode(servo_pin, OUTPUT);
  servo.attach(servo_pin, 500, 2400);
  pinMode(buzzer_pin, OUTPUT);
  buzzer.attach(buzzer_pin, 500, 2400);
  //
  pinMode(LDR, INPUT);
  pinMode(pulsador, INPUT_PULLUP);
  pinMode(temperatura, INPUT);
}

void loop() 
{ 
  RemoteXY_Handler ();
  //SENSA ENTRADAS
  //pulsador
  if(digitalRead(pulsador)){RemoteXY.led_01=1;Serial.println("Pulsador ON");}
  if(!digitalRead(pulsador)){RemoteXY.led_01=0;Serial.println("Pulsador OFF");}
  //LDR
  valor_iluminacion=analogRead(LDR);
  RemoteXY.onlineGraph_01_var1=valor_iluminancion;
  //temperatura
  valor_temperatur=analogRead(temperatura);
  RemoteXY.instrument_01=valor_temperatura;
  
  //ACTUADORES
  //led
  digitalWrite(led,RemoteXY.switch_01);
  //servo
  servo.write(RemoteXY.slider_01);
  //buzzer
  if(RemoteXY.button_01){buzzer.write(100);}
  
  
  RemoteXY_delay(100);
  // TODO you loop code
  // use the RemoteXY structure for data transfer
  // do not call delay(), use instead RemoteXY_delay() 


}