#define BLYNK_TEMPLATE_ID "TMPL6mJ-kH7m_"
#define BLYNK_TEMPLATE_NAME "esp1"
#define BLYNK_AUTH_TOKEN "8yPiBN4PurpJ_Wmd8PqED2E_114-Scw5"

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
  { '1', '2', '3', 'A' },
  { '4', '5', '6', 'B' },
  { '7', '8', '9', 'C' },
  { '*', '0', '#', 'D' }
};

uint8_t colPins[COLS] = { 26, 25, 33, 32 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 13, 12, 14, 27 }; // Pins connected to R1, R2, R3, R4

#define I2C_ADDR    0x27
#define LCD_COLUMNS 20
#define LCD_LINES   4

#define SEND_BTN 19
#define CLEAR_BTN 18
#define DHT_PIN 15

#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32.h>
#include <DHTesp.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>


char ssid[] = "Wokwi-GUEST";
char pass[] = "";

//---- HiveMQ Cloud Broker settings
const char* mqtt_server = "7c1592ca3d0840a7be27dc76609a4f81.s1.eu.hivemq.cloud";
const char* mqtt_username = "yazan";
const char* mqtt_password = "Yazan@123";
const int mqtt_port = 8883;
// HiveMQ Cloud Let's Encrypt CA certificate
static const char *root_ca PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
ZXQgU2Vj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Miu/X1+mvoiBOv/2X/qkSsisRcOj/KK
NFtY2PwByVS5uCbMiogziUwthDyC3+6WVwW6LLv3xLfHTjuCvjHIInNzktHCgKQ5
ORAzI4JMPJ+GslWYHb4phowim57iaztXOoJwTdwJx4nLCgdNbOhdjsnvzqvHu7Ur
TkXWStAmzOVyyghqpZXjFaH3pO3JLF+l+/+sKAIuvtd7u+Nxe5AW0wdeRlN8NwdC
jNPElpzVmbUq4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
-----END CERTIFICATE-----
)EOF";

WiFiClientSecure espClient;
PubSubClient client(espClient);

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);

BlynkTimer timer;

DHTesp dhtSensor;


int lastCursorLocation = 1;
String myCurrentText = "";
String firstLine =  "";
String secondLine =  "";
String thirdLine =  "";
const char* pubTopic = "person1";
const char* subTopic = "person2";


void updateLCD(String newMessage){
  lcd.setCursor(0, 0);
  firstLine = secondLine;
  lcd.print("                    ");
  lcd.setCursor(0, 0);
  lcd.print(firstLine);

  lcd.setCursor(0, 1);
  secondLine = thirdLine;
  lcd.print("                    ");
  lcd.setCursor(0, 1);
  lcd.print(secondLine);

  lcd.setCursor(0, 2);
  thirdLine = newMessage;
  lcd.print("                    ");
  lcd.setCursor(0, 2);
  lcd.print(thirdLine);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  String msg = "";
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    msg += (char)payload[i];
  }
  updateLCD("-"+msg);
  Blynk.virtualWrite(V2, msg);
  Serial.println();
}

void sendSensor()
{
 TempAndHumidity  data = dhtSensor.getTempAndHumidity();
 Serial.println("Temp: " + String(data.temperature, 2) + "°C");
 Serial.println("Humidity: " + String(data.humidity, 1) + "%");
 Serial.println("---");
 Blynk.virtualWrite(V3, data.temperature);
 Blynk.virtualWrite(V4, data.humidity);
}

void reconnect() {
  // Loop until we’re reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection… ");
    String clientId = "ESP32Client";
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("connected!");
      // Once connected, publish an announcement...
      client.publish("esp", "connected");
    } else {
      Serial.print("failed, rc = ");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V5)
{
  int value = param.asInt();
  Serial.println("Value : "+ String(value));

  if(value) digitalWrite(2, HIGH);
  else digitalWrite(2, LOW);

  // Blynk.virtualWrite(V1, value);
}



void setup()
{
  
  // Debug console
  Serial.begin(115200);

  setup_wifi();

  pinMode(2, OUTPUT);
  pinMode(SEND_BTN, INPUT);
  pinMode(CLEAR_BTN, INPUT);

  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  timer.setInterval(1000, sendSensor);

  Blynk.config(BLYNK_AUTH_TOKEN);
  
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 3);
  lcd.print(">");

  espClient.setCACert(root_ca);
  client.setServer(mqtt_server, mqtt_port);
  reconnect();
  client.subscribe(subTopic);
  client.setCallback(callback);
}


void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  Blynk.run();
  timer.run();

  char key = keypad.getKey();

  if (key != NO_KEY) {
    if(lastCursorLocation >= 19){
      return;
    }
    lcd.setCursor(lastCursorLocation, 3);
    lcd.print(key);
    myCurrentText += key;
    lastCursorLocation++;
  }

  if(digitalRead(SEND_BTN)){
    Serial.println("Sending message...");
    lcd.setCursor(0, 3);
    lcd.print(">                   ");

    char payload[20];
    snprintf(payload, sizeof(payload), "%s", myCurrentText);
    client.publish(pubTopic, payload);
    Blynk.virtualWrite(V1, myCurrentText); 

    updateLCD("+"+myCurrentText);

    myCurrentText = "";
    lcd.setCursor(0, 3);
    lastCursorLocation = 1;
  }

  if(digitalRead(CLEAR_BTN)){
    if(lastCursorLocation>1){
      lastCursorLocation = 1;
      lcd.setCursor(0, 3);
      lcd.print(">                   ");
      myCurrentText = "";
    }
  }
}