Skip to content

Commit

Permalink
Better handling of packet errors
Browse files Browse the repository at this point in the history
Debug logging off
  • Loading branch information
jantenhove committed Jul 17, 2019
1 parent ea1fd36 commit 4fb44de
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
5 changes: 3 additions & 2 deletions GoodWeCommunicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void GoodWeCommunicator::checkIncomingData()
{
//packet start received
startPacketReceived = true;
lastReceived = millis();
curReceivePtr = 0;
numToRead = 0;
lastReceivedByte = 0x00; //reset last received for next packet
Expand Down Expand Up @@ -172,9 +173,9 @@ void GoodWeCommunicator::checkIncomingData()
lastReceivedByte = incomingData; //keep track of the last incoming byte so we detect the packet start
}

lastReceived = millis();

}
else if (startPacketReceived && millis() - lastReceived > PACKET_TIMEOUT) // 0.5 sec timoeut
if (startPacketReceived && millis() - lastReceived > PACKET_TIMEOUT) // 0.5 sec timoeut
{
//there is an open packet timeout.
startPacketReceived = false; //wait for start packet again
Expand Down
2 changes: 1 addition & 1 deletion GoodWeCommunicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "SettingsManager.h"

#define GOODWE_COMMS_ADDRES 0xAB
#define PACKET_TIMEOUT 500 //0.5 sec packet timeout
#define PACKET_TIMEOUT 5000 //5 sec packet timeout
#define OFFLINE_TIMEOUT 30000 //30 seconds no data -> inverter offline
#define DISCOVERY_INTERVAL 10000 //10 secs between discovery
#define INFO_INTERVAL 1000 //get inverter info every second
Expand Down
41 changes: 33 additions & 8 deletions GoodWeLogger.ino
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


#include <TimeLib.h>
#include <NTPClient.h>
#include <ESP8266WiFi.h>
Expand All @@ -11,18 +9,17 @@
#include "SettingsManager.h"
#include "MQTTPublisher.h"
#include "PVOutputPublisher.h"
#include "ESP8266mDNS.h"
#include "Settings.h" //change and then rename Settings.example.h to Settings.h to compile


SettingsManager settingsManager;
GoodWeCommunicator goodweComms(&settingsManager, true);
MQTTPublisher mqqtPublisher(&settingsManager, &goodweComms, true);
PVOutputPublisher pvoutputPublisher(&settingsManager, &goodweComms, true);
GoodWeCommunicator goodweComms(&settingsManager, false);
MQTTPublisher mqqtPublisher(&settingsManager, &goodweComms, false);
PVOutputPublisher pvoutputPublisher(&settingsManager, &goodweComms, false);
WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "pool.ntp.org");
bool validTimeSet =false;
int reconnectCounter = 0;

void setup()
{
Expand Down Expand Up @@ -62,7 +59,7 @@ void setup()
Serial.println("Connected!");

timeClient.begin();

ArduinoOTA.setHostname("GoodWeLogger");

ArduinoOTA.onStart([]() {
Expand Down Expand Up @@ -94,6 +91,30 @@ void setup()
timeClient.setTimeOffset(settings->timezone * 60 * 60);
}

bool checkConnectToWifi()
{
//check for wifi connection
while (WiFi.status() != WL_CONNECTED || WiFi.localIP() == IPAddress(0, 0, 0, 0))
{
Serial.println("Connecting to WIFI...");
reconnectCounter++;
if (reconnectCounter > 60)
return false;
auto settings = settingsManager.GetSettings();
WiFi.disconnect();
WiFi.begin(settings->wifiSSID.c_str(), settings->wifiPassword.c_str());
delay(2000);

if (WiFi.status() == WL_CONNECTED)
{
Serial.println("Connected to the WiFi network");
reconnectCounter = 0;
}
else
delay(5000);//further delay
}
return true;
}

void loop()
{
Expand All @@ -110,6 +131,10 @@ void loop()
setTime(timeClient.getEpochTime());
}

//check wifi connection
if (!checkConnectToWifi())
ESP.restart();

ArduinoOTA.handle();
yield();
goodweComms.handle();
Expand Down
4 changes: 4 additions & 0 deletions GoodWeLogger.vcxproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>
1 change: 1 addition & 0 deletions MQTTPublisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ void MQTTPublisher::handle()
if (!client.connected() && millis() - lastConnectionAttempt > RECONNECT_TIMEOUT) {
if (!reconnect()) return;
}

//got a valid mqtt connection. Loop through the inverts and send out the data if needed
client.loop();

Expand Down

0 comments on commit 4fb44de

Please sign in to comment.