#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "EEPROM.h"
// RemoteXY select connection mode and include library
#define REMOTEXY_MODE__ESP32CORE_BLE
#include <BLEDevice.h>
// RemoteXY connection settings
#define REMOTEXY_BLUETOOTH_NAME "RemoteXY"
#include <RemoteXY.h>
// RemoteXY GUI configuration
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 83 bytes
{ 255,1,0,17,0,76,0,18,0,0,0,31,1,106,200,1,1,5,0,72,
22,69,63,63,20,166,140,2,26,0,0,0,0,0,240,127,69,0,0,0,
0,67,33,123,40,10,84,2,26,67,33,143,40,10,84,2,26,67,32,177,
40,10,68,2,26,11,2,30,25,44,22,0,2,26,31,31,79,78,0,79,
70,70,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t GPIO25; // =1 if switch ON and =0 if OFF
// output variables
int16_t adc34; // from 0 to 4095
int16_t adc34_value; // -32768 .. +32767
int16_t convert_value; // -32768 .. +32767
char value_01[11]; // string UTF8 end zero
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#include <esp_task_wdt.h>
#define WDT_TIMEOUT 5
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
// Size Address
#define EEPROM_SIZE 4096
// Display Resolution
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SCREEN_ADDRESS 0x3C // Standard I2C OLED Address (change if needed)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
// Define Switch
#define SW1 16
#define SW2 17
#define SW3 5
// Define GPIO of POT and LED
#define potPin 34
#define LED 25
#define PIN_GPIO25 25
// Initial Value
int potValue = 0;
int lastTime1000ms = 0;
int Value_0x000, Value_0x09B;
int Now_ADDR = 0;
void setup() {
// Begin
Serial.begin(115200);
Wire.begin();
SerialBT.begin("RemoteXY"); //Bluetooth device name
RemoteXY_Init();
// Set PIN MODE
pinMode(LED, OUTPUT);
pinMode(SW1, INPUT_PULLUP);
pinMode(SW2, INPUT_PULLUP);
pinMode(SW3, INPUT_PULLUP);
// Setup EEPROM
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("Failed init EEPROM");
delay(1000);
ESP.restart();
}
Serial.println("Success init EEPROM");
// Read Value From EEPROM
Value_0x000 = EEPROM.readUChar(0x000);
Value_0x09B = EEPROM.readUChar(0x09B);
potValue = analogRead(potPin);
// Setup Display
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
}
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.printf("EEPROM ADC : %d", potValue * 255 / 4095);
display.setCursor(0, 40);
display.printf("Address : 0000(0x000)");
display.setCursor(0, 50);
display.printf("Data : %d", Value_0x000);
display.display();
// setup LED
analogWrite(LED, Value_0x000);
// setup task WDT
Serial.println("Configuring WDT...");
esp_task_wdt_config_t wdt_config = {
.timeout_ms = WDT_TIMEOUT * 1000, // Convert seconds to milliseconds
.trigger_panic = true // Enable panic so ESP32 restarts on WDT timeout
};
esp_task_wdt_init(&wdt_config); //enable panic so ESP32 restarts
esp_task_wdt_add(NULL); //add current thread to WDT watch
}
int pot, conv, check;
void loop() {
RemoteXY_Handler();
digitalWrite(PIN_GPIO25, (RemoteXY.GPIO25==0)?LOW:HIGH);
analogWrite(LED, (RemoteXY.GPIO25 == 0) ? 0 : (digitalRead(SW2) == HIGH) ? Value_0x000 : Value_0x09B);
Value_0x000 = EEPROM.readUChar(0x000);
Value_0x09B = EEPROM.readUChar(0x09B);
potValue = analogRead(potPin);
RemoteXY.adc34= potValue;
RemoteXY.adc34_value = potValue;
RemoteXY.convert_value = EEPROM.read((digitalRead(SW2) == HIGH) ? 0x000 : 0x09B);
// Show Value every 1 sec
if (millis() - lastTime1000ms >= 1000) {
lastTime1000ms = millis();
Serial.printf("POT : %d CONV : %d 0x000: %d 0x09B: %d\n", potValue, potValue * 255 / 4095, Value_0x000, Value_0x09B);
}
// Reset WDT on SW3 press
esp_task_wdt_reset();
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
snprintf(RemoteXY.value_01, sizeof(RemoteXY.value_01), "Addr: 0x%02X LED: %s", Now_ADDR == 0 ? 0x000 : 0x09B, digitalRead(LED) == HIGH ? "ON" : "OFF");
// Check SW State
if (RemoteXY.GPIO25 == 0) {
//digitalWrite(PIN_GPIO25, LOW); // ปิดไฟ LED เมื่อปุ่มอยู่ในสถานะ OFF
} else {
//digitalWrite(PIN_GPIO25, HIGH); // เปิดไฟ LED เมื่อปุ่มอยู่ในสถานะ ON
}
// ตรวจสอบสถานะของปุ่ม SW2 และปรับความสว่างของไฟ LED หรือตำแหน่ง Address
if ((digitalRead(SW2) == HIGH) && (RemoteXY.GPIO25 == 1)) {
analogWrite(LED, Value_0x000); // ตั้งค่าความสว่างของไฟ LED จาก Address 0x000 ใน EEPROM
Now_ADDR = 0;
} else if((digitalRead(SW2) == LOW )&&(RemoteXY.GPIO25 == 1)){
analogWrite(LED, Value_0x09B); // ตั้งค่าความสว่างของไฟ LED จาก Address 0x09B ใน EEPROM
Now_ADDR = 1;
}else{
analogWrite(LED,0);
}
pot = analogRead(potPin);
conv = (pot * 255) / 4095;
if (digitalRead(SW1) == HIGH) {
analogWrite(LED, conv);
if (Now_ADDR == 0) {
EEPROM.write(0x000, conv);
} else {
EEPROM.write(0x09B, conv);
}
EEPROM.commit();
}
}