Advertisement
GaabMM88

norm

Jan 3rd, 2025
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.26 KB | None | 0 0
  1. const char compile_date[] = __DATE__;
  2. //Included with the name printing
  3. #include <HardwareSerial.h>
  4. #include "AiEsp32RotaryEncoder.h"
  5. #include "Arduino.h"
  6. #include "Wire.h"
  7. #include <Adafruit_NeoPixel.h>
  8. #include <esp_task_wdt.h>
  9. #include <WiFi.h>
  10. #include <RTClib.h>
  11. #include <PinDefinitionsAndMore.h>  // Define macros for input and output pin etc.
  12. #include <IRremote.hpp>
  13. #include "TC74.h"
  14.  
  15.  
  16. #define first 2000
  17. #define second 2050
  18. #define ROTARY_ENCODER_A_PIN 35
  19. #define ROTARY_ENCODER_B_PIN 32
  20. #define ROTARY_ENCODER_BUTTON_PIN 33
  21. #define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */
  22. #define ROTARY_ENCODER_STEPS 4
  23. #define powerSW 23
  24. #define inputSW
  25. #define dcSenseRight 19  //-> pin / dcErrorRight (current state) / dcErroRightLast (last state of dcErrorRight)
  26. #define dcSenseLeft 18   //-> pin / dcErrorLeft (current state) / dcErrorLeftLast (last state of dcErrorLeft)
  27. #define acSense 5        //-> pin / acError (current state) / acErrorLast (last state of acError)
  28. #define relay 16         // Standby relay
  29. #define speaker 17       // Speaker relay
  30. #define WS2812 2         // WS2812 pin
  31. #define NUMPIXELS 8      // number of LED
  32. #define LDR 12           // LDR pin
  33. #define RXN_PIN 26       // Serial1 RX to Nextion TX
  34. #define TXN_PIN 25       // Serial1 TX to Nextion RX
  35. #define RX_PIN 27        // Serial2 RX a Amp TX
  36. #define TX_PIN 14        // Serial2 TX a Amp RX
  37. #define SDA 21           // I2C Thermometer, Expander, etc.
  38. #define SCL 22
  39.  
  40. #define boot 0  // Nextion page number 0..11
  41. #define standby 1
  42. #define main 2
  43. #define digiSetup 3
  44. #define ampSetup 4
  45. #define digiInput 5
  46. #define digiPreset 6
  47. #define dsp1 7
  48. #define dsp2 8
  49. #define dsp3 9
  50. #define info 10
  51. #define dataRecord 11
  52.  
  53. RTC_DS3231 rtc;
  54. TC74 tmp1(0x4A);         //A2 Address
  55. TC74 tmp2(0x4B);         //A3 Address
  56. HardwareSerial uart(2);  // Uso de la interfaz de hardware Serial2
  57. Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
  58. AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
  59.  
  60. const int powerOffDelayForRelay = 400;  //in millis seconds
  61. //const byte LDR = 12;                //Light Dependant Resistor
  62. const long eventTime_1_LDR = 5000;  //check brigtness in ms
  63. unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0, irMillis = 0, irVolMillis, strengthMillis = 0;
  64. unsigned long ac_time = 0, ac_time_last = 0;
  65. boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
  66. int offset = 2, beep = 0, NextionPage;
  67. int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1, task = 0;
  68. int initDigi = 0, toDigi = 1;
  69. int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1;  //variables for protection
  70. int debug = 2;                                                                                                      //1 - writing all data; 2 - only protection data
  71. int ac_protect = 0;
  72. int self_test = 1;
  73. int currentPage = -1;  // current page of nextion
  74. int irLastCode = -1;
  75. int volStep = 2;
  76. int sourceS = 0;
  77. int devState = 1;
  78.  
  79. String sReceived, nReceived;
  80.  
  81. //instead of changing here, rather change numbers above
  82.  
  83.  
  84. #if (1)
  85. const char* ssid = "TP-Link_F072";
  86. const char* password = "12778072";
  87. #else
  88. const char* ssid = "SirRouter";
  89. const char* password = "19801989";
  90. #endif
  91.  
  92. class c_NextionWrite {
  93. public:
  94.   void init(int speed, int RXN, int TXN) {
  95.     Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
  96.     // if (debug) Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
  97.   }
  98.   void txt(String Name, String text) {
  99.     Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  100.     if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  101.   }
  102.   void val(String Name, int value) {
  103.     Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  104.     if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  105.   }
  106.   void systemVal(String Name, int value) {
  107.     Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  108.     if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  109.   }
  110.   void pageChange(int Name) {
  111.     Serial1.print("page " + String(Name) + "\xFF\xFF\xFF");
  112.     if (debug == 1) Serial.print("page " + String(Name) + "\xFF\xFF\xFF");
  113.     NextionPage = Name;
  114.   }
  115.   void setPco(String name, int pco) {  // for global variable need a page number / page name too
  116.     Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  117.     if (debug == 1) Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  118.   }
  119.   void timerEnable(String name, int en) {
  120.     Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
  121.   }
  122.   void vis(String name, int en) {
  123.     Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
  124.   }
  125.   void dim(int en) {
  126.     Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
  127.   }
  128.   void touchSet(String name, int en) {  //nextion.touchSet("preset", 0/1);
  129.     Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
  130.   }
  131.   void click(String name, int en) {
  132.     Serial.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
  133.     Serial1.print("click " + String(name) + "," + en + "\xFF\xFF\xFF");
  134.   }
  135.  
  136.   void setTime(int hour, int min, int sec) {
  137.     hour = (hour + 24) % 24;
  138.     Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF");
  139.     Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF");
  140.     Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF");
  141.     if (debug == 1) Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", hour, min, sec);
  142.     if (debug == 1) Serial.println("--------------------");
  143.   }
  144. };
  145. c_NextionWrite nextion;
  146.  
  147.  
  148. void initWiFi() {  ////////////////////////////////// INIT WIFI
  149.   WiFi.mode(WIFI_STA);
  150.   WiFi.begin(ssid, password);
  151.   Serial.print("Connecting to WiFi ..");
  152.   while (WiFi.status() != WL_CONNECTED) {
  153.     Serial.print('.');
  154.     delay(1000);
  155.   }
  156.   Serial.println(WiFi.localIP());
  157. }
  158. void rotary_loop() {  ////////////////////////////////// ENCODER LOOP
  159.   if (rotaryEncoder.encoderChanged()) {
  160.     if (debug == 1) Serial.print("Value: ");
  161.     if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
  162.     uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
  163.     digiVolume = rotaryEncoder.readEncoder();
  164.   }
  165.   if (rotaryEncoder.isEncoderButtonClicked()) {
  166.     rotary_onButtonClick();
  167.   }
  168. }
  169. void rotary_onButtonClick() {  ////////////////////////////////// ENCODER SW
  170.   static unsigned long lastTimePressed = 0;
  171.   //ignore multiple press in that time milliseconds
  172.   if (millis() - lastTimePressed < 500) {
  173.     return;
  174.   }
  175.   lastTimePressed = millis();
  176.   if (debug == 1) Serial.print("button pressed ");
  177.   if (debug == 1) Serial.print(millis());
  178.   if (debug == 1) Serial.println(" milliseconds after restart");
  179.   if (dmute == 0) {
  180.     uart.print("MUT:1;");
  181.   } else {
  182.     uart.print("MUT:0;");
  183.   }
  184. }
  185. void IRAM_ATTR readEncoderISR() {  ////////////////////////////////// ENCODER INTERRUPT
  186.   rotaryEncoder.readEncoder_ISR();
  187. }
  188. void IRAM_ATTR stateRight_ISR() {  ////////////////////////////////// RIGHT INTERRUPT
  189.   dcErrorRight = 1;
  190. }
  191. void IRAM_ATTR stateLeft_ISR() {  ////////////////////////////////// LEFT INTERRUPT
  192.   dcErrorLeft = 1;
  193. }
  194. void IRAM_ATTR stateAC_ISR() {  ////////////////////////////////// AC INTERRUPT
  195.   ac_time = millis();
  196.   if (ac_time - ac_time_last > 250) {
  197.     acError = 1;
  198.     ac_protect = 1;
  199.     digitalWrite(relay, 0);
  200.     ac_time_last = ac_time;
  201.   }
  202. }
  203. void meta(int t, int e, int v, int i) {  ////////////////////////////////// DIGI METADATA
  204.   nextion.vis("main.title", t);
  205.   nextion.vis("main.elapsed", e);
  206.   nextion.vis("main.vendor", v);
  207.   nextion.vis("main.infoText", i);
  208. }
  209. void beep_2k() {  ////////////////////////////////// 2KhZ BEEP
  210.   unsigned long currentMillis = millis();
  211.   if (currentMillis - beepMillis >= 1000) {
  212.     if (beep == 0) {
  213.       tone(12, 2000, 50);
  214.     } else {
  215.       tone(12, 2000, 50);
  216.     }
  217.     beepMillis = currentMillis;
  218.   }
  219. }
  220. void beep_3k() {  ////////////////////////////////// 3KhZ BEEP
  221.   unsigned long currentMillis = millis();
  222.   if (currentMillis - beepMillis >= 2000) {
  223.     if (beep == 0) {
  224.       tone(12, 3000, 1000);
  225.     } else {
  226.       tone(12, 3000, 1000);
  227.     }
  228.     beepMillis = currentMillis;
  229.   }
  230. }
  231. void inputLed(int input) {  ////////////////////////////////// LED FOR SOURCE
  232.   source = input;
  233.   int r = 0, g = 0, b = 0;
  234.   switch (input) {
  235.     case 0:  // WiFi - net
  236.       // if (powerState == 0) break;
  237.       r = 59;
  238.       g = 60;
  239.       b = 61;
  240.       break;
  241.     case 1:  // Bluetooth - bt
  242.       // if (powerState == 0) break;
  243.       r = 0;
  244.       g = 0;
  245.       b = 50;
  246.       break;
  247.     case 2:  // Line-In - line_in
  248.       // if (powerState == 0) break;
  249.       r = 0;
  250.       g = 50;
  251.       b = 0;
  252.       break;
  253.     case 3:  // SPDIF - opt
  254.       // if (powerState == 0) break;
  255.       r = 50;
  256.       g = 0;
  257.       b = 0;
  258.       break;
  259.     case 4:  // USB-DAC - usb dac
  260.       r = 12;
  261.       g = 67;
  262.       b = 5;
  263.  
  264.       break;
  265.     case 5:  //Standby
  266.       r = 55;
  267.       g = 35;
  268.       b = 0;
  269.       break;
  270.   }
  271.  
  272.   for (int i = 0; i < NUMPIXELS; i++) {
  273.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  274.   }
  275.   pixels.show();  // Send the updated pixel colors to the hardware.
  276. }
  277. void functionLed(int function) {  ////////////////////////////////// LED FOR FUNCTIONS
  278.   task = function;
  279.   int r = 0, g = 0, b = 0;
  280.   switch (function) {
  281.     case 0:  // task is "ON"
  282.       if (powerState == 0) break;
  283.       r = 49;
  284.       g = 50;
  285.       b = 51;
  286.       break;
  287.     case 1:  // task is "OFF"
  288.       if (powerState == 0) break;
  289.       r = 0;
  290.       g = 0;
  291.       b = 50;
  292.       break;
  293.     case 2:  // task is "DC ERROR"
  294.       if (powerState == 0) break;
  295.       r = 0;
  296.       g = 50;
  297.       b = 0;
  298.       break;
  299.     case 3:  // task is "Temp ERROR"
  300.       if (powerState == 0) break;
  301.       r = 50;
  302.       g = 0;
  303.       b = 0;
  304.       break;
  305.     case 4:  // task is "System starting..."
  306.       r = 55;
  307.       g = 35;
  308.       b = 0;
  309.       break;
  310.   }
  311.   for (int i = 0; i < NUMPIXELS; i++) {
  312.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  313.   }
  314.   pixels.show();  // Send the updated pixel colors to the hardware.
  315. }
  316. void senseBrightness() {  ////////////////////////////////// SENSE BRIGHTNESS
  317.   unsigned long currentTime = millis();
  318.   if (currentTime - previousTime_1 >= eventTime_1_LDR) {
  319.     String m;
  320.     switch (analogRead(LDR)) {
  321.       case 0 ... 40:
  322.         dimN = 25;
  323.         m = "Dark";
  324.         break;
  325.       case 41 ... 800:
  326.         dimN = 40;
  327.         m = "Light";
  328.         break;
  329.       case 801 ... 2000:
  330.         dimN = 60;
  331.         m = "Bright";
  332.         break;
  333.       case 2001 ... 3200:
  334.         dimN = 80;
  335.         m = "Very Bright";
  336.         break;
  337.       case 3201 ... 4500:
  338.         dimN = 100;
  339.         m = "Very Very Bright";
  340.         break;
  341.     }
  342.     if (dim != dimN) {
  343.       nextion.val("ampSetup.nDimESP", dimN);
  344.       nextion.val("ampSetup.hSlideESP", dimN);
  345.       Serial.println(" => " + m);
  346.       Serial.println(analogRead(LDR));
  347.       dim = dimN;
  348.     }
  349.     previousTime_1 = currentTime;
  350.   }
  351. }
  352. void senseTemp() {  ////////////////////////////////// SENSE TEMPERATURE
  353.   static unsigned long timer1, timer2;
  354.   if (millis() - timer1 > first) {
  355.     timer1 = millis();
  356.     //Serial.print("Temp 1: ");
  357.     //Serial.println(tmp1.readTemperature('c'));
  358.     nextion.val("ampSetup.nTempLeft", tmp1.readTemperature('c'));
  359.   }
  360.   if (millis() - timer2 > second) {
  361.     timer2 = millis();
  362.     //Serial.print("Temp 2: ");
  363.     //Serial.println(tmp2.readTemperature('C'));
  364.     nextion.val("ampSetup.nTempRight", tmp2.readTemperature('c'));
  365.   }
  366. }
  367. void readProtection() {  ////////////////////////////////// READ COMPLETE PROTECTION (AC+DC)
  368.   dcErrorRight = digitalRead(dcSenseRight);
  369.   dcErrorLeft = digitalRead(dcSenseLeft);
  370.   acError = digitalRead(acSense);
  371.  
  372.   if (dcErrorRight != dcErrorRightLast) {
  373.     if (dcErrorRight == 0) {
  374.       if (debug == 2) Serial.println("Right channel is OK!");
  375.       nextion.systemVal("warning", 0);
  376.       nextion.val("ampSetup.vaRight", 0);
  377.       if (dcErrorLeft == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
  378.     }
  379.     if (dcErrorRight == 1) {
  380.       if (debug == 2) Serial.println("DC voltage on Right channel!");
  381.       nextion.systemVal("warning", 1);
  382.       nextion.val("ampSetup.vaRight", 1);
  383.       if (self_test == 1) nextion.val("boot.vaProtection", 0);
  384.     }
  385.     dcErrorRightLast = dcErrorRight;
  386.   }
  387.  
  388.   if (dcErrorLeft != dcErrorLeftLast) {
  389.     if (dcErrorLeft == 0) {
  390.       if (debug == 2) Serial.println("Left channel is OK!");
  391.       nextion.systemVal("warning", 0);
  392.       nextion.val("ampSetup.vaLeft", 0);
  393.       if (dcErrorRight == 0 && self_test == 1) nextion.val("boot.vaProtection", 1);
  394.     }
  395.     if (dcErrorLeft == 1) {
  396.       if (debug == 2) Serial.println("DC voltage on Left channel!");
  397.       nextion.systemVal("warning", 1);
  398.       nextion.val("ampSetup.vaLeft", 1);
  399.       if (self_test == 1) nextion.val("boot.vaProtection", 0);
  400.     }
  401.     dcErrorLeftLast = dcErrorLeft;
  402.   }
  403.  
  404.   if (acError != acErrorLast) {
  405.     if (ac_protect == 1) {
  406.       Serial.println("Interrupt on AC pin");
  407.     }
  408.     if (acError == 0) {
  409.       if (debug == 2) Serial.println("AC is OK!");
  410.       nextion.systemVal("warning", 0);
  411.       nextion.val("ampSetup.vaAC", 0);
  412.       nextion.val("boot.vaSupply", 1);
  413.     }
  414.     if (acError == 1) {
  415.       if (debug == 2) Serial.println("Missing AC voltage!");
  416.       nextion.systemVal("warning", 1);
  417.       nextion.val("ampSetup.vaAC", 1);
  418.       nextion.val("boot.vaSupply", 1);  // -> 0
  419.       ac_protect = 0;
  420.     }
  421.     acErrorLast = acError;
  422.   }
  423. }
  424. void readAC() {  ////////////////////////////////// READ POWER SUPPLY
  425.   acError = digitalRead(acSense);
  426.  
  427.   if (acError != acErrorLast) {
  428.     if (ac_protect == 1) {
  429.       Serial.println("Interrupt on AC pin");
  430.     }
  431.     if (acError == 0) {
  432.       if (debug == 2) Serial.println("AC is OK!");
  433.       nextion.systemVal("warning", 0);
  434.       nextion.val("ampSetup.vaAC", 0);
  435.       nextion.val("boot.vaSupply", 1);
  436.     }
  437.     if (acError == 1) {
  438.       if (debug == 2) Serial.println("Missing AC voltage!");
  439.       nextion.systemVal("warning", 1);
  440.       nextion.val("ampSetup.vaAC", 1);
  441.       nextion.val("boot.vaSupply", 1);  // -> 0
  442.       ac_protect = 0;
  443.     }
  444.     acErrorLast = acError;
  445.   }
  446. }
  447. void setup() {  ////////////////////////////////// START OF SETUP
  448.   Wire.begin(SDA, SCL);
  449.   Serial.begin(115200);
  450.   Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
  451.   uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  452.   while (!Serial1)
  453.     ;
  454.   while (!uart)
  455.     ;
  456.   rtc.begin();
  457.   tmp1.begin();
  458.   tmp2.begin();
  459.   pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
  460.   pinMode(relay, OUTPUT);
  461.   delay(1);
  462.   //digitalWrite(speaker, 0);
  463.   digitalWrite(relay, 0);
  464.   pinMode(powerSW, INPUT_PULLUP);  // buttons is active LOW
  465.   pinMode(dcSenseRight, INPUT_PULLUP);
  466.   pinMode(dcSenseLeft, INPUT_PULLUP);
  467.   pinMode(acSense, INPUT_PULLUP);
  468.   rotaryEncoder.begin();
  469.   rotaryEncoder.setup(readEncoderISR);
  470.   //ir***********************************************************************************
  471.   Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
  472.   // Start the receiver and if not 3. parameter specified, take LED_BUILTIN pin from the internal boards definition as default feedback LED
  473.   IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  474.   Serial.print(F("Ready to receive IR signals of protocols: "));
  475.   printActiveIRProtocols(&Serial);
  476.   Serial.println(F("at pin " STR(IR_RECEIVE_PIN)));
  477.   //ir***********************************************************************************
  478.   inputLed(4);
  479.   nextion.pageChange(boot);
  480.   nextion.txt("boot.infoText", "System is starting...");
  481.   nextion.val("boot.vaMCU", 1);
  482.   delay(1000);
  483.   nextion.val("boot.vaRTC", 1);
  484.   delay(1000);
  485.   nextion.val("boot.vaArylic", 0);
  486.  
  487.  
  488.   bool circleValues = false;
  489.   /*Rotary acceleration introduced 25.2.2021.
  490.    * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
  491.    * without accelerateion you need long time to get to that number
  492.    * Using acceleration, faster you turn, faster will the value raise.
  493.    * For fine tuning slow down.
  494.    */
  495.   //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
  496.   rotaryEncoder.setAcceleration(0);  //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
  497.   rotaryEncoder.setEncoderValue(digiVolume);
  498.   rotaryEncoder.setBoundaries(0, 100, circleValues);      //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  499.   attachInterrupt(dcSenseRight, stateRight_ISR, RISING);  //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
  500.   attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
  501.   attachInterrupt(acSense, stateAC_ISR, FALLING);
  502.   //initWiFi();   //in the kitchen, no WIFI :D
  503.   //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
  504.   //Serial.println(WiFi.localIP());
  505.   //Serial.println(LocalIP);
  506.   if (debug) Serial.println("Starting..");
  507.   if (debug == 1) Serial.println("Started...");
  508.   if (debug == 1) Serial.println(compile_date);
  509.   uart.print("PMT:1;");
  510.   uart.print("BEP:0;");
  511.   uart.print("BEP;");
  512.   uart.print("PMT;");
  513.   nextion.touchSet("main.preset", 0);
  514.   dcErrorRight = digitalRead(dcSenseRight);
  515.   dcErrorLeft = digitalRead(dcSenseLeft);
  516.   acError = digitalRead(acSense);
  517.   //if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
  518.   uart.print("SYS:REBOOT;");  //Reboot Digi
  519.   Serial.println(compile_date);
  520.   Serial.println("Arylic_0823");
  521. }
  522. void loop() {  ////////////////////////////////// START OF LOOP
  523.   if (strengthMillis + 1000 < millis() && source == 0) {
  524.     uart.print("WSS;");
  525.     strengthMillis = millis();
  526.   }
  527.   if (strengthMillis + 1000 < millis() && source == 1) {
  528.     uart.print("BSS;");
  529.     strengthMillis = millis();
  530.   }
  531.   if (self_test == 0) {
  532.     readProtection();
  533.     senseBrightness();
  534.     senseTemp();
  535.     /*
  536. // devState == 0
  537. // devState == 1 -> initial tests (i2c devices, temp, RTC, AC, DC L + R ->  DIGI), running self test
  538.     - runing selftest -> Standby or Error page
  539. // devstate == 2 -> Standby
  540.     - IR power on/off
  541.     - Power SW
  542. // devState == 3 -> Running...
  543.     - work DIGI all funkction, STA
  544.     - encoder, IR control, Nextion control, protect check, temp, etc.
  545. // devstate == 4 -> Error mode -> page 2  !!! SPEAKERS & DIGI OFF !!!
  546.     - check protection, temp, main power supply
  547.     - power off?
  548.  
  549. switch (devState){
  550.   case 0:
  551.   break;
  552.   case 1:
  553.     devState1
  554.     .
  555.     .
  556.     break;
  557.   case 2:
  558. }
  559. */
  560.     if (IrReceiver.decode()) {  //////////// IR REMOTE HANDLING
  561.       Serial.println(NextionPage);
  562.       IrReceiver.printIRResultShort(&Serial);
  563.       IrReceiver.printIRSendUsage(&Serial);
  564.       if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
  565.         //Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));  // We have an unknown protocol here, print more inf
  566.         IrReceiver.printIRResultRawFormatted(&Serial, true);
  567.       }
  568.       Serial.println();
  569.       IrReceiver.resume();                                                             // Enable receiving of the next value
  570.       if (IrReceiver.decodedIRData.command == 0x45 && (irMillis + 5000) < millis()) {  // Power button && irLastCode != IrReceiver.decodedIRData.command
  571.         if (NextionPage == 2) {                                                        //0
  572.           nextion.click("powerOff", 1);
  573.         }
  574.         if (NextionPage == 1) {  //2
  575.           nextion.click("powerOn", 1);
  576.         }
  577.         irMillis = millis();
  578.         Serial.println("Pressed: POWER");
  579.       } else if (IrReceiver.decodedIRData.command == 0x19) {  // BT button
  580.         uart.print("SRC:BT;");
  581.         Serial.println("Pressed: BT");
  582.       } else if (IrReceiver.decodedIRData.command == 0xD) {  // FM button
  583.         //uart.print("SRC:BT;");
  584.         Serial.println("Pressed: FM");
  585.       } else if (IrReceiver.decodedIRData.command == 0x09 && irVolMillis + 300 < millis()) {  // VolUp
  586.         if (digiVolume < 100) {
  587.           digiVolume = digiVolume + volStep;
  588.           if (digiVolume > 100) digiVolume = 100;
  589.         }
  590.         irVolMillis = millis();
  591.         Serial.println(digiVolume);
  592.         uart.print("VOL:" + String(digiVolume) + ";");
  593.         Serial.println("Pressed: VOL UP");
  594.       } else if (IrReceiver.decodedIRData.command == 0x15 && irVolMillis + 300 < millis()) {  // VolDown
  595.         if (digiVolume > 0) {
  596.           digiVolume = digiVolume - volStep;
  597.           if (digiVolume < 0) digiVolume = 0;
  598.         }
  599.         irVolMillis = millis();
  600.         Serial.println(digiVolume);
  601.         uart.print("VOL:" + String(digiVolume) + ";");
  602.         Serial.println("Pressed: VOL DOWN");
  603.       } else if (IrReceiver.decodedIRData.command == 0x46 && (irMillis + 5000) < millis()) {  // Input stepping
  604.         if (sourceS < 4) {
  605.           sourceS++;
  606.         } else {
  607.           sourceS = 0;
  608.         }
  609.         String inp[5] = { "NET", "BT", "LINE-IN", "USBDAC", "OPT" };
  610.         uart.print("SRC:" + inp[sourceS] + ";");
  611.         Serial.println("SRC:" + inp[sourceS] + ";");
  612.         Serial.println(sourceS);
  613.         irMillis = millis();
  614.         Serial.println("Pressed: MODE (input stepping)");
  615.       } else if (IrReceiver.decodedIRData.command == 0x7 && (irMillis + 5000) < millis()) {  // Muting
  616.         if (NextionPage == 0) {
  617.           uart.print("MUT:1;");
  618.         }
  619.         if (NextionPage == 0) {
  620.           uart.print("MUT:0;");
  621.         }
  622.         irMillis = millis();
  623.         Serial.println("Pressed: MUTE");
  624.       } else if (IrReceiver.decodedIRData.command == 0x47) {  // Scan
  625.         // uart.print("SRC:NET;");
  626.         Serial.println("Pressed: SCAN");
  627.       } else if (IrReceiver.decodedIRData.command == 0x43) {  // >> / Next track, only available in BT/NET/USB mode (*1)
  628.         uart.print("NXT;");
  629.         Serial.println("Pressed: NEXT");
  630.       } else if (IrReceiver.decodedIRData.command == 0x40) {  // << / Previous track, only available in BT/NET/USB mode (*1)
  631.         uart.print("PRV;");
  632.         Serial.println("Pressed: PREV");
  633.       } else if (IrReceiver.decodedIRData.command == 0x44) {  // Play/Pause track, only available in BT/NET/USB mode (*1)
  634.         uart.print("POP;");
  635.         Serial.println("Pressed: PLAY/PAUSE");
  636.       } else if (IrReceiver.decodedIRData.command == 0xC) {  // 1
  637.         uart.print("PST:1;");
  638.         // digi.volume=digi.maxVolume-10;
  639.         nextion.txt("main.preset", "Preset 1");
  640.         Serial.println("Pressed: BT1");
  641.       } else if (IrReceiver.decodedIRData.command == 0x18) {  // 2
  642.         uart.print("PST:2;");
  643.         nextion.txt("main.preset", "Preset 2");
  644.         Serial.println("Pressed: BT2");
  645.       } else if (IrReceiver.decodedIRData.command == 0x5E) {  // 3
  646.         uart.print("PST:3;");
  647.         nextion.txt("main.preset", "Preset 3");
  648.         Serial.println("Pressed: BT3");
  649.       } else if (IrReceiver.decodedIRData.command == 0x8) {  // 4
  650.         uart.print("PST:4;");
  651.         nextion.txt("main.preset", "Preset 4");
  652.         Serial.println("Pressed: BT4");
  653.       } else if (IrReceiver.decodedIRData.command == 0x1C) {  // 5
  654.         uart.print("PST:5;");
  655.         nextion.txt("main.preset", "Preset 5");
  656.         Serial.println("Pressed: BT5");
  657.       } else if (IrReceiver.decodedIRData.command == 0x5A) {  // 6
  658.         uart.print("PST:6;");
  659.         nextion.txt("main.preset", "Preset 6");
  660.         Serial.println("Pressed: BT6");
  661.       } else if (IrReceiver.decodedIRData.command == 0x42) {  // 7
  662.         uart.print("PST:7;");
  663.         nextion.txt("main.preset", "Preset 7");
  664.         Serial.println("Pressed: BT7");
  665.       } else if (IrReceiver.decodedIRData.command == 0x52) {  // 8
  666.         uart.print("PST:8;");
  667.         nextion.txt("main.preset", "Preset 8");
  668.         Serial.println("Pressed: BT8");
  669.       } else if (IrReceiver.decodedIRData.command == 0x4A) {  // 9
  670.         uart.print("PST:9;");
  671.         nextion.txt("main.preset", "Preset 9");
  672.         Serial.println("Pressed: BT9");
  673.       } else if (IrReceiver.decodedIRData.command == 0x16) {  // 0
  674.         uart.print("PST:10;");
  675.         nextion.txt("main.preset", "Preset 10");
  676.         Serial.println("Pressed: BT0");
  677.       }
  678.     }
  679.  
  680.     if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {  //////////// POWER SW HANDLING
  681.       if (debug == 1) Serial.println(digitalRead(powerSW));
  682.       if (powerState == 0) {
  683.         powerState = 1;
  684.         uart.print("SYS:REBOOT;");
  685.         Serial.println("---------- Digi REBOOT... ----------");
  686.         tone(12, 1000, 50);
  687.       } else {
  688.         powerState = 0;
  689.         uart.print("SYS:STANDBY;");
  690.         Serial.println("---------- Digi STANDBY... ----------");
  691.         tone(12, 1000, 50);
  692.         inputLed(4);
  693.       }
  694.       lastPressed = millis();
  695.     }
  696.     lastPowerState = digitalRead(powerSW);  //1
  697.     rotary_loop();
  698.     while (uart.available()) {  //////////// UART HANDLING FOR ARYLIC
  699.       nextion.val("boot.vaArylic", 1);
  700.       sReceived = uart.readStringUntil('\n');
  701.       sReceived.trim();
  702.       //Serial.println(sReceived);
  703.       if (debug == 1) Serial.println("uart:___|----------" + sReceived);
  704.       if (debug == 1) Serial.println("");
  705.       if (sReceived.startsWith("PLA:0")) {
  706.         nextion.txt("main.infoText", "Szünet/Megállítva");
  707.       } else if (sReceived.startsWith("PLA:1")) {
  708.         nextion.txt("main.infoText", "Lejátszás...");
  709.       } else if (sReceived.startsWith("STA:")) {  //////////// STA HANDLING
  710.         if (initDigi == 1) {
  711.           initDigi = 0;
  712.           nextion.val("boot.vaArylic", 1);
  713.         } else if (NextionPage != 6) {
  714.           nextion.pageChange(main);
  715.         }
  716.         nextion.vis("main.infoText", 0);
  717.         if (debug == 1) Serial.println("STA received");
  718.         if (debug == 1) Serial.println("Before: " + sReceived);
  719.         nextion.txt("main.infoText", "");
  720.         nextion.vis("main.title", 0);
  721.         nextion.vis("main.elapsed", 0);
  722.         nextion.vis("main.vendor", 0);
  723.         nextion.touchSet("main.preset", 0);
  724.         reduce4();
  725.  
  726.         if (sReceived.startsWith("NET")) {  //////////// WIFI
  727.           sourceS = 0;
  728.           inputLed(0);
  729.           meta(1, 1, 1, 1);
  730.           if (debug == 1) Serial.println("...WIFI...");
  731.           nextion.txt("main.input", "WiFi");
  732.         } else if (sReceived.startsWith("BT")) {  //////////// BLUETOOTH
  733.           sourceS = 1;
  734.           inputLed(1);
  735.           meta(0, 0, 0, 0);
  736.           if (debug == 1) Serial.println("...BLUETOOTH...");
  737.           nextion.txt("main.input", "Bluetooth");
  738.         } else if (sReceived.startsWith("LINE-IN")) {  //////////// LINE-IN
  739.           sourceS = 2;
  740.           inputLed(2);
  741.           meta(0, 0, 0, 0);
  742.           if (debug == 1) Serial.println("...LINE-IN...");
  743.           nextion.txt("main.input", "Line In");
  744.         } else if (sReceived.startsWith("USBDAC")) {  //////////// USB DAC
  745.           sourceS = 3;
  746.           inputLed(3);
  747.           meta(0, 0, 0, 0);
  748.           if (debug == 1) Serial.println("...USB DAC...");
  749.           nextion.txt("main.input", "USB DAC");
  750.         } else if (sReceived.startsWith("OPT")) {  //////////// USB DAC
  751.           sourceS = 4;
  752.           inputLed(4);
  753.           meta(0, 0, 0, 0);
  754.           if (debug == 1) Serial.println("...SPDIF...");
  755.           nextion.txt("main.input", "SPDIF");
  756.         }
  757.  
  758.       } else if (sReceived.startsWith("SYS:STANDBY")) {  //////////// POWER OFF HANDLING
  759.         inputLed(4);
  760.         Serial.println("Stand by mode...");
  761.         tone(12, 1000, 50);
  762.         nextion.pageChange(standby);
  763.         digitalWrite(relay, 0);
  764.         nextionSetTimebit = 1;
  765.       } else if (sReceived.startsWith("SRC:")) {  //////////// INPUT SOURCE HANDLING
  766.         nextion.vis("main.title", 0);
  767.         nextion.vis("main.elapsed", 0);
  768.         nextion.vis("main.vendor", 0);
  769.         nextion.touchSet("main.preset", 0);
  770.         nextion.pageChange(main);
  771.         reduce4();
  772.  
  773.         if (sReceived.startsWith("NET")) {  //////////// WIFI
  774.           sourceS = 0;
  775.           inputLed(0);
  776.           meta(1, 1, 1, 1);
  777.           if (debug == 1) Serial.println("...WIFI...");
  778.           nextion.txt("main.input", "WiFi");
  779.         } else if (sReceived.startsWith("BT")) {  //////////// BLUETOOTH
  780.           sourceS = 1;
  781.           inputLed(1);
  782.           meta(0, 0, 0, 0);
  783.           if (debug == 1) Serial.println("...BLUETOOTH...");
  784.           nextion.txt("main.input", "Bluetooth");
  785.         } else if (sReceived.startsWith("LINE-IN")) {  //////////// LINE-IN
  786.           sourceS = 2;
  787.           inputLed(2);
  788.           meta(0, 0, 0, 0);
  789.           if (debug == 1) Serial.println("...LINE-IN...");
  790.           nextion.txt("main.input", "Line In");
  791.         } else if (sReceived.startsWith("USBDAC")) {  //////////// USB DAC
  792.           sourceS = 3;
  793.           inputLed(3);
  794.           meta(0, 0, 0, 0);
  795.           if (debug == 1) Serial.println("...USB DAC...");
  796.           nextion.txt("main.input", "USB DAC");
  797.         } else if (sReceived.startsWith("OPT")) {  //////////// USB DAC
  798.           sourceS = 4;
  799.           inputLed(4);
  800.           meta(0, 0, 0, 0);
  801.           if (debug == 1) Serial.println("...SPDIF...");
  802.           nextion.txt("main.input", "SPDIF");
  803.         }
  804.       } else if (sReceived.startsWith("VOL:")) {  //////////// VOLUME HANDLING
  805.         reduce4();
  806.         int index = sReceived.indexOf(';');
  807.         sReceived = sReceived.substring(0, index);
  808.         if (sReceived == "100") {
  809.           nextion.txt("volText", "MAX");
  810.         } else if (sReceived == "0") {
  811.           nextion.txt("volText", "MIN");
  812.         } else {
  813.           if (debug == 1) Serial.println("volume:  -|:" + sReceived);
  814.           digiVolume = sReceived.toInt();
  815.           nextion.txt("volText", sReceived);
  816.         }
  817.         nextion.systemVal("digiVol", digiVolume);
  818.         rotaryEncoder.setEncoderValue(digiVolume);
  819.       } else if (sReceived.startsWith("MUT:")) {  //////////// MUTE HANDLING
  820.         reduce4();
  821.         sReceived = sReceived.substring(0, 1);
  822.         if (debug == 1) Serial.println("Mute:_____/:|" + sReceived);
  823.         if (sReceived == "1") {
  824.           dmute = 1;
  825.           nextion.txt("volText", "MIN");
  826.           nextion.systemVal("digiVol", 0);
  827.         } else if (sReceived == "0") {
  828.           dmute = 0;
  829.           nextion.txt("volText", String(digiVolume));
  830.           nextion.systemVal("digiVol", digiVolume);
  831.         }
  832.  
  833.       } else if (sReceived.startsWith("BTC:")) {  //////////// BLUETOOTH CONNECTION HANDLING
  834.         reduce4();
  835.         sReceived = sReceived.substring(0, 1);
  836.         if (sReceived == "1") {
  837.           nextion.txt("main.infoText", "CONNECTED");
  838.           uart.print("TIT;");
  839.         } else if (sReceived == "0") {
  840.           nextion.txt("main.infoText", "DISCONNECTED");
  841.         }
  842.       } else if (sReceived.endsWith("SYS:ON;")) {  //////////// POWER ON HANDLING
  843.         tone(12, 2000, 50);
  844.         nextion.txt("standby.powerOn", "STARTING");
  845.         nextion.setPco("standby.powerOn", 34784);
  846.         Serial.println("arrived SYS:ON...(1)");
  847.       } else if (sReceived.startsWith("NET:")) {  //////////// NETWORK CONNECTION HANDLING
  848.         reduce4();
  849.         sReceived = sReceived.substring(0, 1);
  850.         if (sReceived == "1") {
  851.           nextion.txt("main.infoText", "CONNECTED");
  852.           inputLed(0);
  853.           uart.print("TIT;");
  854.           nextion.touchSet("main.preset", 1);
  855.           //inputLed(0);
  856.         } else if (sReceived == "0") {
  857.           nextion.txt("main.infoText", "DISCONNECTED");
  858.           nextion.touchSet("main.preset", 0);
  859.           inputLed(5);
  860.         }
  861.       } else if (sReceived.startsWith("TIT:")) {  //////////// Title
  862.         reduce4();
  863.         if (debug == 1) Serial.println("Title: " + sReceived);
  864.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  865.         nextion.txt("main.title", sReceived);
  866.  
  867.       } else if (sReceived.startsWith("ELP:")) {  //////////// Elapsed playing time
  868.         reduce4();
  869.         int index = sReceived.indexOf("/");
  870.         sReceived = sReceived.substring(0, index);
  871.         // Serial1.println(sReceived);
  872.         long time = sReceived.toInt();
  873.         time = time / 100;
  874.         int tenth = time % 10;
  875.         time = time / 10;
  876.         long hour = time / 3600;
  877.         time = time - (hour * 3600);
  878.         long min = time / 60;
  879.         long sec = time - (min * 60);
  880.         String timeS = "Time: ";
  881.         if (hour < 10) timeS += "0";
  882.         timeS += String(hour) + ":";
  883.         if (min < 10) timeS += "0";
  884.         timeS += String(min) + ":";
  885.         if (sec < 10) timeS += "0";
  886.         timeS += String(sec);  // + "." + String(tenth);
  887.         if (time > 0) nextion.txt("main.elapsed", timeS);
  888.  
  889.       } else if (sReceived.startsWith("BAS:")) {  //////////// BASS
  890.         reduce4();
  891.         int bass = sReceived.toInt();
  892.         nextion.val("digiSetup.nbass", bass);
  893.         if (bass < 0) {
  894.           bass = 11 - abs(bass);
  895.         } else {
  896.           bass = bass + 11;
  897.         }
  898.         nextion.val("digiSetup.hbass", bass);
  899.  
  900.       } else if (sReceived.startsWith("TRE:")) {  //////////// TREBLE
  901.         reduce4();
  902.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  903.         int treb = sReceived.toInt();
  904.         nextion.val("digiSetup.ntreb", treb);
  905.         if (treb < 0) {
  906.           treb = 11 - abs(treb);
  907.         } else {
  908.           treb = treb + 11;
  909.         }
  910.         nextion.val("digiSetup.htreb", treb);
  911.  
  912.       } else if (sReceived.startsWith("WSS:")) {  //////////// WIFI SIGNAL STRENGTH
  913.         reduce4();
  914.         //nextion.txt("main.infoText", sReceived);
  915.         Serial.println("WiFi signal strength: " + sReceived);
  916.  
  917.       } else if (sReceived.startsWith("BSS:")) {  //////////// BLUETOOTH SIGNAL STRENGTH
  918.         reduce4();
  919.         //nextion.txt("main.infoText", sReceived);
  920.         Serial.println("Bluetooth signal strength: " + sReceived);
  921.  
  922.  
  923.       } else if (sReceived.startsWith("MXV:")) {  //////////// Max volume, %
  924.         reduce4();
  925.         int volMax = sReceived.toInt();
  926.         nextion.val("digiSetup.nVolMax", volMax);
  927.         nextion.val("digiSetup.hVolMax", volMax);
  928.         if (debug == 2) Serial.println(volMax);
  929.  
  930.       } else if (sReceived.startsWith("VBS:")) {  //////////// Virtual Bass ON/OFF
  931.         reduce4();
  932.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  933.         if (sReceived == "1") {
  934.           nextion.val("digiSetup.vbs", 1);
  935.           if (debug == 2) Serial.println("VBS: on");
  936.         } else {
  937.           nextion.val("digiSetup.vbs", 0);
  938.           if (debug == 2) Serial.println("VBS: off");
  939.         }
  940.       } else if (sReceived.startsWith("PMT:")) {  //////////// Promt Voice ON/OFF
  941.         reduce4();
  942.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  943.         if (sReceived == "1") {
  944.           nextion.val("digiSetup.pmt", 1);
  945.           if (debug == 2) Serial.println("PMT: on");
  946.         } else {
  947.           nextion.val("digiSetup.pmt", 0);
  948.           if (debug == 2) Serial.println("PMT: off");
  949.         }
  950.  
  951.       } else if (sReceived.startsWith("NAM:")) {  //////////// Device name
  952.         reduce4();
  953.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  954.         String dname, Nname;
  955.         int h = 16, sz = 0, dsz = 0;
  956.         if (sReceived.length() > 0) {
  957.           for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  958.             dname = sReceived.substring(i, i + 1);
  959.             if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  960.               sz = dname.toInt();
  961.               // if (debug) Serial.println(dname);
  962.             }
  963.             if (dname == "A") sz = 10;
  964.             if (dname == "B") sz = 11;
  965.             if (dname == "C") sz = 12;
  966.             if (dname == "D") sz = 13;
  967.             if (dname == "E") sz = 14;
  968.             if (dname == "F") sz = 15;
  969.             // sz += sz * h;
  970.             if (h == 0) {
  971.               dsz += sz;
  972.               // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
  973.               Nname += char(dsz);
  974.               dsz = 0;
  975.             } else {
  976.               dsz = sz * 16;
  977.             }
  978.             h = 16 - h;
  979.           }
  980.           if (debug == 1) Serial.println(Nname);
  981.           nextion.txt("main.NAME", Nname);
  982.         }
  983.       } else if (sReceived.startsWith("IPA:")) {  //////////// Device IP address for connecting to browser's control panel, psw:admin
  984.         reduce4();
  985.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  986.         nextion.txt("digiSetup.digiIP", sReceived);
  987.       } else if (sReceived.startsWith("TME:")) {  //////////// Time, undefinied time zone, your need offset!
  988.  
  989.         String dc = sReceived.substring(21, 23);
  990.         String nc = sReceived;
  991.         int hour = dc.toInt();
  992.         if (hour % 2) {
  993.           dc = " ";
  994.         } else {
  995.           dc = ":";
  996.         }
  997.         String st = sReceived.substring(15, 17);
  998.         hour = st.toInt();
  999.         hour = hour + offset;  //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
  1000.         if (hour < 10) {
  1001.           sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
  1002.         } else {
  1003.           sReceived = String(hour) + dc + sReceived.substring(18, 20);
  1004.         }
  1005.         if (debug == 1) Serial.println(sReceived);
  1006.         nextion.txt("digiSetup.digiTime", sReceived);
  1007.         dc = nc.substring(4, 8);
  1008.         if (debug == 1) Serial.println(dc);
  1009.         if (debug == 1) Serial.println(nextionSetTimebit);
  1010.         if (dc != "2000" && nextionSetTimebit == 1) {
  1011.           dc = nc.substring(15, 17);
  1012.           int hour = dc.toInt();
  1013.           hour = (hour + offset + 24) % 24;
  1014.           dc = nc.substring(18, 20);
  1015.           int min = dc.toInt();
  1016.           dc = nc.substring(21, 23);
  1017.           int sec = dc.toInt();
  1018.           nextion.setTime(hour, min, sec);
  1019.           nextionSetTimebit = 0;
  1020.         }
  1021.       } else if (sReceived.startsWith("VND:")) {  //////////// Vendor - Tidal, Spotify, etc...
  1022.         reduce4();
  1023.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  1024.         nextion.txt("main.vaVendor", sReceived);
  1025.       } else if (sReceived.startsWith("PST:")) {  //////////// Preset, not response, only command (1-10)
  1026.         reduce4();
  1027.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  1028.         if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
  1029.       }
  1030.       sReceived = "";
  1031.     }
  1032.     while (Serial1.available()) {  //////////// UART HANDLING FOR NEXTION
  1033.       String nReceived = Serial1.readStringUntil(';');
  1034.       if (nReceived.startsWith("VST:")) { //////////// VST HANDLING
  1035.         String nTemp = nReceived.substring(4); //////////// VST HANDLING
  1036.         volStep = nTemp.toInt();
  1037.         Serial.println("VST: " + nReceived);
  1038.       }
  1039.       if (nReceived.startsWith("NPG:")) {  //////////// NEXTION PAGE NUMBER
  1040.         reduce4n();
  1041.         Serial.println(nReceived + "  ");
  1042.         toDigi = 0;
  1043.       }
  1044.       if (nReceived == "SYS:STANDBY") {  //////////// STANDBY COMMAND
  1045.         digitalWrite(relay, 0);
  1046.         delay(powerOffDelayForRelay);
  1047.         inputLed(4);
  1048.         powerState = 0;
  1049.       }
  1050.       if (nReceived == "LDR:0") {  //////////// LDR OFF
  1051.         toDigi = 0;
  1052.         if (debug == 2) Serial.println("LDR OFF");
  1053.       }
  1054.       if (nReceived == "LDR:1") {  //////////// LDR ON
  1055.         toDigi = 0;
  1056.         if (debug == 2) Serial.println("LDR ON");
  1057.       }
  1058.       if (nReceived == "ESP:RESTART") { //////////// FULL RESTART
  1059.         ESP.restart();
  1060.       }
  1061.       if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
  1062.       if (toDigi == 1) uart.print(nReceived + ";");
  1063.       toDigi = 1;
  1064.     }
  1065.     if (NextionPage == 0)
  1066.       powerState = 1;
  1067.   } else {
  1068.     readProtection();
  1069.     if (acError == 1 && dcErrorRight == 0 && dcErrorLeft == 0) {  //AC ERROR =0
  1070.       self_test = 0;
  1071.       initDigi = 1;
  1072.       uart.print("SYS:REBOOT;");
  1073.       digitalWrite(relay, 1);
  1074.     }
  1075.     Serial.printf("Selftest %d\n", self_test);
  1076.   }
  1077. }  //////////////////// End of Loop  ////////////////////
  1078. void reduce4() {
  1079.   sReceived = sReceived.substring(4);
  1080. }
  1081. void reduce4n() {
  1082.   nReceived = nReceived.substring(4);
  1083. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement