title: MQTT google cloud connection with ESP8266
body: <p>I am trying to send temperature data from a NodeMCU to Google Cloud with Arduino IDE and using this library <a href="https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino" rel="nofollow noreferrer">https://github.com/GoogleCloudPlatform/google-cloud-iot-arduino</a>. 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:</p>
  
  <pre><code>14:11:14.342 -&gt; Connecting to WiFi
  14:11:15.725 -&gt; Waiting on time sync...
  14:11:15.866 -&gt; Success to open ca file
  14:11:15.913 -&gt; loaded
  14:11:15.913 -&gt; 
  14:11:15.913 -&gt; connecting...Refreshing JWT
  14:11:19.797 -&gt; 
  14:11:19.797 -&gt; connected!
  14:11:19.843 -&gt; 
  14:11:19.843 -&gt; connecting...Refreshing JWT
  14:11:23.727 -&gt; 
  14:11:23.727 -&gt; connected!
  14:11:23.774 -&gt; 
  14:11:23.774 -&gt; connecting...Refreshing JWT
  </code></pre>
  
  <p>and so on....</p>
  
  <p>The data is not receive in the Cloud dashboard under Device > Configuration &amp; State History, I can see a yellow warning "Not yet acknowledged by device"</p>
  
  <p>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?</p>
  
  <p>Thank you</p>
  
  <pre><code>#include "DHT.h"
  #include &lt;CloudIoTCore.h&gt;
  #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-&gt;loop();
    delay(10);  // &lt;- fixes some issues with WiFi stability
  
    if (!mqttClient-&gt;connected()) {
      connect();
    }
  
    if (millis() - lastMillis &gt; 20000) {
      lastMillis = millis();
    String payload =
        String("{\"timestamp\":") + time(nullptr) +
        String(",\"temperature\":") + dht.readTemperature() +
        String(",\"humidity\":") + dht.readHumidity() +
        String("}");
      publishTelemetry(payload);
    }
  }
  </code></pre>
  
  <p>UPDATE:
  Serial Monitor output with debug:</p>
  
  <pre><code>19:16:17.266 -&gt; 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 -&gt; sta config unchangedscandone
  19:16:17.313 -&gt; Connecting to WiFi
  19:16:17.313 -&gt; wifi evt: 2
  19:16:17.406 -&gt; scandone
  19:16:17.406 -&gt; state: 0 -&gt; 2 (b0)
  19:16:17.406 -&gt; state: 2 -&gt; 3 (0)
  19:16:17.406 -&gt; state: 3 -&gt; 5 (10)
  19:16:17.406 -&gt; add 0
  19:16:17.406 -&gt; aid 14
  19:16:17.406 -&gt; cnt 
  19:16:17.453 -&gt; 
  19:16:17.453 -&gt; connected with Internet, channel 6
  19:16:17.453 -&gt; dhcp client start...
  19:16:17.453 -&gt; wifi evt: 0
  19:16:19.282 -&gt; ip:XX.X.X.X,mask:XXX.XXX.XXX.X,gw:XX.X.X.XXX
  19:16:19.282 -&gt; wifi evt: 3
  19:16:19.282 -&gt; Waiting on time sync...
  19:16:19.751 -&gt; SPIFFSImpl: allocating %zd+%z d+%z  d=%z   d bytes
  19:16:19.751 -&gt; SPIFFSImpl: mounting fs @100000, size=2fb000, block=2000, page=100
  19:16:19.798 -&gt; SPIFFSImpl: mount rc=0
  19:16:19.798 -&gt; Success to open ca file
  19:16:19.845 -&gt; loaded
  19:16:19.845 -&gt; SPIFFS_close: fd=1
  19:16:19.845 -&gt; 
  19:16:19.845 -&gt; connecting...Refreshing JWT
  19:16:23.255 -&gt; [hostByName] request IP for: mqtt.googleapis.com
  19:16:23.301 -&gt; [hostByName] Host: mqtt.googleapis.com IP: XX.XXX.XXX.XXX
  19:16:23.301 -&gt; :ref 1
  19:16:23.395 -&gt; :wr 224 0
  19:16:23.395 -&gt; :wrc 224 224 0
  19:16:23.395 -&gt; :ack 224
  19:16:23.395 -&gt; :rn 536
  19:16:23.395 -&gt; :rd 5, 536, 0
  19:16:23.395 -&gt; :rdi 536, 5
  19:16:23.442 -&gt; :rd 87, 536, 5
  19:16:23.442 -&gt; :rdi 531, 87
  19:16:23.442 -&gt; :rd 5, 536, 92
  19:16:23.442 -&gt; :rdi 444, 5
  19:16:23.442 -&gt; :rd 439, 536, 97
  19:16:23.442 -&gt; :rdi 439, 439
  19:16:23.442 -&gt; :c0 439, 536
  19:16:23.676 -&gt; :rn 536
  19:16:23.676 -&gt; :rd 536, 536, 0
  19:16:23.676 -&gt; :rdi 536, 536
  19:16:23.676 -&gt; :c0 536, 536
  19:16:23.770 -&gt; :rn 1072
  19:16:23.770 -&gt; :rch 1072, 536
  19:16:23.770 -&gt; :rd 1471, 1608, 0
  19:16:23.770 -&gt; :rdi 536, 536
  19:16:23.770 -&gt; :c 536, 536, 1608
  19:16:23.770 -&gt; :rdi 536, 536
  19:16:23.770 -&gt; :c 536, 536, 1072
  19:16:23.770 -&gt; :rdi 536, 399
  19:16:24.086 -&gt; :rch 536, 177
  19:16:24.086 -&gt; 
  19:16:24.086 -&gt; connected!
  19:16:24.086 -&gt; 
  19:16:24.086 -&gt; connecting...Refreshing JWT
  19:16:27.510 -&gt; [hostByName] request IP for: mqtt.googleapis.com
  19:16:27.510 -&gt; [hostByName] Host: mqtt.googleapis.com IP: XX.XXX.XXX.XXX
  19:16:27.510 -&gt; pm open,type:2 0
  19:16:27.510 -&gt; :close
  19:16:27.510 -&gt; :ur 1
  19:16:27.510 -&gt; :del
  19:16:27.510 -&gt; :ref 1
  19:16:27.604 -&gt; :wr 224 0
  19:16:27.604 -&gt; :wrc 224 224 0
  19:16:27.651 -&gt; :ack 224
  19:16:27.651 -&gt; :rn 536
  19:16:27.651 -&gt; :rch 536, 536
  19:16:27.651 -&gt; :rch 1072, 536
  19:16:27.651 -&gt; :rch 1608, 536
  19:16:27.651 -&gt; :rd 5, 2144, 0
  19:16:27.651 -&gt; :rdi 536, 5
  19:16:27.651 -&gt; :rd 87, 2144, 5
  19:16:27.651 -&gt; :rdi 531, 87
  19:16:27.651 -&gt; :rd 5, 2144, 92
  19:16:27.651 -&gt; :rdi 444, 5
  19:16:27.651 -&gt; :rd 2047, 2144, 97
  19:16:27.651 -&gt; :rdi 439, 439
  19:16:27.651 -&gt; :c 439, 536, 2144
  19:16:27.651 -&gt; :rdi 536, 536
  19:16:27.651 -&gt; :c 536, 536, 1608
  19:16:27.651 -&gt; :rdi 536, 536
  19:16:27.651 -&gt; :c 536, 536, 1072
  19:16:27.651 -&gt; :rdi 536, 536
  19:16:27.651 -&gt; :c0 536, 536
  19:16:27.885 -&gt; :rn 536
  19:16:27.885 -&gt; :rch 536, 177
  19:16:27.885 -&gt; :rd 399, 713, 0
  19:16:27.885 -&gt; :rdi 536, 399
  19:16:28.073 -&gt; 
  19:16:28.073 -&gt; connected!
  19:16:28.120 -&gt; 
  19:16:28.120 -&gt; connecting...Refreshing JWT
  </code></pre>
  
  <p>UPDATE2: JWT validation and credential debug</p>
  
  <p>Below the credential and JWT obtained from Serial Monitor:</p>
  
  <pre><code>18:54:41.890 -&gt; Waiting on time sync...
  18:54:42.904 -&gt; Success to open ca file
  18:54:42.938 -&gt; loaded
  18:54:42.971 -&gt; projects/iot-esp8266-new/locations/us-central1/registries/iotcore-registry/devices/esp8266
  18:54:42.971 -&gt; Refreshing JWT
  18:54:46.318 -&gt; XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  18:54:46.351 -&gt; 
  18:54:46.351 -&gt; connecting...Refreshing JWT
  </code></pre>
  
  <p>The JWT generated has been validated with PyJWT as below:</p>
  
  <pre><code>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'}
  </code></pre>