/*
-- esp1 --
This source code of graphical user interface
has been generated automatically by RemoteXY editor.
To compile this code using RemoteXY library 3.1.10 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.11 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__ESP32CORE_WIFI_CLOUD
#include <WiFi.h>
// RemoteXY connection settings
#define REMOTEXY_WIFI_SSID "Wokwi-GUEST"
#define REMOTEXY_WIFI_PASSWORD ""
#define REMOTEXY_CLOUD_SERVER "cloud.remotexy.com"
#define REMOTEXY_CLOUD_PORT 6376
#define REMOTEXY_CLOUD_TOKEN "e3fc6b548d3b85cf6e368da9368e1bd6"
#include <RemoteXY.h>
#define blink_led 2
#define led 27 //D4: alive indicator: blinking led
#define DOOR_PIN 34 //A0:door sensor input
#define timeTick 100
//#define DEBOUNCE_TIME 30 // The debounce time in millisecond, increase this time if it still chatters
int lastSteadyState = LOW; // The previous steady state from the input pin
//int lastFlickerableState = LOW; // The previous flickerable state from the input pin
int button_state; // The current reading from the input pin
// The following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
//unsigned long lastDebounceTime = 0; // The last time the output pin was toggled
const int iopenValue=0;//4095;
const int iclosedValue=4000; //1410;
int openValue=iopenValue;
int closedValue=iclosedValue;
int doorValue = 0;
// RemoteXY configurate
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 36 bytes
{ 255,1,0,12,0,38,0,17,0,0,0,31,1,106,200,1,1,3,0,1,
67,6,24,24,0,2,31,0,66,18,7,14,32,1,2,26,67,37,78,40,
10,4,2,26,11 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t button_01; // =1 if button pressed, else =0
// output variables
int8_t level_01; // =0..100 level position
char text_01[11]; // string UTF8 end zero
// other variable
uint8_t connect_flag=0;; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
void button_process();
void door_status();
int mymap(int inp, float in_min, float in_max, int out_min, int out_max);
void setup()
{
RemoteXY_Init ();
Serial.begin(115200);
Serial.println("*****************************************************");
Serial.println("********** Program Start : ESP controls via RemoteXY .....");
pinMode(led, OUTPUT);
pinMode(blink_led, OUTPUT);
pinMode(DOOR_PIN,INPUT);
digitalWrite(led, LOW);
digitalWrite(blink_led, LOW);
// TODO you setup code
}
bool blink_led_toggle=LOW;
void loop()
{
digitalWrite(blink_led, blink_led_toggle);
Serial.print("Blinking.... ");
Serial.println(blink_led_toggle);
blink_led_toggle=not(blink_led_toggle);
RemoteXY_Handler ();
button_process();
door_status();
// TODO you loop code
// use the RemoteXY structure for data transfer
// do not call delay(), use instead RemoteXY_delay()
//digitalWrite(PIN_SWITCH_1, (RemoteXY.button_01==0)?LOW:HIGH);
RemoteXY_delay(200);
}
void button_process(){
//digitalWrite(led, button_state );
//igitalWrite(led, (RemoteXY.button_01==0)?LOW:HIGH);
button_state=RemoteXY.button_01;
lastSteadyState = button_state;
/*
// If the switch/button changed, due to noise or pressing:
if (button_state != lastFlickerableState) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the the last flickerable state
lastFlickerableState = button_state;
}
if ((millis() - lastDebounceTime) > DEBOUNCE_TIME) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if(lastSteadyState == HIGH && button_state == LOW)
Serial.println("The button is pressed");
else if(lastSteadyState == LOW && button_state == HIGH)
Serial.println("The button is released");
// save the the last steady state
}*/
if (button_state==HIGH){
Serial.println(" button == HIGH");
digitalWrite(led, HIGH);
RemoteXY_delay(timeTick);
}else{
Serial.println(" button == LOW");
digitalWrite(led,LOW);
}
}
void door_status(){
int tempValue=0;
int sumValue=0;
for (int i=0; i<20; i++){//avg 20 readings to find mean reading
tempValue=tempValue+analogRead(DOOR_PIN);
//tempValue=tempValue+constrain(tempValue, 0, 4095);
//tempValue=tempValue+constrain(tempValue, 0, 4095);
RemoteXY_delay(5);
}
tempValue=tempValue/20;
doorValue=mymap(tempValue, openValue,closedValue, 0, 100);
RemoteXY.level_01=doorValue;
dtostrf(doorValue, 0, 0, RemoteXY.text_01);
Serial.print("Door value=");
//Serial.println(doorValue);
Serial.println(String(RemoteXY.text_01));
}
int mymap(int inp, float in_min, float in_max, int out_min, int out_max){
int t=map(inp,in_min,in_max,out_min,out_max+1);
return constrain(t,out_min,out_max);
}