你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

将Adafruit WICED Feather连接到AWS IoT

[复制链接]
点点&木木 发布时间:2019-3-20 22:50
本帖最后由 点点&木木 于 2019-4-12 12:01 编辑

这是Adafruit提供的 将AdafruitFeather WICED连接到适用于Mac OS X / Linux的AWS IoT的说明的更新。

1.jpg


硬件组件
Adafruit WICED WiFi Feather - STM32F205 with Cypress WICED WiFi  ×  1      


目的
我将向您展示如何将Adafruit WICEDFeather连接到AWS IoT。WICED Feather基于赛普拉斯WICED(嵌入式设备的无线互联网连接)平台和ARM Cortex M3处理器。
连接Raspberry Pi可能会更容易,而新的Zero W甚至更便宜。但是Raspberry Pi是一台成熟的计算机,而不是IoT(imho)意义上的东西。
我很快发现要找到连接到AWS IoT的东西并不容易。

一个原因是AWS IoT的安全层:
“与其他MQTT解决方案相比,AWS IoT脱颖而出的一个关键方面是安全层。除了使用证书验证远程MQTT服务器(类似于HTTPS的工作方式)之外,还通过以下方式验证各个传感器节点:第二个设备端证书。这意味着,设备将数据推送到MQTT代理或从中订阅数据的设备非常高,只能是您认为的设备。“

另一个原因是亚马逊提供的AWS物联网套件在德国不易获得,价格昂贵(特别是如果包含运费)并且不是最新的。我发现Adafruit WICEDFeather以及将其连接到AWS IoT 的教程。但是我很快就知道教程已经过时了,而且给定的Git存储库不再存在了。

因此,我决定使用此处给出的说明更新教程。我希望你玩得开心,它会帮助你完成你的项目。以下说明是假定您使用的是Mac OS X或Linux。

设置Adafruit WICED Feather
按照此处提供的说明操作。请注意,您的计算机上需要Python 2.7。
登录您的AWS账户并选择AWS IoT,单击“连接”,然后单击“配置设备”/“入门”:
2.jpg
AWS IoT:连接设备


然后选择一个平台和一个SDK。我选择了Linux / OSX和Node.js - 对于这个项目,你选择哪个SDK并不重要,直到你可以注册一个东西。输入名称并继续。我选择了“二”。下载连接套件。该套件包含:
·        私钥(要在草图文件中复制)
·        公钥(要在草图文件夹中移动)
·        证书(用pycert.py转换)
·        一个启动脚本(这里你不需要这个)
完成对话,直到您回到AWS IoT仪表板上。



ArduinoIDE
根据该图打开“Adafruit WICED Examples”/“MQTT”文件夹中名为“aws”的示例。
3.jpg
Arduino IDE

将草图保存到草图文件夹中。将文件从AWS IoT连接工具包复制到此文件夹。在草图副本中插入WiFi网络的SSID凭据:
#define WLAN_SSID         "yourSSID" #define WLAN_PASS         "yourPass"
使用订阅框旁边的“测试”窗格中的AWS IoT UI中找到的主机替换下面的主机。
#define AWS_IOT_MQTT_HOST "A1B0KY2Z6UVZ7R.iot.us-west-2.amazonaws.com"
打开下拉菜单,然后根据图中点击“Viewendpoint”:
4.jpg
AWS IoT:获取MQTT端点

替换客户端ID和事物名称。
#define AWS_IOT_MQTT_CLIENT_ID         "feather-two" #define AWS_IOT_MY_THING_NAME          "feather-two"
现在,您必须使用Adafruit提供的Python工具转换证书,这是使用WICED Feather设置安装的代码的一部分。
python ~/Library/Arduino15/packages/adafruit/hardware/wiced/0.6.2/tools/pycert/pycert.py convert -c local_cert -l LOCAL_CERT_LEN feather-two.cert.pem  
请注意,我的证书名为feather-two.cert.pem。如果您为设备使用了其他名称,请更改名称。
Loaded certificate feather-two.cert.pem Wrote certificates.h
现在打开文本编辑器并加载私钥(feather-two.private.key)。在每行前后放置引号,并在每行后添加'\ n'换行符!你最终会得到这样的东西:

const charaws_private_key[] =  
"-----BEGINRSA PRIVATE KEY-----\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"
"-----ENDRSA PRIVATE KEY-----";
用您的私钥替换行。
"-----BEGINRSA PRIVATE KEY-----\n"
"Your Keycontents line 1 with newline and quoted\n"
"Quoteeach of your key's lines like this example\n"
"-----ENDRSA PRIVATE KEY-----";

现在将草图上传到您的WICED Feather,然后单击Arduino IDE右上角的按钮打开串行监视器。在串行监视器窗口的右下角选择115200波特。
注意:我的串行控制台有些问题。我通过再次单击115200波特率条目的菜单解决了这个问题。

主题
切换到AWS IoT控制台。在物联网控制台中,单击“测试”,然后单击“订阅主题”。
5.jpg
AWS IoT:订阅主题

现在输入:
$aws/things/feather-two/shadow/update/accepted
作为主题的名称。这是草图中的名称:
#define AWS_IOT_MQTT_TOPIC "$aws/things/" AWS_IOT_MY_THING_NAME "/shadow/update"
添加“/ accepted”。
6.jpg
AWS IoT:主题订阅

生成数据
返回到Arduino IDE并通过在串行监视器的输入字段中输入0或1并按返回来生成要发布的数据。再次切换到AWS IoT控制台。您现在应该看到表单的条目:
7.jpg
AWS IoT:第一个MQTT消息

条目由草图中的以下代码生成。
   //Serial.print("Publishing to " AWS_IOT_MQTT_TOPIC " ... ");    if ( c == '0' )   
{      
mqtt.publish(AWS_IOT_MQTT_TOPIC, SHADOW_PUBLISH_STATE_OFF, MQTT_QOS_AT_LEAST_ONCE); // Will halted if an error occurs   


}else if ( c == '1' )   
{     
mqtt.publish(AWS_IOT_MQTT_TOPIC, SHADOW_PUBLISH_STATE_ON, MQTT_QOS_AT_LEAST_ONCE); // Will halted if an error occurs   


}




原理图
8.png



代码

/*********************************************************************
Thisis an example for our Feather WIFI modules

Pickone up today in the adafruit shop!

Adafruit invests time and resources providingthis open source code,
please support Adafruit and open-sourcehardware by purchasing
products from Adafruit!

MITlicense, check LICENSE for more information
Alltext above, and the splash screen below must be included in
anyredistribution
*********************************************************************/

#include <adafruit_feather.h>
#include <adafruit_mqtt.h>

/* This sketch connects to a AWS IOT withmutual verification.
*User enter '0' or '1' to update AWS shadow device as well as
*the LED on WICED module
*
* Torun this demo:
* 1.Create an account and log in http://aws.amazon.com/iot
* 2.Create a THING and named it 'led'
* 3.Select "led" thing and choose "Connect a Device", thenselect "Embeeded C" and click "Genereate Certificate andPolicy"
* 4.Download the generated certificate and private key to this sketch folder. Thenclick "Confirm & Start connecting"
* 5.Then You could see these AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT,AWS_IOT_MQTT_CLIENT_ID, AWS_IOT_MY_THING_NAME, copy these
*and change those in this sketch. (You could always view this later in yourresource management).
* 6.Copy the contents of private key to 'aws_private_key' variable.
*   NOTE: Each line must be added with '\n' and quoted with " " tobe parsed successfully.
* 7.Using pycert to convert certificate to header file by running
*      $python ../../../tools/pycert/pycert.py convert -c local_cert -l LOCAL_CERT_LEN214900e9fd-certificate.pem.crt.txt
*   This should create an certificates.h with variable local_cert.
* 8.Close and reopen this sketch to load certificates.h
* 9.Compile and run
*/

#include "certificates.h"

#define WLAN_SSID         "yourSSID"
#define WLAN_PASS         "yourPass"

#define MQTT_TX_BUFSIZE   1024
#define MQTT_RX_BUFSIZE   1024

int ledPin = PA15;

AdafruitMQTT mqtt;

//======================================================
#define AWS_IOT_MQTT_HOST             "A1B0KY2Z6UVZ7R.iot.us-west-2.amazonaws.com"
#define AWS_IOT_MQTT_PORT              8883
#define AWS_IOT_MQTT_CLIENT_ID         "led"
#define AWS_IOT_MY_THING_NAME          "led"
//======================================================

#define AWS_IOT_MQTT_TOPIC             "$aws/things/"AWS_IOT_MY_THING_NAME "/shadow/update"

#define SHADOW_PUBLISH_STATE_OFF      "{ \"state\":{\"reported\": { \"status\": \"OFF\" } } }"
#define SHADOW_PUBLISH_STATE_ON       "{ \"state\":{\"reported\": { \"status\": \"ON\" } } }"

const char aws_private_key[] =
"-----BEGIN RSA PRIVATE KEY-----\n"
"Your Key contents line 1 with newlineand quoted\n"
"Quote each of your key's lines likethis example\n"
"-----END RSA PRIVATE KEY-----";

/**************************************************************************/
/*!
   @brief  Disconnect handler forMQTT broker connection
*/
/**************************************************************************/
void disconnect_callback(void)
{
Serial.println();
Serial.println("-----------------------------");
Serial.println("DISCONNECTEDFROM MQTT BROKER");
  Serial.println("-----------------------------");
Serial.println();
}

/**************************************************************************/
/*!
   @brief  The setup function runsonce when the board comes out of reset
*/
/**************************************************************************/
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(115200);

  // Wait for the Serial Monitor to open
  while (!Serial)
  {
    /* Delay required to avoid RTOS task switching problems*/
   delay(1);
  }

Serial.println("AWS IOT Example\r\n");

  // Print all software versions
Feather.printVersions();

  while ( !connectAP() )
  {
   delay(500); // delay between each attempt
  }

  // Connected: Print network info
Feather.printNetwork();

  // Tell the MQTT client to auto print error codes andhalt on errors
mqtt.err_actions(true, true);

  // Set ClientID
mqtt.clientID(AWS_IOT_MQTT_CLIENT_ID);

mqtt.setBufferSize(MQTT_TX_BUFSIZE, MQTT_RX_BUFSIZE);

  // Set the disconnect callback handler
mqtt.setDisconnectCallback(disconnect_callback);

  // default RootCA include certificate to verify AWS
Feather.useDefaultRootCA(true);

  // Setting Indentity with AWS Private Key &Certificate
mqtt.tlsSetIdentity(aws_private_key, local_cert, LOCAL_CERT_LEN);

  // Connect with SSL/TLS
Serial.printf("Connecting to" AWS_IOT_MQTT_HOST " port %d ... ", AWS_IOT_MQTT_PORT);
mqtt.connectSSL(AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT);
Serial.println("OK");

Serial.print("Subscribing to " AWS_IOT_MQTT_TOPIC " ... ");
mqtt.subscribe(AWS_IOT_MQTT_TOPIC, MQTT_QOS_AT_MOST_ONCE,subscribed_callback); // Will halted if anerror occurs
Serial.println("OK");

  // Message to user
Serial.print("Enter '0' or '1' to updatefeed: ");
}

/**************************************************************************/
/*!
   @brief  This loop function runsover and over again
*/
/**************************************************************************/
void loop()
{
  // Get input from user '0' to off, '1' to update feed
  if ( Serial.available())
  {
   char c = Serial.read();

    // echo
   Serial.println(c);
   Serial.print("Enter '0' or '1' to updatefeed: ");

    //Serial.print("Publishing to "AWS_IOT_MQTT_TOPIC " ... ");
    if ( c == '0' )
    {
      mqtt.publish(AWS_IOT_MQTT_TOPIC,SHADOW_PUBLISH_STATE_OFF, MQTT_QOS_AT_LEAST_ONCE); // Will halted if an error occurs
    }else if ( c == '1' )
    {
     mqtt.publish(AWS_IOT_MQTT_TOPIC, SHADOW_PUBLISH_STATE_ON,MQTT_QOS_AT_LEAST_ONCE); // Will halted if anerror occurs
    }else
    {
     // do nothing
    }
  }

delay(5000);
}

/**************************************************************************/
/*!
   @brief  MQTT subscribe eventcallback handler

   @param  topic      The topic causing this callback to fire
   @param  message    The new value associated with 'topic'

   @note   'topic' and 'message' areUTF8Strings (byte array), which means
           they are not null-terminated like C-style strings. You can
           access its data and len using .data & .len, although there is
           also a Serial.print override to handle UTF8String data types.
*/
/**************************************************************************/
void subscribed_callback(UTF8String topic,UTF8String message)
{
  if ( 0 ==memcmp(SHADOW_PUBLISH_STATE_OFF, message.data, message.len) )
  {
   digitalWrite(ledPin, LOW);
  }

  if ( 0 ==memcmp(SHADOW_PUBLISH_STATE_ON, message.data, message.len) )
  {
   digitalWrite(ledPin, HIGH);
  }
}

/**************************************************************************/
/*!
   @brief  Connect to defined AccessPoint
*/
/**************************************************************************/
bool connectAP(void)
{
  // Attempt to connect to an AP
  Serial.print("Please wait while connecting to: '" WLAN_SSID "' ... ");

  if (Feather.connect(WLAN_SSID, WLAN_PASS) )
  {
   Serial.println("Connected!");
  }
  else
  {
   Serial.printf("Failed! %s(%d)", Feather.errstr(), Feather.errno());
   Serial.println();
  }
Serial.println();

  return Feather.connected();
}



收藏 评论0 发布时间:2019-3-20 22:50

举报

0个回答

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版