×

适用于ESP32的AWS IoT Arduino库

消耗积分:0 | 格式:zip | 大小:0.81 MB | 2022-11-29

英雄孤寂

分享资料个

描述

https://github.com/aws-samples/arduino-aws-greengrass-iot

该库使用 AWS C-SDK 来实现 Arduino 类AWSGreenGrassIoT ,以便轻松地将传感器/执行器安全地连接到 AWS IoT Core,直接或通过使用 X509 证书的 AWS Greengrass 设备(即 Raspberry PI)。

AWSGreenGrassIoT 类公开了以下方法:

Constructor: AWSGreenGrassIoT(const char * AwsIoTCoreurl, // AWS IoT core URL
                              const char * thingName,     // AWS thing name
                              const char * iotCoreCA,     // AWS IoT core certificate (defined in certificate.c)
                              const char * thingCA,       // thing certificate (defined in certificates.c)
                              const char * thingKey);     // thing private key (defined in certificate.c)
 bool connectToGG(void);       // connect the device to greengrass
 bool connectToIoTCore(void);  // connect the device directly to AWS IoT Core
 bool publish(char * pubtopic, char * pubPayLoad);  // publish a JSON record to "pubTopic"
 bool subscribe(char * subTopic, pSubCallBackHandler_t pSubCallBackHandler); // subscribe to "subTopic" and define the callback function to handle the messages coming from the IoT broker

库依赖项

 该库基于最新(截至 2020 年 1 月)Amazon iot C-SDK 版本 3.0.1 https://github.com/aws/aws-iot-device-sdk-embedded-C/releases/tag/v3。 0.1 .未来版本的 SDK 可以在https://github.com/aws/aws-iot-device-sdk-embedded-C上找到,可能需要重新编译 AWSGreengrassIoT 代码。该库使用了一些标准的 Arduino库:WiFi、WifiClientSecure、HTTPClient。

这些示例需要从公共存储库安装以下库:

  • NPTClient,用于与 nptd 服务器同步实时时钟并为传感器测量生成时间戳(在发布示例中,gg_SGP30_publisher 和 was_SGP30_publisher);
  • ADAFruit_SGP30,使用gg_SGP30_publisher和was_SGP30_publisher示例中的空气杂质传感器;
  • ESP32Servo,在gg_subscriber和aws_subscriber例子中控制伺服电机的转动;

先决条件

  • 从 Arduino IDE 主菜单 -> 首选项
  • 安装 ESP32 板管理器:
  • 从 Arduino IDE 主菜单 -> 工具 -> 板管理器
  • 在如下所示的搜索字段中输入 ESP32 并安装新的 eps32board 管理器:
  • 在 Tools-> Board 中选择您的 ESP32 模块类型,在我的情况下为 ESP32 Dev Module
  • 安装 Arduino 库“NPTClient”、“Adafruit_SGP30”、“ESP32Servo”,以便能够使用文件菜单 -> 示例 -> AWSGreengrassIoT 中的发布和订阅示例。下图显示了如何添加简单伺服电机的支持库:

安装 AWSGreengrassIoT 库

  • 如图所示,以 zip 格式下载库:
  • 在 Sketch->Include Library->Add.ZIP 库中添加 Zip 文件作为新的 Arduino 库:
  • 检查该库是否在 Sketch -> Include Library 中可见,如下面的屏幕截图所示:

例子

在使用 Sketch->Examples 菜单中的示例之前,请记住:

  • 在 AWS IoT Core 中创建“事物”:
  • 生成并下载Thing的证书、Thing的私钥和AWS服务证书(如果您的终端节点与eu-central-1不同);
  • 使用上一步中生成的证书/密钥创建 aws_certificates.c 文件;
  • 通过定义 AWS IoT Core URL、“事物”和 WiFi 网络的参数来自定义 Arduino 示例代码:
char WIFI_SSID[]="SSID";
char WIFI_PASSWORD[]="PASSWORD";
char AWSIOTURL[]="xxxxxxxxxxxxxxx-ats.iot.region.amazonaws.com";
char THING[]= "your device name here";

AWSGreenGrassIoT 库附带 5 个示例:

aws_servo_subscriber, gg_servo_subscriber

在这些示例中,伺服电机连接到 ESP32 上的模拟 GPIO 端口 0,并根据订阅主题“窗口”通过旋转电机 ±90 度来模拟远程打开和关闭窗口。“打开”将使电机旋转 +90 度,“关闭”将使电机以相反的方向旋转 -90 度。

电路原理图:

这两个示例之间的唯一区别是:

  • aws_servo_subscriber 使用成员函数“connectToIoTCore”直接连接到 AWS IoT Core;
if(greengrass->connectToIoTCore() == true)
  {
      Serial.println("Connected to AWS IoT core");
      delay(2000);
     if( true == greengrass->subscribe(TOPIC_NAME,subscribeCallback)) {
          Serial.println("Subscribe to Window/# topic successful ");
     }
     else {
          Serial.println("Subscribe to Window/# Failed, Check the Thing Name and Certificates");
          while(1);
     }
   }
  else
  {
      Serial.println("Connection to AWS IoT core failed");
      while(1);
  }
  • gg_servo_subscriber 使用成员函数“connectToGG”通过 greengrass 设备(它可以是连接到同一 WiFi 网络的 Raspberry Pi 或 AWS 云中的远程 EC2 实例)进行连接;
if(greengrass->connectToGG() == true)
 {
     Serial.println("Connected to AWS GreenGrass");
     delay(2000);
    if( true == greengrass->subscribe(TOPIC_NAME,subscribeCallback)) {
         Serial.println("Subscribe to Window/# topic successful ");
    }
    else {
         Serial.println("Subscribe to Window/# Failed, Check the Thing Name and Certificates");
         while(1);
    }
  }
 else
 {
     Serial.println("Connection to Greengrass failed, check if Greengrass is on and connected to the WiFi");
     while(1);
 }

对于两个用例,处理主题订阅的回调函数是相同的:

static void subscribeCallback (char *topicName, int payloadLen, char *payLoad)
{
    //check if the topic is Window/close or Window/open
    rcvdPayload = String(payLoad);
    cmdReceived = CMD_UNKNOWN;
    String topic = String(topicName);
    if ( topic.startsWith(topicClose+ "{")) {
      cmdReceived = CMD_CLOSE;
      rcvdTopic = topicClose;
    }
    else if (topic.startsWith(topicOpen + "{")) {
      cmdReceived = CMD_OPEN;
      rcvdTopic = topicOpen;
    }
    else
      rcvdTopic = topicName;
    msgReceived = 1;
}

aws_sgp30_publisher、gg_sgp30_publisher

这两个示例使用 Adafruit 的空气杂质传感器 SGP30 连接到 ESP32 上的 I2C 端口之一,如下图所示。这些示例需要安装 ADAFruit_SGP30 库,如上一节中的第 3 点所示。

电路原理图:

除了将 ESP32 Arduino 连接到云端的部分之外,这两个示例共享相同的代码。在 aws_sgp30_publisher 中,我们使用“connectToIoTCore”函数将测量结果直接发布到 AWS IoT 核心。在 gg_sgp30_publisher 中,我们使用“connectToGG”成员函数将测量结果发送到 greengrass 边缘设备。

aws_bme280_sgp30_publisher

在此示例中,我们展示了如何将两个传感器用于同一 I2C 总线,BME280(温度、湿度、压力、高度)和 SGP30。示例草图与 aws_sgp30_publisher 类似,但增加了初始化和读取 Adafruit BME280 传感器的测量值。

这是电路图:


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

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !