#include <EEPROM.h>
#include <Adafruit_SSD1306.h>
#include <esp_task_wdt.h>
#define REMOTEXY_MODE__ESP32CORE_BLE
#include <BLEDevice.h>
// RemoteXY connection settings
#define REMOTEXY_BLUETOOTH_NAME "Natty_RemoteXY"
#include <RemoteXY.h>
// RemoteXY GUI configuration
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] = // 85 bytes
{ 255,1,0,6,0,78,0,18,0,0,0,31,1,106,200,1,1,5,0,2,
34,18,33,16,0,20,26,31,31,79,78,0,79,70,70,0,72,9,57,38,
38,20,166,140,20,26,0,0,0,0,0,240,127,69,0,0,0,0,67,8,
92,40,10,85,20,26,67,59,92,40,10,85,20,26,129,68,77,24,12,64,
8,65,68,68,0 };
// this structure defines all the variables and events of your control interface
struct {
// input variables
uint8_t GPIO2; // =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 ADDRESS; // -32768 .. +32767
// other variable
uint8_t connect_flag; // =1 if wire connected, else =0
} RemoteXY;
#pragma pack(pop)
/////////////////////////////////////////////
// END RemoteXY include //
/////////////////////////////////////////////
#define WDT_TIMEOUT 5
#define PIN_GPIO2 25
#define LED_PIN 25
#define POT 34
#define sw14 16
#define sw15 17
#define sw10 5
#define EEPROM_SIZE 4056
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
int pot, timecount, cov,nowpot,nowcov,now,lasttime,counting;
int suit(int value) {
return (value * 255) / 4095;
}
void showoled(int add, int value, int cov) {
display.display();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.println(F("EEPORM"));
display.setCursor(60, 0);
display.println(F("ADC : "));
display.setCursor(80, 0);
display.println(value);
display.setCursor(0, 15);
display.println(F("Address : "));
display.setCursor(60, 15);
display.println(add);
display.setCursor(0, 25);
display.println(F("Data : "));
display.setCursor(40, 25);
display.println(cov);
display.display();
display.clearDisplay();
}
void setup()
{
RemoteXY_Init ();
pinMode (PIN_GPIO2, OUTPUT);
pinMode(sw14, INPUT_PULLUP);
pinMode(sw15, INPUT_PULLUP);
pinMode(sw10, INPUT_PULLUP);
esp_task_wdt_deinit();
esp_task_wdt_config_t wdt_config = {
.timeout_ms = WDT_TIMEOUT * 1000,
.idle_core_mask = (1 << portNUM_PROCESSORS) - 1,
.trigger_panic = true
};
esp_task_wdt_init(&wdt_config);
esp_task_wdt_add(NULL);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
if (!EEPROM.begin(EEPROM_SIZE)) {
Serial.println("Failed to initialize EEPROM");
ESP.restart();
}
pot = analogRead(POT);
cov = suit(pot);
nowpot = pot;
nowcov = cov;
now = 0;
EEPROM.writeUChar(0,nowcov);
EEPROM.commit();
// TODO you setup code
}
void loop()
{
RemoteXY_Handler ();
RemoteXY_Handler ();
if(millis() - lasttime >= 1000)
{
lasttime = millis();
if(digitalRead(sw10) == HIGH)
{
esp_task_wdt_reset();
}
if(digitalRead(sw15) == LOW)
{
RemoteXY.ADC34 = nowpot;
RemoteXY.ADC34_VALUE = nowpot;
RemoteXY.ADDRESS = now;
now = 0;
Serial.println("\nChoose SW15");
Serial.print("\nAddress : ");
Serial.print(0);
Serial.print("\nPOT : ");
Serial.print(nowpot);
Serial.print("\nData : ");
Serial.print(nowcov);
}
else if (digitalRead(sw14)==LOW)
{
now =82;
RemoteXY.ADC34 = nowpot;
RemoteXY.ADC34_VALUE = nowpot;
RemoteXY.ADDRESS = now;
Serial.println("\nChoose SW14");
Serial.print("\nAddress : ");
Serial.print(82);
Serial.print("\nPOT : ");
Serial.print(pot);
Serial.print("\nData : ");
Serial.print(suit(pot));
}
else if(digitalRead(sw10)==LOW)
{
EEPROM.writeUChar(now,nowcov);
EEPROM.commit();
Serial.println("\n\nCommit successful\n");
Serial.print("\nAddress : ");
Serial.print(now);
Serial.print("\nPOT : ");
Serial.print(EEPROM.read(now));
Serial.print("\nData : ");
Serial.print(suit(EEPROM.read(now)));
}
else
{
nowpot = analogRead(POT);
nowcov = suit(pot);
}
showoled(now,nowpot,nowcov);
if(RemoteXY.GPIO2 == 1)
{
analogWrite(PIN_GPIO2, EEPROM.readUChar(now));
}
else if(RemoteXY.GPIO2 == 0)
{
analogWrite(PIN_GPIO2,0);
}
}
}