Thanks again - I’ll think I get that volatile thing now. All variables within my function blink that are altered by an interupt, have to be ‘volatile’. Ok changed that
Also changed -hopefully all- String an placeholder things to better code that extra double quote and also the request.path problem should be copy and paste problems… changed them
At the moment the following code runs without any SOS for about an hour now…
//HTTP Client
unsigned int nextTime = 0;
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 = 129;
int ReportIntervall = 15;
byte minPulseWidth = 25;
int AnzahlPulseProKwh = 2000;
int aktiveNutzung = 975;
int passiveNutzung = 800;
char resultstr[128];
unsigned long PulseTime=0;
volatile int PulseCount=0;
int DifPulseCount=0;
int OldPulseCount=0;
unsigned long PulsePower=0;
volatile float kwhCount=0;
volatile float kwhCount_startup=0;
float kwhCount_overall=0;
int Timestamp=0;
String deviceID=Particle.deviceID();;
byte ConnectTry =1;
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, die einmalig am Anfang ausgeführt wird
void setup() {
pinMode(D3, INPUT);
attachInterrupt(D3, blink, RISING);
Particle.publish("pmr-photon-1", String::format("1.0) Version: %d - gestarted", 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";
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("2.%d) Version: %d - Response.status: %d Response.body: %s", ConnectTry, TryNr, response.status, (const char*)response.body));
ConnectTry = ConnectTry+1;
delay(5000);
} while(response.status != 200 && millis() - msTimeout < 60000);
if(response.status == 200){
kwhCount_startup = response.body.toFloat();
kwhCount_overall = kwhCount + kwhCount_startup;
Particle.publish("pmr-photon-1", String::format("2.%d) Version: %d - Response.body: %s kwhCount_overall: %.2f", ConnectTry, TryNr, response.status, (const char*)response.body, kwhCount_overall));
}
}
//Loop
void loop() {
//kwhCount_overall = 0; // removed
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: "+PassiveUsageCounter ); PassiveUsageMessage=2;}
UsageMessage = ActiveUsageMessage+PassiveUsageMessage;
//http Client
request.hostname = "www.__mywebsite__.xyz";
request.port = 80;
request.path = String::format("/Testumgebung/Photon/index2.php?deviceID=%s&Timestamp=%d&PulseCount=%d&DifPulseCount=%d&kwhCount=%.2f&kwhCount_overall=%.2f&PulsePower=%d&UsageMessage=%s"
, (const char*)deviceID
, Timestamp
, PulseCount
, DifPulseCount
, kwhCount
, kwhCount_overall
, PulsePower
, (const char*)UsageMessage
);
Particle.publish("pmr-photon-1", String::format("3.0) Version: %d - kwh: %.2f - kwh_overall: %.2f - Difpulses: %d", TryNr, kwhCount, kwhCount_overall, DifPulseCount));
sprintf(resultstr, "{\"data1\":%d,\"data2\":%d,\"data3\":%d,\"data4\":%d,\"data5\":%.2f,\"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; EEPROM.put(0, kwhCount_overall); Particle.publish("power-meter-reporter", "EEPROM gespeichert:"+kwhCount_overall);}
//day_old = day_now;
}
But there is one thing I don’t understand yet - maybe you can give me a hint again
The photon is going on- and offline in short periods…
A second photon is doing its work about 3meters away…could that be a problem?