Advertisement
GaabMM88

Untitled

Aug 13th, 2024
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.89 KB | None | 0 0
  1. //
  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.  
  11. HardwareSerial uart(2);  // Uso de la interfaz de hardware Serial2
  12.  
  13. #define ROTARY_ENCODER_A_PIN 35
  14. #define ROTARY_ENCODER_B_PIN 32
  15. #define ROTARY_ENCODER_BUTTON_PIN 33
  16. #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 */
  17. #define ROTARY_ENCODER_STEPS 4
  18. //#define muteSW
  19. //#define inputSW
  20. #define powerSW 23
  21. #define dcSenseRight 19
  22. #define dcSenseLeft 18
  23. #define acSense 5
  24. #define relay 13
  25. #define WS2812 2
  26. #define NUMPIXELS 8
  27. Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
  28. const byte LDR = 36;                //Light Dependant Resistor
  29. const long eventTime_1_LDR = 1000;  //check brigtness in ms
  30. unsigned long previousTime_1 = 0, beepMillis = 0;
  31. int offset = 2, beep = 0;
  32. int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim;
  33. boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
  34. //boolean dcSense = 0, lastDcSense = 0;
  35. //boolean acSense = 0, lastAcSense = 0;
  36. String sReceived;
  37. //instead of changing here, rather change numbers above
  38. AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
  39.  
  40. void rotary_onButtonClick() {
  41.   static unsigned long lastTimePressed = 0;
  42.   //ignore multiple press in that time milliseconds
  43.   if (millis() - lastTimePressed < 500) {
  44.     return;
  45.   }
  46.   lastTimePressed = millis();
  47.   Serial.print("button pressed ");
  48.   Serial.print(millis());
  49.   Serial.println(" milliseconds after restart");
  50.   if (dmute == 0) {
  51.     uart.print("MUT:1;");
  52.   } else {
  53.     uart.print("MUT:0;");
  54.   }
  55. }
  56.  
  57.  
  58. void beep_2k() {
  59.   unsigned long currentMillis = millis();
  60.  
  61.   if (currentMillis - beepMillis >= 1000) {
  62.     if (beep == 0) {
  63.       tone(12, 2000, 50);
  64.       // LedState = 1;
  65.     } else {
  66.       tone(12, 2000, 50);
  67.       // LedState = 0;
  68.     }
  69.     beepMillis = currentMillis;
  70.   }
  71. }
  72.  
  73. void beep_3k() {
  74.   unsigned long currentMillis = millis();
  75.  
  76.   if (currentMillis - beepMillis >= 2000) {
  77.     if (beep == 0) {
  78.       tone(12, 3000, 1000);
  79.       // LedState = 1;
  80.     } else {
  81.       tone(12, 3000, 1000);
  82.       // LedState = 0;
  83.     }
  84.     beepMillis = currentMillis;
  85.   }
  86. }
  87.  
  88. void rotary_loop() {
  89.   //dont print anything unless value changed
  90.   if (rotaryEncoder.encoderChanged()) {
  91.     Serial.print("Value: ");
  92.     Serial.println(rotaryEncoder.readEncoder());
  93.     uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
  94.     digiVolume = rotaryEncoder.readEncoder();
  95.   }
  96.   if (rotaryEncoder.isEncoderButtonClicked()) {
  97.     rotary_onButtonClick();
  98.   }
  99. }
  100.  
  101. void IRAM_ATTR readEncoderISR() {
  102.   rotaryEncoder.readEncoder_ISR();
  103. }
  104.  
  105. void inputLed(int input) {
  106.   source = input;
  107.   int r = 0, g = 0, b = 0;
  108.   switch (input) {
  109.     case 0:  // WiFi
  110.       r = 49;
  111.       g = 50;
  112.       b = 51;
  113.       break;
  114.     case 1:  // Bluetooth
  115.       r = 0;
  116.       g = 0;
  117.       b = 50;
  118.       break;
  119.     case 2:  // Line-In
  120.       r = 0;
  121.       g = 50;
  122.       b = 0;
  123.       break;
  124.     case 3:  // USB-DAC
  125.       r = 50;
  126.       g = 0;
  127.       b = 0;
  128.       break;
  129.     case 4:  // OFF
  130.       r = 55;
  131.       g = 35;
  132.       b = 0;
  133.       break;
  134.   }
  135.   for (int i = 0; i < NUMPIXELS; i++) {
  136.     pixels.setPixelColor(i, pixels.Color(g, r, b));
  137.     pixels.show();  // Send the updated pixel colors to the hardware.
  138.   }
  139. }
  140.  
  141.  
  142. class c_NextionWrite {
  143. public:
  144.   void init(int speed, int RXN, int TXN) {
  145.     Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
  146.     // Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
  147.   }
  148.   void txt(String Name, String text) {
  149.     Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  150.     Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
  151.   }
  152.   void val(String Name, int value) {
  153.     Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  154.     Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
  155.   }
  156.   void pageChange(int nr) {
  157.     Serial1.print("page " + String(nr) + "\xFF\xFF\xFF");
  158.     Serial.print("page " + String(nr) + "\xFF\xFF\xFF");
  159.   }
  160.   void setPco(String name, int pco) {
  161.     Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  162.     Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
  163.   }
  164.   void timerEnable(String name, int en) {
  165.     Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
  166.   }
  167.   void vis(String name, int en) {
  168.     Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
  169.   }
  170.   void setTime(int hour, int min, int sec) {
  171.     hour = (hour + 24) % 24;
  172.     Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF");
  173.     Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF");
  174.     Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF");
  175.     Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", hour, min, sec);
  176.     Serial.println("--------------------");
  177.   }
  178. };
  179. c_NextionWrite nextion;
  180.  
  181. #define RXN_PIN 26  // Serial1 RX to Nextion TX
  182. #define TXN_PIN 25  // Serial1 TX to Nextion RX
  183.  
  184. #define RX_PIN 14  // Serial2 RX a Amp TX
  185. #define TX_PIN 27  // Serial2 TX a Amp RX
  186.  
  187. #define SDA 21  // I2C Thermometer, Expander, etc.
  188. #define SCL 22
  189. //#define INT ?
  190.  
  191. void setup() {
  192.   pixels.begin();  // INITIALIZE NeoPixel strip object (REQUIRED)
  193.   pinMode(relay, OUTPUT);
  194.   delay(1);
  195.   digitalWrite(relay, 0);
  196.   //pinMode(muteSW, INPUT);
  197.   //pinMode(inputSW, INPUT);
  198.   pinMode(powerSW, INPUT_PULLUP);
  199.   pinMode(dcSenseRight, INPUT_PULLUP);
  200.   pinMode(dcSenseLeft, INPUT_PULLUP);
  201.   pinMode(acSense, INPUT_PULLUP);
  202.   rotaryEncoder.begin();
  203.   rotaryEncoder.setup(readEncoderISR);
  204.   //set boundaries and if values should cycle or not
  205.   //in this example we will set possible values between 0 and 1000;
  206.   bool circleValues = false;
  207.   rotaryEncoder.setBoundaries(0, 100, circleValues);  //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
  208.  
  209.   /*Rotary acceleration introduced 25.2.2021.
  210.    * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
  211.    * without accelerateion you need long time to get to that number
  212.    * Using acceleration, faster you turn, faster will the value raise.
  213.    * For fine tuning slow down.
  214.    */
  215.   //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
  216.   rotaryEncoder.setAcceleration(0);  //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
  217.   rotaryEncoder.setEncoderValue(digiVolume);
  218.  
  219.   // put your setup code here, to run once:
  220.   Wire.begin(SDA, SCL);
  221.   Serial.begin(115200);
  222.   Serial.println("Starting..");
  223.   Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
  224.   // Inicializar la comunicación UART
  225.   uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
  226.   // uart.setTimeout(2000);
  227.   delay(2000);
  228.   Serial.println("Started...");
  229.   Serial.println(compile_date);
  230.   uart.print("PMT:1;");
  231.   uart.print("BEP:0;");
  232.   uart.print("BEP;");
  233.   uart.print("PMT;");
  234.   nextion.txt("bt_text", "ESP32 now starting...");
  235.   delay(500);
  236.   nextion.vis("bt_text", 0);
  237.   delay(500);
  238.   nextion.vis("bt_text", 1);
  239.   //uart.print("SYS:REBOOT;");
  240.   //delay(3500);
  241.   uart.print("SYS:STANDBY;");
  242.   uart.print("SYS:STANDBY;");
  243.   uart.print("SYS:STANDBY;");
  244.   uart.print("SYS:STANDBY;");
  245.   Serial.println("Initial OFF");
  246.   nextion.pageChange(3);
  247.   inputLed(4);
  248. }
  249.  
  250. void loop() {
  251.  
  252.   unsigned long currentTime = millis();
  253.   if (currentTime - previousTime_1 >= eventTime_1_LDR) {
  254.     if (analogRead(LDR) < 40) {
  255.       Serial.print(analogRead(LDR));
  256.       Serial.println(" => Dark");
  257.       Serial1.print("dim=25");
  258.       Serial1.print("\xFF\xFF\xFF");
  259.     } else if (analogRead(LDR) < 800) {
  260.       Serial.print(analogRead(LDR));
  261.       Serial.println(" => Dim");
  262.       Serial1.print("dim=40");
  263.       Serial1.print("\xFF\xFF\xFF");
  264.     } else if (analogRead(LDR) < 2000) {
  265.       Serial.print(analogRead(LDR));
  266.       Serial.println(" => Light");
  267.       Serial1.print("dim=60");
  268.       Serial1.print("\xFF\xFF\xFF");
  269.     } else if (analogRead(LDR) < 3200) {
  270.       Serial.print(analogRead(LDR));
  271.       Serial.println(" => Bright");
  272.       Serial1.print("dim=80");
  273.       Serial1.print("\xFF\xFF\xFF");
  274.     } else {
  275.       Serial.print(analogRead(LDR));
  276.       Serial.println(" => Very bright");
  277.       Serial1.print("dim=100");
  278.       Serial1.print("\xFF\xFF\xFF");
  279.     }
  280.     previousTime_1 = currentTime;
  281.   }
  282.  
  283.  
  284.  
  285.   if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {
  286.     Serial.println(digitalRead(powerSW));
  287.     if (powerState == 0) {
  288.       powerState = 1;
  289.       uart.print("SYS:REBOOT;");
  290.       Serial.println("Digi REBOOT...");
  291.       tone(12, 1000, 50);
  292.     } else {
  293.       powerState = 0;
  294.       uart.print("SYS:STANDBY;");
  295.       Serial.println("Digi STANDBY...");
  296.       tone(12, 1000, 50);
  297.       inputLed(4);
  298.     }
  299.     lastPressed = millis();
  300.   }
  301.  
  302.   if ((digitalRead(dcSenseRight) == 0)) {  
  303.     Serial.println("DC RIGHT FAIL");
  304.     //nextion.txt("vVendor", sReceived);    // Send LEFT FAIL message
  305.     //beep_2k();
  306.   }
  307.   if ((digitalRead(dcSenseLeft) == 0)) {  
  308.     Serial.println("DC LEFT FAIL");
  309.     //nextion.txt("vVendor", sReceived);    // Send  LEFT FAIL message
  310.     //beep_2k();
  311.   }
  312.   if ((digitalRead(acSense) == 0)) {  
  313.     Serial.println("AC FAIL");
  314.     //nextion.txt("vVendor", sReceived);    // Send AC fail message
  315.     //beep_3k();
  316.   }
  317.   lastPowerState = digitalRead(powerSW);  //1
  318.   rotary_loop();
  319.   // put your main code here, to run repeatedly:
  320.   while (uart.available()) {
  321.     sReceived = uart.readStringUntil('\n');
  322.     sReceived.trim();
  323.     Serial.println("uart:___|" + sReceived);
  324.     if (sReceived.startsWith("PLA:0")) {
  325.       nextion.txt("bt_text", "Szünet/Megállítva");
  326.     } else if (sReceived.startsWith("PLA:1")) {
  327.       nextion.txt("bt_text", "Lejátszás...");
  328.     } else if (sReceived.startsWith("STA:")) {
  329.       digitalWrite(relay, 1);
  330.       nextion.pageChange(0);
  331.       nextion.vis("bt_text", 0);
  332.       Serial.println("STA received");
  333.       Serial.println("Before: " + sReceived);
  334.       nextion.val("wifi", 0);
  335.       nextion.val("bluetooth", 0);
  336.       nextion.val("line_in", 0);
  337.       nextion.val("usbdac", 0);
  338.       nextion.txt("bt_text", "");
  339.       nextion.vis("bt3", 0);
  340.       nextion.vis("title", 0);
  341.       nextion.vis("elapsed", 0);
  342.       nextion.vis("vendor", 0);
  343.       // reduce4();
  344.       reduce4();
  345.       if (sReceived.startsWith("USBDAC")) {
  346.         nextion.val("usbdac", 1);
  347.         inputLed(3);
  348.         // nextion.txt("bt_text", "USBDAC");   //rem if not needed(ONLY FOR TESTING)
  349.         Serial.println("...USBADC...");
  350.       } else if (sReceived.startsWith("NET")) {
  351.         nextion.vis("title", 1);
  352.         nextion.vis("elapsed", 1);
  353.         nextion.vis("vendor", 1);
  354.         nextion.val("wifi", 1);
  355.         nextion.vis("bt_text", 1);
  356.         inputLed(0);
  357.         // nextion.txt("bt_text", "WIFI");   //rem if not needed
  358.         Serial.println("...WIFI...");
  359.       } else if (sReceived.startsWith("BT")) {
  360.         nextion.val("bluetooth", 1);
  361.         nextion.vis("bt_text", 1);
  362.         nextion.vis("bt3", 1);
  363.         inputLed(1);
  364.         // nextion.txt("bt_text", "BLUETOOTH");    //rem if not needed
  365.         Serial.println("...BLUETOOTH...");
  366.       } else if (sReceived.startsWith("LINE-IN")) {
  367.         nextion.val("line_in", 1);
  368.         inputLed(2);
  369.         // nextion.txt("bt_text", "LINE IN");    //rem if not needed
  370.         Serial.println("...LINE-IN...");
  371.       }
  372.     } else if (sReceived.startsWith("SYS:STANDBY"))  //END OF STA:
  373.     {
  374.       inputLed(4);
  375.       Serial.println("Stand by mode...");
  376.       tone(12, 1000, 50);
  377.       nextion.pageChange(3);
  378.       digitalWrite(relay, 0);
  379.       nextionSetTimebit = 1;
  380.     } else if (sReceived.startsWith("SRC:")) {
  381.       nextion.vis("bt_text", 0);
  382.       nextion.val("wifi", 0);
  383.       nextion.val("bluetooth", 0);
  384.       nextion.val("line_in", 0);
  385.       nextion.val("usbdac", 0);
  386.       nextion.vis("title", 0);
  387.       nextion.vis("elapsed", 0);
  388.       nextion.vis("vendor", 0);
  389.       reduce4();
  390.       if (sReceived.startsWith("USBDAC")) {
  391.         inputLed(3);
  392.         nextion.val("usbdac", 1);
  393.         // nextion.txt("bt_text", "USBDAC");   //rem if not needed
  394.         Serial.println("...USBDAC...");
  395.       } else if (sReceived.startsWith("NET")) {
  396.         inputLed(0);
  397.         nextion.val("wifi", 1);
  398.         nextion.vis("bt_text", 1);
  399.         // nextion.txt("bt_text", "WIFI");   //rem if not needed
  400.         Serial.println("...WIFI...");
  401.         nextion.vis("title", 1);
  402.         nextion.vis("elapsed", 1);
  403.         nextion.vis("vendor", 1);
  404.       } else if (sReceived.startsWith("BT")) {
  405.         inputLed(1);
  406.         nextion.val("bluetooth", 1);
  407.         nextion.vis("bt_text", 1);
  408.         // nextion.txt("bt_text", "BLUETOOTH");    //rem if not needed
  409.         Serial.println("...BLUETOOTH...");
  410.       } else if (sReceived.startsWith("LINE-IN")) {
  411.         inputLed(2);
  412.         nextion.val("line_in", 1);
  413.         // nextion.txt("bt_text", "LINE IN");    //rem if not needed
  414.         Serial.println("...LINE-IN...");
  415.       }
  416.     } else if (sReceived.startsWith("VOL:"))  //end of SRC:
  417.     {
  418.       reduce4();
  419.       int index = sReceived.indexOf(';');
  420.       sReceived = sReceived.substring(0, index);
  421.       if (sReceived == "100") {
  422.         nextion.txt("volText", "MAX");
  423.       } else if (sReceived == "0") {
  424.         nextion.txt("volText", "MIN");
  425.       } else {
  426.         Serial.println("volume:  -|:" + sReceived);
  427.         digiVolume = sReceived.toInt();
  428.         nextion.txt("volText", sReceived);
  429.       }
  430.       nextion.val("volSlide", digiVolume);
  431.       rotaryEncoder.setEncoderValue(digiVolume);
  432.     } else if (sReceived.startsWith("MUT:"))  //end of VOL:
  433.     {
  434.       reduce4();
  435.       sReceived = sReceived.substring(0, 1);
  436.       Serial.println("Mute:_____:|" + sReceived);
  437.       if (sReceived == "1") {
  438.         dmute = 1;
  439.         nextion.txt("volText", "MIN");
  440.         nextion.val("volSlide", 0);
  441.       } else if (sReceived == "0") {
  442.         dmute = 0;
  443.         nextion.txt("volText", String(digiVolume));
  444.         nextion.val("volSlide", digiVolume);
  445.       }
  446.  
  447.     } else if (sReceived.startsWith("BTC:") || sReceived.startsWith("NET:"))  //end of MUT:
  448.     {
  449.       reduce4();
  450.       sReceived = sReceived.substring(0, 1);
  451.       if (sReceived == "1") {
  452.         nextion.txt("bt_text", "CONNECTED");
  453.         uart.print("TIT;");
  454.       } else if (sReceived == "0") {
  455.         nextion.txt("bt_text", "DISCONNECTED");
  456.       }
  457.     } else if (sReceived.endsWith("SYS:ON;")) {
  458.       tone(12, 2000, 50);
  459.       nextion.txt("powerOn", "STARTED");
  460.       nextion.setPco("powerOn", 34784);
  461.       Serial.println("arrived SYS:ON...");
  462.     } else if (sReceived.startsWith("TIT:")) {
  463.       reduce4();
  464.       Serial.println("Title: " + sReceived);
  465.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  466.       nextion.txt("title", sReceived);
  467.       // nextion.timerEnable("tm1", 1);
  468.     } else if (sReceived.startsWith("PMT:0")) {
  469.       nextion.txt("pmt", "PMT ON");
  470.       Serial.println(sReceived);
  471.     } else if (sReceived.startsWith("PMT:1")) {
  472.       nextion.txt("pmt", "PMT OFF");
  473.       Serial.println(sReceived);
  474.     } else if (sReceived.startsWith("ELP:")) {
  475.       reduce4();
  476.       int index = sReceived.indexOf("/");
  477.       sReceived = sReceived.substring(0, index);
  478.       // Serial1.println(sReceived);
  479.       long time = sReceived.toInt();
  480.       time = time / 100;
  481.       int tenth = time % 10;
  482.       time = time / 10;
  483.       long hour = time / 3600;
  484.       time = time - (hour * 3600);
  485.       long min = time / 60;
  486.       long sec = time - (min * 60);
  487.       // if(nextionSetTimebit==1){
  488.       //   nextion.setTime(hour,min,sec);
  489.       // }
  490.       String timeS = "Time: ";
  491.       if (hour < 10) timeS += "0";
  492.       timeS += String(hour) + ":";
  493.       if (min < 10) timeS += "0";
  494.       timeS += String(min) + ":";
  495.       if (sec < 10) timeS += "0";
  496.       timeS += String(sec) + "." + String(tenth);
  497.       // sReceived = String(hour) + ":" + String(min) + ":" + String(sec);
  498.       // Serial.println("ELP:.....|" + timeS);
  499.       if (time > 0) nextion.txt("elapsed", timeS);
  500.     } else if (sReceived.startsWith("BAS:")) {
  501.       // Serial.println("BASS BEFORE substring....|" + sReceived);
  502.       reduce4();
  503.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  504.       // Serial.println("BASS STILL STRING:.....|" + sReceived);
  505.       int bass = sReceived.toInt();
  506.       // Serial.println("BASS INT:....|" + String(bass));
  507.       nextion.val("nbass", bass);
  508.       if (bass < 0) {
  509.         bass = 11 - abs(bass);
  510.       } else {
  511.         bass = bass + 11;
  512.       }
  513.       // Serial.println("BASS AFTERCALC....|"+String(bass));
  514.       nextion.val("hbass", bass);
  515.       // Serial.println("BASS:.....|" + String(bass));
  516.     } else if (sReceived.startsWith("TRE:")) {
  517.       reduce4();
  518.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  519.       int treb = sReceived.toInt();
  520.       Serial.println("TREB INT VALUE:...|" + String(treb));
  521.       nextion.val("ntreb", treb);
  522.       if (treb < 0) {
  523.         treb = 11 - abs(treb);
  524.       } else {
  525.         treb = treb + 11;
  526.       }
  527.       nextion.val("htreb", treb);
  528.     } else if (sReceived.startsWith("VBS:")) {
  529.       reduce4();
  530.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  531.       if (sReceived == "1") {
  532.         nextion.val("vbs", 1);
  533.         nextion.txt("t3", "ON");
  534.       } else {
  535.         nextion.val("vbs", 0);
  536.         nextion.txt("t3", "OFF");
  537.       }
  538.     } else if (sReceived.startsWith("VST:")) {
  539.       reduce4();
  540.       // write something to nextion SETUP (1) page
  541.       nextion.val("nVolStep", sReceived.toInt());
  542.       nextion.val("hVolStep", sReceived.toInt());
  543.     } else if (sReceived.startsWith("NAM:")) {
  544.       reduce4();
  545.       sReceived = sReceived.substring(0, sReceived.length() - 1);
  546.       String dname, Nname;
  547.       int h = 16, sz = 0, dsz = 0;
  548.       if (sReceived.length() > 0) {
  549.         for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
  550.           dname = sReceived.substring(i, i + 1);
  551.           if (dname.toInt() >= 0 && dname.toInt() <= 9) {
  552.             sz = dname.toInt();
  553.             // Serial.println(dname);
  554.           }
  555.           if (dname == "A") sz = 10;
  556.           if (dname == "B") sz = 11;
  557.           if (dname == "C") sz = 12;
  558.           if (dname == "D") sz = 13;
  559.           if (dname == "E") sz = 14;
  560.           if (dname == "F") sz = 15;
  561.           // sz += sz * h;
  562.           if (h == 0) {
  563.             dsz += sz;
  564.             Serial.printf("%i. sz=%i\n", i, dsz);
  565.             // Serial.printf("%i, | ", dsz);
  566.             Nname += char(dsz);
  567.             dsz = 0;
  568.           } else {
  569.             dsz = sz * 16;
  570.           }
  571.           h = 16 - h;
  572.         }
  573.         Serial.println(Nname);
  574.         nextion.txt("NAME", Nname);
  575.       }
  576.     } else if (sReceived.startsWith("IPA:")) {
  577.       reduce4();
  578.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  579.       nextion.txt("page1.t5", sReceived);
  580.     } else if (sReceived.startsWith("TME:")) {
  581.  
  582.       String dc = sReceived.substring(21, 23);
  583.       String nc = sReceived;
  584.       int hour = dc.toInt();
  585.       if (hour % 2) {
  586.         dc = " ";
  587.       } else {
  588.         dc = ":";
  589.       }
  590.       // Serial.println("Second: "+dc);
  591.       String st = sReceived.substring(15, 17);
  592.       hour = st.toInt();
  593.       // Serial.printf("Hour: %i\n",hour);
  594.       hour = hour + offset;  //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
  595.       //hour = (hour + 24) % 24;
  596.       if (hour < 10) {
  597.         sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
  598.       } else {
  599.         sReceived = String(hour) + dc + sReceived.substring(18, 20);
  600.       }
  601.       Serial.println(sReceived);
  602.       nextion.txt("t6", sReceived);
  603.       dc = nc.substring(4, 8);
  604.       Serial.println(dc);
  605.       Serial.println(nextionSetTimebit);
  606.       if (dc != "2000" && nextionSetTimebit == 1) {
  607.         dc = nc.substring(15, 17);
  608.         int hour = dc.toInt();
  609.         hour = (hour + offset + 24) % 24;
  610.         dc = nc.substring(18, 20);
  611.         int min = dc.toInt();
  612.         dc = nc.substring(21, 23);
  613.         int sec = dc.toInt();
  614.         nextion.setTime(hour, min, sec);
  615.         nextionSetTimebit = 0;
  616.       }
  617.  
  618.     } else if (sReceived.startsWith("VND:")) {
  619.       reduce4();
  620.       sReceived = sReceived.substring(0, sReceived.length() - 1);  //removing ";"
  621.       nextion.txt("vVendor", sReceived);
  622.     }
  623.     sReceived = "";
  624.   }  //end of uart
  625.   while (Serial1.available()) {
  626.     String nReceived = Serial1.readStringUntil(';');
  627.     Serial.println("Serial1:__|" + nReceived + ";");
  628.     uart.print(nReceived + ";");
  629.   }
  630. }
  631. void reduce4() {
  632.   sReceived = sReceived.substring(4);
  633. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement