0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

如何使用ArduinoIDE设置ESP32

454398 来源:工程师吴畏 2019-07-30 11:28 次阅读

材料清单

ESP32

AmbiMate MS4多传感器模块

面包板和跳线

USB连接器

Arduino IDE(软件)

什么是AmbiMate MS4?

AmbiMate传感器模块MS4系列集成了一套传感器,用于楼宇自动化和连接家庭应用到PCB组件上。它在即用型PCB组件上提供了一组专用传感器,可轻松集成到主机产品中。

传感器包括运动,光线,温度,湿度,VSO(挥发性有机化合物),CO2和声音检测。通过捕获VOC浓度来增加监测空气质量的能力。有许多MS4系列传感器可用 - 我建议您选择带有嵌入式麦克风的传感器来增强运动检测或者能够收听声音事件。

所有MS4系列传感器模块都可以灵活地共享一个通用的七位连接。这使得设计人员可以布置单个PCB封装,以适应生产中所有可用的传感器配置。

MS4应用

室内照明

能源管理

楼宇自动化

工作空间舒适度

区域环境控制

自动化

空气质量测量系统

MS4规范

在MS4版本1中-2314291-2有九个传感器用于:

运动(PIR)

温度

湿度

运动(PIR)

温度

声音(麦克风)

VOC

CO2

MS4 Pinout

将ESP32连接到MS4

按如下方式将MS4连接到ESP32:

使用Arduino IDE设置ESP32

按照下面的屏幕截图所示的步骤使用Arduino IDE对ESP32进行编程

单击文件》首选项

添加URL链接。您可以通过逗号分隔其他链接。

https://dl.espressif.com/dl/package_esp32_index.json

单击 确定 。

单击工具》板》板管理器

搜索 ESP32 并安装包。

再次转到 工具 并选择 ESP32板 和 COM端口

上传源代码

复制下面的源代码,将其粘贴到Arduino IDE中,然后上传。您可能需要在上传过程中长按ESP32板上的启动按钮。

#include

unsigned char buf[20];

unsigned char opt_sensors;

int incomingByte = 0;

int loopCount = 0;

char showTemp = 0, showHum = 0, showLight = 0, showSound = 0, degCorf = 0, showCO2 = 0, showVOC = 0, showPwr = 0, showEvents = 0;

String sampPeriodTxt;

float sampPeriod = 1;

void setup() {

// put your setup code here, to run once:

Wire.begin(); // join i2c bus (address optional for master)

Serial.begin(9600); // start serial for output monitor

}

void restart_info() {

// Data and basic information are acquired from the module

Wire.beginTransmission(0x2A); // transmit to device

Wire.write(byte(0x80)); // sends instruction to read firmware version

Wire.endTransmission(); // stop transmitting

Wire.requestFrom(0x2A, 1); // request byte from slave device

unsigned char fw_ver = Wire.read(); // receive a byte

Wire.beginTransmission(0x2A); // transmit to device

Wire.write(byte(0x81)); // sends instruction to read firmware subversion

Wire.endTransmission(); // stop transmitting

Wire.requestFrom(0x2A, 1); // request byte from slave device

unsigned char fw_sub_ver = Wire.read(); // receive a byte

Wire.beginTransmission(0x2A); // transmit to device

Wire.write(byte(0x82)); // sends instruction to read optional sensors byte

Wire.endTransmission(); // stop transmitting

Wire.requestFrom(0x2A, 1); // request byte from slave device

opt_sensors = Wire.read(); // receive a byte

delay(1000);

//If device contains additional CO2 and audio sensor, it is indicated here

Serial.print(“AmbiMate sensors: 4 core”);

if (opt_sensors & 0x01)

Serial.print(“ + CO2”);

if (opt_sensors & 0x04)

Serial.print(“ + Audio”);

Serial.println(“ ”);

Serial.print(“AmbiMate Firmware version ”);

Serial.print(fw_ver);

Serial.print(“。”);

Serial.println(fw_sub_ver);

Serial.print(“Select which sensors to receive data from: ”);

Serial.print(“Temperature [Y/N]: ”);

while (Serial.available() == 0);

showTemp = Serial.read();

Serial.println(showTemp);

Serial.print(“deg C or F [C/F]: ”);

while (Serial.available() == 0);

degCorf = Serial.read();

Serial.println(degCorf);

Serial.print(“Humidity [Y/N]: ”);

while (Serial.available() == 0);

showHum = Serial.read();

Serial.println(showHum);

Serial.print(“Ambient Light [Y/N]: ”);

while (Serial.available() == 0);

showLight = Serial.read();

Serial.println(showLight);

if (opt_sensors & 0x04)

{

Serial.print(“Audio [Y/N]: ”);

while (Serial.available() == 0);

showSound = Serial.read();

Serial.println(showSound);

}

if (opt_sensors & 0x01)

{

Serial.print(“eCO2 [Y/N]: ”);

while (Serial.available() == 0);

showCO2 = Serial.read();

Serial.println(showCO2);

Serial.print(“VOC [Y/N]: ”);

while (Serial.available() == 0);

showVOC = Serial.read();

Serial.println(showVOC);

}

Serial.print(“Power [Y/N]: ”);

while (Serial.available() == 0);

showPwr = Serial.read();

Serial.println(showPwr);

Serial.print(“PIR and Motion Events [Y/N]: ”);

while (Serial.available() == 0);

showEvents = Serial.read();

Serial.println(showEvents);

Serial.print(“ ”);

Serial.print(“Sample Interval[Secs]: ”);

while (Serial.available() == 0);

sampPeriodTxt = “”;

while (Serial.available() 》 0)

{

int inChar = Serial.read();

sampPeriodTxt += (char)inChar;

}

Serial.println(sampPeriodTxt);

sampPeriod = sampPeriodTxt.toFloat();

if (sampPeriod 《 0.5)

{

sampPeriod = 0.5;

Serial.print(“Input exceeds limits, Sample period set to minimum, 0.5 seconds ”);

}

sampPeriod = sampPeriod * 1000; // convert to mSecs

}

//Top line of headings are printed using the following

void printLabels(void) {

Serial.println(“ ”);

Serial.println(“ ”);

// Construct the first line of the headings

if ((showTemp == ‘Y’) || (showTemp == ‘y’))

Serial.print(“Temperature ”);

if ((showHum == ‘Y’) || (showHum == ‘y’))

Serial.print(“Humidity ”);

if ((showLight == ‘Y’) || (showLight == ‘y’))

Serial.print(“Light ”);

if (opt_sensors & 0x04)

{

if ((showSound == ‘Y’) || (showSound == ‘y’))

Serial.print(“Audio ”);

}

if (opt_sensors & 0x01)

{

if ((showCO2 == ‘Y’) || (showCO2 == ‘y’))

Serial.print(“eCO2 ”);

if ((showVOC == ‘Y’) || (showVOC == ‘y’))

Serial.print(“VOC ”);

}

if ((showPwr == ‘Y’) || (showPwr == ‘y’))

Serial.print(“Power ”);

if ((showEvents == ‘Y’) || (showEvents == ‘y’))

Serial.print(“Event ”);

else

Serial.print(“ ”);

// Construct the second line of the headings

if ((showTemp == ‘Y’) || (showTemp == ‘y’))

{

if ((degCorf == ‘F’) || (degCorf == ‘f’))

Serial.print(“deg F ”);

else

Serial.print(“deg C ”);

}

if ((showHum == ‘Y’) || (showHum == ‘y’))

Serial.print(“% ”);

if ((showLight == ‘Y’) || (showLight == ‘y’))

Serial.print(“Lux ”);

if (opt_sensors & 0x04)

{

if ((showSound == ‘Y’) || (showSound == ‘y’))

Serial.print(“dB ”);

}

if (opt_sensors & 0x01)

{

if ((showCO2 == ‘Y’) || (showCO2 == ‘y’))

Serial.print(“PPM ”);

if ((showVOC == ‘Y’) || (showVOC == ‘y’))

Serial.print(“PPB ”);

}

if ((showPwr == ‘Y’) || (showPwr == ‘y’))

Serial.print(“volts”);

Serial.print(“ ”);

}

//Loop starts here

void loop() {

if (loopCount == 0)

{

restart_info();

loopCount = 1;

}

if (loopCount == 1)

{

printLabels();

loopCount = 2;

}

// All sensors except the CO2 sensor are scanned in response to this command

Wire.beginTransmission(0x2A); // transmit to device

// Device address is specified in datasheet

Wire.write(byte(0xC0)); // sends instruction to read sensors in next byte

Wire.write(0xFF); // 0xFF indicates to read all connected sensors

Wire.endTransmission(); // stop transmitting

// Delay to make sure all sensors are scanned by the AmbiMate

delay(100);

Wire.beginTransmission(0x2A); // transmit to device

Wire.write(byte(0x00)); // sends instruction to read sensors in next byte

Wire.endTransmission(); // stop transmitting

Wire.requestFrom(0x2A, 15); // request 6 bytes from slave device

// Acquire the Raw Data

unsigned int i = 0;

while (Wire.available()) { // slave may send less than requested

buf[i] = Wire.read(); // receive a byte as character and store in buffer

i++;

}

// convert the raw data to engineering units, see application spec for more information

unsigned int status = buf[0];

float temperatureC = (buf[1] * 256.0 + buf[2]) / 10.0;

float temperatureF = ((temperatureC * 9.0) / 5.0) + 32.0;

float Humidity = (buf[3] * 256.0 + buf[4]) / 10.0;

unsigned int light = (buf[5] * 256.0 + buf[6]);

unsigned int audio = (buf[7] * 256.0 + buf[8]);

float batVolts = ((buf[9] * 256.0 + buf[10]) / 1024.0) * (3.3 / 0.330);

unsigned int co2_ppm = (buf[11] * 256.0 + buf[12]);

unsigned int voc_ppm = (buf[13] * 256.0 + buf[14]);

if ((showTemp == ‘Y’) || (showTemp == ‘y’))

{

if ((degCorf == ‘F’) || (degCorf == ‘f’))

Serial.print(temperatureF, 1);

else

Serial.print(temperatureC, 1);

Serial.print(“ ”);

}

if ((showHum == ‘Y’) || (showHum == ‘y’))

{

Serial.print(Humidity, 1);

Serial.print(“ ”);

}

if ((showLight == ‘Y’) || (showLight == ‘y’))

{

Serial.print(light);

Serial.print(“ ”);

}

if (opt_sensors & 0x04)

{

if ((showSound == ‘Y’) || (showSound == ‘y’))

{

Serial.print(audio);

Serial.print(“ ”);

}

}

if (opt_sensors & 0x01)

{

if ((showCO2 == ‘Y’) || (showCO2 == ‘y’))

{

Serial.print(co2_ppm);

Serial.print(“ ”);

}

if ((showVOC == ‘Y’) || (showVOC == ‘y’))

{

Serial.print(voc_ppm);

Serial.print(“ ”);

}

}

if ((showPwr == ‘Y’) || (showPwr == ‘y’))

{

Serial.print(batVolts);

Serial.print(“ ”);

}

if ((showEvents == ‘Y’) || (showEvents == ‘y’))

{

if (status & 0x80)

Serial.print(“ PIR_EVENT”);

if (status & 0x02)

Serial.print(“ AUDIO_EVENT”);

if (status & 0x01)

Serial.print(“ MOTION_EVENT”);

}

Serial.print(“ ”);

// all sensors except the CO2VOC sensor are scanned at this rate.

// CO2/VOC sensor is only updated in AmbiMate every 60 seconds

delay(sampPeriod - 100);

incomingByte = Serial.read();

if ((incomingByte == ‘R’) || (incomingByte == ‘r’))

{

//Serial.print(“Got R ”);

loopCount = 0;

}

}

上传代码后,点击串行监视器。

选择你想要的传感器喜欢通过输入‘ Y ’来选择它。

您现在可以看到传感器数据进来。

我希望这个超级传感器模块能够为您未来的项目提供便利!

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Arduino
    +关注

    关注

    184

    文章

    6427

    浏览量

    184828
  • ESP32
    +关注

    关注

    13

    文章

    896

    浏览量

    15806
收藏 人收藏

    评论

    相关推荐

    ESP32-C2在小魔方遥控器的应用

    我们经常提起的ESP32-C3和ESP32-S3,ESP32-C2感觉话题度没有那么多,但其应用上不可小觑。今天小启就跟大家讲讲ESP32-C2在小魔方遥控器的应用。1射频性能拉满,成
    的头像 发表于 01-13 08:03 257次阅读
    <b class='flag-5'>ESP32</b>-C2在小魔方遥控器的应用

    ESP32-PICO系列技术规格书

    ESP32-PICO 系列是基于 ESP32 芯片 的系统级封装 (SiP) 产品。ESP32-PICO 系列产品包括 ESP32-PICO-D4、
    发表于 12-12 16:56 0次下载
    <b class='flag-5'>ESP32</b>-PICO系列技术规格书

    esp32s3使用多串口

    esp32s3使用多串口 我按照别人博客中设置串口2,串口打印有问题,因为没有看到esp32s3 多串口,就总结了一下自己的经验 下图为esp32的引脚图 下图为
    的头像 发表于 11-06 11:29 1418次阅读
    <b class='flag-5'>esp32</b>s3使用多串口

    ESP32怎么设置静态IP?

    有什么办法可以设置ESP32为静态IP
    发表于 10-11 08:10

    ESP32学习笔记:WiFi

    今天我们来说说ESP32 的WiFi。
    的头像 发表于 07-15 16:20 2350次阅读
    <b class='flag-5'>ESP32</b>学习笔记:WiFi

    ESP32学习笔记:双核

    今天我们来说说ESP32 for Arduino 的双核。
    的头像 发表于 07-15 16:16 3020次阅读
    <b class='flag-5'>ESP32</b>学习笔记:双核

    ESP32-C2 ESP8684模组AT固件示例

    ESP32-C2 ESP8684模组AT固件示例
    的头像 发表于 07-10 13:39 593次阅读
    <b class='flag-5'>ESP32</b>-C2 <b class='flag-5'>ESP</b>8684模组AT固件示例

    ESP32-C2 8684 AT固件

    ESP32-C2模组 ESP8684芯片 智能家居设备开发模组
    的头像 发表于 07-03 13:41 543次阅读
    <b class='flag-5'>ESP32</b>-C2  8684  AT固件

    ESP32ESP32通过Internet进行通信

    电子发烧友网站提供《ESP32ESP32通过Internet进行通信.zip》资料免费下载
    发表于 06-15 09:58 2次下载
    <b class='flag-5'>ESP32</b>到<b class='flag-5'>ESP32</b>通过Internet进行通信

    ESP8266或ESP32上的WiFi Webradio

    电子发烧友网站提供《ESP8266或ESP32上的WiFi Webradio.zip》资料免费下载
    发表于 06-13 11:38 0次下载
    <b class='flag-5'>ESP</b>8266或<b class='flag-5'>ESP32</b>上的WiFi Webradio

    [esp32教程] 5、UART使用

    基于Ubuntu下,利用esp-idf进行esp32开发的教程
    的头像 发表于 06-13 09:04 4360次阅读
    [<b class='flag-5'>esp32</b>教程] 5、UART使用

    [esp32教程] 4、LEDC使用

    基于Ubuntu下,利用esp-idf进行esp32开发的教程
    的头像 发表于 06-03 09:39 3123次阅读
    [<b class='flag-5'>esp32</b>教程] 4、LEDC使用

    AsyncMqttClient ArduinoIDE库的问题求解

    我一直在我的项目中使用 AsyncMqttClient ArduinoIDE 库,但发现其中有很多致命错误,所以我自己写了一个。我主要将它用于我所有的 ESP8266 设备,但它也适用于 ESP32
    发表于 05-16 07:42

    [esp32教程]2、按键中断

    基于Ubuntu,利用esp-idf进行esp32的教学
    的头像 发表于 05-05 09:04 2203次阅读
    [<b class='flag-5'>esp32</b>教程]2、按键中断

    [esp32教程]1、点灯之术

    基于Ubuntu下,利用esp-idf进行esp32开发的教程
    的头像 发表于 04-30 18:11 2431次阅读
    [<b class='flag-5'>esp32</b>教程]1、点灯之术