2
votes

I am trying to send temperature data from a NodeMCU to Google Cloud with Arduino IDE and using this library https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino. I created a registry and a device on google iot core and uploaded there the public key (ES256). On my side I uploaded the csa certificate on the ESP8266 though SPIFFS and correctly set all parameters and private key string in 'ciotc_config.h'. When I try to connect I get in the Serial Monitor the following output repeting itself:

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

and so on....

The data is not receive in the Cloud dashboard under Device > Configuration & State History, I can see a yellow warning "Not yet acknowledged by device"

It seems I cannot connect with MQTT, because the data is not sent and I do not know how actually understand where is the mistake. Do you guys have any idea?

Thank you

#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);
  }
}

UPDATE: Serial Monitor output with debug:

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 validation and credential debug

Below the credential and JWT obtained from Serial Monitor:

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

The JWT generated has been validated with PyJWT as below:

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
The log suggests that you are actually getting the connection and losing it. With other MQTT brokers, that can happen if a device has permission to connect, but then tries to subscribe to another topic for which it lacks permission. So much of the device firmware here is in the library that I would check the cloud configuration first. - MBer
Hi @MBer, thank you for the answer...I see that is getting connected and disconnected, almost immediately...I think on cloud side I did everything I had to do, I activated billing, api and pubsub, then created project, registry and device and attached the public key there, do not actually know what else I should do... - Melvin86
Good luck then - unless someone who has had the same problem stops by, solving will be difficult without logs from Cloud Pub/Sub. - MBer
A few questions: Which version of the library do you have installed? Which SDK do you have for the 8266? It may be helpful to enable more verbose debugging in Arduino by setting the debug level in the IDE under Tools > Core Debug Level > Verbose. - class
Did you put the root certificate from Google on the device? Just making sure the csa cert you're talking about is both the private half of the SSL key pair you registered with the device registry when you created the device AND the root certificate you get from pki.google.com/roots.pem. - Gabe Weiss

1 Answers

2
votes

Update There were changes in the 2.5.x branch of the ESP8266 community SDK. The library will need updates to support it alongside the 2.4.x libraries and they are not yet complete. For now, it's easiest to just use the 2.4.2 SDK or you can use the workaround mentioned in the library documentation until a fix is released.

Previous response

I have anecdotally verified the 1.0.5 version of the library is working with ESP8266 hardware. You may want to try printing the parameters out and make sure they are getting set correctly. This is probably easiest done by replacing this:

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

with the following line, in your main project file:

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

If the configuration looks correct, you may want to try verifying the JWT with something like jwt.io. It makes sense to add a helper to the library for this.