aws / aws-iot-device-sdk-arduino-yun

SDK for connecting to AWS IoT from an Arduino Yún.
Apache License 2.0
163 stars 77 forks source link

Can not figure out what is going on with message I am trying to publish #42

Closed OtisHarrison24 closed 6 years ago

OtisHarrison24 commented 7 years ago

So I am trying to publish 4 ct measures to my topic but it seems to not publish when the title length is too long. Below is my code

#include <aws_iot_mqtt.h>
#include <aws_iot_version.h>
#include "aws_iot_config.h"
#include <Process.h>
#include "EmonLib.h"                   
EnergyMonitor emon1;
EnergyMonitor emon2;
EnergyMonitor emon3;
EnergyMonitor emon4;
aws_iot_mqtt_client myClient; // init iot_mqtt_client
char msg[32]; // read-write buffer
char data[100];
Process date;  
int cnt = 0; // loop counts
int rc = -100; // return value placeholder
bool success_connect = false; // whether it is connected

// Basic callback function that prints out the message
void msg_callback(char* src, unsigned int len, Message_status_t flag) {
  if(flag == STATUS_NORMAL) {
    Serial.println("CALLBACK:");
    int i;
    for(i = 0; i < (int)(len); i++) {
      Serial.print(src[i]);
    }
    Serial.println("");
  }
}

void setup() {
  // Start Serial for print-out and wait until it's ready
  Serial.begin(115200);
  while(!Serial)
  //
  emon1.current(0, 111.1);
  emon2.current(1, 111.1);
  emon3.current(2, 111.1);
  emon4.current(3, 111.1);
  char curr_version[80];
  snprintf_P(curr_version, 80, PSTR("AWS IoT SDK Version(dev) %d.%d.%d-%s\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
  Serial.println(curr_version);
  // Set up the client
  if((rc = myClient.setup(AWS_IOT_CLIENT_ID)) == 0) {
    // Load user configuration
    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) {
      // Use default connect: 60 sec for keepalive
      if((rc = myClient.connect()) == 0) {
        success_connect = true;
        // Subscribe to "topic1"
        if((rc = myClient.subscribe("greenhouse/ct_sensor", 1, msg_callback)) != 0) {
          Serial.println("Subscribe failed!");
          Serial.println(rc);
        }
      }
      else {
        Serial.println(F("Connect failed!"));
        Serial.println(rc);
      }
    }
    else {
      Serial.println(F("Config failed!"));
      Serial.println(rc);
    }
  }
  else {
    Serial.println(F("Setup failed!"));
    Serial.println(rc);
  }
  // Delay to make sure SUBACK is received, delay time could vary according to the server
  delay(2000);
}

void loop() {
  if(success_connect) {

    double Irms = emon1.calcIrms(1480);
    double Irms2 = emon2.calcIrms(1480);
    double Irms3 = emon3.calcIrms(1480);
    //double Irms4 = emon4.calcIrms(1480);
    int IrmsShadow = Irms/4.67;
    int IrmsShadow2 = Irms2/4.67;
    int IrmsShadow3 = Irms2/4.67;
    int IrmsShadow4 = Irms2/4.67;
     //Creating the JSON payload  
    String thing_id = "\"id\": \""  +String(AWS_IOT_MY_THING_NAME) +"\"" ;
    String currentcurrent = ", \"ct_1\": " + String(IrmsShadow) ;
    String currentcurrent2 = ", \"ct_2\": " + String(IrmsShadow2) ;
    String currentcurrent3 = ", \"ct_3\": " + String(IrmsShadow3) ;
    String currentcurrent4 = ", \"ct_4\": " + String(IrmsShadow4) ;
    // Add both value together to send as one string. 
    String value = thing_id + currentcurrent + currentcurrent2 + currentcurrent3 + currentcurrent4;
    Serial.println(value);
    String payload = "{" + value + "}";
    payload.toCharArray(data, (payload.length() + 1));
    Serial.println(strlen(data));

    if((rc = myClient.publish("greenhouse/ct_sensor", data, strlen(data), 1, false)) != 0) {
      Serial.println(F("Publish failed!"));
      Serial.println(rc);
    }

    // Get a chance to run a callback
    if((rc = myClient.yield()) != 0) {
      Serial.println("Yield failed!");
      Serial.println(rc);
    }

    // Done with the current loop
    sprintf_P(msg, PSTR("loop %d done"), cnt++);
    Serial.println(msg);

    delay(1000);
  }
}

By changing id to thing_id it will not publish my message. when i do Serial.println(strlen(data)); it goes from 66 to 0. I have now clue what I'm doing wrong.

liuszeng commented 7 years ago

Hi @OtisHarrison24 ,

Thank you very much for your interest in AWS IoT Arduino Yun SDK.

In the code you are filling the assembled payload string into a buffer with limited size. Can you try to print out the assembled data string? Can you check again the payload string length is within the allowed size of the data buffer?

In the mean time, did you see any compilation warning message about memory consumption when you compile the sketch?

Thanks, Liusu

OtisHarrison24 commented 7 years ago

is the buffer size the "char msg[32]"? I can see the entire message printout but the callback just never shows any published messages. I'm not getting any warnings at all. Thank you so much for all the help. I have 20 other devices working it's just this. I can have up to 3 ct values working greating but adding the last one just stops it. Could changing the char msg[32] help with larger strings?

Thanks

liuszeng commented 7 years ago

Hi @OtisHarrison24 ,

I am more worried about the size of the buffer: data, which you declared as char data[100] here. It is used to store the constructed shadow JSON string. As you keep adding more values into your shadow, the size of the JSON string might hit the overflow line for the buffer.

Can you increase the size and see if the problem still exists?

Thanks, Liusu

vareddy-zz commented 6 years ago

Hi @OtisHarrison24, We haven't heard from you in a long time and hence we are closing this issue. Please open another issue if you have any questions about the Arduino SDK. Thanks! Varun