/arduinoEpeverSolarController/arduinoEpeverSolarController.ino |
@@ -76,7 +76,6 @@ |
// { // |
// "action" : "settings" // |
// "sleep" : 120 // |
// "value" : // |
// } // |
// // |
// Note that for the switch to work, the Epever device has to be set to // |
@@ -96,6 +95,11 @@ |
#include <PubSubClient.h> |
#include <ArduinoJson.h> |
#include <ArduinoOTA.h> |
#if defined(ARDUINO_ARCH_ESP8266) |
#include <ESP_EEPROM.h> |
#elif defined(ARDUINO_ARCH_ESP32) |
#include <EEPROM.h> |
#endif |
|
/////////////////////////////////////////////////////////////////////////// |
// configuration // |
@@ -135,8 +139,7 @@ |
WiFiClient wifi_client; |
PubSubClient mqtt_client(wifi_client); |
bool loadState = true; |
// 1 minutes default |
unsigned int sleepSeconds = 60; |
int sleepSeconds; |
const int JSON_DOCUMENT_SIZE = 512; |
|
StaticJsonDocument<JSON_DOCUMENT_SIZE> controllerStatusPayload; |
@@ -442,11 +445,9 @@ |
node.clearResponseBuffer(); |
node.writeSingleCoil(0x0001, 1); |
result = node.writeSingleCoil(0x0002, loadState); |
if(result == node.ku8MBSuccess) { |
if (result == node.ku8MBSuccess) { |
Serial.println("success"); |
} |
else |
{ |
} else { |
Serial.println("failure"); |
Serial.print("Miss write loadState, ret val: "); |
Serial.println(result, HEX); |
@@ -456,13 +457,25 @@ |
if (epeverControlPayload["action"] == "settings") { |
if (epeverControlPayload.containsKey("sleep")) { |
// input sanitization |
Serial.print("Set sleep seconds to: "); |
int seconds = (unsigned int)epeverControlPayload["sleep"]; |
if (seconds == sleepSeconds) { |
Serial.println("no change"); |
return; |
} |
|
if (seconds < MIN_SLEEP_SECONDS) { |
sleepSeconds = MIN_SLEEP_SECONDS; |
Serial.println(MIN_SLEEP_SECONDS); |
} else { |
sleepSeconds = seconds; |
} |
sleepSeconds = seconds; |
|
EEPROM.put(0, sleepSeconds); |
if (!EEPROM.commit()) { |
Serial.println("Failure setting sleep seconds."); |
return; |
} |
|
Serial.print("Set sleep seconds to: "); |
Serial.println(sleepSeconds); |
} |
|
@@ -471,18 +484,14 @@ |
} |
} |
|
/////////////////////////////////////////////////////////////////////////// |
////////////////////////// |
///////////////////////////////////////////////// |
// Arduino functions // |
/////////////////////////////////////////////////////////////////////////// |
void setup() { |
// say hello |
Serial1.begin (74880); |
Serial1.println("Serial1 Initialised"); |
|
Serial.begin(115200); // DO NOT CHANGE! |
while (!Serial) { |
Serial.begin(115200); // DO NOT CHANGE! |
while (!Serial) |
; |
} |
Serial.println(); |
Serial.println("Hello World! I'm an EpEver Solar Monitor!"); |
|
@@ -492,12 +501,11 @@ |
digitalWrite(MAX485_RE, 0); |
digitalWrite(MAX485_DE, 0); |
|
// EPEver Device ID 1 |
node.begin(1, Serial); |
|
// modbus callbacks |
node.preTransmission(preTransmission); |
node.postTransmission(postTransmission); |
// EPEver Device ID 1 |
node.begin(1, Serial); |
|
// Connect D0 to RST to wake up |
if (USE_DEEP_SLEEP) { |
@@ -504,6 +512,17 @@ |
pinMode(D0, WAKEUP_PULLUP); |
} |
|
// variable handling |
EEPROM.begin(16); |
EEPROM.get(0, sleepSeconds); |
if (sleepSeconds < MIN_SLEEP_SECONDS) { |
sleepSeconds = 60; |
EEPROM.put(0, sleepSeconds); |
if (!EEPROM.commit()) { |
Serial.println("Unable to set default sleep."); |
} |
} |
|
// Initialize the LED_BUILTIN pin as an output, low active |
pinMode(LED_BUILTIN, OUTPUT); |
digitalWrite(LED_BUILTIN, HIGH); |
@@ -751,6 +770,25 @@ |
controllerStatusPayload.clear(); |
mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer); |
|
controllerStatusPayload["solar"]["monitor"]["status"] = "waiting"; |
serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer); |
controllerStatusPayload.clear(); |
mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer); |
|
// wait for MQTT subscription processing |
Serial.println("Waiting for MQTT and OTA events."); |
unsigned int now = millis(); |
while (millis() - now < MQTT_SUBSCRIBE_WAIT * 1000) { |
// Loop for MQTT. |
if (!mqtt_client.loop() || WiFi.status() != WL_CONNECTED) { |
break; |
} |
// Loop for OTA. |
ArduinoOTA.handle(); |
delay(100); |
} |
Serial.println("Done waiting for MQTT and OTA events."); |
|
// publish via mqtt |
// |
Serial.print("Publishing to MQTT: "); |
@@ -837,25 +875,6 @@ |
|
Serial.println("done"); |
|
controllerStatusPayload["solar"]["monitor"]["status"] = "waiting"; |
serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer); |
controllerStatusPayload.clear(); |
mqtt_client.publish((String(MQTT_TOPIC_PUB) + "/" + "status").c_str(), tmpJsonPayloadBuffer); |
|
// wait for MQTT subscription processing |
Serial.print("Waiting for MQTT and OTA events."); |
unsigned int now = millis(); |
while (millis() - now < MQTT_SUBSCRIBE_WAIT * 1000) { |
// Loop for MQTT. |
if (!mqtt_client.loop() || WiFi.status() != WL_CONNECTED) { |
break; |
} |
// Loop for OTA. |
ArduinoOTA.handle(); |
delay(100); |
} |
Serial.println("Done waiting for MQTT and OTA events."); |
|
controllerStatusPayload["solar"]["monitor"]["status"] = "offline"; |
serializeJson(controllerStatusPayload, tmpJsonPayloadBuffer); |
controllerStatusPayload.clear(); |