与ESP8266的MQTT谷歌云连接

时间:2019-04-22 12:18:42

标签: google-cloud-platform mqtt iot arduino-esp8266

我正在尝试使用Arduino IDE将温度数据从NodeMCU发送到Google Cloud,并使用此库https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino。我在Google IoT核心版上创建了一个注册表和一个设备,并在此处上传了公钥(ES256)。在我这边,我通过SPIFFS在ESP8266上上传了csa证书,并在'ciotc_config.h'中正确设置了所有参数和私钥字符串。当我尝试连接时,进入串行监视器,以下输出重复出现:

14:11:14.342 -> Connecting to WiFi
14:11:15.725 -> Waiting on time sync...
14:11:15.866 -> Success to open ca file
14:11:15.913 -> loaded
14:11:15.913 -> 
14:11:15.913 -> connecting...Refreshing JWT
14:11:19.797 -> 
14:11:19.797 -> connected!
14:11:19.843 -> 
14:11:19.843 -> connecting...Refreshing JWT
14:11:23.727 -> 
14:11:23.727 -> connected!
14:11:23.774 -> 
14:11:23.774 -> connecting...Refreshing JWT

以此类推。...

“设备”>“配置和状态历史记录”下的“云”仪表板中未接收到数据,我看到一个黄色警告“设备尚未确认”

似乎我无法与MQTT连接,因为未发送数据,并且我不知道如何真正理解错误所在。你们有什么主意吗?

谢谢

#include "DHT.h"
#include <CloudIoTCore.h>
#include "esp8266_mqtt.h"

#define DHTPIN 2 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
unsigned long lastMillis = 0;
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  setupCloudIoT(); // Creates globals for MQTT
  dht.begin();
}

void loop() {
  mqttClient->loop();
  delay(10);  // <- fixes some issues with WiFi stability

  if (!mqttClient->connected()) {
    connect();
  }

  if (millis() - lastMillis > 20000) {
    lastMillis = millis();
  String payload =
      String("{\"timestamp\":") + time(nullptr) +
      String(",\"temperature\":") + dht.readTemperature() +
      String(",\"humidity\":") + dht.readHumidity() +
      String("}");
    publishTelemetry(payload);
  }
}

更新: 带有调试的串行监视器输出:

19:16:17.266 -> SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
19:16:17.266 -> sta config unchangedscandone
19:16:17.313 -> Connecting to WiFi
19:16:17.313 -> wifi evt: 2
19:16:17.406 -> scandone
19:16:17.406 -> state: 0 -> 2 (b0)
19:16:17.406 -> state: 2 -> 3 (0)
19:16:17.406 -> state: 3 -> 5 (10)
19:16:17.406 -> add 0
19:16:17.406 -> aid 14
19:16:17.406 -> cnt 
19:16:17.453 -> 
19:16:17.453 -> connected with Internet, channel 6
19:16:17.453 -> dhcp client start...
19:16:17.453 -> wifi evt: 0
19:16:19.282 -> ip:XX.X.X.X,mask:XXX.XXX.XXX.X,gw:XX.X.X.XXX
19:16:19.282 -> wifi evt: 3
19:16:19.282 -> Waiting on time sync...
19:16:19.751 -> SPIFFSImpl: allocating %zd+%z d+%z  d=%z   d bytes
19:16:19.751 -> SPIFFSImpl: mounting fs @100000, size=2fb000, block=2000, page=100
19:16:19.798 -> SPIFFSImpl: mount rc=0
19:16:19.798 -> Success to open ca file
19:16:19.845 -> loaded
19:16:19.845 -> SPIFFS_close: fd=1
19:16:19.845 -> 
19:16:19.845 -> connecting...Refreshing JWT
19:16:23.255 -> [hostByName] request IP for: mqtt.googleapis.com
19:16:23.301 -> [hostByName] Host: mqtt.googleapis.com IP: XX.XXX.XXX.XXX
19:16:23.301 -> :ref 1
19:16:23.395 -> :wr 224 0
19:16:23.395 -> :wrc 224 224 0
19:16:23.395 -> :ack 224
19:16:23.395 -> :rn 536
19:16:23.395 -> :rd 5, 536, 0
19:16:23.395 -> :rdi 536, 5
19:16:23.442 -> :rd 87, 536, 5
19:16:23.442 -> :rdi 531, 87
19:16:23.442 -> :rd 5, 536, 92
19:16:23.442 -> :rdi 444, 5
19:16:23.442 -> :rd 439, 536, 97
19:16:23.442 -> :rdi 439, 439
19:16:23.442 -> :c0 439, 536
19:16:23.676 -> :rn 536
19:16:23.676 -> :rd 536, 536, 0
19:16:23.676 -> :rdi 536, 536
19:16:23.676 -> :c0 536, 536
19:16:23.770 -> :rn 1072
19:16:23.770 -> :rch 1072, 536
19:16:23.770 -> :rd 1471, 1608, 0
19:16:23.770 -> :rdi 536, 536
19:16:23.770 -> :c 536, 536, 1608
19:16:23.770 -> :rdi 536, 536
19:16:23.770 -> :c 536, 536, 1072
19:16:23.770 -> :rdi 536, 399
19:16:24.086 -> :rch 536, 177
19:16:24.086 -> 
19:16:24.086 -> connected!
19:16:24.086 -> 
19:16:24.086 -> connecting...Refreshing JWT
19:16:27.510 -> [hostByName] request IP for: mqtt.googleapis.com
19:16:27.510 -> [hostByName] Host: mqtt.googleapis.com IP: XX.XXX.XXX.XXX
19:16:27.510 -> pm open,type:2 0
19:16:27.510 -> :close
19:16:27.510 -> :ur 1
19:16:27.510 -> :del
19:16:27.510 -> :ref 1
19:16:27.604 -> :wr 224 0
19:16:27.604 -> :wrc 224 224 0
19:16:27.651 -> :ack 224
19:16:27.651 -> :rn 536
19:16:27.651 -> :rch 536, 536
19:16:27.651 -> :rch 1072, 536
19:16:27.651 -> :rch 1608, 536
19:16:27.651 -> :rd 5, 2144, 0
19:16:27.651 -> :rdi 536, 5
19:16:27.651 -> :rd 87, 2144, 5
19:16:27.651 -> :rdi 531, 87
19:16:27.651 -> :rd 5, 2144, 92
19:16:27.651 -> :rdi 444, 5
19:16:27.651 -> :rd 2047, 2144, 97
19:16:27.651 -> :rdi 439, 439
19:16:27.651 -> :c 439, 536, 2144
19:16:27.651 -> :rdi 536, 536
19:16:27.651 -> :c 536, 536, 1608
19:16:27.651 -> :rdi 536, 536
19:16:27.651 -> :c 536, 536, 1072
19:16:27.651 -> :rdi 536, 536
19:16:27.651 -> :c0 536, 536
19:16:27.885 -> :rn 536
19:16:27.885 -> :rch 536, 177
19:16:27.885 -> :rd 399, 713, 0
19:16:27.885 -> :rdi 536, 399
19:16:28.073 -> 
19:16:28.073 -> connected!
19:16:28.120 -> 
19:16:28.120 -> connecting...Refreshing JWT

UPDATE2:JWT验证和凭据调试

从串行监视器获得的凭据和JWT下面:

18:54:41.890 -> Waiting on time sync...
18:54:42.904 -> Success to open ca file
18:54:42.938 -> loaded
18:54:42.971 -> projects/iot-esp8266-new/locations/us-central1/registries/iotcore-registry/devices/esp8266
18:54:42.971 -> Refreshing JWT
18:54:46.318 -> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
18:54:46.351 -> 
18:54:46.351 -> connecting...Refreshing JWT

生成的JWT已通过PyJWT进行了如下验证:

import jwt
public_key =  open('ec_public.pem').read()
token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
payload = jwt.decode(token, public_key, algorithms=['ES256'], audience="iot-esp8266-new")
payload
{'iat': 1556988880, 'exp': 1556992480, 'aud': 'iot-esp8266-new'}

1 个答案:

答案 0 :(得分:1)

更新 ESP8266社区SDK的2.5.x分支进行了更改。该库将需要更新以与2.4.x库一起支持它,但它们尚未完成。目前,最简单的方法是只使用2.4.2 SDK,也可以use the workaround mentioned in the library documentation,直到发布修复程序为止。

上次答复

我已轶事验证该库的1.0.5版本可与ESP8266硬件一起使用。您可能需要尝试打印出参数,并确保正确设置了参数。替换掉这个可能最简单:

  if (!mqttClient->connected()) {
    connect();
  }

在主项目文件中带有以下行:

  if (!mqttClient->connected()) {
    // Example debug line
    Serial.println(device->getClientId());
    Serial.println(getJwt());
    // End debug line
    connect();
  }

如果配置看起来正确,则可能要尝试使用jwt.io之类的方法来验证JWT。为此,可以在库中添加一个辅助程序。