Code: Select all
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <limits.h>
#include <sys/time.h>
void myInterrupt1(void);
void printtime(){
char buffer[30];
struct timeval tv;
time_t curtime;
gettimeofday(&tv, NULL);
curtime=tv.tv_sec;
strftime(buffer,30,"%m-%d-%Y %T.",localtime(&curtime));
printf("%s%ld\n",buffer,tv.tv_usec);
fflush(stdout);
}
void callJson(void){
struct timeval tv;
gettimeofday(&tv, NULL);
int time = tv.tv_sec;
static int timeprev = 0;
if (time - timeprev > 1){
timeprev = time;
system("./watermeter.sh");
printf("SEND!!!!\n");
fflush(stdout);
}
}
void myInterrupt0(void){
if (digitalRead(1)==1){
wiringPiISR(1, INT_EDGE_FALLING, &myInterrupt1);
printf("rising\n");
printtime();
callJson();
}
}
void myInterrupt1(void){
if (digitalRead(1)==0){
wiringPiISR(1, INT_EDGE_RISING, &myInterrupt0);
printf("falling\n");
printtime();
}
}
int main (void){
wiringPiSetup();
if (digitalRead(1)==0){
wiringPiISR(1, INT_EDGE_RISING, &myInterrupt0);
}
else{
wiringPiISR(1, INT_EDGE_FALLING, &myInterrupt1);
}
// printtime();
for(;;){
sleep(UINT_MAX);
}
return 0;
This approach uses a lot of workarounds but it does seem to work reliability, there is only one problem. After about 24 hours (depending on the water usage), the program has a vmem size of more than 3GB and stops working, and I really have no idea why. I don't do any memory management myself, so I don't see how there could be a leak. One hypothesis I had is that WiringPi didn't like initializing the pin again all the time, so I am now running a version that uses this construction:
Code: Select all
system("gpio edge 1 rising");
wiringPiISR(1, INT_EDGE_SETUP, &myInterrupt0);