完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我在编程方面相当陌生,想知道如何压缩我为使用连接到我的 NodeMCU 的灵活数量的 CJMCU-8110 编写的代码。在下面附带的测试代码中,我连接了 2 个,但在不久的将来我想扩展到 3 或 4,并希望通过变量 (fi int SensorNumber = 2) 驱动它,而不是像我一样为每个传感器复制代码在示例中做了。
谁能指导我解决方案? 代码:全选#include #include // I2C library #include "ccs811.h" // CCS811 library #include "ClosedCube_HDC1080.h" CCS811 ccs811_1; CCS811 ccs811_2; ClosedCube_HDC1080 HDC1080_1; ClosedCube_HDC1080 HDC1080_2; int SDApin; int SCLpin = D1; HDC1080_SerialNumber sernum; char format[12]; bool vBootphase = true; uint16_t eco2, etvoc, errstat, raw; void setup() {Serial.begin(115200); while(!Serial){;}} void loop() { if (vBootphase == true) { // Channel 1: Setup SDApin = D2; Wire.begin(SDApin, SCLpin); delay(500); // ** CCS811 ** Serial.println(""); Serial.println("*** CCS1 ***"); Serial.print("CCS :"); ccs811_1.set_i2cdelay(50); ccs811_1.begin(); delay(500); ccs811_1.start(CCS811_MODE_1SEC); delay(1000); Serial.print("setup: hardware version: "); Serial.println(ccs811_1.hardware_version(),HEX); Serial.print("setup: bootloader version: "); Serial.println(ccs811_1.bootloader_version(),HEX); Serial.print("setup: application version: "); Serial.println(ccs811_1.application_version(),HEX); // HDC1080 HDC1080_1.begin(0x40); Serial.print("Manufacturer ID=0x"); Serial.println(HDC1080_1.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments Serial.print("Device ID=0x"); Serial.println(HDC1080_1.readDeviceId(), HEX); // 0x1050 ID of the device Serial.print("Device Serial Number="); sernum = HDC1080_1.readSerialNumber(); sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast); Serial.println(format); // Channel 2: Setup SDApin = D6; Wire.begin(SDApin, SCLpin); delay(500); // ** CCS811 ** Serial.println("*** CCS2 ***"); // Print CCS811 versions Serial.print("CCS2 :"); ccs811_2.set_i2cdelay(50); ccs811_2.begin(); delay(500); ccs811_2.start(CCS811_MODE_1SEC); delay(1000); Serial.print("setup: hardware version: "); Serial.println(ccs811_2.hardware_version(),HEX); Serial.print("setup: bootloader version: "); Serial.println(ccs811_2.bootloader_version(),HEX); Serial.print("setup: application version: "); Serial.println(ccs811_2.application_version(),HEX); // HDC1080 HDC1080_2.begin(0x40); Serial.print("Manufacturer ID=0x"); Serial.println(HDC1080_2.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments Serial.print("Device ID=0x"); Serial.println(HDC1080_2.readDeviceId(), HEX); // 0x1050 ID of the device Serial.print("Device Serial Number="); sernum = HDC1080_2.readSerialNumber(); sprintf(format, "%02X-%04X-%04X", sernum.serialFirst, sernum.serialMid, sernum.serialLast); Serial.println(format); vBootphase = false; } else { // Channel 1 SDApin = D2; Wire.begin(SDApin, SCLpin); delay(500); // ** CCS811 ** Serial.print("\nSensor connected on: "); Serial.print("SDA - ");Serial.print(SDApin); Serial.print(" | SCL - ");Serial.print(SCLpin); Serial.print("\n"); ccs811_1.read(&eco2,&etvoc,&errstat,&raw); delay(500); Serial.print("ErrStat: ");Serial.print(errstat);Serial.print(" | ");Serial.println(ccs811_1.errstat_str(errstat)); if(!errstat==CCS811_ERRSTAT_OK) { Serial.print("CCS811-1: "); Serial.println("Failed to read from sensor"); ccs811_1.begin(); delay(500); ccs811_1.start(CCS811_MODE_1SEC); } else { Serial.print("CCS811-1: "); Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm | "); Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb"); Serial.print("\n"); } // ** HDC1080 ** Serial.print("T="); Serial.print(HDC1080_1.readTemperature()); Serial.print("C"); Serial.print(", RH="); Serial.print(HDC1080_1.readHumidity()); Serial.println("%"); // Channel 2 SDApin = D6; Wire.begin(SDApin, SCLpin); delay(500); // ** CCS811 ** Serial.print("Sensor connected on: "); Serial.print("SDA - ");Serial.print(SDApin); Serial.print(" | SCL - ");Serial.print(SCLpin); Serial.print("\n"); ccs811_2.read(&eco2,&etvoc,&errstat,&raw); delay(500); Serial.print("ErrStat: ");Serial.print(errstat);Serial.print(" | ");Serial.println(ccs811_2.errstat_str(errstat)); if(errstat!=CCS811_ERRSTAT_OK) { Serial.print("CCS811-2: "); Serial.println("Failed to read from sensor"); ccs811_1.begin(); delay(500); ccs811_2.start(CCS811_MODE_1SEC); } else { Serial.print("CCS811-2: "); Serial.print("eco2="); Serial.print(eco2); Serial.print(" ppm | "); Serial.print("etvoc="); Serial.print(etvoc); Serial.print(" ppb"); Serial.print("\n"); } // ** HDC1080 ** Serial.print("T="); Serial.print(HDC1080_1.readTemperature()); Serial.print("C"); Serial.print(", RH="); Serial.print(HDC1080_1.readHumidity()); Serial.println("%"); delay(10000); } } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
406浏览 6评论
338浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
321浏览 5评论
322浏览 4评论
309浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-8-16 03:24 , Processed in 0.934985 second(s), Total 42, Slave 36 queries .
Powered by 电子发烧友网
© 2015 www.ws-dc.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号