简体   繁体   English

使用 amazon mqtt 从 esp8266 发布按钮按下

[英]Using amazon mqtt to publish a button press from a esp8266

I have the following code that prints to my serial console in mongoose os.我有以下代码打印到我在 mongoose os 中的串行控制台。 I can see the device in amazon aws, and in mdash as online.我可以在 amazon aws 中看到该设备,在 mdash 中可以看到在线。 I get back a 0 from the pub and it never sends a message to aws mqtt I have subscribed to the topic in the test seciton in mqtt so not sure what I am doing wrong我从酒吧得到一个 0 并且它从不向 aws mqtt 发送消息我已经订阅了 mqtt 测试部分中的主题,所以不确定我做错了什么

load("api_gpio.js");
load("api_mqtt.js");
load("api_sys.js");
let pin = 0;
GPIO.set_button_handler(
  pin,
  GPIO.PULL_UP,
  GPIO.INT_EDGE_NEG,
  50,
  function (x) {
    let topic = "mOS/topic1";
    let message = JSON.stringify({
      total_ram: Sys.total_ram(),
      free_ram: Sys.free_ram(),
    });
    let ok = MQTT.pub(topic, message, message.length);
    let test = ok;
    let res = MQTT.pub("mOS/topic1", JSON.stringify({ a: 1, b: 2 }), 1);

    print(res);
    print("Published:", res ? "yes" : "no");
  },
  true
);
print("Flash button is configured on GPIO pin", pin);
print("Press the flash button now!");

MQTT.sub(
  "mOS/topic1",
  function (conn, topic, msg) {
    print("Topic:", topic, "message:", msg);
  },
  null
);

I am pretty sure I have fullfilled all these requirments我很确定我已经满足了所有这些要求

A valid connection有效的连接

A valid and active certificate有效且有效的证书

A policy that allows the desired connection and operation允许所需连接和操作的策略

I have a mos-default policy with the following我有一个 mos-default 策略,其中包含以下内容

{
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "*"
    }
  ],
  "Version": "2012-10-17"
}

There is a mos.yml file of the following.有一个 mos.yml 文件如下。 And I do have aws.crt.pem and aws.key.pem files in the app.我的应用程序中有 aws.crt.pem 和 aws.key.pem 文件。

author: mongoose-os
description: A JS-enabled demo Mongoose OS firmware
# arch: PLATFORM
version: 1.0
manifest_version: 2017-05-18
libs_version: ${mos.version}
modules_version: ${mos.version}
mongoose_os_version: ${mos.version}

config_schema:
  - ["mqtt.server", "iot.eclipse.org:1883"]
  - ["i2c.enable", true]

tags:
  - js

filesystem:
  - fs

libs:
  - origin: https://github.com/mongoose-os-libs/boards
  - origin: https://github.com/mongoose-os-libs/js-demo-bundle

conds:
  # It's not that we can't work with multicore
  # but we cannot afford it because of space (32K).
  - when: mos.platform == "esp32"
    apply:
      build_vars:
        ESP_IDF_SDKCONFIG_OPTS: >
          ${build_vars.ESP_IDF_SDKCONFIG_OPTS}
          CONFIG_FREERTOS_UNICORE=y

let connected = MQTT.isConnected();

print(connected);

returns false, I am not sure how to debug this返回 false,我不知道如何调试

I followed every instruction here including the internet button video https://mongoose-os.com/docs/mongoose-os/cloud/aws.md我遵循了这里的每一条指令,包括互联网按钮视频https://mongoose-os.com/docs/mongoose-os/cloud/aws.md

Ok it is working again, I needed to delete the device in amazon, and run the mos aws setup command again.好的,它又可以工作了,我需要在 amazon 中删除该设备,然后再次运行 mos aws setup 命令。

So the real problem was I was flashing my device each time I updated my code and it would overwrite my wifi settings, my aws settings and my mdash settings.所以真正的问题是我每次更新代码时都在刷新我的设备,它会覆盖我的 wifi 设置、我的 aws 设置和我的 mdash 设置。

Thanks to help from a different source this solved it for me感谢来自不同来源的帮助,这为我解决了

Use mos call FS.List to get a directory listing of the files in your device
Use mos get to retrieve your credentials and save them locally in your fs dir.
E.g.:

$ mos get aws-esp32_807A98.key.pem > fs/aws-esp32_807A98.key.pem
$ mos get aws-esp32_807A98.crt.pem > fs/aws-esp32_807A98.crt.pem
$ mos get ca.pem > fs/ca.pem
Use mos config-get to read your config, or just copy from that verbose output you get when you initialize your device. Save those to mos.yml.
E.g.:

["aws.thing_name", ""]
["mqtt.enable", true]
["mqtt.server", "a1234b5678cde-ats.iot.us-east-2.amazonaws.com:8883"]
["mqtt.ssl_ca_cert", "ca.pem"]
["mqtt.ssl_cert", "aws-esp32_807A98.crt.pem"]
["mqtt.ssl_key", "aws-esp32_807A98.key.pem"]
So, when you re-flash, you re-write your credentials and configure your device with the same data again.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM