arduino-sketches – Diff between revs 13 and 21

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 13 Rev 21
Line 82... Line 82...
82 // "manual mode". From the manual, that seems to be setting the load // 82 // "manual mode". From the manual, that seems to be setting the load //
83 // setting for the first timer to 117 and the second should display 2, n // 83 // setting for the first timer to 117 and the second should display 2, n //
84 // and then the load can be toggled using this sketch. // 84 // and then the load can be toggled using this sketch. //
85 /////////////////////////////////////////////////////////////////////////// 85 ///////////////////////////////////////////////////////////////////////////
Line 86... Line -...
86   -  
87 #include <ModbusMaster.h> -  
88 #if defined(ARDUINO_ARCH_ESP8266) -  
89 #include <ESP8266WiFi.h> -  
90 #include <ESP8266mDNS.h> -  
91 #elif defined(ARDUINO_ARCH_ESP32) -  
92 #include <WiFi.h> -  
93 #include <ESPmDNS.h> -  
94 #endif -  
95 #include <PubSubClient.h> -  
96 #include <ArduinoJson.h> -  
97 #include <ArduinoOTA.h> -  
98 #if defined(ARDUINO_ARCH_ESP8266) -  
99 #include <ESP_EEPROM.h> -  
100 #elif defined(ARDUINO_ARCH_ESP32) -  
101 #include <EEPROM.h> -  
102 #endif -  
103   86  
104 /////////////////////////////////////////////////////////////////////////// 87 ///////////////////////////////////////////////////////////////////////////
105 // configuration // 88 // configuration //
-   89 ///////////////////////////////////////////////////////////////////////////
-   90 // Whether to send data over the serial port.
106 /////////////////////////////////////////////////////////////////////////// 91 #define SERIAL_DATA 1
107 // RS-485 module pin DE maps to D2 GPIO on ESP. 92 // RS-485 module pin DE maps to D2 GPIO on ESP.
108 #define MAX485_DE D2 93 #define MAX485_DE D2
109 // RS-485 module pin RE maps to D1 GPIO on ESP. 94 // RS-485 module pin RE maps to D1 GPIO on ESP.
Line 110... Line 95...
110 #define MAX485_RE D1 95 #define MAX485_RE D1
111   96  
112 const char* STA_SSID = ""; 97 #define STA_SSID ""
113 const char* STA_PASSWORD = ""; 98 #define STA_PASSWORD ""
114 const char* MQTT_SERVER = ""; 99 #define MQTT_SERVER ""
115 const int MQTT_PORT = 1883; 100 #define MQTT_PORT 1883
116 const char* MQTT_CLIENT_ID = "EpEver Solar Monitor"; 101 #define MQTT_CLIENT_ID "EpEver Solar Monitor"
117 const char* MQTT_TOPIC_PUB = "epever-40a/talk"; 102 #define MQTT_TOPIC_PUB "epever-40a/talk"
118 const char* MQTT_TOPIC_SUB = "epever-40a/hear"; 103 #define MQTT_TOPIC_SUB "epever-40a/hear"
Line 119... Line 104...
119 // seconds to wait for MQTT message to be delivered and processed 104 // Seconds to wait for MQTT message to be delivered and processed.
120 const int MQTT_SUBSCRIBE_WAIT = 10; 105 #define MQTT_SUBSCRIBE_WAIT 10
121   106  
122 // The OTA hostname. 107 // The OTA hostname.
123 const char* OTA_HOSTNAME = ""; 108 //#define OTA_HOSTNAME
124 // The OTA port on which updates take place. 109 // The OTA port on which updates take place.
125 const unsigned int OTA_PORT = 8266; 110 //#define OTA_PORT 8266
Line 126... Line 111...
126 // The authentication password to use for OTA updates. 111 // The authentication password to use for OTA updates.
127 // This should be set to the unsalted MD5 hash of the plaintext password. 112 // This should be set to the unsalted MD5 hash of the plaintext password.
128 const char* OTA_PASSWORD_HASH = ""; 113 //#define OTA_PASSWORD_HASH
129   114  
Line 130... Line 115...
130 // Whether to use deep sleep or not (requires hardware modifications). 115 // Whether to use deep sleep or not (requires hardware modifications).
131 const bool USE_DEEP_SLEEP = true; 116 //#define USE_DEEP_SLEEP 1
132 // the minimal amount that the ESP should sleep for. 117 // the minimal amount that the ESP should sleep for.
-   118 #define MIN_SLEEP_SECONDS 60
-   119  
-   120 ///////////////////////////////////////////////////////////////////////////
-   121 // general variable declarations and libraries //
-   122 ///////////////////////////////////////////////////////////////////////////
-   123 #include <ModbusMaster.h>
-   124 #if defined(ARDUINO_ARCH_ESP8266)
-   125 #include <ESP8266WiFi.h>
-   126 #include <ESP8266mDNS.h>
-   127 #elif defined(ARDUINO_ARCH_ESP32)
-   128 #include <WiFi.h>
-   129 #include <ESPmDNS.h>
-   130 #endif
-   131 #include <PubSubClient.h>
-   132 #include <ArduinoJson.h>
-   133 #include <ArduinoOTA.h>
-   134 #if defined(ARDUINO_ARCH_ESP8266)
133 const int MIN_SLEEP_SECONDS = 60; 135 #include <ESP_EEPROM.h>
134   136 #elif defined(ARDUINO_ARCH_ESP32)
135 /////////////////////////////////////////////////////////////////////////// 137 #include <EEPROM.h>
136 // general variable declarations // 138 #endif
137 /////////////////////////////////////////////////////////////////////////// 139  
Line 373... Line 375...
373 // Loop until we're reconnected 375 // Loop until we're reconnected
374 Serial.println("Checking MQT connection..."); 376 Serial.println("Checking MQT connection...");
375 while (!mqtt_client.connected()) { 377 while (!mqtt_client.connected()) {
376 Serial.print("MQTT Reconnecting: "); 378 Serial.print("MQTT Reconnecting: ");
Line 377... Line -...
377   -  
378 // Create a client ID -  
379 String clientId = MQTT_CLIENT_ID; -  
380   379  
381 // Attempt to connect 380 // Attempt to connect
382 if (mqtt_client.connect(clientId.c_str())) { 381 if (mqtt_client.connect(MQTT_CLIENT_ID)) {
Line 383... Line 382...
383 Serial.println("success"); 382 Serial.println("success");
384   383  
385 Serial.print("Subscribing MQTT: "); 384 Serial.print("Subscribing MQTT: ");
Line 482... Line 481...
482 epeverControlPayload.clear(); 481 epeverControlPayload.clear();
483 return; 482 return;
484 } 483 }
485 } 484 }
Line 486... Line -...
486   -  
487 ////////////////////////// 485  
488 ///////////////////////////////////////////////// 486 ///////////////////////////////////////////////////////////////////////////
489 // Arduino functions // 487 // Arduino functions //
490 /////////////////////////////////////////////////////////////////////////// 488 ///////////////////////////////////////////////////////////////////////////
491 void setup() { 489 void setup() {
492 Serial.begin(115200); // DO NOT CHANGE! 490 Serial.begin(115200); // DO NOT CHANGE!
Line 506... Line 504...
506 node.postTransmission(postTransmission); 504 node.postTransmission(postTransmission);
507 // EPEver Device ID 1 505 // EPEver Device ID 1
508 node.begin(1, Serial); 506 node.begin(1, Serial);
Line 509... Line 507...
509   507  
510 // Connect D0 to RST to wake up 508 // Connect D0 to RST to wake up
511 if (USE_DEEP_SLEEP) { 509 #ifdef USE_DEEP_SLEEP
512 pinMode(D0, WAKEUP_PULLUP); 510 pinMode(D0, WAKEUP_PULLUP);
Line 513... Line 511...
513 } 511 #endif
514   512  
515 // variable handling 513 // variable handling
516 EEPROM.begin(16); 514 EEPROM.begin(16);
Line 529... Line 527...
529 } 527 }
Line 530... Line 528...
530   528  
531 void loop() { 529 void loop() {
Line 532... Line 530...
532 uint8_t i, result; 530 uint8_t i, result;
533   -  
534 // flash the led -  
535 for (i = 0; i < 3; i++) { -  
536 digitalWrite(LED_BUILTIN, LOW); 531  
537 delay(200); -  
538 digitalWrite(LED_BUILTIN, HIGH); -  
Line 539... Line 532...
539 delay(200); 532 // Turn on LED
540 } -  
541   533 digitalWrite(LED_BUILTIN, HIGH);
542 // clear old data 534  
543 // 535 // Clear old data
Line 544... Line 536...
544 memset(rtc.buf, 0, sizeof(rtc.buf)); 536 memset(rtc.buf, 0, sizeof(rtc.buf));
545 memset(live.buf, 0, sizeof(live.buf)); -  
546 memset(stats.buf, 0, sizeof(stats.buf)); 537 memset(live.buf, 0, sizeof(live.buf));
547   538 memset(stats.buf, 0, sizeof(stats.buf));
548 // Read registers for clock 539  
549 // 540 // Read registers for clock
550 node.clearResponseBuffer(); 541 node.clearResponseBuffer();
Line 557... Line 548...
557 } else { 548 } else {
558 Serial.print("Miss read rtc-data, ret val:"); 549 Serial.print("Miss read rtc-data, ret val:");
559 Serial.println(result, HEX); 550 Serial.println(result, HEX);
560 } 551 }
Line 561... Line 552...
561   552  
562 // read LIVE-Data -  
563 // 553 // Read LIVE-Data
564 node.clearResponseBuffer(); 554 node.clearResponseBuffer();
Line 565... Line 555...
565 result = node.readInputRegisters(LIVE_DATA, LIVE_DATA_CNT); 555 result = node.readInputRegisters(LIVE_DATA, LIVE_DATA_CNT);
Line 572... Line 562...
572 Serial.print("Miss read liva-data, ret val:"); 562 Serial.print("Miss read liva-data, ret val:");
573 Serial.println(result, HEX); 563 Serial.println(result, HEX);
574 } 564 }
Line 575... Line 565...
575   565  
576 // Statistical Data -  
577 // 566 // Statistical Data
578 node.clearResponseBuffer(); 567 node.clearResponseBuffer();
579 result = node.readInputRegisters(STATISTICS, STATISTICS_CNT); 568 result = node.readInputRegisters(STATISTICS, STATISTICS_CNT);
580 if (result == node.ku8MBSuccess) { -  
-   569 if (result == node.ku8MBSuccess) {
581   570 for (i = 0; i < STATISTICS_CNT; i++) {
582 for (i = 0; i < STATISTICS_CNT; i++) stats.buf[i] = node.getResponseBuffer(i); 571 stats.buf[i] = node.getResponseBuffer(i);
583   572 }
584 } else { 573 } else {
585 Serial.print("Miss read statistics, ret val:"); 574 Serial.print("Miss read statistics, ret val:");
586 Serial.println(result, HEX); 575 Serial.println(result, HEX);
Line 587... Line 576...
587 } 576 }
588   -  
589 // Battery SOC 577  
590 // 578 // Battery SOC
591 node.clearResponseBuffer(); 579 node.clearResponseBuffer();
592 result = node.readInputRegisters(BATTERY_SOC, 1); -  
593 if (result == node.ku8MBSuccess) { 580 result = node.readInputRegisters(BATTERY_SOC, 1);
594   -  
595 batterySOC = node.getResponseBuffer(0); 581 if (result == node.ku8MBSuccess) {
596   582 batterySOC = node.getResponseBuffer(0);
597 } else { 583 } else {
598 Serial.print("Miss read batterySOC, ret val:"); 584 Serial.print("Miss read batterySOC, ret val:");
Line 599... Line 585...
599 Serial.println(result, HEX); 585 Serial.println(result, HEX);
600 } -  
601   586 }
602 // Battery Net Current = Icharge - Iload 587  
603 // 588 // Battery Net Current = Icharge - Iload
604 node.clearResponseBuffer(); -  
605 result = node.readInputRegisters(BATTERY_CURRENT_L, 2); 589 node.clearResponseBuffer();
606 if (result == node.ku8MBSuccess) { 590 result = node.readInputRegisters(BATTERY_CURRENT_L, 2);
607   -  
608 batteryCurrent = node.getResponseBuffer(0); 591 if (result == node.ku8MBSuccess) {
609 batteryCurrent |= node.getResponseBuffer(1) << 16; 592 batteryCurrent = node.getResponseBuffer(0);
610   593 batteryCurrent |= node.getResponseBuffer(1) << 16;
611 } else { 594 } else {
Line 612... Line 595...
612 Serial.print("Miss read batteryCurrent, ret val:"); 595 Serial.print("Miss read batteryCurrent, ret val:");
613 Serial.println(result, HEX); -  
614 } 596 Serial.println(result, HEX);
615   597 }
616 // State of the Load Switch 598  
617 // -  
618 node.clearResponseBuffer(); 599 // State of the Load Switch
619 result = node.readCoils(LOAD_STATE, 1); -  
620 if (result == node.ku8MBSuccess) { 600 node.clearResponseBuffer();
621   601 result = node.readCoils(LOAD_STATE, 1);
622 loadState = node.getResponseBuffer(0); 602 if (result == node.ku8MBSuccess) {
623   603 loadState = node.getResponseBuffer(0);
Line 624... Line 604...
624 } else { 604 } else {
625 Serial.print("Miss read loadState, ret val:"); -  
626 Serial.println(result, HEX); 605 Serial.print("Miss read loadState, ret val:");
627 } 606 Serial.println(result, HEX);
628   607 }
629 // Read Status Flags -  
630 // 608  
631 node.clearResponseBuffer(); 609 // Read Status Flags
632 result = node.readInputRegisters(0x3200, 2); 610 node.clearResponseBuffer();
Line 633... Line 611...
633 if (result == node.ku8MBSuccess) { 611 result = node.readInputRegisters(0x3200, 2);
634   612 if (result == node.ku8MBSuccess) {
635 uint16_t temp = node.getResponseBuffer(0); 613 uint16_t temp = node.getResponseBuffer(0);
636 Serial.print("Batt Flags : "); 614 Serial.print("Batt Flags : ");
Line 637... Line -...
637 Serial.println(temp); -  
638   615 Serial.println(temp);
639 status_batt.volt = temp & 0b1111; 616  
640 status_batt.temp = (temp >> 4) & 0b1111; 617 status_batt.volt = temp & 0b1111;
Line 641... Line 618...
641 status_batt.resistance = (temp >> 8) & 0b1; 618 status_batt.temp = (temp >> 4) & 0b1111;
Line 657... Line 634...
657 //Serial.print( "charger_input : "); Serial.println( charger_input ); 634 //Serial.print( "charger_input : "); Serial.println( charger_input );
658 Serial.print("charger_mode : "); 635 Serial.print("charger_mode : ");
659 Serial.println(charger_mode); 636 Serial.println(charger_mode);
660 //Serial.print( "charger_oper : "); Serial.println( charger_operation ); 637 //Serial.print( "charger_oper : "); Serial.println( charger_operation );
661 //Serial.print( "charger_state : "); Serial.println( charger_state ); 638 //Serial.print( "charger_state : "); Serial.println( charger_state );
662   -  
663   -  
664 } else { 639 } else {
665 Serial.print("Miss read ChargeState, ret val:"); 640 Serial.print("Miss read ChargeState, ret val:");
666 Serial.println(result, HEX); 641 Serial.println(result, HEX);
667 } 642 }
Line 668... Line 643...
668   643  
669 // Print out to serial 644 // Print out to serial
670 // 645 #ifdef SERIAL_DATA
671 Serial.printf("\n\nTime: 20%02d-%02d-%02d %02d:%02d:%02d \n", rtc.r.y, rtc.r.M, rtc.r.d, rtc.r.h, rtc.r.m, rtc.r.s); 646 Serial.printf("\n\nTime: 20%02d-%02d-%02d %02d:%02d:%02d \n", rtc.r.y, rtc.r.M, rtc.r.d, rtc.r.h, rtc.r.m, rtc.r.s);
672 Serial.print("\nLive-Data: Volt Amp Watt "); 647 Serial.print("\nLive-Data: Volt Amp Watt ");
673 Serial.printf("\n Panel: %7.3f %7.3f %7.3f ", live.l.pV / 100.f, live.l.pI / 100.f, live.l.pP / 100.0f); 648 Serial.printf("\n Panel: %7.3f %7.3f %7.3f ", live.l.pV / 100.f, live.l.pI / 100.f, live.l.pP / 100.0f);
674 Serial.printf("\n Batt: %7.3f %7.3f %7.3f ", live.l.bV / 100.f, live.l.bI / 100.f, live.l.bP / 100.0f); 649 Serial.printf("\n Batt: %7.3f %7.3f %7.3f ", live.l.bV / 100.f, live.l.bI / 100.f, live.l.bP / 100.0f);
Line 691... Line 666...
691 Serial.printf("\n batt.volt: %s ", batt_volt_status[status_batt.volt]); 666 Serial.printf("\n batt.volt: %s ", batt_volt_status[status_batt.volt]);
692 Serial.printf("\n batt.temp: %s ", batt_temp_status[status_batt.temp]); 667 Serial.printf("\n batt.temp: %s ", batt_temp_status[status_batt.temp]);
693 Serial.printf("\n charger.charging: %s ", charger_charging_status[charger_mode]); 668 Serial.printf("\n charger.charging: %s ", charger_charging_status[charger_mode]);
694 Serial.println(); 669 Serial.println();
695 Serial.println(); 670 Serial.println();
-   671 #endif
Line 696... Line -...
696   -  
697 // Go Online to publish via mqtt -  
698 // 672  
699 // get wifi going 673 // Start WiFi connection.
700 digitalWrite(LED_BUILTIN, LOW); 674 digitalWrite(LED_BUILTIN, LOW);
701 WiFi.mode(WIFI_STA); 675 WiFi.mode(WIFI_STA);
702 WiFi.begin(STA_SSID, STA_PASSWORD); 676 WiFi.begin(STA_SSID, STA_PASSWORD);
703 if (WiFi.waitForConnectResult() != WL_CONNECTED) { 677 if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Line 710... Line 684...
710 Serial.println("WiFi connected"); 684 Serial.println("WiFi connected");
711 Serial.println("IP address: "); 685 Serial.println("IP address: ");
712 Serial.println(WiFi.localIP()); 686 Serial.println(WiFi.localIP());
Line 713... Line 687...
713   687  
-   688 // Port defaults to 8266
714 // Port defaults to 8266 689 #ifdef OTA_PORT
-   690 ArduinoOTA.setPort(OTA_PORT);
Line 715... Line 691...
715 ArduinoOTA.setPort(OTA_PORT); 691 #endif
-   692  
716   693 // Hostname defaults to esp8266-[ChipID]
717 // Hostname defaults to esp8266-[ChipID] 694 #ifdef OTA_HOSTNAME
718 if (strlen(OTA_HOSTNAME) != 0) { 695 if (strlen(OTA_HOSTNAME) != 0) {
-   696 ArduinoOTA.setHostname(OTA_HOSTNAME);
Line 719... Line 697...
719 ArduinoOTA.setHostname(OTA_HOSTNAME); 697 }
-   698 #endif
720 } 699  
721   700 // No authentication by default
722 // No authentication by default 701 #ifdef OTA_PASSWORD_HASH
-   702 if (strlen(OTA_PASSWORD_HASH) != 0) {
Line 723... Line 703...
723 if (strlen(OTA_PASSWORD_HASH) != 0) { 703 ArduinoOTA.setPasswordHash(OTA_PASSWORD_HASH);
724 ArduinoOTA.setPasswordHash(OTA_PASSWORD_HASH); 704 }
725 } 705 #endif
726   706  
Line 755... Line 735...
755 Serial.println("End Failed"); 735 Serial.println("End Failed");
756 } 736 }
757 }); 737 });
758 ArduinoOTA.begin(); 738 ArduinoOTA.begin();
Line 759... Line 739...
759   739  
760 // establish/keep mqtt connection -  
761 // 740 // Establish/keep mqtt connection
762 mqtt_client.setServer(MQTT_SERVER, MQTT_PORT); 741 mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
763 mqtt_client.setCallback(mqtt_callback); 742 mqtt_client.setCallback(mqtt_callback);
764 mqtt_reconnect(); 743 mqtt_reconnect();
Line 773... Line 752...
773 controllerStatusPayload["solar"]["monitor"]["status"] = "waiting"; 752 controllerStatusPayload["solar"]["monitor"]["status"] = "waiting";
774 serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer); 753 serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer);
775 controllerStatusPayload.clear(); 754 controllerStatusPayload.clear();
776 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer); 755 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer);
Line 777... Line 756...
777   756  
778 // wait for MQTT subscription processing 757 // Wait for MQTT subscription processing
779 Serial.println("Waiting for MQTT and OTA events."); 758 Serial.println("Waiting for MQTT and OTA events.");
780 unsigned int now = millis(); 759 unsigned int now = millis();
781 while (millis() - now < MQTT_SUBSCRIBE_WAIT * 1000) { 760 while (millis() - now < MQTT_SUBSCRIBE_WAIT * 1000) {
782 // Loop for MQTT. 761 // Loop for MQTT.
Line 787... Line 766...
787 ArduinoOTA.handle(); 766 ArduinoOTA.handle();
788 delay(100); 767 delay(100);
789 } 768 }
790 Serial.println("Done waiting for MQTT and OTA events."); 769 Serial.println("Done waiting for MQTT and OTA events.");
Line 791... Line 770...
791   770  
792 // publish via mqtt -  
793 // 771 // Publish to MQTT
Line 794... Line 772...
794 Serial.print("Publishing to MQTT: "); 772 Serial.print("Publishing to MQTT: ");
795   -  
796 // panel 773  
797 // 774 // Panel
798 epeverMetricsPayload["solar"]["panel"]["V"] = String(live.l.pV / 100.f, 2); 775 epeverMetricsPayload["solar"]["panel"]["V"] = String(live.l.pV / 100.f, 2);
799 epeverMetricsPayload["solar"]["panel"]["I"] = String(live.l.pI / 100.f, 2); 776 epeverMetricsPayload["solar"]["panel"]["I"] = String(live.l.pI / 100.f, 2);
800 epeverMetricsPayload["solar"]["panel"]["P"] = String(live.l.pP / 100.f, 2); 777 epeverMetricsPayload["solar"]["panel"]["P"] = String(live.l.pP / 100.f, 2);
Line 801... Line 778...
801 epeverMetricsPayload["solar"]["panel"]["minV"] = String(stats.s.pVmin / 100.f, 3); 778 epeverMetricsPayload["solar"]["panel"]["minV"] = String(stats.s.pVmin / 100.f, 3);
802 epeverMetricsPayload["solar"]["panel"]["maxV"] = String(stats.s.pVmax / 100.f, 3); 779 epeverMetricsPayload["solar"]["panel"]["maxV"] = String(stats.s.pVmax / 100.f, 3);
803   780  
Line 804... Line 781...
804 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer); 781 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer);
805 epeverMetricsPayload.clear(); -  
806 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "panel").c_str(), tmpJsonPayloadBuffer); 782 epeverMetricsPayload.clear();
807   783 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "panel").c_str(), tmpJsonPayloadBuffer);
808 // battery 784  
809 // 785 // Battery
810 epeverMetricsPayload["solar"]["battery"]["V"] = String(live.l.bV / 100.f, 2); 786 epeverMetricsPayload["solar"]["battery"]["V"] = String(live.l.bV / 100.f, 2);
Line 819... Line 795...
819   795  
820 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer); 796 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer);
821 epeverMetricsPayload.clear(); 797 epeverMetricsPayload.clear();
Line 822... Line 798...
822 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "battery").c_str(), tmpJsonPayloadBuffer); 798 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "battery").c_str(), tmpJsonPayloadBuffer);
823   -  
824 // load 799  
825 // 800 // Load
826 epeverMetricsPayload["solar"]["load"]["V"] = String(live.l.lV / 100.f, 2); 801 epeverMetricsPayload["solar"]["load"]["V"] = String(live.l.lV / 100.f, 2);
827 epeverMetricsPayload["solar"]["load"]["I"] = String(live.l.lI / 100.f, 2); 802 epeverMetricsPayload["solar"]["load"]["I"] = String(live.l.lI / 100.f, 2);
828 epeverMetricsPayload["solar"]["load"]["P"] = String(live.l.lP / 100.f, 2); 803 epeverMetricsPayload["solar"]["load"]["P"] = String(live.l.lP / 100.f, 2);
Line 838... Line 813...
838   813  
839 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer); 814 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer);
840 epeverMetricsPayload.clear(); 815 epeverMetricsPayload.clear();
Line 841... Line 816...
841 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "load").c_str(), tmpJsonPayloadBuffer); 816 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "load").c_str(), tmpJsonPayloadBuffer);
842   -  
843 // energy 817  
844 // 818 // Energy
845 epeverMetricsPayload["solar"]["energy"]["consumed_day"] = String(stats.s.consEnerDay / 100.f, 3); 819 epeverMetricsPayload["solar"]["energy"]["consumed_day"] = String(stats.s.consEnerDay / 100.f, 3);
846 epeverMetricsPayload["solar"]["energy"]["consumed_all"] = String(stats.s.consEnerTotal / 100.f, 3); 820 epeverMetricsPayload["solar"]["energy"]["consumed_all"] = String(stats.s.consEnerTotal / 100.f, 3);
Line 847... Line 821...
847 epeverMetricsPayload["solar"]["energy"]["generated_day"] = String(stats.s.genEnerDay / 100.f, 3); 821 epeverMetricsPayload["solar"]["energy"]["generated_day"] = String(stats.s.genEnerDay / 100.f, 3);
848 epeverMetricsPayload["solar"]["energy"]["generated_all"] = String(stats.s.genEnerTotal / 100.f, 3); 822 epeverMetricsPayload["solar"]["energy"]["generated_all"] = String(stats.s.genEnerTotal / 100.f, 3);
849   823  
Line 850... Line 824...
850 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer); 824 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer);
851 epeverMetricsPayload.clear(); -  
852 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "energy").c_str(), tmpJsonPayloadBuffer); 825 epeverMetricsPayload.clear();
853   826 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "energy").c_str(), tmpJsonPayloadBuffer);
854 // extra 827  
855 // 828 // Extra
856 epeverMetricsPayload["solar"]["extra"]["CO2"]["t"] = String(stats.s.c02Reduction / 100.f, 2); 829 epeverMetricsPayload["solar"]["extra"]["CO2"]["t"] = String(stats.s.c02Reduction / 100.f, 2);
Line 863... Line 836...
863   836  
864 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer); 837 serializeJson(epeverMetricsPayload, tmpJsonPayloadBuffer);
865 epeverMetricsPayload.clear(); 838 epeverMetricsPayload.clear();
Line 866... Line 839...
866 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "extra").c_str(), tmpJsonPayloadBuffer); 839 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "extra").c_str(), tmpJsonPayloadBuffer);
867   -  
868 // settings 840  
Line 869... Line 841...
869 // 841 // Settings
870 epeverMetricsPayload["solar"]["monitor"]["settings"]["sleep"].set(sleepSeconds); 842 epeverMetricsPayload["solar"]["monitor"]["settings"]["sleep"].set(sleepSeconds);
871   843  
Line 878... Line 850...
878 controllerStatusPayload["solar"]["monitor"]["status"] = "offline"; 850 controllerStatusPayload["solar"]["monitor"]["status"] = "offline";
879 serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer); 851 serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer);
880 controllerStatusPayload.clear(); 852 controllerStatusPayload.clear();
881 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer); 853 mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer);
Line 882... Line 854...
882   854  
883 // ensure all messages are sent 855 // Ensure all messages are sent
884 mqtt_client.unsubscribe(MQTT_TOPIC_SUB); 856 mqtt_client.unsubscribe(MQTT_TOPIC_SUB);
885 mqtt_client.disconnect(); 857 mqtt_client.disconnect();
886 while (mqtt_client.state() != -1) { 858 while (mqtt_client.state() != -1) {
887 delay(100); 859 delay(100);
Line 893... Line 865...
893 // power down MAX485_DE 865 // power down MAX485_DE
894 // low active 866 // low active
895 digitalWrite(MAX485_RE, 0); 867 digitalWrite(MAX485_RE, 0);
896 digitalWrite(MAX485_DE, 0); 868 digitalWrite(MAX485_DE, 0);
Line 897... Line -...
897   -  
898 // DeepSleep n microseconds 869  
899 // 870 // Sleep
900 Serial.print("\nDeepSleep for "); 871 Serial.print("\nSleep for ");
901 Serial.print(sleepSeconds); 872 Serial.print(sleepSeconds);
-   873 Serial.println(" Seconds");
-   874 digitalWrite(LED_BUILTIN, LOW);
902 Serial.println(" Seconds"); 875 #ifdef USE_DEEP_SLEEP
903 if (USE_DEEP_SLEEP) { 876 if (USE_DEEP_SLEEP) {
904 ESP.deepSleep(sleepSeconds * 1000000); 877 ESP.deepSleep(sleepSeconds * 1000000);
905 } else { 878 } else {
906 delay(sleepSeconds * 1000); 879 delay(sleepSeconds * 1000);
-   880 }
-   881 #else
-   882 delay(sleepSeconds * 1000);
907 } 883 #endif
908 } 884 }