Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Make SSL connection work reliably
Browse files Browse the repository at this point in the history
free up memory to stabilize SSL
  • Loading branch information
helmut-s committed Oct 18, 2016
1 parent a51d18c commit fe7e2d8
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 126 deletions.
6 changes: 3 additions & 3 deletions DisplayCharter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void DisplayCharter::SetBackground(String color){
backgroundColor = (int)strtol(color.c_str(), NULL, 16);
}
}
void DisplayCharter::SetWirl(byte wspeed, bool clockwise){
void DisplayCharter::SetWhirl(byte wspeed, bool clockwise){
whirlSpeed = wspeed;
whirlTick = 0xFF - whirlSpeed;
whirlClockwise = clockwise;
Expand All @@ -40,8 +40,8 @@ void DisplayCharter::SetMorph(int period, byte mspeed){
morphSpeed = 10;
else
morphSpeed = mspeed;
//Serial.println("SetMorph(" + String(morphPeriod) + ", " + String(morphSpeed) + ")");
}

unsigned int DisplayCharter::ParseLedArg(String argument, unsigned int iPos){
byte seg = 0;
String pos;
Expand Down Expand Up @@ -93,7 +93,7 @@ void DisplayCharter::ParseWhirlArg(String argument){
wspeed.trim();

if (wspeed.length() > 0){
SetWirl((byte)wspeed.toInt(), !seg);
SetWhirl((byte)wspeed.toInt(), !seg);
}
}
void DisplayCharter::ParseMorphArg(String argument){
Expand Down
3 changes: 1 addition & 2 deletions DisplayCharter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ class DisplayCharter
public:
DisplayCharter();
void Init();
void Clear();
void SetLeds(byte pos, byte count, unsigned int color);
void SetBackground(String color);
void SetWirl(byte wspeed, bool clockwise);
void SetWhirl(byte wspeed, bool clockwise);
void SetMorph(int period, byte mspeed);
unsigned int ParseLedArg(String argument, unsigned int iPos);
void ParseWhirlArg(String argument);
Expand Down
27 changes: 7 additions & 20 deletions api.ino
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
void apiHandler() {
ipDisplay.StopShowingIp();

/*if (debug) { // print arguments
for (int i = 0; i < httpServer.args(); i++) {
Serial.println(F("API call argument: ") + httpServer.argName(i) + F(" value: ") + httpServer.arg(i));
}
if (httpServer.args() == 0) {
Serial.println(F("API call with no arguments"));
} else {
Serial.println(F(" ---end of arguments---"));
}
}*/
// adjust logo brightness (on/off right now)
if (httpServer.hasArg(F("logo"))) {
if (httpServer.arg(F("logo")).equals(F("on"))) {
Expand All @@ -25,9 +15,9 @@ void apiHandler() {
// to quickly set the RGB colors of the logo remotely
if (httpServer.hasArg(F("logoled"))) {
byte led = byte(httpServer.arg(F("logoled")).toInt());
byte r = byte(httpServer.arg("r").toInt());
byte g = byte(httpServer.arg("g").toInt());
byte b = byte(httpServer.arg("b").toInt());
byte r = byte(httpServer.arg(F("r")).toInt());
byte g = byte(httpServer.arg(F("g")).toInt());
byte b = byte(httpServer.arg(F("b")).toInt());
ledstrip_logo.setPixelColor(led, r, g, b);
}

Expand Down Expand Up @@ -82,13 +72,10 @@ void apiHandler() {
}

if (httpServer.hasArg(F("dynatrace-environmentid")) || httpServer.hasArg(F("dynatrace-apikey"))) {
//if (debug) Serial.println(F("Storing Dynatrace SaaS/Managed environment integration settings"));
if (debug) Serial.println(F("Storing Dynatrace SaaS/Managed environment integration settings"));
dynatraceEnvironmentID = httpServer.arg(F("dynatrace-environmentid"));
dynatraceApiKey = httpServer.arg(F("dynatrace-apikey"));
//if (debug) Serial.println("Stored: " + httpServer.arg("dynatrace-environmentid") + " / " + httpServer.arg("dynatrace-apikey"));
boolean saved = configWrite();
//if (debug) Serial.println("Config saved. " + String(saved) + " rebooting.....");
//if (debug) Serial.flush();
configWrite();
httpReboot(F("Configuration succeeded!"));
}

Expand All @@ -104,7 +91,7 @@ void apiHandler() {
}

if (debug) {
Serial.println(String(F("New Wifi settings: ")) + newWifiSSID + " / " + newWifiPwd);
Serial.println(String(F("New Wifi settings: ")) + newWifiSSID + F(" / ") + newWifiPwd);
Serial.println(String(F("Restarting....")));
Serial.flush();
}
Expand All @@ -116,7 +103,7 @@ void apiHandler() {
String newWifiHostname = httpServer.arg(F("hostname"));
//TODO##################################################################
}

httpServer.sendHeader(F("cache-control"), F("private, max-age=0, no-cache, no-store"));
httpServer.send(200);
}
Expand Down
1 change: 0 additions & 1 deletion config.ino
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ bool configRead() {
}



// note that writing to the SPIFFS wears the flash memory; so make sure to only use it when saving is really required.
bool configWrite() {
StaticJsonBuffer < MAX_CONFIGFILESIZE + 1 > jsonBuffer;
Expand Down
100 changes: 84 additions & 16 deletions datapolling.ino
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@

void urlSetup(){
httpClient.begin(String(F("https://")) + dynatraceEnvironmentID + F(".live.dynatrace.com/api/v1/problem/status?Api-Token=") + dynatraceApiKey, F("CD:57:0F:00:D7:50:2D:8A:F1:2C:5D:6C:AB:5E:E5:B1:3C:08:99:AC"));
httpClient.setReuse(true);
}

void pollDynatraceProblemAPI() {

if (wifiStationOK) {
Expand All @@ -11,44 +17,106 @@ void pollDynatraceProblemAPI() {
// configure server and url
// NOTE: SSL takes 18kB extra RAM memory!!! leads out-of-memory crash!!!! thus the HTTPserver and all other RAM consuming items must be turned off
// the fingerprint is needed to validate the server certificate
httpClient.begin("https://" + dynatraceEnvironmentID + ".live.dynatrace.com/api/v1/problem/status?Api-Token=" + dynatraceApiKey, F("CD:57:0F:00:D7:50:2D:8A:F1:2C:5D:6C:AB:5E:E5:B1:3C:08:99:AC"));

// httpClient.begin(String(F("https://")) + dynatraceEnvironmentID + F(".live.dynatrace.com/api/v1/problem/status?Api-Token=") + dynatraceApiKey, F("CD:57:0F:00:D7:50:2D:8A:F1:2C:5D:6C:AB:5E:E5:B1:3C:08:99:AC"));
if (debug) Serial.println(String(F("http.begin executed. free heap: ")) + String(ESP.getFreeHeap()));
if (debug) Serial.flush();


int httpCode = httpClient.GET();
if (httpCode == HTTP_CODE_OK) {
if (httpClient.getSize() > 128) {
if (debug) Serial.println(String(F("Dynatrace Problem API call response too large to handle! ")) + String(httpClient.getSize()) + " bytes");
if (debug) Serial.println(String(F("Dynatrace Problem API call response too large to handle! ")) + String(httpClient.getSize()) + String(F(" bytes")));
httpClient.end();
urlSetup();
return;
}

if (debug) Serial.println(String(F("GET resulted in OK. free heap: ")) + String(ESP.getFreeHeap()));
if (debug) Serial.flush();

String json = httpClient.getString(); // this allocates memory, so lets not get this string if the HTTP response is too large

if (debug) Serial.println(String(F("Dynatrace Problem API call: ")) + json);
if (debug) Serial.flush();
StaticJsonBuffer < 128 + 2 > jsonBuffer; // attention, too large buffers cause stack overflow and thus crashes!
JsonObject& jsonObject = jsonBuffer.parseObject(json);
long applicationProblems = jsonObject["result"]["totalOpenProblemsCount"];
//long applicationProblems = jsonObject["result"]["openProblemCounts"]["APPLICATION"];
if (debug) Serial.println(String(F("Dynatrace Problem API call - Application problems: ")) + String(applicationProblems));
if (applicationProblems) {
redcountUpperring = 14;
redcountLowerring = 14;
} else {
redcountUpperring = 1;
redcountLowerring = 1;

bool changeDetected = false;
int i = jsonObject[F("result")][F("openProblemCounts")][F("APPLICATION")];
if (i != applicationProblems){
changeDetected = true;
applicationProblems = i;
}
i = jsonObject[F("result")][F("openProblemCounts")][F("SERVICE")];
if (i != serviceProblems){
changeDetected = true;
serviceProblems = i;
}
i = jsonObject[F("result")][F("openProblemCounts")][F("INFRASTRUCTURE")];
if (i != infrastructureProblems){
changeDetected = true;
infrastructureProblems = i;
}
if (debug){
Serial.println(String(F("Dynatrace Problem API call - Application problems: ")) + String(applicationProblems));
Serial.println(String(F("Dynatrace Problem API call - Service problems: ")) + String(serviceProblems));
Serial.println(String(F("Dynatrace Problem API call - Infrastructure problems: ")) + String(infrastructureProblems));
}

if (changeDetected){
int count = applicationProblems + serviceProblems + infrastructureProblems;

displayCharter_upperring.Init();
displayCharter_lowerring.Init();
switch (count){
case 0:
displayCharter_upperring.SetLeds(0, 15, 0xff0000);
displayCharter_lowerring.SetLeds(0, 15, 0xff0000);
displayCharter_upperring.SetMorph(4000, 6);
displayCharter_lowerring.SetMorph(4000, 6);
break;
case 1:
displayCharter_upperring.SetLeds(0, 15, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(0, 15, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetMorph(1000, 8);
displayCharter_lowerring.SetMorph(1000, 8);
break;
case 2:
displayCharter_upperring.SetLeds(0, 7, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(0, 7, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetLeds(8, 6, (applicationProblems > 1) ? 0x00ff00 : ((applicationProblems + serviceProblems > 1) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(8, 6, (applicationProblems > 1) ? 0x00ff00 : ((applicationProblems + serviceProblems > 1) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetWhirl(180, true);
displayCharter_lowerring.SetWhirl(180, true);
break;
default:
displayCharter_upperring.SetLeds(0, 4, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(0, 4, (applicationProblems > 0) ? 0x00ff00 : ((serviceProblems > 0) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetLeds(5, 4, (applicationProblems > 1) ? 0x00ff00 : ((applicationProblems + serviceProblems > 1) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(5, 4, (applicationProblems > 1) ? 0x00ff00 : ((applicationProblems + serviceProblems > 1) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetLeds(10, 4, (applicationProblems > 2) ? 0x00ff00 : ((applicationProblems + serviceProblems > 2) ? 0x00ffaa : 0xaaff00));
displayCharter_lowerring.SetLeds(10, 4, (applicationProblems > 2) ? 0x00ff00 : ((applicationProblems + serviceProblems > 2) ? 0x00ffaa : 0xaaff00));
displayCharter_upperring.SetWhirl(180, true);
displayCharter_lowerring.SetWhirl(180, true);
break;
}
}

} else {
if (debug) Serial.println(String(F("Dynatrace Problem API call FAILED (error code ")) + httpClient.errorToString(httpCode) + ")" );
redcountUpperring = 7;
redcountLowerring = 8;
if (debug) Serial.println(String(F("Dynatrace Problem API call FAILED (error code ")) + httpClient.errorToString(httpCode) + F(")") );
displayCharter_upperring.Init();
displayCharter_lowerring.Init();
displayCharter_upperring.SetLeds(0, 3, 0x0000ff);
displayCharter_lowerring.SetLeds(0, 3, 0x0000ff);
displayCharter_upperring.SetWhirl(220, true);
displayCharter_lowerring.SetWhirl(220, false);
applicationProblems = -1;
serviceProblems = -1;
infrastructureProblems = -1;
}
httpClient.end();
//httpClient.end();



/* {
Expand Down
Loading

0 comments on commit fe7e2d8

Please sign in to comment.