Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

publish String in Adafruit IO
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

publish String in Adafruit IO

by weal on Wed Dec 23, 2020 3:55 am

I want to Publish string but I got an error, Could you help me?
no matching function for call to 'Adafruit_MQTT_Publish::publish(String&)'

weal
 
Posts: 66
Joined: Sun Sep 29, 2019 12:30 am

Re: publish String in Adafruit IO

by brubell on Wed Dec 23, 2020 11:43 am

Could you post your entire code below?

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: publish String in Adafruit IO

by weal on Wed Dec 23, 2020 1:48 pm

Code: Select all | TOGGLE FULL SIZE
/***************************************************

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

bool enableHeater = false;
uint8_t loopCnt = 0;

Adafruit_SHT31 sht31 = Adafruit_SHT31();
const int AirValue = 520;   //you need to replace this value with Value_1
const int WaterValue = 260;  //you need to replace this value with Value_2
int intervals = (AirValue - WaterValue)/3;
int soilMoistureValue = 0;
/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "************"
#define WLAN_PASS       "*****************"

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

#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   // use 8883 for SSL
#define AIO_USERNAME    "************"
#define AIO_KEY         "*********************"

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

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiClientSecure for SSL
//WiFiClientSecure 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 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
Adafruit_MQTT_Publish Sht31_temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Sht31_temp");
Adafruit_MQTT_Publish Sht31_hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Sht31_hum");
Adafruit_MQTT_Publish Moisture_state = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/Moisture_state");
// Setup a feed called 'onoff' for subscribing to changes.
//Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");

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

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
  Serial.begin(9600);
  delay(10);
 sht31.begin(0x44);
  Serial.println(F("Adafruit MQTT demo"));
sht31.isHeaterEnabled();
  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());


}

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
  // try to spend your time here

 



  // ping the server to keep the mqtt connection alive
  // NOT required if you are publishing once every KEEPALIVE seconds
  /*
  if(! mqtt.ping()) {
    mqtt.disconnect();
  }
  */

 float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.print(t); Serial.print("\t\t");
  } else {
    Serial.println("Failed to read temperature");
  }
 
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
  } else {
    Serial.println("Failed to read humidity");
  }

//*******************************************************************
String A;
soilMoistureValue = analogRead(A0);  //put Sensor insert into soil

if(soilMoistureValue > WaterValue && soilMoistureValue < (WaterValue + intervals))
{
  Serial.println("Very Wet");
  A="Very Wet";
Moisture_state.publish(A);
}
else if(soilMoistureValue > (WaterValue + intervals) && soilMoistureValue < (AirValue - intervals))
{
  Serial.println("Wet");
  A="Wet";
  Moisture_state.publish(A);
}
else if(soilMoistureValue < AirValue && soilMoistureValue > (AirValue - intervals))
{
  Serial.println("Dry");
  A="Dry";
  Moisture_state.publish(A);
}
delay(100);




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

Sht31_hum.publish(h);
Sht31_temp.publish(t);

}
 
// 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;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}


}

weal
 
Posts: 66
Joined: Sun Sep 29, 2019 12:30 am

Re: publish String in Adafruit IO

by brubell on Thu Dec 24, 2020 10:16 am

The Adafruit MQTT library expects a payload in the following format:
Code: Select all | TOGGLE FULL SIZE
  bool publish(const char *topic, const char *payload, uint8_t qos = 0);


"A" is a string, you'll need to convert it to match the expected const char *payload variable.

brubell
 
Posts: 1381
Joined: Fri Jul 17, 2015 10:33 pm

Re: publish String in Adafruit IO

by weal on Fri Dec 25, 2020 10:37 pm

I still getting an error:
no matching function for call to 'Adafruit_MQTT_Publish::publish(String&)'

weal
 
Posts: 66
Joined: Sun Sep 29, 2019 12:30 am

Re: publish String in Adafruit IO

by weal on Mon Dec 28, 2020 11:01 am

bool publish(const char *topic, const char *payload, uint8_t qos = 0);

Where/how to use this code?

weal
 
Posts: 66
Joined: Sun Sep 29, 2019 12:30 am

Please be positive and constructive with your questions and comments.