我正在尝试使用 70 元人民币和 Raspberry PI B+ 的 gpio 上的其他一些组件(包括比较器)读取我的水表。我使用接线 pi 中断来执行此操作,并在上升和下降之间切换以处理错误中断。我的程序包含一个奇怪的解决方法,但似乎有效。唯一的问题是它需要更多的 vmem 并且在大约 10 小时后停止工作。这很奇怪,因为我不在任何地方进行直接的内存管理,整个事情包含 69 行代码。这是接线 pi 错误还是发生了其他事情?
这是整个程序:
#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;
}
.watermeter.sh 是一个处理调用 Domoticz API 的 bash 脚本。整个 printtime 函数最初是为了调查双重和错误中断的问题,但这是一个已知的接线 pi 问题。