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!â);
}