66 #define MY_SPLASH_SCREEN_DISABLED
71 #define DIGITAL_INPUT_SENSOR 3
75 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
76 #define DIGITAL_INPUT_SENSOR_INTF INTF1
80 #define PULSE_FACTOR 1000.0d
83 #define SLEEP_MODE false
90 #define FLOW_RESET_TO_ZERO_TIMEOUT 120000
96 #define CHILD_ID_VAR1 2
99 #define SEND_FREQUENCY 30000
103 #define FIRST_VALUE_SENT_FLAG_POSITION 0
104 #define FIRST_VALUE_SENT_FLAG_YES 1
105 #define FIRST_VALUE_SENT_FLAG_NO 255
109 MyMessage lastCounterMsg(CHILD_ID,V_VAR1);
110 MyMessage volumeAdd(CHILD_ID_VAR1,V_TEXT);
113 double ppl = ((double)PULSE_FACTOR)/1000;
115 volatile uint32_t pulseCount = 0;
116 volatile uint32_t lastBlink = 0;
117 volatile double flow = 0;
118 bool pcReceived =
false;
119 uint32_t oldPulseCount = 0;
122 uint32_t lastSend =0;
123 uint32_t lastPulse =0;
124 bool firstValuesMessageSent =
false;
129 uint32_t newBlink = micros();
130 uint32_t interval = newBlink-lastBlink;
133 lastPulse = millis();
134 if (interval<500000L) {
139 flow = (3600000.0 /interval) / ppl;
141 lastBlink = newBlink;
148 #ifdef FORCE_FACTORY_RESET
151 hwWriteConfig(i,0xFF);
155 Serial.println(
"Factory reset complete");
161 pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
163 pulseCount = oldPulseCount = 0;
166 checkAndFirstTimeInitValuesOnHomeAssistant();
171 lastSend = lastPulse = millis();
175 #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
176 EIFR |= (1 << DIGITAL_INPUT_SENSOR_INTF);
179 attachInterrupt(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), onPulse, FALLING);
192 present(CHILD_ID_VAR1, S_INFO);
197 uint32_t currentTime = millis();
200 if (SLEEP_MODE || (currentTime - lastSend > SEND_FREQUENCY)) {
201 lastSend=currentTime;
210 if (!SLEEP_MODE && flow != oldflow) {
213 Serial.print(
"m3/h:");
214 Serial.println(flow);
218 if (flow<((uint32_t)MAX_FLOW)) {
225 if(currentTime - lastPulse > FLOW_RESET_TO_ZERO_TIMEOUT) {
230 if ((pulseCount != oldPulseCount)||(!SLEEP_MODE)) {
231 oldPulseCount = pulseCount;
233 Serial.print(
"pulsecount:");
234 Serial.println(pulseCount);
237 send(lastCounterMsg.
set(pulseCount));
239 double volume = pulseCount / PULSE_FACTOR;
240 if ((volume != oldvolume)||(!SLEEP_MODE)) {
244 Serial.print(
"volume:");
245 Serial.println(volume, 3);
248 send(volumeMsg.
set(volume, 3));
253 sleep(SEND_FREQUENCY,
false);
260 void forceFactoryReset() {
261 saveState(FIRST_VALUE_SENT_FLAG_POSITION, FIRST_VALUE_SENT_FLAG_NO);
264 void checkAndFirstTimeInitValuesOnHomeAssistant() {
267 uint8_t state =
loadState(FIRST_VALUE_SENT_FLAG_POSITION);
268 if (state==FIRST_VALUE_SENT_FLAG_NO) {
271 Serial.println(
"First time init");
273 firstValuesMessageSent =
true;
279 send(lastCounterMsg.
set((uint32_t)0));
287 if (message.
getType()==V_VAR1) {
288 if (firstValuesMessageSent) {
291 saveState(FIRST_VALUE_SENT_FLAG_POSITION,
292 FIRST_VALUE_SENT_FLAG_YES);
295 uint32_t gwPulseCount=message.
getULong();
296 pulseCount += gwPulseCount;
300 Serial.print(
"Received last pulse count from gw:");
301 Serial.println(pulseCount);
309 if (message.
getType()==V_TEXT) {
311 if ((val+(int32_t)pulseCount)<0) {
314 Serial.println(
"out of range. Won't add");
322 Serial.print(
"New pulseCount: ");
323 Serial.println(pulseCount);