@@ -21,6 +21,7 @@ aws_iot_mqtt_client myClient; // init iot_mqtt_client
2121char msg[32 ]; // read-write buffer
2222int cnt = 0 ; // loop counts
2323int rc = -100 ; // return value placeholder
24+ bool success_connect = false ; // whether it is connected
2425
2526// Basic callback function that prints out the message
2627void msg_callback (char * src, int len) {
@@ -41,46 +42,55 @@ void setup() {
4142 sprintf (curr_version, " AWS IoT SDK Version(dev) %d.%d.%d-%s\n " , VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
4243 Serial.println (curr_version);
4344 // Set up the client
44- if ((rc = myClient.setup (AWS_IOT_CLIENT_ID)) != 0 ) {
45- Serial.println (" Setup failed!" );
46- Serial.println (rc);
47- }
48- // Load user configuration
49- if ((rc = myClient.config (AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, AWS_IOT_ROOT_CA_PATH, AWS_IOT_PRIVATE_KEY_PATH, AWS_IOT_CERTIFICATE_PATH)) != 0 ) {
50- Serial.println (" Config failed!" );
51- Serial.println (rc);
52- }
53- // Use default connect: 60 sec for keepalive
54- if ((rc = myClient.connect ()) != 0 ) {
55- Serial.println (" Connect failed!" );
56- Serial.println (rc);
45+ if ((rc = myClient.setup (AWS_IOT_CLIENT_ID)) == 0 ) {
46+ // Load user configuration
47+ if ((rc = myClient.config (AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, AWS_IOT_ROOT_CA_PATH, AWS_IOT_PRIVATE_KEY_PATH, AWS_IOT_CERTIFICATE_PATH)) == 0 ) {
48+ // Use default connect: 60 sec for keepalive
49+ if ((rc = myClient.connect ()) == 0 ) {
50+ success_connect = true ;
51+ // Subscribe to "topic1"
52+ if ((rc = myClient.subscribe (" topic1" , 1 , msg_callback)) != 0 ) {
53+ Serial.println (" Subscribe failed!" );
54+ Serial.println (rc);
55+ }
56+ }
57+ else {
58+ Serial.println (" Connect failed!" );
59+ Serial.println (rc);
60+ }
61+ }
62+ else {
63+ Serial.println (" Config failed!" );
64+ Serial.println (rc);
65+ }
5766 }
58- // Subscribe to "topic1"
59- if ((rc = myClient.subscribe (" topic1" , 1 , msg_callback)) != 0 ) {
60- Serial.println (" Subscribe failed!" );
67+ else {
68+ Serial.println (" Setup failed!" );
6169 Serial.println (rc);
6270 }
6371 // Delay to make sure SUBACK is received, delay time could vary according to the server
6472 delay (2000 );
6573}
6674
6775void loop () {
68- // Generate a new message in each loop and publish to "topic1"
69- sprintf (msg, " new message %d" , cnt);
70- if ((rc = myClient.publish (" topic1" , msg, strlen (msg), 1 , false )) != 0 ) {
71- Serial.println (" Publish failed!" );
72- Serial.println (rc);
73- }
76+ if (success_connect) {
77+ // Generate a new message in each loop and publish to "topic1"
78+ sprintf (msg, " new message %d" , cnt);
79+ if ((rc = myClient.publish (" topic1" , msg, strlen (msg), 1 , false )) != 0 ) {
80+ Serial.println (" Publish failed!" );
81+ Serial.println (rc);
82+ }
7483
75- // Get a chance to run a callback
76- if ((rc = myClient.yield ()) != 0 ) {
77- Serial.println (" Yield failed!" );
78- Serial.println (rc);
79- }
84+ // Get a chance to run a callback
85+ if ((rc = myClient.yield ()) != 0 ) {
86+ Serial.println (" Yield failed!" );
87+ Serial.println (rc);
88+ }
8089
81- // Done with the current loop
82- sprintf (msg, " loop %d done" , cnt++);
83- Serial.println (msg);
90+ // Done with the current loop
91+ sprintf (msg, " loop %d done" , cnt++);
92+ Serial.println (msg);
8493
85- delay (1000 );
94+ delay (1000 );
95+ }
8696}
0 commit comments