Hi ScruffR,
thanks for your reply and hints… I thought not showing the “unneeded parts” helps focus on the problems…
Here is the complete code…
I have one photon up and running without the "new"part within setup to get the startup value from the website. It does its job without problems for months now. After adding the setup-get-the-value-part I allways get and SOS-Hard fault error on this “test”-Photon.
I started to install the CLI on my system to get Serial working. But to problem is, that system is not accessible from outside my home… that is the reason why Build IDE is a really nice option…
I did find the problem in the marked line of code… if delete this line everything is working “fine”
And yes I know I have a serious problem with the different variable typs, there format and usage… but I’m working on it
That is the reason I need an explaination of “declare all variables changed inside your ISR as volatile”
//HTTP Client
HttpClient http;
// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
// { "Content-Type", "application/json" },
// { "Accept" , "application/json" },
{ "Accept" , "*/*"},
{ NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;
byte TryNr = 128;
int ReportIntervall = 15;
byte minPulseWidth = 25;
int AnzahlPulseProKwh = 2000;
int aktiveNutzung = 975;
int passiveNutzung = 800;
char resultstr[128];
unsigned long PulseTime=0;
int PulseCount=0;
int DifPulseCount=0;
int OldPulseCount=0;
unsigned long PulsePower=0;
float kwhCount=0;
float kwhCount_startup=0;
float kwhCount_overall=0;
int Timestamp=0;
String deviceID=Particle.deviceID();;
int day_now = 0;
int day_old = 0;
int hour_now = 0;
int hour_old =0;
int PassiveUsageCounter = 0;
int ActiveUsageMessage;
int PassiveUsageMessage;
int UsageMessage;
//Funktion blink
void blink() {
if ( (millis() - PulseTime) > minPulseWidth) {
PulseCount++;
kwhCount = PulseCount / AnzahlPulseProKwh ;
kwhCount_overall = kwhCount + kwhCount_startup;
}
}
//Funktion Setup
void setup() {
pinMode(D3, INPUT);
attachInterrupt(D3, blink, RISING);
Particle.publish("pmr-photon-1", "gestarted - Version: "+String(TryNr));
Particle.variable("result", resultstr, STRING);
//http Client
uint32_t msTimeout;
request.hostname = "www.___mywebsite__.xyz";
request.port = 80;
request.path = "/Testumgebung/Photon/index2.php?deviceID=" + deviceID + "&requestID=startupkwhlookup";
request.path = String::format(request.path);
msTimeout = millis(); // for timeout check
do { // enter here anyway and try till success but max 60sec
http.get(request, response, headers);
Particle.publish("pmr-photon-1", String::format("Response.status: %d Response.body: %s", response.status, (const char*)response.body));
delay(5000);
} while(response.status != 200 && millis() - msTimeout < 60000);
if(response.status == 200) {
kwhCount_startup = response.body.toFloat();
kwhCount_overall = kwhCount + kwhCount_startup; // Here seems to be the problem... if deleted everything is ok
}
}
//Loop
void loop() {
Timestamp = Time.now();
DifPulseCount = PulseCount - OldPulseCount;
PulsePower = DifPulseCount * 30 / ReportIntervall;
PulseTime = millis();
OldPulseCount = PulseCount;
day_now = Time.day(); // if (day_now != day_old ) { /*reset all*/ }
hour_now = Time.hour(); // if (hour_now != hour_old ) { /*reset all*/ }
if (DifPulseCount >= aktiveNutzung) {
Particle.publish("pmr-photon-1", "NUTZUNG (Aktiv)entdeckt");
ActiveUsageMessage=1;
}
else {
ActiveUsageMessage=0;
}
if ((DifPulseCount >= passiveNutzung) && (DifPulseCount <= (aktiveNutzung-1) )) {
PassiveUsageCounter++;
}
else {
PassiveUsageCounter=0;
PassiveUsageMessage=0;
}
if (PassiveUsageCounter>4) {
Particle.publish("pmr-photon-1", "NUTZUNG (passiv) entdeckt" );
PassiveUsageMessage=2;
}
UsageMessage = ActiveUsageMessage+PassiveUsageMessage;
//http Client
request.hostname = "www.___mywebsite__.xyz";
request.port = 80;
request.path = "/Testumgebung/Photon/";
request.path = String::format("/Testumgebung/Photon/index2.php?deviceID=%s&Timestamp=%d&PulseCount=%d&DifPulseCount=%d&kwhCount=%d&kwhCount_overall=%d&PulsePower=%d&UsageMessage=%s"
, (const char*)deviceID
, Timestamp
, PulseCount
, DifPulseCount
, kwhCount
, kwhCount_overall
, PulsePower
, (const char*)UsageMessage
);
Particle.publish("pmr-photon-1", "Versuch: "+String(TryNr)+" - kwh: " + String(kwhCount) + " - kwh_overall: " + String(kwhCount_overall) + " - Difpulses: " + String(DifPulseCount));
sprintf(resultstr, "{\"data1\":%d,\"data2\":%d,\"data3\":%d,\"data4\":%d,\"data5\":%d,\"data6\":%d,\"data7\":%d}", Timestamp, PulseCount, DifPulseCount, ReportIntervall, kwhCount, PulsePower, UsageMessage);
delay(ReportIntervall * 60 * 1000); //Publish every (10Sec = 10000, 60sec = 60000)
//if (day_now != day_old) { kwhCount_overall=kwhCount_overall+kwhCount; Particle.publish("power-meter-reporter", "Wert wird irgendwann gespeichert");}
//day_old = day_now;
}