title: AWS iot with esp8266
body: <p>Please help me connect with AWS IOT services with esp8266 12e.
  I am using aws-mqtt-websocket example but I an not able to connect with AWS IOT. 
  The code is as follows and the error I get is: <code>please start sntp first ! error connection to the websocket server</code>.</p>
  
  <pre><code>#include &lt;Arduino.h&gt;
  #include &lt;Stream.h&gt;
  
  #include &lt;ESP8266WiFi.h&gt;
  
  //AWS
  #include "sha256.h"
  #include "Utils.h"
  #include "AWSClient2.h"
  
  //WEBSockets
  #include &lt;Hash.h&gt;
  #include &lt;WebSocketsClient.h&gt;
  
  //MQTT PAHO
  #include &lt;SPI.h&gt;
  #include &lt;IPStack.h&gt;
  #include &lt;Countdown.h&gt;
  #include &lt;MQTTClient.h&gt;
  
  //AWS MQTT Websocket
  #include "Client.h"
  #include "AWSWebSocketClient.h"
  #include "CircularByteBuffer.h"
  
  //AWS IOT config, change these:
  
  const char* ssid = "XXXX";
  const char* password = "XXXXXXXX";
  char aws_endpoint[] = "XXXXXXXXXXXXXXXXXXXXXXX";
  char aws_key[] = "XXXXXXXXXXXXXXXXXX";
  char aws_secret[] = "XXXXXXXXXXXXXXXXXXXX";
  char aws_region[] = "XXXXXXXXXXXXXXXX";
  const char* aws_topic = "XXXXXXXXXXXXXXXXXX";
  int port = 443;
  
  //MQTT config
  const int maxMQTTpackageSize = 512;
  const int maxMQTTMessageHandlers = 1;
  
  WiFiClientSecure espclient;
  
  AWSWebSocketClient awsWSclient(1000);
  
  IPStack ipstack(awsWSclient);
  
  MQTT::Client&lt;IPStack, Countdown, maxMQTTpackageSize, maxMQTTMessageHandlers&gt; *client = NULL;
  
  //# of connections
  long connection = 0;
  
  //generate random mqtt clientID
  char* generateClientID() {
      char* cID = new char[23]();
      for (int i = 0; i&lt;22; i += 1)
          cID[i] = (char)random(1, 256);
      return cID;
  }
  
  //count messages arrived
  int arrivedcount = 0;
  
  
  //callback to handle mqtt messages
  void messageArrived(MQTT::MessageData&amp; md)
  {
      MQTT::Message &amp;message = md.message;
  
      Serial.print("Message ");
      Serial.print(++arrivedcount);
      Serial.print(" arrived: qos ");
      Serial.print(message.qos);
      Serial.print(", retained ");
      Serial.print(message.retained);
      Serial.print(", dup ");
      Serial.print(message.dup);
      Serial.print(", packetid ");
      Serial.println(message.id);
      Serial.print("Payload ");
      char* msg = new char[message.payloadlen + 1]();
      memcpy(msg, message.payload, message.payloadlen);
      Serial.println(msg);
      delete msg;
  }
  
  //connects to websocket layer and mqtt layer
  bool connect() {
  
      if (client == NULL) {
          client = new MQTT::Client&lt;IPStack, Countdown, maxMQTTpackageSize, maxMQTTMessageHandlers&gt;(ipstack);
      }
      else {
  
          if (client-&gt;isConnected()) {
              client-&gt;disconnect();
          }
          delete client;
          client = new MQTT::Client&lt;IPStack, Countdown, maxMQTTpackageSize, maxMQTTMessageHandlers&gt;(ipstack);
      }
  
  
      //delay is not necessary... it just help us to get a "trustful" heap space value
      delay(1000);
      Serial.print(millis());
      Serial.print(" - conn: ");
      Serial.print(++connection);
      Serial.print(" - (");
      Serial.print(ESP.getFreeHeap());
      Serial.println(")");
  
  
  
  
      int rc = ipstack.connect(aws_endpoint, port);
      Serial.print(rc);
  
      if (rc != 1)
      {
          Serial.println("error connection to the websocket server");
          return false;
      }
      else {
          Serial.println("websocket layer connected");
      }
  
  
      Serial.println("MQTT connecting");
      MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
      data.MQTTVersion = 3;
      char* clientID = generateClientID();
      data.clientID.cstring = clientID;
      rc = client-&gt;connect(data);
      delete[] clientID;
      if (rc != 0)
      {
          Serial.print("error connection to MQTT server");
          Serial.println(rc);
          return false;
      }
      Serial.println("MQTT connected");
      return true;
  }
  
  //subscribe to a mqtt topic
  void subscribe() {
      //subscript to a topic
      int rc = client-&gt;subscribe(aws_topic, MQTT::QOS0, messageArrived);
      Serial.println(rc);
      if (rc != 0) {
          Serial.print("rc from MQTT subscribe is ");
          Serial.println(rc);
          return;
      }
      Serial.println("MQTT subscribed");
  }
  
  //send a message to a mqtt topic
  void sendmessage() {
      //send a message
      MQTT::Message message;
      char buf[100];
      strcpy(buf, "{\"state\":{\"reported\":{\"on\": false}, \"desired\":{\"on\": false}}}");
      message.qos = MQTT::QOS0;
      message.retained = false;
      message.dup = false;
      message.payload = (void*)buf;
      message.payloadlen = strlen(buf) + 1;
      int rc = client-&gt;publish(aws_topic, message);
  }
  
  void setup_wifi() {
  
      delay(10);
      // We start by connecting to a WiFi network
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
  
      WiFi.begin(ssid, password);
  
      while (WiFi.status() != WL_CONNECTED) {
          delay(500);
          Serial.print(".");
      }
  
      Serial.println("");
      Serial.println("WiFi connected");
      Serial.println("IP address: ");
      Serial.println(WiFi.localIP());
  }
  
  void setup() {
      Serial.begin(115200);
      setup_wifi();
  
      Serial.setDebugOutput(1);
      //fill AWS parameters    
      awsWSclient.setAWSRegion(aws_region);
      awsWSclient.setAWSDomain(aws_endpoint);
      awsWSclient.setAWSKeyID(aws_key);
      awsWSclient.setAWSSecretKey(aws_secret);
      awsWSclient.setUseSSL(true);
  
      if (connect()) {
          subscribe();
          sendmessage();
      }
  }
  
  void loop() {
      //keep the mqtt up and running
      if (awsWSclient.connected()) {
          client-&gt;yield();
      }
      else {
          //handle reconnection
          if (connect()) {
              subscribe();
          }
      }
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/vyD8x.jpg" alt="enter image description here"></p>