Use same logic for counter on gateway and Arduino.