Skip to content

Commit

Permalink
ESP32-C3 Tests with real inverter completed
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHeimann committed Mar 23, 2023
1 parent 39cf561 commit 4b352c0
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,37 @@ float temperature1 = 0;
float totalGeneratedPower1 = 0;
float state1 = 0;

// Return OpenNETek/<inverterID>/<parameter
char* get_mqtt_topic(const char* subdir){
//memset(&subdir[0], 0, sizeof(subdir));

char* topic_tmp = new char[50];
strncpy(topic_tmp, mqtt_topic, sizeof(mqtt_topic));
strncat(topic_tmp, subdir, sizeof(subdir));

return topic_tmp;
}

#ifdef ESP32WROOM
// Code to be compiled for ESP32WROOM32
constexpr const uint8_t RX_PIN = 16; /// RX pin
constexpr const uint8_t TX_PIN = 17; /// TX pin
#define clientSerial Serial2
#endif

#ifdef ESP32C3
// Code to be compiled for ESP32C3
constexpr const uint8_t RX_PIN = 9; /// RX pin
constexpr const uint8_t TX_PIN = 10; /// TX pin
#define clientSerial Serial1 /// ESP32C3 does not have Serial2. Serial1 should work on GPIO9 and 10.


#endif


constexpr const uint8_t PROG_PIN = 4; /// Programming enable pin of RF module (not needed when replacing LC12S with ESP)

NETSGPClient pvclient(clientSerial, PROG_PIN); /// NETSGPClient instance

void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Expand Down Expand Up @@ -192,21 +223,27 @@ void callback(char* topic, byte* message, unsigned int length) {
}
}
}

void reconnect() {
// Loop until we're reconnected
while (!mqtt.connected()) {
Serial.print("Attempting MQTT connection...");
//WebSerial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt.connect(hostname)) {
Serial.println("connected");
//WebSerial.print("Attempting MQTT connection...");
// Subscribe
mqtt.subscribe(get_mqtt_topic("/led"));
mqtt.subscribe(get_mqtt_topic("/powerlevel"));
mqtt.subscribe(get_mqtt_topic("/activate"));
mqtt.subscribe(get_mqtt_topic("/reboot"));
} else {
Serial.print("failed, rc=");
//WebSerial.print("failed, rc=");

Serial.print(mqtt.state());
//WebSerial.print(mqtt.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
Expand All @@ -229,27 +266,8 @@ void recvMsg(uint8_t *data, size_t len){
}
}

#ifdef ESP32WROOM
// Code to be compiled for ESP32WROOM32
constexpr const uint8_t RX_PIN = 16; /// RX pin
constexpr const uint8_t TX_PIN = 17; /// TX pin
#define clientSerial Serial2
#endif

#ifdef ESP32C3
// Code to be compiled for ESP32C3
constexpr const uint8_t RX_PIN = 9; /// RX pin
constexpr const uint8_t TX_PIN = 10; /// TX pin
#define clientSerial Serial1 /// ESP32C3 does not have Serial2. Serial1 should work on GPIO9 and 10.


#endif


constexpr const uint8_t PROG_PIN = 4; /// Programming enable pin of RF module (not needed when replacing LC12S with ESP)

NETSGPClient pvclient(clientSerial, PROG_PIN); /// NETSGPClient instance

// Start WifiManager Config Portal. unsigned long as a Parameter, zero for no timeout.
void do_wifi_config(const unsigned long &timeout){
bool config_changed = false;
Expand Down Expand Up @@ -345,16 +363,7 @@ void do_wifi_config(const unsigned long &timeout){
}


// Return OpenNETek/<inverterID>/<parameter
char* get_mqtt_topic(const char* subdir){
//memset(&subdir[0], 0, sizeof(subdir));

char* topic_tmp = new char[50];
strncpy(topic_tmp, mqtt_topic, sizeof(mqtt_topic));
strncat(topic_tmp, subdir, sizeof(subdir));

return topic_tmp;
}


String processor(const String& var){
Expand Down Expand Up @@ -788,6 +797,7 @@ void setup()
delay(1000);
}
// we get here only when WiFi is connected. yay.
wifi_connected = true; // wasn't set by the event yet, since that's not yet started

// Start WiFi Monitoring
WiFi.onEvent(WiFiEvent);
Expand Down Expand Up @@ -1035,9 +1045,13 @@ if (currentMillis - lastSendMillis > 4000)
// do the mqtt thing if MQTT server is given and Wifi is connected
if ( sizeof(mqtt_server) && wifi_connected )
{
//WebSerial.println("MQTT part beginning...");

if (!mqtt.connected())
{
Serial.println("MQTT not connected. Reconecting...");
//WebSerial.print("MQTT not connected. Reconecting...");

reconnect();
}
mqtt.loop();
Expand Down Expand Up @@ -1116,7 +1130,7 @@ if (currentMillis - lastSendMillis > 4000)
Serial.print("Received no Inverter Status from ");
Serial.println(inverterID,16);

//WebSerial.print("Received no Inverter Status from ");
//WebSerial.print("Received no Inverter Status from (ID printed with base10) ");
//WebSerial.println(inverterID);

}
Expand Down

0 comments on commit 4b352c0

Please sign in to comment.