Advertisement
GaabMM88

Untitled

Sep 30th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.89 KB | None | 0 0
  1. // If you select the preset on a mobile device, it is selected, but Nextion does not display it, you have to query the "preset"
  2.  
  3. const char compile_date[] = __DATE__;
  4. //Included with the name printing
  5. #include <HardwareSerial.h>
  6. #include "AiEsp32RotaryEncoder.h"
  7. #include "Arduino.h"
  8. #include "Wire.h"
  9. #include <Adafruit_NeoPixel.h>
  10. #include <esp_task_wdt.h>
  11. #include <WiFi.h>
  12. #include <RTClib.h>
  13.  
  14.  
  15. RTC_DS3231 rtc;
  16. HardwareSerial uart(2);  // Uso de la interfaz de hardware Serial2
  17.  
  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         // LED
  29. //#define speaker
  30. #define WS2812 2
  31. #define NUMPIXELS 8
  32. Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
  33.  
  34. const byte LDR = 36;                //Light Dependant Resistor
  35. const long eventTime_1_LDR = 1000;  //check brigtness in ms
  36. unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0;
  37. unsigned long ac_time = 0, ac_time_last = 0;
  38. boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
  39. int offset = 2, beep = 0, NextionPage;
  40. int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1;
  41. int initDigi = 0, toDigi = 1;
  42. int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1;  //variables for protection
  43. int debug = 2;                                                                                                      //1 - writing all data; 2 - only protection data
  44. int ac_protect = 0;
  45. int self_test = 1;
  46. int currentPage = -1;  // current page of nextion
  47. String sReceived;
  48. //instead of changing here, rather change numbers above
  49. AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
  50.  
  51. #if (1)
  52. const char* ssid = "TP-Link_F072";
  53. const char* password = "12778072";
  54. #else
  55. const char* ssid = "SirRouter";
  56. const char* password = "19801989";
  57. #endif
  58.  
  59. void initWiFi() {  // initialize WiFi
  60.   WiFi.mode(WIFI_STA);
  61.   WiFi.begin(ssid, password);
  62.   Serial.print("Connecting to WiFi ..");
  63.   while (WiFi.status() != WL_CONNECTED) {
  64.     Serial.print('.');
  65.     delay(1000);
  66.   }
  67.   Serial.println(WiFi.localIP());
  68. }
  69.  
  70. void rotary_onButtonClick() {  // initialize Encoder SW
  71.   static unsigned long lastTimePressed = 0;
  72.   //ignore multiple press in that time milliseconds
  73.   if (millis() - lastTimePressed < 500) {
  74.     return;
  75.   }
  76.   lastTimePressed = millis();
  77.   if (debug == 1) Serial.print("button pressed ");
  78.   if (debug == 1) Serial.print(millis());
  79.   if (debug == 1) Serial.println(" milliseconds after restart");
  80.   if (dmute == 0) {
  81.     uart.print("MUT:1;");
  82.   } else {
  83.     uart.print("MUT:0;");
  84.   }
  85. }
  86.  
  87. void beep_2k() {  // initialize 2kHz "beep"
  88.   unsigned long currentMillis = millis();
  89.   if (currentMillis - beepMillis >= 1000) {
  90.     if (beep == 0) {
  91.       tone(12, 2000, 50);
  92.     } else {
  93.       tone(12, 2000, 50);
  94.     }
  95.     beepMillis = currentMillis;
  96.   }
  97. }
  98.  
  99. void beep_3k() {  // initialize 3kHz "beep"
  100.   unsigned long currentMillis = millis();
  101.   if (currentMillis - beepMillis >= 2000) {
  102.     if (beep == 0) {
  103.       tone(12, 3000, 1000);
  104.     } else {
  105.       tone(12, 3000, 1000);
  106.     }
  107.     beepMillis = currentMillis;
  108.   }
  109. }
  110.  
  111. void rotary_loop() {  // initialize Encoder loop
  112.   if (rotaryEncoder.encoderChanged()) {
  113.     if (debug == 1) Serial.print("Value: ");
  114.     if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
  115.     uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
  116.     digiVolume = rotaryEncoder.readEncoder();
  117.   }
  118.   if (rotaryEncoder.isEncoderButtonClicked()) {
  119.     rotary_onButtonClick();
  120.   }
  121. }
  122.  
  123. void IRAM_ATTR readEncoderISR() {
  124.   rotaryEncoder.readEncoder_ISR();
  125. }
  126.  
  127. void inputLed(int input) {
  128.   source = input;
  129.   int r = 0, g = 0, b = 0;
  130.   switch (input) {
  131.     case 0:  // WiFi
  132.       if (powerState == 0) break;
  133.       r = 49;
  134.       g = 50;
  135.       b = 51;
  136.       break;
  137.     case 1:  // Bluetooth
  138.       if (powerState == 0) break;
  139.       r = 0;
  140.       g = 0;
  141.       b = 50;
  142.       break;
  143.     case 2:  // Line-In
  144.       if (powerState == 0) break;
  145.       r = 0;
  146.       g = 50;
  147.       b = 0;
  148.       break;
  149.     case 3:  // USB-DAC
  150.       if (powerState == 0) break;
  151.       r = 50;
  152.       g = 0;
  153.       b = 0;
  154.       break;
  155.     case 4:  // Standby
  156.       r = 55;
  157.       g = 35;
  158.       b = 0;
  159.       break;
  160.     case 5:  // OFF
  161.       r = 35;
  162.       g = 15;
  163.       b = 0;
  164.       break;
  165.   }
  166.   for (int i = 0; i < NUMPIXELS; i++) {
  167.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  168.     pixels.show();  // Send the updated pixel colors to the hardware.
  169.   }
  170. }
  171.  
  172. class c_NextionWrite {
  173. public:
  174.   void init(int speed, int RXN, int TXN) {
  175.     Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
  176.     // if (debug) Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
  177.   }
  178.   void txt(String Name, String text) {
  179.     Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  180.     if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  181.   }
  182.   void val(String Name, int value) {
  183.     Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  184.     if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  185.   }
  186.   void systemVal(String Name, int value) {
  187.     Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  188.     if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
  189.   }
  190.   void pageChange(int nr) {
  191.     Serial1.print("page " + String(nr) + "\xFF\xFF\xFF");
  192.     if (debug == 1) Serial.print("page " + String(nr) + "\xFF\xFF\xFF");
  193.     NextionPage = nr;
  194.   }
  195.   void setPco(String name, int pco) {  // for global variable need a page number / page name too
  196.     Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  197.     if (debug == 1) Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  198.   }
  199.   void timerEnable(String name, int en) {
  200.     Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
  201.   }
  202.   void vis(String name, int en) {
  203.     Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
  204.   }
  205.   void dim(int en) {
  206.     Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
  207.   }
  208.   void touchSet(String name, int en) {  //nextion.touchSet("preset", 0/1);
  209.     Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
  210.   }
  211.  
  212.   void setTime(int hour, int min, int sec) {
  213.     hour = (hour + 24) % 24;
  214.     Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF");
  215.     Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF");
  216.     Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF");
  217.     if (debug == 1) Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", hour, min, sec);
  218.     if (debug == 1) Serial.println("--------------------");
  219.   }
  220. };
  221.  
  222. c_NextionWrite nextion;
  223.  
  224. #define RXN_PIN 26  // Serial1 RX to Nextion TX
  225. #define TXN_PIN 25  // Serial1 TX to Nextion RX
  226. #define RX_PIN 27   // Serial2 RX a Amp TX
  227. #define TX_PIN 14   // Serial2 TX a Amp RX
  228. #define SDA 21      // I2C Thermometer, Expander, etc.
  229. #define SCL 22
  230.  
  231.  
  232. //////////////////// Start of Protection  ////////////////////
  233.  
  234. void IRAM_ATTR stateRight_ISR() {  //Right channel state
  235.   dcErrorRight = 1;
  236. }
  237. void IRAM_ATTR stateLeft_ISR() {  //Left channel state
  238.   dcErrorLeft = 1;
  239. }
  240. void IRAM_ATTR stateAC_ISR() {  //AC voltage state
  241.   ac_time = millis();
  242.   if (ac_time - ac_time_last > 250) {
  243.     acError = 1;
  244.     ac_protect = 1;
  245.     digitalWrite(relay, 0);
  246.     ac_time_last = ac_time;
  247.   }
  248. }
  249.  
  250. void senseBrightness() {
  251.   unsigned long currentTime = millis();
  252.   if (currentTime - previousTime_1 >= eventTime_1_LDR) {
  253.     String m;
  254.     switch (analogRead(LDR)) {
  255.       case 0 ... 40:
  256.         dimN = 25;
  257.         m = "Dark";
  258.         break;
  259.       case 41 ... 800:
  260.         dimN = 40;
  261.         m = "Light";
  262.         break;
  263.       case 801 ... 2000:
  264.         dimN = 60;
  265.         m = "Bright";
  266.         break;
  267.       case 2001 ... 3200:
  268.         dimN = 80;
  269.         m = "Very Bright";
  270.         break;
  271.       case 3201 ... 4500:
  272.         dimN = 100;
  273.         m = "Very Very Bright";
  274.         break;
  275.     }
  276.     if (dim != dimN) {
  277.       nextion.val("page2.nDimESP", dimN);
  278.       nextion.val("page2.hSlideESP", dimN);
  279.       Serial.println(" => " + m);
  280.       dim = dimN;
  281.     }
  282.     previousTime_1 = currentTime;
  283.   }
  284. }
  285.  
  286. void readProtection() {
  287.   dcErrorRight = digitalRead(dcSenseRight);
  288.   dcErrorLeft = digitalRead(dcSenseLeft);
  289.   acError = digitalRead(acSense);
  290.  
  291.   if (dcErrorRight != dcErrorRightLast) {
  292.     if (dcErrorRight == 0) {
  293.       if (debug == 2) Serial.println("Right channel is OK!");
  294.       nextion.systemVal("warning", 0);
  295.       nextion.val("page2.vaRight", 0);
  296.       if (dcErrorLeft == 0 && self_test == 1) nextion.val("page6.vaProtection", 1);
  297.     }
  298.     if (dcErrorRight == 1) {
  299.       if (debug == 2) Serial.println("DC voltage on Right channel!");
  300.       nextion.systemVal("warning", 1);
  301.       nextion.val("page2.vaRight", 1);
  302.       if (self_test == 1) nextion.val("page6.vaProtection", 0);
  303.     }
  304.     dcErrorRightLast = dcErrorRight;
  305.   }
  306.  
  307.   if (dcErrorLeft != dcErrorLeftLast) {
  308.     if (dcErrorLeft == 0) {
  309.       if (debug == 2) Serial.println("Left channel is OK!");
  310.       nextion.systemVal("warning", 0);
  311.       nextion.val("page2.vaLeft", 0);
  312.       if (dcErrorRight == 0 && self_test == 1) nextion.val("page6.vaProtection", 1);
  313.     }
  314.     if (dcErrorLeft == 1) {
  315.       if (debug == 2) Serial.println("DC voltage on Left channel!");
  316.       nextion.systemVal("warning", 1);
  317.       nextion.val("page2.vaLeft", 1);
  318.       if (self_test == 1) nextion.val("page6.vaProtection", 0);
  319.     }
  320.     dcErrorLeftLast = dcErrorLeft;
  321.   }
  322.  
  323.   if (acError != acErrorLast) {
  324.     if (ac_protect == 1) {
  325.       Serial.println("Interrupt on AC pin");
  326.     }
  327.     if (acError == 0) {
  328.       if (debug == 2) Serial.println("AC is OK!");
  329.       nextion.systemVal("warning", 0);
  330.       nextion.val("page2.vaAC", 0);
  331.       nextion.val("page6.vaSupply", 1);
  332.     }
  333.     if (acError == 1) {
  334.       if (debug == 2) Serial.println("Missing AC voltage!");
  335.       nextion.systemVal("warning", 1);
  336.       nextion.val("page2.vaAC", 1);
  337.       nextion.val("page6.vaSupply", 0);
  338.     }
  339.     acErrorLast = acError;
  340.     ac_protect = 0;
  341.   }
  342. }  //////////////////// End of Protection  ////////////////////
  343.  
  344.  
  345. void setup() {
  346.   pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
  347.   rtc.begin();
  348.   pinMode(relay, OUTPUT);
  349.   delay(1);
  350.   //digitalWrite(speaker, 0);
  351.   digitalWrite(relay, 0);
  352.   pinMode(powerSW, INPUT_PULLUP);  // buttons is active LOW
  353.   pinMode(dcSenseRight, INPUT_PULLUP);
  354.   pinMode(dcSenseLeft, INPUT_PULLUP);
  355.   pinMode(acSense, INPUT_PULLUP);
  356.   rotaryEncoder.begin();
  357.   rotaryEncoder.setup(readEncoderISR);
  358.   Wire.begin(SDA, SCL);
  359.   Serial.begin(115200);
  360.   Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
  361.   uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  362.   while (!Serial1)
  363.     ;
  364.   while (!uart)
  365.     ;
  366.   inputLed(4);
  367.   nextion.pageChange(7);
  368.   nextion.txt("page6.infoText", "System is starting...");
  369.   nextion.val("page6.vaMCU", 1);
  370.   delay(1000);
  371.   nextion.val("page6.vaRTC", 1);
  372.   delay(1000);
  373.   nextion.val("page6.vaArylic", 0);
  374.  
  375.  
  376.   bool circleValues = false;
  377.   /*Rotary acceleration introduced 25.2.2021.
  378.    * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
  379.    * without accelerateion you need long time to get to that number
  380.    * Using acceleration, faster you turn, faster will the value raise.
  381.    * For fine tuning slow down.
  382.    */
  383.   //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
  384.   rotaryEncoder.setAcceleration(0);  //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
  385.   rotaryEncoder.setEncoderValue(digiVolume);
  386.   rotaryEncoder.setBoundaries(0, 100, circleValues);      //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  387.   attachInterrupt(dcSenseRight, stateRight_ISR, RISING);  //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
  388.   attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
  389.   attachInterrupt(acSense, stateAC_ISR, FALLING);
  390.   //initWiFi();   //in the kitchen, no WIFI :D
  391.   //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
  392.   //Serial.println(WiFi.localIP());
  393.   //Serial.println(LocalIP);
  394.   if (debug) Serial.println("Starting..");
  395.   if (debug == 1) Serial.println("Started...");
  396.   if (debug == 1) Serial.println(compile_date);
  397.   uart.print("PMT:1;");
  398.   uart.print("BEP:0;");
  399.   uart.print("BEP;");
  400.   uart.print("PMT;");
  401.   nextion.touchSet("page0.preset", 0);
  402.   dcErrorRight = digitalRead(dcSenseRight);
  403.   dcErrorLeft = digitalRead(dcSenseLeft);
  404.   acError = digitalRead(acSense);
  405.   //if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
  406.   uart.print("SYS:REBOOT;");  //Reboot Digi
  407.   Serial.println(compile_date);
  408.   Serial.println("Arylic_0823");
  409. }  //////////////////// End of Setup  ////////////////////
  410.  
  411. void loop() {
  412.   if (self_test == 0) {
  413.     readProtection();
  414.     senseBrightness();
  415.     //if (debug == 2) Serial.println(rtc.getTemp());
  416.     if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {
  417.       if (debug == 1) Serial.println(digitalRead(powerSW));
  418.       if (powerState == 0) {
  419.         powerState = 1;
  420.         uart.print("SYS:REBOOT;");
  421.         Serial.println("---------- Digi REBOOT... ----------");
  422.         tone(12, 1000, 50);
  423.       } else {
  424.         powerState = 0;
  425.         uart.print("SYS:STANDBY;");
  426.         Serial.println("---------- Digi STANDBY... ----------");
  427.         tone(12, 1000, 50);
  428.         inputLed(4);
  429.       }
  430.       lastPressed = millis();
  431.     }
  432.  
  433.     lastPowerState = digitalRead(powerSW);  //1
  434.     rotary_loop();
  435.     // put your main code here, to run repeatedly:
  436.     while (uart.available()) {
  437.       nextion.val("page6.vaArylic", 1);
  438.       sReceived = uart.readStringUntil('\n');
  439.       sReceived.trim();
  440.       if (debug == 1) Serial.println("uart:___|----------" + sReceived);
  441.       if (debug == 1) Serial.println("");
  442.       if (sReceived.startsWith("PLA:0")) {
  443.         nextion.txt("page0.infoText", "Szünet/Megállítva");
  444.       } else if (sReceived.startsWith("PLA:1")) {
  445.         nextion.txt("page0.infoText", "Lejátszás...");
  446.       } else if (sReceived.startsWith("STA:")) {        ///////////////////////   STA
  447.         if (initDigi == 1) {
  448.           initDigi = 0;
  449.           nextion.val("page6.vaArylic", 1);
  450.         } else nextion.pageChange(0);
  451.         nextion.vis("page0.infoText", 0);
  452.         if (debug == 1) Serial.println("STA received");
  453.         if (debug == 1) Serial.println("Before: " + sReceived);
  454.         nextion.txt("page0.infoText", "");
  455.         nextion.vis("page0.title", 0);
  456.         nextion.vis("page0.elapsed", 0);
  457.         nextion.vis("page0.vendor", 0);
  458.         nextion.touchSet("page0.preset", 0);
  459.         reduce4();
  460.         if (sReceived.startsWith("USBDAC")) {  //input USB DAC
  461.           inputLed(3);
  462.           if (debug == 1) Serial.println("...USBADC...");
  463.           nextion.txt("page0.input", "USB DAC");
  464.         } else if (sReceived.startsWith("NET")) {  //input NET
  465.           nextion.vis("page0.title", 1);
  466.           nextion.vis("page0.elapsed", 1);
  467.           nextion.vis("page0.vendor", 1);
  468.           nextion.vis("page0.infoText", 1);
  469.           nextion.touchSet("page0.preset", 1);
  470.           if (powerState == 1 && currentPage == 0) inputLed(0);
  471.           if (debug == 1) Serial.println("...WIFI...");
  472.           nextion.txt("page0.input", "WiFi");
  473.         } else if (sReceived.startsWith("BT")) {  //input BT
  474.           nextion.vis("page0.infoText", 1);
  475.           if (powerState == 1) inputLed(1);
  476.           if (debug == 1) Serial.println("...BLUETOOTH...");
  477.           nextion.txt("page0.input", "Bluetooth");
  478.         } else if (sReceived.startsWith("LINE-IN")) {  //input Line-IN
  479.           if (powerState == 1) inputLed(2);
  480.           if (debug == 1) Serial.println("...LINE-IN...");
  481.           nextion.txt("page0.input", "Line In");
  482.         }
  483.       } else if (sReceived.startsWith("SYS:STANDBY"))  //END OF STA:
  484.       {
  485.         inputLed(4);
  486.         Serial.println("Stand by mode...");
  487.         tone(12, 1000, 50);
  488.         nextion.pageChange(3);
  489.         digitalWrite(relay, 0);
  490.         nextionSetTimebit = 1;
  491.       } else if (sReceived.startsWith("SRC:")) {
  492.         nextion.vis("page0.title", 0);
  493.         nextion.vis("page0.elapsed", 0);
  494.         nextion.vis("page0.vendor", 0);
  495.         nextion.touchSet("page0.preset", 0);
  496.         nextion.pageChange(0);
  497.         reduce4();
  498.         if (sReceived.startsWith("USBDAC")) {
  499.           if (powerState == 1) inputLed(3);
  500.           if (debug == 1) Serial.println("...USBDAC...");
  501.           nextion.txt("page0.input", "USB DAC");
  502.         } else if (sReceived.startsWith("NET")) {
  503.           nextion.vis("page0.infoText", 1);
  504.           if (debug == 1) Serial.println("...WIFI...");
  505.           nextion.vis("page0.title", 1);
  506.           nextion.vis("page0.elapsed", 1);
  507.           nextion.vis("page0.vendor", 1);
  508.           nextion.touchSet("page0.preset", 1);
  509.           if (powerState == 1) inputLed(0);
  510.           nextion.txt("page0.input", "WiFi");
  511.         } else if (sReceived.startsWith("BT")) {
  512.           if (powerState == 1) inputLed(1);
  513.           nextion.vis("page0.infoText", 1);
  514.           if (debug == 1) Serial.println("...BLUETOOTH...");
  515.           nextion.txt("page0.input", "Bluetooth");
  516.         } else if (sReceived.startsWith("LINE-IN")) {
  517.           if (powerState == 1) inputLed(2);
  518.           if (debug == 1) Serial.println("...LINE-IN...");
  519.           nextion.txt("page0.input", "Line In");
  520.         }
  521.       } else if (sReceived.startsWith("VOL:"))  //end of SRC:
  522.  
  523.       {
  524.         reduce4();
  525.         int index = sReceived.indexOf(';');
  526.         sReceived = sReceived.substring(0, index);
  527.         if (sReceived == "100") {
  528.           nextion.txt("volText", "MAX");
  529.         } else if (sReceived == "0") {
  530.           nextion.txt("volText", "MIN");
  531.         } else {
  532.           if (debug == 1) Serial.println("volume:  -|:" + sReceived);
  533.           digiVolume = sReceived.toInt();
  534.           nextion.txt("volText", sReceived);
  535.         }
  536.         nextion.systemVal("digiVol", digiVolume);
  537.         rotaryEncoder.setEncoderValue(digiVolume);
  538.       } else if (sReceived.startsWith("MUT:"))  //end of VOL:
  539.  
  540.       {
  541.         reduce4();
  542.         sReceived = sReceived.substring(0, 1);
  543.         if (debug == 1) Serial.println("Mute:_____/:|" + sReceived);
  544.         if (sReceived == "1") {
  545.           dmute = 1;
  546.           nextion.txt("volText", "MIN");
  547.           nextion.systemVal("digiVol", 0);
  548.         } else if (sReceived == "0") {
  549.           dmute = 0;
  550.           nextion.txt("volText", String(digiVolume));
  551.           nextion.systemVal("digiVol", digiVolume);
  552.         }
  553.  
  554.       } else if (sReceived.startsWith("BTC:"))  //end of BTC:
  555.  
  556.       {
  557.         reduce4();
  558.         sReceived = sReceived.substring(0, 1);
  559.         if (sReceived == "1") {
  560.           nextion.txt("page0.infoText", "CONNECTED");
  561.           uart.print("TIT;");
  562.         } else if (sReceived == "0") {
  563.           nextion.txt("page0.infoText", "DISCONNECTED");
  564.         }
  565.       } else if (sReceived.endsWith("SYS:ON;")) {
  566.         tone(12, 2000, 50);
  567.         nextion.txt("page3.powerOn", "STARTED");
  568.         nextion.setPco("page3.powerOn", 34784);
  569.         Serial.println("arrived SYS:ON...(1)");
  570.       } else if (sReceived.startsWith("NET:"))  //end of NET:
  571.  
  572.       {
  573.         reduce4();
  574.         sReceived = sReceived.substring(0, 1);
  575.         if (sReceived == "1") {
  576.           nextion.txt("page0.infoText", "CONNECTED");
  577.           uart.print("TIT;");
  578.           nextion.touchSet("page0.preset", 1);
  579.           //inputLed(0);
  580.         } else if (sReceived == "0") {
  581.           nextion.txt("page0.infoText", "DISCONNECTED");
  582.           nextion.touchSet("page0.preset", 0);
  583.         }
  584.  
  585.       } else if (sReceived.endsWith("SYS:ON;")) {
  586.         tone(12, 2000, 50);
  587.         nextion.txt("page3.powerOn", "STARTED");
  588.         nextion.setPco("page3.powerOn", 34784);
  589.         if (debug == 1) Serial.println("arrived SYS:ON... (2)");
  590.  
  591.       } else if (sReceived.startsWith("TIT:")) {  //Title
  592.         reduce4();
  593.         if (debug == 1) Serial.println("Title: " + sReceived);
  594.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  595.         nextion.txt("page0.title", sReceived);
  596.  
  597.       } else if (sReceived.startsWith("ELP:")) {  //Elapsed playing time
  598.         reduce4();
  599.         int index = sReceived.indexOf("/");
  600.         sReceived = sReceived.substring(0, index);
  601.         // Serial1.println(sReceived);
  602.         long time = sReceived.toInt();
  603.         time = time / 100;
  604.         int tenth = time % 10;
  605.         time = time / 10;
  606.         long hour = time / 3600;
  607.         time = time - (hour * 3600);
  608.         long min = time / 60;
  609.         long sec = time - (min * 60);
  610.         String timeS = "Time: ";
  611.         if (hour < 10) timeS += "0";
  612.         timeS += String(hour) + ":";
  613.         if (min < 10) timeS += "0";
  614.         timeS += String(min) + ":";
  615.         if (sec < 10) timeS += "0";
  616.         timeS += String(sec);  // + "." + String(tenth);
  617.         if (time > 0) nextion.txt("page0.elapsed", timeS);
  618.  
  619.       } else if (sReceived.startsWith("BAS:")) {  //BASS, dB
  620.         reduce4();
  621.         int bass = sReceived.toInt();
  622.         nextion.val("page1.nbass", bass);
  623.         if (bass < 0) {
  624.           bass = 11 - abs(bass);
  625.         } else {
  626.           bass = bass + 11;
  627.         }
  628.         nextion.val("page1.hbass", bass);
  629.  
  630.       } else if (sReceived.startsWith("TRE:")) {  //Treble, dB
  631.         reduce4();
  632.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  633.         int treb = sReceived.toInt();
  634.         nextion.val("page1.ntreb", treb);
  635.         if (treb < 0) {
  636.           treb = 11 - abs(treb);
  637.         } else {
  638.           treb = treb + 11;
  639.         }
  640.         nextion.val("page1.htreb", treb);
  641.  
  642.       } else if (sReceived.startsWith("MXV:")) {  //Max volume, %
  643.         reduce4();
  644.         int volMax = sReceived.toInt();
  645.         nextion.val("page1.nVolMax", volMax);
  646.         nextion.val("page1.hVolMax", volMax);
  647.         if (debug == 2) Serial.println(volMax);
  648.  
  649.       } else if (sReceived.startsWith("VBS:")) {  //Virtual Bass
  650.         reduce4();
  651.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  652.         if (sReceived == "1") {
  653.           nextion.val("page1.vbs", 1);
  654.           if (debug == 2) Serial.println("VBS: on");
  655.         } else {
  656.           nextion.val("page1.vbs", 0);
  657.           if (debug == 2) Serial.println("VBS: off");
  658.         }
  659.       }
  660.  
  661.       else if (sReceived.startsWith("PMT:")) {  //Promt Voice
  662.         reduce4();
  663.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  664.         if (sReceived == "1") {
  665.           nextion.val("page1.pmt", 1);
  666.           if (debug == 2) Serial.println("PMT: on");
  667.         } else {
  668.           nextion.val("page1.pmt", 0);
  669.           if (debug == 2) Serial.println("PMT: off");
  670.         }
  671.  
  672.       } else if (sReceived.startsWith("NAM:")) {  //Device name
  673.         reduce4();
  674.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  675.         String dname, Nname;
  676.         int h = 16, sz = 0, dsz = 0;
  677.         if (sReceived.length() > 0) {
  678.           for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  679.             dname = sReceived.substring(i, i + 1);
  680.             if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  681.               sz = dname.toInt();
  682.               // if (debug) Serial.println(dname);
  683.             }
  684.             if (dname == "A") sz = 10;
  685.             if (dname == "B") sz = 11;
  686.             if (dname == "C") sz = 12;
  687.             if (dname == "D") sz = 13;
  688.             if (dname == "E") sz = 14;
  689.             if (dname == "F") sz = 15;
  690.             // sz += sz * h;
  691.             if (h == 0) {
  692.               dsz += sz;
  693.               // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
  694.               Nname += char(dsz);
  695.               dsz = 0;
  696.             } else {
  697.               dsz = sz * 16;
  698.             }
  699.             h = 16 - h;
  700.           }
  701.           if (debug == 1) Serial.println(Nname);
  702.           nextion.txt("page0.NAME", Nname);
  703.         }
  704.       } else if (sReceived.startsWith("IPA:")) {  //Device IP address for connecting to browser's control panel, psw:admin
  705.         reduce4();
  706.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  707.         nextion.txt("page1.digiIP", sReceived);
  708.       } else if (sReceived.startsWith("TME:")) {  //Time, undefinied time zone, your need offset!
  709.  
  710.         String dc = sReceived.substring(21, 23);
  711.         String nc = sReceived;
  712.         int hour = dc.toInt();
  713.         if (hour % 2) {
  714.           dc = " ";
  715.         } else {
  716.           dc = ":";
  717.         }
  718.         String st = sReceived.substring(15, 17);
  719.         hour = st.toInt();
  720.         hour = hour + offset;  //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
  721.         if (hour < 10) {
  722.           sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
  723.         } else {
  724.           sReceived = String(hour) + dc + sReceived.substring(18, 20);
  725.         }
  726.         if (debug == 1) Serial.println(sReceived);
  727.         nextion.txt("page1.digiTime", sReceived);
  728.         dc = nc.substring(4, 8);
  729.         if (debug == 1) Serial.println(dc);
  730.         if (debug == 1) Serial.println(nextionSetTimebit);
  731.         if (dc != "2000" && nextionSetTimebit == 1) {
  732.           dc = nc.substring(15, 17);
  733.           int hour = dc.toInt();
  734.           hour = (hour + offset + 24) % 24;
  735.           dc = nc.substring(18, 20);
  736.           int min = dc.toInt();
  737.           dc = nc.substring(21, 23);
  738.           int sec = dc.toInt();
  739.           nextion.setTime(hour, min, sec);
  740.           nextionSetTimebit = 0;
  741.         }
  742.       } else if (sReceived.startsWith("VND:")) {  //Vendor - Tidal, Spotify, etc...
  743.         reduce4();
  744.         sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  745.         nextion.txt("page0.vaVendor", sReceived);
  746.       } else if (sReceived.startsWith("PST:")) {  //Preset, not response, only command (1-10)
  747.         reduce4();
  748.         sReceived = sReceived.substring(0, sReceived.length() - 1);
  749.         if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
  750.       }
  751.       sReceived = "";
  752.     }  //end of uart
  753.  
  754.     while (Serial1.available()) {  //convert Nextion to Digi
  755.       String nReceived = Serial1.readStringUntil(';');
  756.       if (nReceived == "SYS:STANDBY") {
  757.         inputLed(4);
  758.         powerState = 0;
  759.       }
  760.       if (nReceived == "LDR:0") {
  761.         toDigi = 0;
  762.         if (debug == 2) Serial.println("LDR OFF");
  763.       }
  764.       if (nReceived == "LDR:1") {
  765.         toDigi = 0;
  766.         if (debug == 2) Serial.println("LDR ON");
  767.       }
  768.       if (nReceived == "ESP:RESTART") {
  769.         ESP.restart();
  770.       }
  771.       if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
  772.       if (toDigi == 1) uart.print(nReceived + ";");
  773.       toDigi = 1;
  774.     }
  775.     if (currentPage == 0)
  776.       powerState = 1;
  777.   } else {
  778.     readProtection();
  779.     if (acError == 0 && dcErrorRight == 0 && dcErrorLeft == 0) {
  780.       self_test = 0;
  781.       initDigi = 1;
  782.       uart.print("SYS:REBOOT;");
  783.       digitalWrite(relay, 1);
  784.     }
  785.     Serial.printf("Selftest %d\n", self_test);
  786.   }
  787.  
  788. }  //////////////////// End of Loop  ////////////////////
  789.  
  790. void reduce4() {
  791.   sReceived = sReceived.substring(4);
  792. }
  793.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement