# Counting Problems

**URL:** <https://community.particle.io/t/counting-problems/16284>\
**Category:** Firmware\
**Created:** [October 6, 2015, 2:45pm UTC](https://community.particle.io/t/counting-problems/16284 "2015-10-06T14:45:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 6, 2015, 2:45pm UTC](https://community.particle.io/t/counting-problems/16284/1 "2015-10-06T14:45:36Z")

</div>

Hi,  
I have an application running on a Photon (works on Spark too) and it’s purpose is to count the number of times a red light on a power meter flashes. Basically it measures the consumption by counting the flashes which translate to 1Wh. I have a light sensor connected to D0 as a digital input and every time I detect the input changing state from low to high, I increment two counters - _PulseCount_ and _TotalPulseCount_. I use something like `PulseCount += 1` to increment both variables and they are both declared as _uint32\_t_.

The application attempts to send an http request with the readings at the turn of every minute. The only difference between the two counters is that PulseCount resets to zero with each successful upload while the other one should keep on incrementing until it reaches 10000 and then resets to zero. However, for some strange reason, it is resetting to zero half way through, some times after just 200 counts. Before I post all the code and ask you to look through it for obvious bugs which I might miss, I wanted to ask you if you are aware of any problems where the Photon loses count at seemingly random intervals.

Thanks!

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 6, 2015, 3:21pm UTC](https://community.particle.io/t/counting-problems/16284/2 "2015-10-06T15:21:44Z")

</div>

Nothing I’m aware of, but how’s about system restarts due to cloud connection fail/stall, powerloss, hard faults, …?  
Any buffers overflowing into your counter?

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 6, 2015, 3:49pm UTC](https://community.particle.io/t/counting-problems/16284/3 "2015-10-06T15:49:47Z")

</div>

I suspect as much. Is there some easy way to keep a log of any resets?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 6, 2015, 3:53pm UTC](https://community.particle.io/t/counting-problems/16284/4 "2015-10-06T15:53:57Z")

</div>

@osprey, is you use the particle [dashboard](https://dashboard.particle.io), you will see the Photon going offline.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 6, 2015, 3:59pm UTC](https://community.particle.io/t/counting-problems/16284/5 "2015-10-06T15:59:34Z")

</div>

There are different options.

- you could write the times when you go through `setup()` into your EEPROM
- you can publish something to your HTTP server each time you go through `setup()`
- you could subscribe to this event

```auto
event: spark/status
data: {"data":"online","ttl":"60","published_at":"2015-10-06T15:54:56.796Z","coreid":"<device ID>"}

```

and of course @peekay123’s suggestion 😉

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 6, 2015, 4:13pm UTC](https://community.particle.io/t/counting-problems/16284/6 "2015-10-06T16:13:23Z")

</div>

> @osprey, is you use the particle dashboard2, you will see the Photon going offline.

I'm not too keen on spending time looking for something to happen since it happens randomly and could take hours between events. Unless you mean there is some kind of log I can look at.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [October 6, 2015, 4:14pm UTC](https://community.particle.io/t/counting-problems/16284/7 "2015-10-06T16:14:44Z")

</div>

@osprey, the dashboard IS a log of events 😄

---

<div class="post-metadata">

**Author:** ![AndyW](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/andyw/32/686_2.png) [@AndyW](https://community.particle.io/u/AndyW)\
**Post date:** [October 6, 2015, 4:25pm UTC](https://community.particle.io/t/counting-problems/16284/8 "2015-10-06T16:25:03Z")

</div>

Yes, but the dashboard scrolls (and is cute about whether it is visible in a browser window) - so if you’re not there to see it, you can be SOL (at least that’s been my experience with the dashboard and transient problems like this.) It’s a good diagnostic for reproducable events, but not for intermittent ones.

I have found using curl to dump the event stream to be more effective (although that can also reset, because the internet/cloud computing doesn’t like long lived TCP connections in general 😄) If you click on the icon circled in the following image, the system should provide a curl command line that you can enter at your command line and capture logs without the limitations of a browser window.

![](https://community.particle.io/uploads/particle/original/e/a/eaf92fbb1e50c7ec819ce88534b5c6d40185423b.png)

Note that in many cases, you will not see a device offlien event, but it must be inferred from the “device online event”.

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 6, 2015, 4:31pm UTC](https://community.particle.io/t/counting-problems/16284/9 "2015-10-06T16:31:34Z")

</div>

> [@peekay123](#):
>
> @osprey, the dashboard IS a log of events

LOL! You're so right! I never tried the dashboard before, and I am only now (now that you mentioned it) slowly getting accustomed to it's benefits.

> [@AndyW](#):
>
> If you click on the icon circled in the following image, the system should provide a curl command line that you can enter at your command line and capture logs without the limitations of a browser window.

By command line, do you mean the DOS command prompt in Windows? When I tried it it said that curl is not a recognized command. Do I need to install something or set some paths? Please bear with me since I am new to this stuff.

---

<div class="post-metadata">

**Author:** ![AndyW](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/andyw/32/686_2.png) [@AndyW](https://community.particle.io/u/AndyW)\
**Post date:** [October 6, 2015, 4:45pm UTC](https://community.particle.io/t/counting-problems/16284/10 "2015-10-06T16:45:52Z")

</div>

Yes, curl is a command that you’ll have to install.

I think you can download it from somewhere linked from [the curl downloads page](http://curl.haxx.se/download.html).

Then you would run it from the DOS command prompt. I’m not a windows user, so I do not know what your options are for ensuring that the scroll history is retained; maybe someone else can chime in here.

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 6, 2015, 5:00pm UTC](https://community.particle.io/t/counting-problems/16284/11 "2015-10-06T17:00:43Z")

</div>

OK. From looking at the dashboard, it appears that if there is a temporary loss of wifi signal, the Photon behaves as if it was reset as soon as a connection is reestablished. I know this is something obvious for you guys. Is there a way to prevent this, that is, have the Photon/Core keep looping, counting and doing its thing and then resume sending the HTTP requests as soon as the wifi signal is back?

---

<div class="post-metadata">

**Author:** ![LukeUSMC](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/lukeusmc/32/6519_2.png) [@LukeUSMC](https://community.particle.io/u/LukeUSMC)\
**Post date:** [October 6, 2015, 7:59pm UTC](https://community.particle.io/t/counting-problems/16284/12 "2015-10-06T19:59:40Z")

</div>

What about the retained var? If it is a soft reset the count should persist through that right?  
[https://docs.particle.io/reference/firmware/photon/#storing-data-in-backup-ram](https://docs.particle.io/reference/firmware/photon/#storing-data-in-backup-ram)

---

<div class="post-metadata">

**Author:** ![Hootie81](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/hootie81/32/2879_2.png) [@Hootie81](https://community.particle.io/u/Hootie81)\
**Post date:** [October 6, 2015, 11:26pm UTC](https://community.particle.io/t/counting-problems/16284/13 "2015-10-06T23:26:38Z")

</div>

Agreed retained variables are perfect for this, and even one better is to use system threading too, that way you can still count while not connected and once connected it can send the totals.

Here is some code I wrote just the other day to do exactly what your doing but reading a reed switch. And I’m actually in progress to make it report total consumption and the days consumption. I also added a function to set the current meter value to the unit, and changed the code slightly to report in litres instead of pulses

> **[Water usage monitor](http://community.particle.io/t/water-usage-monitor/16187)**
>
> Hey all, just wanted to quickly share this project i spent today working on.. This standard Aussie water meter has a little port where you can slip in a reed switch and measure water in 5L increments. So i decided I would use my Photon and a...

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2015, 7:18am UTC](https://community.particle.io/t/counting-problems/16284/14 "2015-10-07T07:18:35Z")

</div>

The first step should maybe be to employ `SYSTEM_MODE(SEMI_AUTOMATIC)` to rid yourself of the majority of problems when the cloud connection fails.  
**_After that_** all the above 😉

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 7, 2015, 11:13am UTC](https://community.particle.io/t/counting-problems/16284/15 "2015-10-07T11:13:55Z")

</div>

> [@ScruffR](#):
>
> The first step should maybe be to employ SYSTEM\_MODE(SEMI\_AUTOMATIC) to rid yourself of the majority of problems when the cloud connection fails.After that all the above

I added `SYSTEM_MODE(SEMI_AUTOMATIC)` and now my Photon is breathing white and appearing offline on dashboard. Also, I can't flash anything. Is there a way to get it out of semi automatic mode (without a factory reset), at least until I can flash a new firmware?

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [October 7, 2015, 11:20am UTC](https://community.particle.io/t/counting-problems/16284/16 "2015-10-07T11:20:39Z")

</div>

> [@osprey](#):
>
> Is there a way to get it out of semi automatic mode (without a factory reset), at least until I can flash a new firmware?

look to put your Photon into [Safe Mode](https://docs.particle.io/guide/getting-started/modes/photon/#safe-mode)

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 7, 2015, 11:23am UTC](https://community.particle.io/t/counting-problems/16284/17 "2015-10-07T11:23:10Z")

</div>

> [@BulldogLowell](#):
>
> look to put your Photon into Safe Mode

Thanks!

---

<div class="post-metadata">

**Author:** ![osprey](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/osprey/32/5600_2.png) [@osprey](https://community.particle.io/u/osprey)\
**Post date:** [October 7, 2015, 11:42am UTC](https://community.particle.io/t/counting-problems/16284/18 "2015-10-07T11:42:01Z")

</div>

After using retained variables, I am still experiencing premature resetting of counter values. Here’s my current code. How do I incorporate `SYSTEM_MODE(SEMI_AUTOMATIC)` in it without making the device go offline? I know my code is “noobish” so if you have any other suggestions about how to make it better, I am all ears. I definitely need to streamline the way I got the value of the _DeviceTime_ variable and I am not sure I am using the best method to send an HTTP request.

```cpp
    #include "HttpClient/HttpClient.h"
    #include "application.h"
    
    STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
    //SYSTEM_MODE(SEMI_AUTOMATIC);
    
    //Settings//
    String DeviceID = "XXXXXX";
    String TargetHostName = "yyy.yyy.com";
    ////////////
    
    
    int val = 0;
    int inPin = D0;
    int outPin = D7;
    int PreviousSecond = -1;
    int ThisSecond = -1;
    int PreviousMinute = -1;
    int ThisMinute = -1;
    bool SensorState = false;
    bool PreviousSensorState = false;
    retained uint32_t PulseCount = 0;
    retained uint32_t TotalPulseCount = 0;
    String PacketData = "";
    String DeviceTime = "";
    String PreviousResponseBody = "";
    int ResponseStatus = 0;
    
    
    HttpClient http;
    http_header_t headers[] = {
        { "Accept" , "*/*"},
        { NULL, NULL } // NOTE: Always terminate headers will NULL
    };
    http_request_t request;
    http_response_t response;
    
    void setup() {
        pinMode(outPin, OUTPUT);
        pinMode(inPin, INPUT);
        Time.zone(2);
        PreviousSecond = Time.second();
        Particle.variable("PulseCount", &TotalPulseCount, INT);
        
        bool success;
        success = Particle.publish("Device Started");
    }
    
    void loop() {
        val = digitalRead(inPin);
        SensorState = !val;
        digitalWrite(outPin, SensorState); //show sensor state on little blue led
        
        //Count Pulses//
        if(SensorState != PreviousSensorState) {
            //Detect Leading Edge//
            if(SensorState==true) {
                PulseCount += 1;
                TotalPulseCount += 1;
                if(TotalPulseCount==10000) {
                   TotalPulseCount=0; 
                }
            }
            ///////////////////////
            PreviousSensorState = SensorState;
        }
        ////////////////
        
        //Transmit Pulsecount at the turn of the minute//
        ThisMinute = Time.minute();
        if(ThisMinute != PreviousMinute) {
            //will optimize this once I figure out how string formatting works//
            DeviceTime = String(Time.year()).substring(2);
            if(String(Time.month()).length()==1)
                {DeviceTime += "0" + String(Time.month());}
            else
                {DeviceTime += String(Time.month());}
            if(String(Time.day()).length()==1)
                {DeviceTime += "0" + String(Time.day());}
            else
                {DeviceTime += String(Time.day());}
    
            if(String(Time.hour()).length()==1)
                {DeviceTime += "0" + String(Time.hour());}
            else
                {DeviceTime += String(Time.hour());}
                
            if(String(Time.minute()).length()==1)
                {DeviceTime += "0" + String(Time.minute());}
            else
                {DeviceTime += String(Time.minute());}
                
            if(String(Time.second()).length()==1)
                {DeviceTime += "0" + String(Time.second());}
            else
                {DeviceTime += String(Time.second());}
            ////////////////////////////////////////////////////////////////////
            
            PacketData = DeviceID + ";" + String(PulseCount) + ";" + DeviceTime + ";" + String(TotalPulseCount);
            
            // ***Using HTTP Client*** //
            request.hostname = TargetHostName;
            request.port = 80;
            request.path = "/spark_receiver.ashx?PD=" + PacketData;
            http.get(request, response, headers);
            ResponseStatus = response.status;
            ///////////////////////////
            
            bool success;
            
            if((response.body != PreviousResponseBody) && (response.body.substring(0,2) == "OK") && (ResponseStatus == 200)) {
                //only reset if connection was successful
                PulseCount = 0;
                PreviousResponseBody = response.body;
                success = Particle.publish("TotalPulseCount", String(TotalPulseCount) , 60, PRIVATE);
            }
            else {
                success = Particle.publish("Upload Failed");
            }
            
            PreviousMinute = ThisMinute;
        }
        /////////////////////////////////////////////////
    }

```

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [October 7, 2015, 12:08pm UTC](https://community.particle.io/t/counting-problems/16284/19 "2015-10-07T12:08:25Z")

</div>

> [@osprey](#):
>
> String DeviceTime = "";

a few comments for you to consider while you hunt down your issues...

You may wish to consider (to get the program performing better) eliminating the use of String class which can really wreak havoc on memory... consider using C strings (char arrays).

you create `bool success` in your `setup()` function it gets destroyed as soon as `setup()` ends, then you recreate it again in `loop()` where it will again get destroyed on exit of `loop()`. Do you want a persistent variable there?. Read up on "[scope](http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm)."

Have you tried [threading](https://docs.particle.io/reference/firmware/photon/#system-thread)?

as a side note, look to make your code more readable by eliminating all those forward-leaning hashmarks (you know that the compiler interprets those... so use them in pairs or with the asterisk, e.g. /_my comment_/) and all that white space... and pick a method to use curly braces that is consistent .

```cpp
#include "HttpClient/HttpClient.h"
#include "application.h"

STARTUP(System.enableFeature(FEATURE_RETAINED_MEMORY));
//SYSTEM_MODE(SEMI_AUTOMATIC);

String DeviceID = "XXXXXX";
String TargetHostName = "yyy.yyy.com";

int val = 0;
int inPin = D0;
int outPin = D7;
int PreviousSecond = -1;
int ThisSecond = -1;
int PreviousMinute = -1;
int ThisMinute = -1;
bool SensorState = false;
bool PreviousSensorState = false;
retained uint32_t PulseCount = 0;
retained uint32_t TotalPulseCount = 0;
String PacketData = "";
String DeviceTime = "";
String PreviousResponseBody = "";
int ResponseStatus = 0;

HttpClient http;
http_header_t headers[] = {
  { "Accept" , "*/*"},
  { NULL, NULL } // NOTE: Always terminate headers will NULL
};
http_request_t request;
http_response_t response;

void setup() 
{
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
  Time.zone(2);
  PreviousSecond = Time.second();
  Particle.variable("PulseCount", &TotalPulseCount, INT);
  bool success;
  success = Particle.publish("Device Started");
}

void loop() 
{
  val = digitalRead(inPin);
  SensorState = !val;
  digitalWrite(outPin, SensorState);
  if (SensorState != PreviousSensorState) 
  {
    //Detect Leading Edge//
    if (SensorState == true) 
    {
      PulseCount += 1;
      TotalPulseCount += 1;
      if (TotalPulseCount == 10000) 
      {
        TotalPulseCount = 0;
      }
    }
    PreviousSensorState = SensorState;
  }
  ThisMinute = Time.minute();
  if (ThisMinute != PreviousMinute) 
  {
    DeviceTime = String(Time.year()).substring(2);
    if (String(Time.month()).length() == 1)
    {
      DeviceTime += "0" + String(Time.month());
    }
    else
    {
      DeviceTime += String(Time.month());
    }
    if (String(Time.day()).length() == 1)
    {
      DeviceTime += "0" + String(Time.day());
    }
    else
    {
      DeviceTime += String(Time.day());
    }
    if (String(Time.hour()).length() == 1)
    {
      DeviceTime += "0" + String(Time.hour());
    }
    else
    {
      DeviceTime += String(Time.hour());
    }

    if (String(Time.minute()).length() == 1)
    {
      DeviceTime += "0" + String(Time.minute());
    }
    else
    {
      DeviceTime += String(Time.minute());
    }

    if (String(Time.second()).length() == 1)
    {
      DeviceTime += "0" + String(Time.second());
    }
    else
    {
      DeviceTime += String(Time.second());
    }
    PacketData = DeviceID + ";" + String(PulseCount) + ";" + DeviceTime + ";" + String(TotalPulseCount);
    request.hostname = TargetHostName;
    request.port = 80;
    request.path = "/spark_receiver.ashx?PD=" + PacketData;
    http.get(request, response, headers);
    ResponseStatus = response.status;
    bool success;
    if ((response.body != PreviousResponseBody) && (response.body.substring(0, 2) == "OK") && (ResponseStatus == 200)) 
    {
      PulseCount = 0;
      PreviousResponseBody = response.body;
      success = Particle.publish("TotalPulseCount", String(TotalPulseCount) , 60, PRIVATE);
    }
    else {
      success = Particle.publish("Upload Failed");
    }
    PreviousMinute = ThisMinute;
  }
}

```

Once you do that, forum members can 'see' a lot more 😉

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [October 7, 2015, 12:25pm UTC](https://community.particle.io/t/counting-problems/16284/20 "2015-10-07T12:25:16Z")

</div>

The docs have a rather helpful search feature.  
When using it with `SYSTEM_MODE` or `SEMI_AUTOMATIC` you’ll find a description of the _System Modes_  
[https://docs.particle.io/reference/firmware/photon/#system-modes](https://docs.particle.io/reference/firmware/photon/#system-modes)

And there you’ll find a reference to `Particle.connect()` which you need to call in order to connect to the cloud.  
You can do this on demand (e.g. via a button, TCP/HTTP packet, …) or in `setup()`.  
If you only want WiFi connection without the cloud you could use `WiFi.connect()` instead.

[Next page](https://community.particle.io/t/counting-problems/16284.md?page=2)
