2

Please look at this sketch for ESP32. It does nothing but:

  1. connects to WiFi
  2. connects to AWS MQTT
  3. subscribes to the /get/accepted topic
  4. every 5 s publish an empty message to the /get topic to retrieve the shadow file
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
#define ETH_PHY_POWER 12
#include <ETH.h>
#include <SSLClient.h>
#include <PubSubClient.h>

#define MQTT_PACKET_SIZE      4096
#define ID "myid"

WiFiClientSecure networkClient;
PubSubClient pubsub;

char *rootCA;
char *privateKey;
char *certificate;

unsigned long old_millis = 0;

bool readFile(const char *path, char **buffer)
{
  File file = SPIFFS.open(path);

  size_t size = file.size();
  *buffer = (char *) malloc(size + 1);

  char *p = *buffer;
  while(file.available()) *p++ = file.read();
  *p = '\0';
  return true;
}

void callback(char *topic, byte *payload, unsigned int length)
{
  Serial.print("Received ");
  Serial.print(length);
  Serial.print(" bytes @ ");
  Serial.println(topic);
  Serial.println((char *) payload);
}

void setup() 
{
  Serial.begin(115200);
  SPIFFS.begin();
  WiFi.mode(WIFI_STA);
  WiFi.begin("myssid", "mypassword");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.println(".");
    delay(1000);
  }

  Serial.println(WiFi.localIP());

  char filename[64];
  readFile("/AmazonRootCA1.pem", &rootCA);
  networkClient.setCACert(rootCA);

  sprintf(filename, "/%s-cert.pem.crt", ID);
  readFile(filename, &certificate);
  networkClient.setCertificate(certificate);

  sprintf(filename, "/%s-private.pem.key", ID);
  readFile(filename, &privateKey);
  networkClient.setPrivateKey(privateKey); 

  pubsub.setServer("myendpoint-ats.iot.us-east-2.amazonaws.com", 8883);
  pubsub.setBufferSize(MQTT_PACKET_SIZE);
  pubsub.setClient(networkClient);
  pubsub.setCallback(callback);

  old_millis = millis();
}

void loop() 
{
  char topic[64];

  if (pubsub.connected())
  {
    pubsub.loop();

    if (millis() - old_millis > 5000)
    {
      sprintf(topic, "$aws/things/%s/shadow/name/module-1/get", ID);
      pubsub.publish(topic, "");
      Serial.print("Publish to ");
      Serial.println(topic);
      old_millis = millis();
    }
  }
  else
  {
    Serial.print("State ");
    Serial.println(pubsub.state());
    if (pubsub.connect(ID))
    {
      Serial.println("MQTT connected");
      sprintf(topic, "$aws/things/%s/shadow/name/module-1/get/accepted", ID);
      if (pubsub.subscribe(topic)) Serial.print("Subscribed to: ");
      else Serial.print("Error while subscribing to: ");
      Serial.println(topic);
    }
    else
    {
      Serial.print("failed, rc=");
      Serial.println(pubsub.state());
      delay(1000);
    }  
  }
}

Here the output:

.
.
.
09:52:35.752 -> .
09:52:36.745 -> .
09:52:37.738 -> 192.168.1.41
09:52:37.837 -> State -1
09:52:40.716 -> MQTT connected
09:52:40.716 -> Subscribed to: $aws/things/myid/shadow/name/module-1/get/accepted
09:52:42.834 -> Publish to $aws/things/myid/shadow/name/module-1/get
09:52:42.967 -> State -3
09:52:45.945 -> MQTT connected
09:52:45.945 -> Subscribed to: $aws/things/myid/shadow/name/module-1/get/accepted
09:52:47.831 -> Publish to $aws/things/myid/shadow/name/module-1/get
09:52:47.997 -> State -3
09:52:50.975 -> MQTT connected
09:52:50.975 -> Subscribed to: $aws/things/myid/shadow/name/module-1/get/accepted
09:52:52.828 -> Publish to $aws/things/myid/shadow/name/module-1/get
09:52:52.993 -> State -3

Every time it publish the message the MQTT connection is lost. Why? What is my mistake?

1 Answer 1

3

Some quick possibilities:

  1. You don't actually have a thing with the name of "myid".
  2. You haven't given this client the rights to the topics you're subscribing to or publishing on.
  3. There's not a named shadow with that name (though that should have got you an empty shadow I'd assume.). Try doing a put (after giving it rights to do so).

Also, does the default shadow work?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.