Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // If you select the preset on a mobile device, it is selected, but Nextion does not display it, you have to query the "preset"
- const char compile_date[] = __DATE__;
- //Included with the name printing
- #include <HardwareSerial.h>
- #include "AiEsp32RotaryEncoder.h"
- #include "Arduino.h"
- #include "Wire.h"
- #include <Adafruit_NeoPixel.h>
- #include <esp_task_wdt.h>
- #include <WiFi.h>
- #include <RTClib.h>
- RTC_DS3231 rtc;
- HardwareSerial uart(2); // Uso de la interfaz de hardware Serial2
- #define ROTARY_ENCODER_A_PIN 35
- #define ROTARY_ENCODER_B_PIN 32
- #define ROTARY_ENCODER_BUTTON_PIN 33
- #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 */
- #define ROTARY_ENCODER_STEPS 4
- #define powerSW 23
- #define inputSW
- #define dcSenseRight 19 //-> pin / dcErrorRight (current state) / dcErroRightLast (last state of dcErrorRight)
- #define dcSenseLeft 18 //-> pin / dcErrorLeft (current state) / dcErrorLeftLast (last state of dcErrorLeft)
- #define acSense 5 //-> pin / acError (current state) / acErrorLast (last state of acError)
- #define relay 16 // LED
- //#define speaker
- #define WS2812 2
- #define NUMPIXELS 8
- Adafruit_NeoPixel pixels(NUMPIXELS, WS2812, NEO_RGB + NEO_KHZ800);
- const byte LDR = 36; //Light Dependant Resistor
- const long eventTime_1_LDR = 1000; //check brigtness in ms
- unsigned long previousTime_1 = 0, beepMillis = 0, protectMillis = 0;
- unsigned long ac_time = 0, ac_time_last = 0;
- boolean powerState = 0, lastPowerState = 0, nextionSetTimebit = 1;
- int offset = 2, beep = 0, NextionPage;
- int digiVolume = 0, dmute = 0, lastPressed = 0, source = 0, dim = 0, dimN = -1;
- int initDigi = 0, toDigi = 1;
- int dcErrorRight = 1, dcErrorRightLast = -1, dcErrorLeft = 1, dcErrorLeftLast = -1, acError = 1, acErrorLast = -1; //variables for protection
- int debug = 2; //1 - writing all data; 2 - only protection data
- int ac_protect = 0;
- int self_test = 1;
- int currentPage = 0; // current page of nextion
- String sReceived;
- //instead of changing here, rather change numbers above
- AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);
- #if (1)
- const char* ssid = "TP-Link_F072";
- const char* password = "12778072";
- #else
- const char* ssid = "SirRouter";
- const char* password = "19801989";
- #endif
- void initWiFi() { // initialize WiFi
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi ..");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println(WiFi.localIP());
- }
- void rotary_onButtonClick() { // initialize Encoder SW
- static unsigned long lastTimePressed = 0;
- //ignore multiple press in that time milliseconds
- if (millis() - lastTimePressed < 500) {
- return;
- }
- lastTimePressed = millis();
- if (debug == 1) Serial.print("button pressed ");
- if (debug == 1) Serial.print(millis());
- if (debug == 1) Serial.println(" milliseconds after restart");
- if (dmute == 0) {
- uart.print("MUT:1;");
- } else {
- uart.print("MUT:0;");
- }
- }
- void beep_2k() { // initialize 2kHz "beep"
- unsigned long currentMillis = millis();
- if (currentMillis - beepMillis >= 1000) {
- if (beep == 0) {
- tone(12, 2000, 50);
- } else {
- tone(12, 2000, 50);
- }
- beepMillis = currentMillis;
- }
- }
- void beep_3k() { // initialize 3kHz "beep"
- unsigned long currentMillis = millis();
- if (currentMillis - beepMillis >= 2000) {
- if (beep == 0) {
- tone(12, 3000, 1000);
- } else {
- tone(12, 3000, 1000);
- }
- beepMillis = currentMillis;
- }
- }
- void rotary_loop() { // initialize Encoder loop
- if (rotaryEncoder.encoderChanged()) {
- if (debug == 1) Serial.print("Value: ");
- if (debug == 1) Serial.println(rotaryEncoder.readEncoder());
- uart.print("VOL:" + String(rotaryEncoder.readEncoder()) + ";");
- digiVolume = rotaryEncoder.readEncoder();
- }
- if (rotaryEncoder.isEncoderButtonClicked()) {
- rotary_onButtonClick();
- }
- }
- void IRAM_ATTR readEncoderISR() {
- rotaryEncoder.readEncoder_ISR();
- }
- void inputLed(int input) {
- source = input;
- int r = 0, g = 0, b = 0;
- switch (input) {
- case 0: // WiFi
- if (powerState == 0) break;
- r = 49;
- g = 50;
- b = 51;
- break;
- case 1: // Bluetooth
- if (powerState == 0) break;
- r = 0;
- g = 0;
- b = 50;
- break;
- case 2: // Line-In
- if (powerState == 0) break;
- r = 0;
- g = 50;
- b = 0;
- break;
- case 3: // USB-DAC
- if (powerState == 0) break;
- r = 50;
- g = 0;
- b = 0;
- break;
- case 4: // Standby
- r = 55;
- g = 35;
- b = 0;
- break;
- case 5: // OFF
- r = 35;
- g = 15;
- b = 0;
- break;
- }
- for (int i = 0; i < NUMPIXELS; i++) {
- pixels.setPixelColor(i, pixels.Color(g, r, b));
- pixels.show(); // Send the updated pixel colors to the hardware.
- }
- }
- class c_NextionWrite {
- public:
- void init(int speed, int RXN, int TXN) {
- Serial1.begin(speed, SERIAL_8N1, RXN, TXN);
- // if (debug) Serial.printf("Serial1 - Speed: %d, RX-pin: %d, TX-pin: %d \n", speed, RX, TX);
- }
- void txt(String Name, String text) {
- Serial1.print(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
- if (debug == 1) Serial.println(Name + ".txt=\"" + text + "\"\xFF\xFF\xFF");
- }
- void val(String Name, int value) {
- Serial1.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print(Name + ".val=" + String(value) + "\xFF\xFF\xFF");
- }
- void systemVal(String Name, int value) {
- Serial1.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print(Name + "=" + String(value) + "\xFF\xFF\xFF");
- }
- void pageChange(int nr) {
- Serial1.print("page " + String(nr) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print("page " + String(nr) + "\xFF\xFF\xFF");
- NextionPage = nr;
- }
- void setPco(String name, int pco) { // for global variable need a page number / page name too
- Serial1.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.print(name + ".pco=" + String(pco) + "\xFF\xFF\xFF");
- }
- void timerEnable(String name, int en) {
- Serial1.print(name + ".en=" + String(en) + "\xFF\xFF\xFF");
- }
- void vis(String name, int en) {
- Serial1.print("vis " + name + "," + String(en) + "\xFF\xFF\xFF");
- }
- void dim(int en) {
- Serial1.print("dim=" + String(en) + "\xFF\xFF\xFF");
- }
- void touchSet(String name, int en) { //nextion.touchSet("preset", 0/1);
- Serial1.print("tsw " + String(name) + "," + String(en) + "\xFF\xFF\xFF");
- }
- void setTime(int hour, int min, int sec) {
- hour = (hour + 24) % 24;
- Serial1.print("rtc3=" + String(hour) + "\xFF\xFF\xFF");
- Serial1.print("rtc4=" + String(min) + "\xFF\xFF\xFF");
- Serial1.print("rtc5=" + String(sec) + "\xFF\xFF\xFF");
- if (debug == 1) Serial.printf("Nextion time/ hour: %d min: %d sec: %d \n", hour, min, sec);
- if (debug == 1) Serial.println("--------------------");
- }
- };
- c_NextionWrite nextion;
- #define RXN_PIN 26 // Serial1 RX to Nextion TX
- #define TXN_PIN 25 // Serial1 TX to Nextion RX
- #define RX_PIN 27 // Serial2 RX a Amp TX
- #define TX_PIN 14 // Serial2 TX a Amp RX
- #define SDA 21 // I2C Thermometer, Expander, etc.
- #define SCL 22
- //////////////////// Start of Protection ////////////////////
- void IRAM_ATTR stateRight_ISR() { //Right channel state
- dcErrorRight = 1;
- }
- void IRAM_ATTR stateLeft_ISR() { //Left channel state
- dcErrorLeft = 1;
- }
- void IRAM_ATTR stateAC_ISR() { //AC voltage state
- ac_time = millis();
- if (ac_time - ac_time_last > 250) {
- acError = 1;
- ac_protect = 1;
- digitalWrite(relay, 0);
- ac_time_last = ac_time;
- }
- }
- void senseBrightness() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime_1 >= eventTime_1_LDR) {
- String m;
- switch (analogRead(LDR)) {
- case 0 ... 40:
- dimN = 25;
- m = "Dark";
- break;
- case 41 ... 800:
- dimN = 40;
- m = "Light";
- break;
- case 801 ... 2000:
- dimN = 60;
- m = "Bright";
- break;
- case 2001 ... 3200:
- dimN = 80;
- m = "Very Bright";
- break;
- case 3201 ... 4500:
- dimN = 100;
- m = "Very Very Bright";
- break;
- }
- if (dim != dimN) {
- nextion.val("page2.nDimESP", dimN);
- nextion.val("page2.hSlideESP", dimN);
- Serial.println(" => " + m);
- dim = dimN;
- }
- previousTime_1 = currentTime;
- }
- }
- void readProtection() {
- dcErrorRight = digitalRead(dcSenseRight);
- dcErrorLeft = digitalRead(dcSenseLeft);
- acError = digitalRead(acSense);
- if (dcErrorRight != dcErrorRightLast) {
- if (dcErrorRight == 0) {
- if (debug == 2) Serial.println("Right channel is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("page2.vaRight", 0);
- if (dcErrorLeft == 0 && self_test == 1) nextion.val("page6.vaProtection", 1);
- }
- if (dcErrorRight == 1) {
- if (debug == 2) Serial.println("DC voltage on Right channel!");
- nextion.systemVal("warning", 1);
- nextion.val("page2.vaRight", 1);
- if (self_test == 1) nextion.val("page6.vaProtection", 0);
- }
- dcErrorRightLast = dcErrorRight;
- }
- if (dcErrorLeft != dcErrorLeftLast) {
- if (dcErrorLeft == 0) {
- if (debug == 2) Serial.println("Left channel is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("page2.vaLeft", 0);
- if (dcErrorRight == 0 && self_test == 1) nextion.val("page6.vaProtection", 1);
- }
- if (dcErrorLeft == 1) {
- if (debug == 2) Serial.println("DC voltage on Left channel!");
- nextion.systemVal("warning", 1);
- nextion.val("page2.vaLeft", 1);
- if (self_test == 1) nextion.val("page6.vaProtection", 0);
- }
- dcErrorLeftLast = dcErrorLeft;
- }
- if (acError != acErrorLast) {
- if (ac_protect == 1) {
- Serial.println("Interrupt on AC pin");
- }
- if (acError == 0) {
- if (debug == 2) Serial.println("AC is OK!");
- nextion.systemVal("warning", 0);
- nextion.val("page2.vaAC", 0);
- nextion.val("page6.vaSupply", 1);
- }
- if (acError == 1) {
- if (debug == 2) Serial.println("Missing AC voltage!");
- nextion.systemVal("warning", 1);
- nextion.val("page2.vaAC", 1);
- nextion.val("page6.vaSupply", 0);
- }
- acErrorLast = acError;
- ac_protect = 0;
- }
- } //////////////////// End of Protection ////////////////////
- void setup() {
- pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
- rtc.begin();
- pinMode(relay, OUTPUT);
- delay(1);
- //digitalWrite(speaker, 0);
- digitalWrite(relay, 0);
- pinMode(powerSW, INPUT_PULLUP); // buttons is active LOW
- pinMode(dcSenseRight, INPUT_PULLUP);
- pinMode(dcSenseLeft, INPUT_PULLUP);
- pinMode(acSense, INPUT_PULLUP);
- rotaryEncoder.begin();
- rotaryEncoder.setup(readEncoderISR);
- Wire.begin(SDA, SCL);
- Serial.begin(115200);
- Serial1.begin(115200, SERIAL_8N1, RXN_PIN, TXN_PIN);
- uart.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);
- while (!Serial1)
- ;
- while (!uart)
- ;
- inputLed(4);
- nextion.pageChange(7);
- nextion.txt("page6.infoText", "System is starting...");
- nextion.val("page6.vaMCU", 1);
- delay(1000);
- nextion.val("page6.vaRTC", 1);
- delay(1000);
- nextion.val("page6.vaArylic", 0);
- bool circleValues = false;
- /*Rotary acceleration introduced 25.2.2021.
- * in case range to select is huge, for example - select a value between 0 and 1000 and we want 785
- * without accelerateion you need long time to get to that number
- * Using acceleration, faster you turn, faster will the value raise.
- * For fine tuning slow down.
- */
- //rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
- rotaryEncoder.setAcceleration(0); //or set the value - larger number = more accelearation; 0 or 1 means disabled acceleration
- rotaryEncoder.setEncoderValue(digiVolume);
- rotaryEncoder.setBoundaries(0, 100, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
- attachInterrupt(dcSenseRight, stateRight_ISR, RISING); //Interrupts for protection: left: RISING,right: RISING,AC: FALLING
- attachInterrupt(dcSenseLeft, stateLeft_ISR, RISING);
- attachInterrupt(acSense, stateAC_ISR, FALLING);
- //initWiFi(); //in the kitchen, no WIFI :D
- //String LocalIP = String() + WiFi.localIP()[0] + "." + WiFi.localIP()[1] + "." + WiFi.localIP()[2] + "." + WiFi.localIP()[3];
- //Serial.println(WiFi.localIP());
- //Serial.println(LocalIP);
- if (debug) Serial.println("Starting..");
- if (debug == 1) Serial.println("Started...");
- if (debug == 1) Serial.println(compile_date);
- uart.print("PMT:1;");
- uart.print("BEP:0;");
- uart.print("BEP;");
- uart.print("PMT;");
- nextion.touchSet("page0.preset", 0);
- dcErrorRight = digitalRead(dcSenseRight);
- dcErrorLeft = digitalRead(dcSenseLeft);
- acError = digitalRead(acSense);
- //if (debug == 2) Serial.printf("AC-%d DCL-%d DCR-%d\n", acError, dcErrorLeft, dcErrorRight);
- uart.print("SYS:REBOOT;"); //Reboot Digi
- Serial.println(compile_date);
- Serial.println("Arylic_0823");
- } //////////////////// End of Setup ////////////////////
- void loop() {
- if (self_test == 0) {
- readProtection();
- senseBrightness();
- //if (debug == 2) Serial.println(rtc.getTemp());
- if (digitalRead(powerSW) == 0 && lastPowerState == 1 && lastPressed + 5000 < millis()) {
- if (debug == 1) Serial.println(digitalRead(powerSW));
- if (powerState == 0) {
- powerState = 1;
- uart.print("SYS:REBOOT;");
- Serial.println("---------- Digi REBOOT... ----------");
- tone(12, 1000, 50);
- } else {
- powerState = 0;
- uart.print("SYS:STANDBY;");
- Serial.println("---------- Digi STANDBY... ----------");
- tone(12, 1000, 50);
- inputLed(4);
- }
- lastPressed = millis();
- }
- lastPowerState = digitalRead(powerSW); //1
- rotary_loop();
- // put your main code here, to run repeatedly:
- while (uart.available()) {
- nextion.val("page6.vaArylic", 1);
- sReceived = uart.readStringUntil('\n');
- sReceived.trim();
- if (debug == 1) Serial.println("uart:___|----------" + sReceived);
- if (debug == 1) Serial.println("");
- if (sReceived.startsWith("PLA:0")) {
- nextion.txt("page0.infoText", "Szünet/Megállítva");
- } else if (sReceived.startsWith("PLA:1")) {
- nextion.txt("page0.infoText", "Lejátszás...");
- } else if (sReceived.startsWith("STA:")) {
- if (initDigi == 1) {
- initDigi = 0;
- nextion.val("page6.vaArylic", 1);
- } else nextion.pageChange(0);
- nextion.vis("page0.infoText", 0);
- if (debug == 1) Serial.println("STA received");
- if (debug == 1) Serial.println("Before: " + sReceived);
- nextion.txt("page0.infoText", "");
- nextion.vis("page0.title", 0);
- nextion.vis("page0.elapsed", 0);
- nextion.vis("page0.vendor", 0);
- nextion.touchSet("page0.preset", 0);
- reduce4();
- if (sReceived.startsWith("USBDAC")) { //input USB DAC
- inputLed(3);
- if (debug == 1) Serial.println("...USBADC...");
- nextion.txt("page0.input", "USB DAC");
- } else if (sReceived.startsWith("NET")) { //input NET
- nextion.vis("page0.title", 1);
- nextion.vis("page0.elapsed", 1);
- nextion.vis("page0.vendor", 1);
- nextion.vis("page0.infoText", 1);
- nextion.touchSet("page0.preset", 1);
- if (powerState = 1) inputLed(0);
- if (debug == 1) Serial.println("...WIFI...");
- nextion.txt("page0.input", "WiFi");
- } else if (sReceived.startsWith("BT")) { //input BT
- nextion.vis("page0.infoText", 1);
- if (powerState = 1) inputLed(1);
- if (debug == 1) Serial.println("...BLUETOOTH...");
- nextion.txt("page0.input", "Bluetooth");
- } else if (sReceived.startsWith("LINE-IN")) { //input Line-IN
- if (powerState = 1) inputLed(2);
- if (debug == 1) Serial.println("...LINE-IN...");
- nextion.txt("page0.input", "Line In");
- }
- } else if (sReceived.startsWith("SYS:STANDBY")) //END OF STA:
- {
- inputLed(4);
- Serial.println("Stand by mode...");
- tone(12, 1000, 50);
- nextion.pageChange(3);
- digitalWrite(relay, 0);
- nextionSetTimebit = 1;
- } else if (sReceived.startsWith("SRC:")) {
- nextion.vis("page0.title", 0);
- nextion.vis("page0.elapsed", 0);
- nextion.vis("page0.vendor", 0);
- nextion.touchSet("page0.preset", 0);
- nextion.pageChange(0);
- reduce4();
- if (sReceived.startsWith("USBDAC")) {
- if (powerState = 1) inputLed(3);
- if (debug == 1) Serial.println("...USBDAC...");
- nextion.txt("page0.input", "USB DAC");
- } else if (sReceived.startsWith("NET")) {
- nextion.vis("page0.infoText", 1);
- if (debug == 1) Serial.println("...WIFI...");
- nextion.vis("page0.title", 1);
- nextion.vis("page0.elapsed", 1);
- nextion.vis("page0.vendor", 1);
- nextion.touchSet("page0.preset", 1);
- if (powerState = 1) inputLed(0);
- nextion.txt("page0.input", "WiFi");
- } else if (sReceived.startsWith("BT")) {
- if (powerState = 1) inputLed(1);
- nextion.vis("page0.infoText", 1);
- if (debug == 1) Serial.println("...BLUETOOTH...");
- nextion.txt("page0.input", "Bluetooth");
- } else if (sReceived.startsWith("LINE-IN")) {
- if (powerState = 1) inputLed(2);
- if (debug == 1) Serial.println("...LINE-IN...");
- nextion.txt("page0.input", "Line In");
- }
- } else if (sReceived.startsWith("VOL:")) //end of SRC:
- {
- reduce4();
- int index = sReceived.indexOf(';');
- sReceived = sReceived.substring(0, index);
- if (sReceived == "100") {
- nextion.txt("volText", "MAX");
- } else if (sReceived == "0") {
- nextion.txt("volText", "MIN");
- } else {
- if (debug == 1) Serial.println("volume: -|:" + sReceived);
- digiVolume = sReceived.toInt();
- nextion.txt("volText", sReceived);
- }
- nextion.systemVal("digiVol", digiVolume);
- rotaryEncoder.setEncoderValue(digiVolume);
- } else if (sReceived.startsWith("MUT:")) //end of VOL:
- {
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (debug == 1) Serial.println("Mute:_____:|" + sReceived);
- if (sReceived == "1") {
- dmute = 1;
- nextion.txt("volText", "MIN");
- nextion.systemVal("digiVol", 0);
- } else if (sReceived == "0") {
- dmute = 0;
- nextion.txt("volText", String(digiVolume));
- nextion.systemVal("digiVol", digiVolume);
- }
- } else if (sReceived.startsWith("BTC:")) //end of BTC:
- {
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- nextion.txt("page0.infoText", "CONNECTED");
- uart.print("TIT;");
- } else if (sReceived == "0") {
- nextion.txt("page0.infoText", "DISCONNECTED");
- }
- } else if (sReceived.endsWith("SYS:ON;")) {
- tone(12, 2000, 50);
- nextion.txt("page3.powerOn", "STARTED");
- nextion.setPco("page3.powerOn", 34784);
- Serial.println("arrived SYS:ON...(1)");
- } else if (sReceived.startsWith("NET:")) //end of NET:
- {
- reduce4();
- sReceived = sReceived.substring(0, 1);
- if (sReceived == "1") {
- nextion.txt("page0.infoText", "CONNECTED");
- uart.print("TIT;");
- nextion.touchSet("page0.preset", 1);
- } else if (sReceived == "0") {
- nextion.txt("page0.infoText", "DISCONNECTED");
- nextion.touchSet("page0.preset", 0);
- }
- } else if (sReceived.endsWith("SYS:ON;")) {
- tone(12, 2000, 50);
- nextion.txt("page3.powerOn", "STARTED");
- nextion.setPco("page3.powerOn", 34784);
- if (debug == 1) Serial.println("arrived SYS:ON... (2)");
- } else if (sReceived.startsWith("TIT:")) { //Title
- reduce4();
- if (debug == 1) Serial.println("Title: " + sReceived);
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- nextion.txt("page0.title", sReceived);
- } else if (sReceived.startsWith("ELP:")) { //Elapsed playing time
- reduce4();
- int index = sReceived.indexOf("/");
- sReceived = sReceived.substring(0, index);
- // Serial1.println(sReceived);
- long time = sReceived.toInt();
- time = time / 100;
- int tenth = time % 10;
- time = time / 10;
- long hour = time / 3600;
- time = time - (hour * 3600);
- long min = time / 60;
- long sec = time - (min * 60);
- String timeS = "Time: ";
- if (hour < 10) timeS += "0";
- timeS += String(hour) + ":";
- if (min < 10) timeS += "0";
- timeS += String(min) + ":";
- if (sec < 10) timeS += "0";
- timeS += String(sec); // + "." + String(tenth);
- if (time > 0) nextion.txt("page0.elapsed", timeS);
- } else if (sReceived.startsWith("BAS:")) { //BASS, dB
- reduce4();
- int bass = sReceived.toInt();
- nextion.val("page1.nbass", bass);
- if (bass < 0) {
- bass = 11 - abs(bass);
- } else {
- bass = bass + 11;
- }
- nextion.val("page1.hbass", bass);
- } else if (sReceived.startsWith("TRE:")) { //Treble, dB
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- int treb = sReceived.toInt();
- nextion.val("page1.ntreb", treb);
- if (treb < 0) {
- treb = 11 - abs(treb);
- } else {
- treb = treb + 11;
- }
- nextion.val("page1.htreb", treb);
- } else if (sReceived.startsWith("MXV:")) { //Max volume, %
- reduce4();
- int volMax = sReceived.toInt();
- nextion.val("page1.nVolMax", volMax);
- nextion.val("page1.hVolMax", volMax);
- if (debug == 2) Serial.println(volMax);
- } else if (sReceived.startsWith("VBS:")) { //Virtual Bass
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (sReceived == "1") {
- nextion.val("page1.vbs", 1);
- if (debug == 2) Serial.println("VBS: on");
- } else {
- nextion.val("page1.vbs", 0);
- if (debug == 2) Serial.println("VBS: off");
- }
- }
- else if (sReceived.startsWith("PMT:")) { //Promt Voice
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (sReceived == "1") {
- nextion.val("page1.pmt", 1);
- if (debug == 2) Serial.println("PMT: on");
- } else {
- nextion.val("page1.pmt", 0);
- if (debug == 2) Serial.println("PMT: off");
- }
- } else if (sReceived.startsWith("NAM:")) { //Device name
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- String dname, Nname;
- int h = 16, sz = 0, dsz = 0;
- if (sReceived.length() > 0) {
- for (int i = 0; i <= sReceived.length() - 1; i = i + 1) {
- dname = sReceived.substring(i, i + 1);
- if (dname.toInt() >= 0 && dname.toInt() <= 9) {
- sz = dname.toInt();
- // if (debug) Serial.println(dname);
- }
- if (dname == "A") sz = 10;
- if (dname == "B") sz = 11;
- if (dname == "C") sz = 12;
- if (dname == "D") sz = 13;
- if (dname == "E") sz = 14;
- if (dname == "F") sz = 15;
- // sz += sz * h;
- if (h == 0) {
- dsz += sz;
- // if (debug) Serial.printf("%i. sz=%i\n", i, dsz);
- Nname += char(dsz);
- dsz = 0;
- } else {
- dsz = sz * 16;
- }
- h = 16 - h;
- }
- if (debug == 1) Serial.println(Nname);
- nextion.txt("page0.NAME", Nname);
- }
- } else if (sReceived.startsWith("IPA:")) { //Device IP address for connecting to browser's control panel, psw:admin
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("page1.digiIP", sReceived);
- } else if (sReceived.startsWith("TME:")) { //Time, undefinied time zone, your need offset!
- String dc = sReceived.substring(21, 23);
- String nc = sReceived;
- int hour = dc.toInt();
- if (hour % 2) {
- dc = " ";
- } else {
- dc = ":";
- }
- String st = sReceived.substring(15, 17);
- hour = st.toInt();
- hour = hour + offset; //1+(-4)=-3 , -3+24=21 % 24 = 21 /// 22+(-4)=18 , 18+24=42 , 42 % 24 = 18
- if (hour < 10) {
- sReceived = "0" + String(hour) + dc + sReceived.substring(18, 20);
- } else {
- sReceived = String(hour) + dc + sReceived.substring(18, 20);
- }
- if (debug == 1) Serial.println(sReceived);
- nextion.txt("page1.digiTime", sReceived);
- dc = nc.substring(4, 8);
- if (debug == 1) Serial.println(dc);
- if (debug == 1) Serial.println(nextionSetTimebit);
- if (dc != "2000" && nextionSetTimebit == 1) {
- dc = nc.substring(15, 17);
- int hour = dc.toInt();
- hour = (hour + offset + 24) % 24;
- dc = nc.substring(18, 20);
- int min = dc.toInt();
- dc = nc.substring(21, 23);
- int sec = dc.toInt();
- nextion.setTime(hour, min, sec);
- nextionSetTimebit = 0;
- }
- } else if (sReceived.startsWith("VND:")) { //Vendor - Tidal, Spotify, etc...
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1); //removing ";"
- nextion.txt("page0.vaVendor", sReceived);
- } else if (sReceived.startsWith("PST:")) { //Preset, not response, only command (1-10)
- reduce4();
- sReceived = sReceived.substring(0, sReceived.length() - 1);
- if (debug == 1) Serial.println(";;;;;;;;;; " + sReceived);
- }
- sReceived = "";
- } //end of uart
- while (Serial1.available()) { //convert Nextion to Digi
- String nReceived = Serial1.readStringUntil(';');
- if (nReceived == "SYS:STANDBY") {
- inputLed(4);
- powerState = 0;
- }
- if (nReceived == "LDR:0") {
- toDigi = 0;
- if (debug == 2) Serial.println("LDR OFF");
- }
- if (nReceived == "LDR:1") {
- toDigi = 0;
- if (debug == 2) Serial.println("LDR ON");
- }
- if (nReceived == "ESP:RESTART") {
- ESP.restart();
- }
- if (debug == 1) Serial.println("++++++++++Serial1:__|" + nReceived + ";");
- if (toDigi == 1) uart.print(nReceived + ";");
- toDigi = 1;
- }
- if (currentPage == 0)
- powerState = 1;
- } else {
- readProtection();
- if (acError == 0 && dcErrorRight == 0 && dcErrorLeft == 0) {
- self_test = 0;
- initDigi = 1;
- uart.print("SYS:REBOOT;");
- digitalWrite(relay, 1);
- }
- Serial.printf("Selftest %d\n", self_test);
- }
- } //////////////////// End of Loop ////////////////////
- void reduce4() {
- sReceived = sReceived.substring(4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement