diff --git a/README.md b/README.md index ddf331b..f5a68ee 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,3 @@ -# raspberrypi +# Raspberrypi GPIO -raspberrypi zero w GPIO 实现的智能家居 -- 参考:http://shumeipai.nxez.com/2016/06/26/raspberry-pi-diy-with-a-smart-home-server.html - -- 我发现传感器的数据是实时的,传感器的工作必须是实时的 -- 有时做不到数据的准确 -- 就做了多进程的shell(不同pid)来满足传感器数据现状,一个进程收集传感器数据,另一个进程处理数据. +raspberrypi GPIO C 程序 diff --git a/app/Screenshot_20200910-131104_LightControl.png b/app/Screenshot_20200910-131104_LightControl.png deleted file mode 100644 index ea6c4fe..0000000 Binary files a/app/Screenshot_20200910-131104_LightControl.png and /dev/null differ diff --git a/app/Screenshot_20200910-131834_LightControl.png b/app/Screenshot_20200910-131834_LightControl.png deleted file mode 100644 index 116f877..0000000 Binary files a/app/Screenshot_20200910-131834_LightControl.png and /dev/null differ diff --git a/app/lightcontrol.apk b/app/lightcontrol.apk deleted file mode 100644 index 970ae63..0000000 Binary files a/app/lightcontrol.apk and /dev/null differ diff --git a/circuits/Smart home.fzz b/circuits/Smart home.fzz deleted file mode 100644 index bd05998..0000000 Binary files a/circuits/Smart home.fzz and /dev/null differ diff --git a/circuits/p20856.db b/circuits/p20856.db deleted file mode 100644 index 30148c1..0000000 Binary files a/circuits/p20856.db and /dev/null differ diff --git a/gpio/Makefile b/gpio/Makefile index 41c1cfe..0ee150c 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -3,7 +3,7 @@ CC := $(CROSS_COMPILE)gcc STRIP := $(CROSS_COMPILE)strip CFLAGS += -g -O2 -Wall LIBS = -lwiringPi -BIN = infrared light pin sound ultrasound wind dht11 dht12 helement key_light +BIN = infrared light pin sound ultrasound wind dht11 dht12 helement key_light ch4 pasco2v01 all: $(BIN) @@ -28,6 +28,11 @@ helement: helement.o key_light: key_light.o $(CC) $(CFLAGS) -o key_light $^ $(LIBS) +ch4: ch4.o + $(CC) $(CFLAGS) -o ch4 $^ $(LIBS) +pasco2v01: pasco2v01.o + $(CC) $(CFLAGS) -o pasco2v01 $^ $(LIBS) + .c.o: $(CC) $(CFLAGS) -c $< $(LIBS) diff --git a/gpio/ch4.c b/gpio/ch4.c new file mode 100644 index 0000000..e5fd545 --- /dev/null +++ b/gpio/ch4.c @@ -0,0 +1,47 @@ +/* + * + * N55A甲烷气体传感器 + * + */ + +#include +#include +#include + +void setup(int pin) { + wiringPiSetup(); + pinMode(pin, INPUT); +} + +int readMethaneLevel(int pin) { + int methaneDetected = digitalRead(pin); + return methaneDetected; +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + printf("Error: Invalid number of arguments.\n"); + printf("Usage: %s PIN\n", argv[0]); + exit(1); + } + + int pin = atoi(argv[1]); + if (pin < 0) { + printf("Error: Invalid PIN value.\n"); + exit(1); + } + + setup(pin); + + while(1) { + int result = readMethaneLevel(pin); + if (result == HIGH) { + printf("CH4\n"); + } + + delay(1000); // 每隔1秒读取一次传感器 + } + + return 0; +} + diff --git a/gpio/light.c b/gpio/light.c index f8833f2..0bf2f97 100644 --- a/gpio/light.c +++ b/gpio/light.c @@ -7,49 +7,46 @@ #include #include #include -//#include #include -static int is_num(char *str) -{ - int i, len; - for (i = 0, len = strlen(str); i < len; i++) { - if (isdigit(str[i]) == 0) { - printf("不是数字\n"); - return 1; +int is_num(const char *str) { + while (*str != '\0') { + if (!isdigit(*str)) { + return 0; } + str++; } - - return 0; + return 1; } -int main(int argc, char *argv[]) -{ + +int main(int argc, char *argv[]) { if (argc != 3) { - printf("Parameter error.\n"); - exit(1); - } - - if (is_num(argv[1]) == 1) { - printf("Parameter error, parameter 1 is PIN pin value\n"); - exit(1); - } - - if (is_num(argv[2]) == 1) { - printf("Parameter error, parameter 2 is true or false\n"); + printf("Error: Invalid number of arguments.\n"); + printf("Usage: %s PIN VALUE\n", argv[0]); + exit(1); + } + + int pin = atoi(argv[1]); + if (!is_num(argv[1]) || (pin < 0)) { + printf("Error: Invalid PIN value.\n"); + exit(1); + } + + int value = atoi(argv[2]); + if (!is_num(argv[2]) || (value != 0 && value != 1)) { + printf("Error: Invalid value. Please enter 0 or 1.\n"); exit(1); } - int pin = atol(argv[1]); wiringPiSetup(); pinMode(pin, OUTPUT); - if (atol(argv[2]) == 1) { - digitalWrite(pin, HIGH); + + if (digitalRead(pin) != value) { + digitalWrite(pin, value); } - if (atol(argv[2]) == 0) { - digitalWrite(pin, LOW); - } - return atol(argv[2]); -} + + return value; +} \ No newline at end of file diff --git a/gpio/mh-z14b.c b/gpio/mh-z14b.c new file mode 100644 index 0000000..323cf06 --- /dev/null +++ b/gpio/mh-z14b.c @@ -0,0 +1,71 @@ +/* + * + * MH-Z14B CO2 Sensor + * Based on Raspberry Pi wiringPi library + * Time: 20240109 + * + */ + +#include +#include +#include +#include + +// 校准 +static void calibration(int fd) +{ + // 校准传感器零点(ZERO) + char zero[9] = { 0XFF, 0X01, 0X87, 0X00, 0X00, 0X00, 0X00, 0X00, 0X78 }; + + write(fd, zero, 9); + delay(200); + + return; +} + +int main() +{ + int fd = 0; + char checksum = 0; + int High = 0; + int Low = 0; + int ppm = 0; + char read_command[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; + char data[9] = { 0 }; + + if ((fd = serialOpen("/dev/serial0", 9600)) < 0) { + printf("Unable to open device\n"); + return 1; + } else { + printf("ttyAMA0 initialised ok\n"); + } + + while (1) { + // 写入读取命令 + write(fd, read_command, 9); + delay(100); + + // 读取 + read(fd, data, 9); + + checksum = (0xFF - (data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7])) + 1; + High = (int)data[2]; + Low = (int)data[3]; + ppm = (256 * High) + Low; + + if (data[8] == checksum) { + for (int loop = 0; loop <= 8; loop++) { + printf("data%d: %X\n", loop, data[loop]); + } + printf("checksum: %X = %X\n", data[8], checksum); + printf("CO2 Concentration: %d ppm\n\r\n", ppm); + } else { + calibration(fd); + continue; + } + + delay(3000); + } + + return 0; +} diff --git a/gpio/pasco2v01.c b/gpio/pasco2v01.c new file mode 100644 index 0000000..490c78b --- /dev/null +++ b/gpio/pasco2v01.c @@ -0,0 +1,65 @@ +/* + * + * CO2 PASCO2V01 传感器 + * + */ + +#include +#include + +// Pasco2V01 设备地址 +#define DEVICE_ADDRESS 0x76 + +// 最大尝试次数 +#define MAX_RETRY 100 + +int main() { + // 初始化 I2C 总线 + int fd = wiringPiI2CSetup(DEVICE_ADDRESS); + if (fd < 0) { + printf("Failed to initialize I2C\n"); + return -1; + } + + // 发送命令开始测量 + int result = wiringPiI2CWrite(fd, 0x00); + if (result < 0) { + printf("Failed to write command\n"); + return -1; + } + + // 等待传感器完成测量 + int retry = 0; + while (retry < MAX_RETRY) { + int status = wiringPiI2CReadReg8(fd, 0x00); + if (status < 0) { + printf("Failed to read status\n"); + return -1; + } + + if (status & 0x08) { + break; + } + + retry++; + } + + if (retry >= MAX_RETRY) { + printf("Measurement timeout\n"); + return -1; + } + + // 读取测量结果 + unsigned char data[3]; + data[0] = wiringPiI2CReadReg8(fd, 0x02); + data[1] = wiringPiI2CReadReg8(fd, 0x03); + data[2] = wiringPiI2CReadReg8(fd, 0x04); + + // 转换为浓度值 + unsigned int concentration = (data[0] << 16) | (data[1] << 8) | data[2]; + + // 打印浓度值 + printf("Gas Concentration: %u ppm\n", concentration); + + return 0; +}