title: Connecting to IoT Core MQTT
body: <p>I am trying to connect to Google's MQTT server but I am getting errors
  I created all the certificates and registered my device (Adafruit huzzah32)</p>
  
  <p>and the documentation says you connect to <code>mqtt.googleapis.com:8883</code></p>
  
  <p>So I do</p>
  
  <pre class="lang-cpp prettyprint-override"><code>WiFiClientSecure wifi;
  MQTTClient client;
  client.begin("mqtt.googleapis.com", 8883, wifi);
  </code></pre>
  
  <p>When I try to connect I use the device path</p>
  
  <pre class="lang-cpp prettyprint-override"><code>const char* jwt = "{json web token}";
  const char* device = "projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{device-id}";
  
  Serial.print("Connecting to mqtt");
  while (!client.connect(device,"unused",jwt)) {
      Serial.print(".");
      delay(1000);
  }
  
  Serial.println();
  Serial.println("Connected to mqtt");
  </code></pre>
  
  <p>but it never connects</p>
  
  <p>I verified the google certificate by calling 
  <code>openssl s_client -showcerts -connect mqtt.googleapis.com:8883</code>
  and I put in my RSA Private key and certificate key that was made</p>
  
  <pre><code>wifi.setCACert(googleCertificate2);
  wifi.setCertificate(myCertificate);
  wifi.setPrivateKey(privateCert);
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p>Here is the connection documentation
  <a href="https://cloud.google.com/iot/docs/how-tos/mqtt-bridge" rel="nofollow noreferrer">https://cloud.google.com/iot/docs/how-tos/mqtt-bridge</a></p>
  
  <p><strong>Update</strong></p>
  
  <p>I made a quick java example to see if I can connect going off the example they have for connecting and I get a <code>MqttException</code> saying <code>Bad user name or password (4)</code></p>
  
  <p>Here is the code for that</p>
  
  <pre><code>private void doStuff(){
          String clientId = String.format("ssl://%s:%s", "mqtt.googleapis.com", 8883);
          String mqttClientId = String.format("projects/%s/locations/%s/registries/%s/devices/%s","{project_id}", "us-central1", "{register}", "{device}");
          MqttConnectOptions connectOptions = new MqttConnectOptions();
          connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
  
          Properties sslProps = new Properties();
          sslProps.setProperty("com.ibm.ssl.protocol", "TLSv1.2");
          connectOptions.setSSLProperties(sslProps);
  
          connectOptions.setUserName("unused");
  
  
          try{
              String jwt = createJwtRsa("{project-id}");
              connectOptions.setPassword(jwt.toCharArray());
              MqttClient client = new MqttClient(clientId, mqttClientId, new MemoryPersistence());
  
              while(!client.isConnected()){
                  try{
                      client.connect(connectOptions);
                  }catch (MqttException e) {
                      e.printStackTrace();
                  }
              }
              Log.d("","");
          }catch (Exception e){
              e.printStackTrace();
          }
  
      }
  
      private String createJwtRsa(String projectId) throws Exception {
          DateTime now = new DateTime();
          JwtBuilder jwtBuilder =
                  Jwts.builder().setIssuedAt(now.toDate()).setExpiration(now.plusDays(1000000).toDate()).setAudience(projectId);
  
          byte[] keyBytes = readBytes(getAssets().open("rsa_private_pkcs8"));
          PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
          KeyFactory kf = KeyFactory.getInstance("RSA");
  
          PrivateKey k = kf.generatePrivate(spec);
  
          return jwtBuilder.signWith(SignatureAlgorithm.RS256, k).compact();
      }
  </code></pre>
  
  <p>As you can see here I have the IoT service acount added to IAM
  <a href="https://i.stack.imgur.com/uCy5H.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uCy5H.png" alt="enter image description here"></a></p>
  
  <p>Does that mean my keys that I generated with openssl are incorrect?</p>