title: Google IOT Core and Raspberry Pi: Error: Connection Refused: Bad username or password
body: <p>I followed the tutorial below to connect my raspberry pi 3 to the Google IOT Core. I setup the Google Core IOT part OK at the Google console and all steps were followed for the raspberry pi part, but, The connection is always refused as per the error messages below.</p>
  
  <pre><code>error { Error: Connection refused: Bad username or password
  at MqttClient._handleConnack (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/lib/client.js:920:15)
  at MqttClient._handlePacket (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/lib/client.js:350:12)
  at work (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/lib/client.js:292:12)
  at Writable.writable._write (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/lib/client.js:302:5)
  at doWrite (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/node_modules/readable- 
  stream/lib/_stream_writable.js:428:64)
  at writeOrBuffer (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/node_modules/readable- 
  stream/lib/_stream_writable.js:417:5)
  at Writable.write (/home/pi/Desktop/Google-IoT- 
  Device/node_modules/mqtt/node_modules/readable- 
  stream/lib/_stream_writable.js:334:11)
  at TLSSocket.ondata (_stream_readable.js:639:20)
  at emitOne (events.js:116:13)
  at TLSSocket.emit (events.js:211:7) code: 4 }
  close
  </code></pre>
  
  <p>The tutorial link:
  <a href="https://hub.packtpub.com/build-google-cloud-iot-application/#comment-53421" rel="nofollow noreferrer">https://hub.packtpub.com/build-google-cloud-iot-application/#comment-53421</a></p>
  
  <p>This is the top part of my index.js file:</p>
  
  <pre><code>var fs = require('fs'); 
  var jwt = require('jsonwebtoken'); 
  var mqtt = require('mqtt'); 
  var rpiDhtSensor = require('rpi-dht-sensor'); 
  
  var dht = new rpiDhtSensor.DHT11(2); // `2` =&gt; GPIO2 
  
  var projectId = 'nifty-*******-******'; 
  var cloudRegion = 'us-central1'; 
  var registryId = 'device-registry'; 
  var deviceId = 'raspberrypi'; 
  
  var mqttHost = 'mqtt.googleapis.com'; 
  var mqttPort = 8883; 
  var privateKeyFile = '../certs/rsa_private.pem'; 
  var algorithm = 'RS256'; 
  var messageType = 'state'; // or event 
  
  var mqttClientId = 'projects/' + projectId + '/locations/' + cloudRegion + 
  '/registries/' + registryId + '/devices/' + deviceId; 
  var mqttTopic = '/devices/' + deviceId + '/' + messageType; 
  
  var connectionArgs = { 
    host: mqttHost, 
    port: mqttPort, 
    clientId: mqttClientId, 
    username: 'unused', 
    password: createJwt(projectId, privateKeyFile, algorithm), 
    protocol: 'mqtts', 
    secureProtocol: 'TLSv1_2_method' 
  }; 
  </code></pre>
  
  <p>The tutorial doesn't say anything about downloading the Google root CA certificate so I followed this tutorial:
  <a href="https://raspberrypi.stackexchange.com/questions/76419/entrusted-certificates-installation">https://raspberrypi.stackexchange.com/questions/76419/entrusted-certificates-installation</a></p>
  
  <p>I also checked the connection route was OK by following this at Google and everything checked OK:
  <a href="https://cloud.google.com/iot/docs/troubleshooting" rel="nofollow noreferrer">https://cloud.google.com/iot/docs/troubleshooting</a></p>
  
  <p>The projectID, registryID, deviceID, and region all checked correct.</p>
  
  <p>I am sure it must very simple but this has frustrated me for a week now. I have trawled the internet but what ever I tried results in the same error.
  Is there anyone that can help?</p>