Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initializing argument 2 of 'bool MQTT::publish(const char*, const uint8_t*, unsigned int)' [-fpermissive] #33

Closed
rayterrill opened this issue Aug 21, 2016 · 7 comments

Comments

@rayterrill
Copy link

Having a heck of a time trying to use the MQTT library in Particle with integer values coming off of a moisture sensor.

Veryfing code using this library in Particle Build fails with this error:

MQTT/MQTT.h:140:10: error:   initializing argument 2 of 'bool MQTT::publish(const char*, const uint8_t*, unsigned int)' [-fpermissive]
     bool publish(const char *, const uint8_t *, unsigned int)

Here's the code that's giving me the problems:
`  if (client.isConnected()) {
     int sensorValue = 0;  // variable to store the value coming from the sensor

     // read the value from the sensor:
     Serial.println("Reading sensor value...");
     sensorValue = analogRead(sensorPin);

     Serial.println(sensorValue);
     Serial.println("Attempting to publish a reading...");
     client.publish("rayterrill/feeds/moisture",(uint8_t)sensorValue,4);
     //client.subscribe("/inTopic");t

Trying to cast the integer value to uint8_t, but that gives the error above. Switching it to (uint8_t*) clears the issue, but I get blank or garbage data in the Adafruit IO dashboard feed I'm using, depending what I have the plength set to.

Am I just doing something totally wrong here?

@hirotakaster
Copy link
Owner

maybe like following.

// client.publish("rayterrill/feeds/moisture",(uint8_t)sensorValue,4);
client.publish("rayterrill/feeds/moisture",(uint8_t *)&sensorValue, sizeof(sensorValue));

@rayterrill
Copy link
Author

I'm still getting garbage even with that code.

I looked at the Adafruit MQTT library, and they've got a publish declaration that allow for an int value, as in this:
bool Adafruit_MQTT_Publish::publish(int32_t i) { char payload[12]; ltoa(i, payload, 10); return mqtt->publish(topic, payload, qos); }

I incorporated some similar char coercing logic into my publish, and I'm indeed now able to publish data using this libraries char publish declaration. I just can't get it to work at all with the unit8_t declaration.

@hirotakaster
Copy link
Owner

Do you want to use char or int type in publish? If you want to use char, you can do like this.

char publishData[120];
sprintf(publishData, "%d", 10);
client.publish("rayterrill/feeds/moisture", publishData, strlen(publishData));

or If you want to use int type. of course you should get published data with int type.

  client.publish("rayterrill/feeds/moisture",(uint8_t *)&sensorValue, sizeof(sensorValue));

@rayterrill
Copy link
Author

I tried it with:
client.publish("rayterrill/feeds/moisture",(uint8_t *)&sensorValue, sizeof(sensorValue));

Still getting garbage data into Arduino IO. It might just be Arduino IO, not sure.

I wanted to post as int, but char is also working.

@hirotakaster
Copy link
Owner

here is my test code.

// photon side
int sensorValue = 65; // character 'A'
void loop() {
    if (client.isConnected())
        client.loop();
    delay(1000);
    client.publish("sensor/val/", (uint8_t *)&sensorValue, sizeof(sensorValue));
    sensorValue++;
}
// mosquitto side
mosquitto_sub  -h [mosquitt_server_ipaddr] -t "sensor/val/"
A
B
C
D
E
F
...

I check works well on my Photon/mosquitto command. Check your Arduino IO side source code.

@hirotakaster
Copy link
Owner

Hi @rayterrill , can you work on Photon with MQTT? I will close this issue.

@rayterrill
Copy link
Author

Sure. Go ahead and close. Thanks for the help!

On Sun, Aug 28, 2016 at 9:41 PM -0700, "Hirotaka" notifications@github.com wrote:

Hi @rayterrill , can you work on Photon with MQTT? I will close this issue.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants