// Code generated by Arduino IoT Cloud
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const char DEVICE_LOGIN_NAME[]  = "da1669b2-a4f1-4aa8-ad06-4d9b01c6a785";
const char SSID[]               = "Wokwi-GUEST";
const char PASS[]               = "";
const char DEVICE_KEY[]         = "MpnT!9SuQrhzHDtUbl18GNT@e";
void onSelect1Change();
void onSelect2Change();
void onScheduleChange();
void onSetChange();
void onTeksChange();
time_t UnixTime;
unsigned long skr = 0;
String teks;
String waktu;
int select_1;
int select_2;
bool set;
CloudSchedule schedule;
int ok = 0, ldr1 = 0, ntc1 = 0, pot1 = 0, sld1 = 0;
bool rly_sched[6] = {false, false, false, false, false, false};
bool rly_onoff[12];
char rly_name[13] = {"ABCDEFGHIJKL"};
CloudSchedule R1, R2, R3, R4, R5, R6;
int count[6] = {0, 0, 0, 0, 0, 0};
int pin_sched[6] = {23, 19, 18, 5, 17, 16};
int pin_onoff[12] = {32, 33, 25, 26, 27, 14, 12, 13, 15, 2, 0, 4};
void initProperties() {
  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(select_1, READWRITE, ON_CHANGE, onSelect1Change);
  ArduinoCloud.addProperty(select_2, READWRITE, ON_CHANGE, onSelect2Change);
  ArduinoCloud.addProperty(schedule, READWRITE, ON_CHANGE, onScheduleChange);
  ArduinoCloud.addProperty(set, READWRITE, ON_CHANGE, onSetChange);
  ArduinoCloud.addProperty(teks, READWRITE, ON_CHANGE, onTeksChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup() {
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for (;;);
  }
  delay(1500);
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  for (int i = 0; i < 6; i++) {
    pinMode(pin_sched[i], OUTPUT);
  }
  for (int i = 0; i < 12; i++) {
    pinMode(pin_onoff[i], OUTPUT);
  }
}
void loop() {
  ArduinoCloud.update();
  int ntc = analogRead(35);
  float cels = 1 / (log(1 / (4095. / ntc - 1)) / 3950 + 1.0 / 298.15) - 273.15;
  int ldr = analogRead(34);
  float voltage = ldr / 4095. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(50 * 1e3 * pow(10, 0.7) / resistance, (1 / 0.7));
  int pot = analogRead(39);
  int sld = analogRead(36);
  if (ntc != ntc1) {
    teks = "cels:" + String(cels);
    ntc1 = ntc;
  }
  if (ldr != ldr1) {
    teks = "ldr:" + String(lux);
    ldr1 = ldr;
  }
  if (pot != pot1) {
    teks = "pot:" + String(pot);
    pot1 = pot;
  }
  if (sld != sld1) {
    teks = "sld:" + String(sld);
    sld1 = sld;
  }
  if (set) {
    if (select_1 == 0) R1 = schedule;
    if (select_1 == 1) R2 = schedule;
    if (select_1 == 2) R3 = schedule;
    if (select_1 == 3) R4 = schedule;
    if (select_1 == 4) R5 = schedule;
    if (select_1 == 5) R6 = schedule;
  }
  rly_sched[0] = R1.isActive();
  rly_sched[1] = R2.isActive();
  rly_sched[2] = R3.isActive();
  rly_sched[3] = R4.isActive();
  rly_sched[4] = R5.isActive();
  rly_sched[5] = R6.isActive();
  for (int i = 0; i < 12; i++) {
    if (select_2 == i && ok != i) {
      digitalWrite(pin_onoff[i], !digitalRead(pin_onoff[i]));
      rly_onoff[i] = !rly_onoff[i];
      ok = i;
    }
  }
  if (millis() - skr > 1000) {
    UnixTime = ArduinoCloud.getLocalTime();
    waktu = ctime(&UnixTime);
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 0);
    display.print(waktu.substring(0, 11));
    display.print(waktu.substring(20));
    display.print("       ");
    display.println(waktu.substring(11, 19));
    for (int i = 0; i < 6; i++) {
      display.print("R");
      display.print(i + 1);
      display.print(":");
      if (rly_sched[i]) {
        digitalWrite(pin_sched[i], true);
        display.print("ON.");
        if (count[i] < 10) display.print(".");
        if (count[i] < 100) display.print(".");
        if (count[i] < 1000) display.print(".");
        display.print(count[i]);
        count[i]++;
      } else {
        digitalWrite(pin_sched[i], false);
        count[i] = 0;
        display.print("OFF....");
      }
      if ((i + 1) % 2 == 0) display.println();
    }
    for (int i = 0; i < 12; i++) {
      display.print(rly_name[i]);
      if (rly_onoff[i]) display.print(":1, ");
      else display.print(":0, ");
      if ((i + 1) % 4 == 0) display.println();
    }
    display.display();
    skr = millis();
  }
}
void onSetChange()  {
}
void onScheduleChange()  {
}
void onSelect1Change()  {
}
void onSelect2Change()  {
}
void onTeksChange()  {
}
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
R1:Scheduled
R2:Scheduled
R3:Scheduled
R4:Scheduled
A:On/Off
B:On/Off
C:On/Off
D:On/Off
E:On/Off
F:On/Off
G:On/Off
H:On/Off
I:On/Off
J:On/Off
K:On/Off
L:On/Off
NOCOMNCVCCGNDINLED1PWRRelay Module
NOCOMNCVCCGNDINLED1PWRRelay Module
R5:Scheduled
R6:Scheduled
NTC Temp. Sensor
Photoresistor
Potentiometer
Slider