Arduino Yun Sketch for IO.Adafruit connection

I am using the following Adafruit sketch written by Tony DiCola to connect my Arduino Yun to the Adafruit IO site using MQTT. I am using a DHT11 sensor to collect temperature and humidity and am trying to transmit this data to Adafruit IO. Using anothet test sketch I can see the sensor is collecting the data and the sketch is printing it in the Serial Monitor(Console) so I know the sensor is operating correctly and the information I am trying to transmit is available to send.

The original DiCola sketch only included code to publish the temperature readings although it set up two streams for the IO connection - Temperature and humidity.
I have attempted to write some additional code to transmit the humidity shown in the following sketch after the lines
// Publish Humidity-LRV Code!
// Get humidity event and print its value.

When I compile this sketch I get the following error
Arduino: 1.8.5 (Windows 10), Board: “Arduino YĂșn” ^

call of overloaded ‘publish(int&)’ is ambiguous

and the following line is highlighted
if (! humidity.publish(humidityValue)) {

Hope someone can give me some suggestions as to why I am experiencing that error and also maybe suggest code that will publish the humidity and temperature readings to the IO.

/***************************************************

Adafruit MQTT Library Arduino Yun Example

Make sure your Arduino Yun is connected to a WiFi access point which
has internet access. Also note this sketch uses the Console class
for debug output so make sure to connect to the Yun over WiFi and
open the serial monitor to see the console output.

Works great with the Arduino Yun:
----> https://www.adafruit.com/products/1498

Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!

Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include <Bridge.h>
#include <Console.h>
#include <BridgeClient.h>
#include “Adafruit_MQTT.h”
#include “Adafruit_MQTT_Client.h”
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER “io.adafruit.com”
#define AIO_SERVERPORT 1883
#define AIO_USERNAME “XXXX”
#define AIO_KEY “XXXXXX”

//Humidity & Temperature
#define DHTPIN 2 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT11 // DHT 11

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

/************ Global State (you don’t need to change this!) ******************/

// Create a BridgeClient instance to communicate using the Yun’s bridge & Linux OS.
BridgeClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called ‘temperature’ and ‘humidity’ for publishing.
// Notice MQTT paths for AIO follow the form: /feeds/
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME “/feeds/photocell”);

Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME “/feeds/temperature”);
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME “/feeds/humidity”);

// Setup a feed called ‘onoff’ for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME “/feeds/onoff”);

/*************************** Sketch Code ************************************/

void setup() {
Bridge.begin();
Console.begin();
Console.println(F(“Adafruit MQTT demo”));
/**
// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoffbutton);
**/
}

//uint32_t x=0;

void loop() {

// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();

// this is our ‘wait for incoming subscription packets’ busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(1000))) {
if (subscription == &onoffbutton) {
Console.print(F("Got: "));
Console.println((char *)onoffbutton.lastread);
}
}

// Delay between measurements.
delay(delayMS);

// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
/**if (isnan(event.temperature)) {
Console.println(“Error reading temperature!”);
}
else {
Console.print(“Temperature-Celsius: “);
Console.print(event.temperature);
Console.println(” *C”);
}
/
// Get humidity event and print its value.
// dht.humidity().getEvent(&event);
/
if (isnan(event.relative_humidity)) {
Console.println(“Error reading humidity!”);
}
else **/ {
Console.print(“Humidity: “);
Console.print(event.relative_humidity);
Console.println(”%”);
}

// Now we can publish stuff!
int temperatureValue = event.temperature;
Console.print(F("\nSending temperature val “));
Console.print(temperatureValue);
Console.print(”
");
if (! temperature.publish(event.temperature)) {
Console.println(F(“Failed”));
} else
{
Console.println(F(“OK!”));
}
// Publish Humidity-LRV Code!
// Get humidity event and print its value.
int humidityValue = event.relative_humidity;
Console.print(F("\nSending humidity val “));
Console.print(humidityValue);
Console.print(”
");
if (! humidity.publish(humidityValue)) {
Console.println(F(“Failed”));
} else
{
Console.println(F(“OK!”));
}

// ping the server to keep the mqtt connection alive
if(! mqtt.ping()) {
Console.println(F(“MQTT Ping failed.”));
}

delay(5000);

}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;

// Stop if already connected.
if (mqtt.connected()) {
return;
}

Console.print("Connecting to MQTT
 ");

while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Console.println(mqtt.connectErrorString(ret));
Console.println(“Retrying MQTT connection in 5 seconds
”);
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Console.println(“MQTT Connected!”);
}

It is not liking something about how this line is being called
if (! humidity.publish(humidityValue)) {
Having a look at the previous example you might find that the following will work.
if (! humidity.publish(event.relative_humidity)){
Overloaded functions are the same function but with a different type of input so it may be important for it to be called from the event object.

On further investigation I realised I had wriiten the code to publish the humidity incorrectly so have adjusted it as follows. The problem now is however that I am getting an unable to connect message from the MQTT connection and have traced this to the text in the following that is between the /** and**/. If I comment these lines out as shown I can connect so obviously I have asked the sketch to do something the MQTT server doesn’t like. Any suggestions?

// Publish Humidity-LRV Code!
// Get humidity event and print its value.
int humidityValue = event.relative_humidity;
Console.print(F("\nSending humidity val “));
Console.print(humidityValue);
Console.print(”
");
/** if (! humidity.publish(event.relative_humidity)) {
Console.println(F(“Failed”));
} else **/
{
Console.println(F(“OK!”));
}

Thanks Clinton, I had not seen this post before I posted again so thank you. I had actually realised I had the code wrong and had corrected that offending line only to know get a new problem with the MQTT connection as described in my reply.
Cheers

Id this block here still commented out
/**if (isnan(event.temperature)) {
Console.println(“Error reading temperature!”);
}
else {
Console.print(“Temperature-Celsius: “);
Console.print(event.temperature);
Console.println(” *C”);
}
/
// Get humidity event and print its value.
// dht.humidity().getEvent(&event);
/ if (isnan(event.relative_humidity)) {
Console.println(“Error reading humidity!”);
}
else **/
you may need to uncomment this line to initialise the Humidity event.
dht.humidity().getEvent(&event);

If I uncomment that block then I get the following in the monitor window

Retrying MQTT connection in 5 seconds

connected!
connected!
connected!
Not authorized to connect
Not authorized to connect
Retrying MQTT connection in 5 seconds

Retrying MQTT connection in 5 seconds

Not authorized to connect
Not authorized to connect
Retrying MQTT connection in 5 seconds

Retrying MQTT connection in 5 seconds

Not authorized to connect
Not authorized to connect
Retrying MQTT connection in 5 seconds

Retrying MQTT connection in 5 seconds

Not authorized to connect
Not authorized to connect
Retrying MQTT connection in 5 seconds

Retrying MQTT connection in 5 seconds


If I comment it out I get a connection and the following in the window

connected!
connected!
Temperature-Celsius: 23.00 *C
Humidity: 63.00%

Sending temperature val 63
OK!

Sending humidity val 63
OK!
Temperature-Celsius: 23.00 *C
Humidity: 63.00%

Sending temperature val 63
OK!

Sending humidity val 63
OK!
connected!
Temperature-Celsius: 24.00 *C
Temperature-Celsius: 24.00 *C
Humidity: 63.00%
Humidity: 63.00%

Sending temperature val 63
OK!
Sending temperature val 63
OK!

As you can see it is measuring the temp at 24d but sending it as 63 which is actually the humidity.

Very weird. I think the section commented out needs to be available to send the correct data but that piece of code is causing my connection to MQTT to fail - and I dont know why!!:slight_smile:

Thank you for spending some time on this Clinton much appreciated. I have had a look at the IO connection data on the IO site and when it rejects the connection I get no logged comment about that rejection.

Hi Leslie,

I suggest you try creating the variable “int temperatureValue = event.temperature;” right after you take the readings and then use the variable throughout the rest of the sketch. It seems like once you read the humidity it makes that the value of tall the event.xxxx callouts for some reason.

On a related note, it’s really hard to understand your code with no formatting. Please select all the code you paste into the forum and use the “preformatted text option” </>. Please consider removing the large comment blocks or putting a // in front of every line just so we can tell.

I resolved my problem when I determined there was nothing wrong with the code I was using and suspected a problem with the Include files. I reinstalled the Adafruit MQTT, Adafruit_Sensor and the DHT files. The connection then worked and the correct data was being transmitted to IO.Adafruit.
Thank you for the help

3 Likes