Photon sends sos after flashing my firmware - help needed

Hi All,

I’m not a totally beginner, but… near that :wink: It is my first post here cause I got stuck with my firmware…
I tried to modify my firmware to hand over values to a PHP page for processing them later… I used the HttpClient library and combined it with my code… but after that firmware is not flashable… It always ends up with an SOS…

Maybe someone can give me a hint what I have done wrong?

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"

/* 
Pulse counter to measure used kWh and calculate current W usage from a GM3D

GND ---\
       |
       O 10k
       |
D3 ----+------ 41

3v3 -----------42

*/


//für den HTTP Client
unsigned int nextTime = 0;    // Next time to contact the server
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;

//Versuch Nr
byte TryNr = 105;

// Eltako pulse lenght 30ms
byte minPulseWidth = 25;

unsigned long PulseTime=0;
volatile unsigned int PulseCount=0;       //Zählt die Impule die länger als 25ms sind
volatile unsigned int DifPulseCount=0;    //Anzahl der Impule zwischen zwei Meldungen
volatile unsigned int OldPulseCount=0;    //Zur Berechnung benötigt
volatile unsigned long PulsePower=0;
volatile unsigned int kwhCount=0;
int Timestamp=0;
String deviceID=0;

void blink() {
  if ( (millis() - PulseTime) > minPulseWidth) {
    PulseCount++;
    kwhCount = PulseCount / 2000;
  }
}

void setup() {
  pinMode(D3, INPUT);  
  attachInterrupt(D3, blink, RISING);
  Particle.publish("power-meter-reporter", "started -"+String(TryNr));

}

void loop() {
  Timestamp = Time.now();
  deviceID  = Particle.deviceID();
  DifPulseCount = PulseCount - OldPulseCount;   //Berechnet die Fiffernz zur letzten Meldung 
  PulsePower = DifPulseCount * 30;              // DifPulseCount / 2000 * 1000 / 1 * 60; // Könnte die Watt im Zeitraum 1 Minute (delay unten) berechnen
  String data = "Pulses:" + String(PulseCount) + ", Dif:" + String(DifPulseCount) + ", Kwh:" + String(kwhCount) + ", Power:" + String(PulsePower);
  OldPulseCount = PulseCount;                   // Setzt den akutellen Wert

  //http Client
  request.hostname = "diefrieds.de";
  request.port = 80;
  request.path = "/diefrieds/Testumgebung/Photon/";
  request.body = "deviceID=" + deviceID + "&Timestamp=" + Timestamp + "&PulseCount=" + PulseCount + "&DifPulseCount=" + DifPulseCount + "&kwhCount=" + kwhCount + "&PulsePower=" + PulsePower;
  http.get(request, response, headers);

  delay(60000); //Publish every (10Sec = 10000, 60sec = 60000)
}

Thanks in advance!!

Before I look at your code, you can usually flash OTA your device when you put it in Safe Mode (search docs for that)


Where do you set your PulseTime?

For formatted strings, I’d suggest this way of writing it

String data = String::format("Pulses:%d, Dif:%d, kWh:%d, Power:%d", PulseCount, DifPulseCount, kwhCount, PulsePower);

I’d also set the deviceID only once in setup().

Thanks for the fast hints - I already flashed it with the “version” before and it is working …without the http

Formatting Strings: Ok I’ll give that a try :wink:
DeviceID… ; I “defined” it in setup and “set” it later in the loop… but you’re right it stays the same and so its enough in the setup

Ok, I changed the things you mentioned to the Code below:

// This #include statement was automatically added by the Particle IDE.
#include "HttpClient/HttpClient.h"
/* 
Pulse counter to measure used kWh and calculate current W usage from a GM3D
GND ---\
       |
       O 10k
       |
D3 ----+------ 41
3v3 -----------42
*/
//für den HTTP Client
unsigned int nextTime = 0;    // Next time to contact the server
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;
//Versuch Nr
byte TryNr = 105;
// Eltako pulse lenght 30ms
byte minPulseWidth = 25;
unsigned long PulseTime=0;
volatile unsigned int PulseCount=0;       //Zählt die Impule die länger als 25ms sind
volatile unsigned int DifPulseCount=0;    //Anzahl der Impule zwischen zwei Meldungen
volatile unsigned int OldPulseCount=0;    //Zur Berechnung benötigt
volatile unsigned long PulsePower=0;
volatile unsigned int kwhCount=0;
int Timestamp=0;
String deviceID=Particle.deviceID();;
void blink() {
  if ( (millis() - PulseTime) > minPulseWidth) {
    PulseCount++;
    kwhCount = PulseCount / 2000;
  }
}
void setup() {
  pinMode(D3, INPUT);  
  attachInterrupt(D3, blink, RISING);
  Particle.publish("power-meter-reporter", "started -"+String(TryNr));
}
void loop() {
  Timestamp = Time.now();
  DifPulseCount = PulseCount - OldPulseCount;   //Berechnet die Fiffernz zur letzten Meldung 
  PulsePower = DifPulseCount * 30;              // DifPulseCount / 2000 * 1000 / 1 * 60; // Könnte die Watt im Zeitraum 1 Minute (delay unten) berechnen
  String data = String::format("Pulses:%d, Dif:%d, kWh:%d, Power:%d", PulseCount, DifPulseCount, kwhCount, PulsePower);
  OldPulseCount = PulseCount;                   // Setzt den akutellen Wert
  //http Client
  request.hostname = "diefrieds.de";
  request.port = 80;
  request.path = "/diefrieds/Testumgebung/Photon/";
  request.body = "deviceID=" + deviceID + "&Timestamp=" + Timestamp + "&PulseCount=" + PulseCount + "&DifPulseCount=" + DifPulseCount + "&kwhCount=" + kwhCount + "&PulsePower=" + PulsePower;
  http.get(request, response, headers);
  delay(60000); //Publish every (10Sec = 10000, 60sec = 60000)
}

And something happend, flashing was successfull, then I could see an SOS again for 2 times, then a normal breathing, and now SOS again....restart SOS.... and so on

I see you don’t use data anywhere in your code, so you could just drop it.
Also try to redo your string building for request.body like this

request.body = String::format("deviceID=%s&Timestamp=%d&PulseCount=%d&DifPulseCount=%d&kwhCount=%d&PulsePower=%d"
                             , (const char*)deviceID
                             , Timestamp 
                             , PulseCount
                             , DifPulseCount
                             , kwhCount
                             , PulsePower
                             );

You could also try to set the static fields of request in setup() once.
One thing with String objects is that they do allocate memory on the heap in a somewhat unpredictable way, so if you see your SOS happen after some while, this could be due to heap fragmentation/overflow.

How often does the Photon blink red in between the two SOS codes and when exactly does the SOS happen?

BTW: I think you want to alter your ISR like this

void blink() {
  if ( (millis() - PulseTime) > minPulseWidth) {
    PulseCount++;
    kwhCount = PulseCount / 2000;
    PulseTime = millis(); // I guess this was missing ;-)
  }
}

:wink: Ok, data was used for a particle.publish before… maybe I should “reactivate” it to see resultes at dashboard.
I used your request.body reformatting (Thanks for that!!) and flashed again
Now there is now SOS !!! but normal breathing - great!

So It seems to me, that it was a problem with formating the strings?

The PulseTime was missing - thanks!!! seems to be deleted by accident :wink:

next thing I will have to figure out is, why the PHP script does not record data :wink: but that is another thing :wink:

Thanks for your fast help…

1 Like

The advantage of living in the same timezone :wink:

Yeah, thats great! Thanks again!