#include <WiFi.h>
#include <EEPROM.h>
#include <PubSubClient.h>
#include <Adafruit_SSD1306.h>
#include <BluetoothSerial.h>
#include <esp_task_wdt.h>
#define WDT_TIMEOUT 5
#define MQTT_SERVER "104.214.189.245"
#define MQTT_PORT 1883
#define MQTT_USERNAME ""
#define MQTT_PASSWORD ""
#define MQTT_NAME "ESP32_1"
#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
//WiFiClient client;
BluetoothSerial SerialBT;
//PubSubClient mqtt(client);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
const char* send_pot = "/ESP32_1/POT";
String text, textclone;
int pot, timecount, cov,nowpot,nowcov,now,lasttime,counting;
int suit(int value) {
return (value * 255) / 4095;
}
/*void callback(char *topic, byte *payload, unsigned int length) {
for (int i = 0; i < length; i++) {
text += (char)payload[i];
}
textclone = text;
text = "";
Serial.print("Text input is : ");
Serial.println(textclone);
}*/
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() {
Serial.begin(115200);
SerialBT.begin("Chyr_BT");
pinMode(LED_PIN, 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");
delay(1000);
ESP.restart();
}
/*WiFi.mode(WIFI_STA);
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");*/
pot = analogRead(POT);
cov = suit(pot);
nowpot = pot;
nowcov = cov;
now = 0;
EEPROM.writeUChar(0,nowcov);
EEPROM.commit();
/*mqtt.setServer(MQTT_SERVER, MQTT_PORT);
mqtt.setCallback(callback);*/
}
void loop() {
if(millis() - lasttime >= 1000)
{
lasttime = millis();
if(digitalRead(sw10) == HIGH)
{
esp_task_wdt_reset();
}
/* if (!mqtt.connected()) {
if (mqtt.connect(MQTT_NAME, MQTT_USERNAME, MQTT_PASSWORD)) {
Serial.println("Connected to MQTT");
mqtt.subscribe("/ESP32_1/POT");
mqtt.subscribe("/ESP32_1/LED");
} else {
delay(5000);
return;
}
}
mqtt.loop(); */
if(digitalRead(sw15) == LOW)
{
esp_task_wdt_reset();
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)
{
esp_task_wdt_reset();
now =82;
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)
{
mqtt.publish("/ESP32_1/POT",String(nowpot).c_str());
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(SerialBT.available())
{
if(SerialBT.read() == "on")
{
analogWrite(LED_PIN, EEPROM.readUChar(now));
}
else if(SerialBT.read() == "off")
{
analogWrite(LED_PIN,0);
}
}
/*if(textclone == "on")
{
analogWrite(LED_PIN, EEPROM.readUChar(now));
}
else if (textclone =="off")
{
analogWrite(LED_PIN,0);
}
else
{
analogWrite(LED_PIN,0);
}*/
}