Go Down

Topic: Multiple Published Topics from ESP8266 (Read 494 times) previous topic - next topic

JraPA86

Sep 19, 2019, 10:22 pm Last Edit: Sep 19, 2019, 10:26 pm by JraPA86
Hello, I have my code nearly done for a project which sends an instance of OneWire Temperature probe to my node red, as well as a DHT11 Temperature/Humidity Reading.

One of my last steps is where I am getting stuck. I am receiving this Error when compiling (See Below).
Basically when I publish two topics (DHTTemperature) and (humidity) it works. But when I add a third: (OWTemp), I receive the following Error:

This third topic I am adding myself. The other two were created in a tutorial with Rui Santos (Randomnerdtutorials).

Any help would be greatly appreciated!! Thanks!

PS
The second line of code is my entire project.


Code: [Select]


In function 'void loop()':
error: no matching function for call to 'PubSubClient::publish(const char [12], float&)'
     client.publish("room/OWTemp", temperatureF);
                                               ^
note: boolean PubSubClient::publish(const char*, const char*)
    boolean publish(const char* topic, const char* payload);
            ^
no known conversion for argument 2 from 'float' to 'const char*'
note: boolean PubSubClient::publish(const char*, const char*, boolean)
    boolean publish(const char* topic, const char* payload, boolean retained);
            ^
boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength);
            ^
boolean PubSubClient::publish(const char*, const uint8_t*, unsigned int, boolean)
    boolean publish(const char* topic, const uint8_t * payload, unsigned int plength, boolean retained);
            ^
candidate expects 4 arguments, 2 provided exit status 1
no matching function for call to 'PubSubClient::publish(const char [12], float&)'





JraPA86

Code: [Select]

//--ESP8266
//--DHT 11 | Status LED | OLED 0.96 SD1306 | OneWire | MQTT |
//--------
//--Inputs
//  D1: GPIO 5 - Connected to SCL of OLED
//  D2: GPIO 4 - Connected to SDA of OLED
//  D3: GPIO 0 - Connected to Temperature Probe
//  D4: GPIO 2 - Connected to DHT11
//  D7: GPIO 13- Connected to On/Off LED
//  D8: GPIO 8 - Connected to Connection LED
//-------
//--MQTT Topics
// room/LED - On/Off Triggers D7 LED to turn on/off
//

/**********---| Libraries |---**********/
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include "DHT.h"
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <OneWire.h>
#include <DallasTemperature.h>

/**********--------| Definitions |---------**********/

////--| OLED |---------
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

////--| DHT |---------
#define DHTTYPE    DHT11

////--| OneWire |---------
const int oneWireBus = 0;     //Connected to D3 so GPIO 0
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

////--| Wifi Login |---------
const char* ssid = "***";
const char* password = "****";

////--| MQTT IP Address - Raspberry Pi IP Address |---------
const char* mqtt_server = "192.168.1.***";


////--| Names itself espClient |---------
//-----Can be changed uniquely if multipe ESPs--////
WiFiClient espClient;
PubSubClient client(espClient);

const int DHTPin = 2;      // DHT Sensor
const int statusLED = 15; //Connection LED
const int LED = 13;        //LED

////--| Initialize DHT |---------
DHT dht(DHTPin, DHTTYPE);

////--| Timer/Auxilar Var |---------
long now = millis();
long lastMeasure = 0;

////--| Connects ESP8266 to router |---------
void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected - ESP IP address: ");
  Serial.println(WiFi.localIP());
}

//-----/********|--MQTT RECEIVER--|**********/-----//
////--| MQTT Action from Message on a Subscription |---------

void callback(String topic, byte* message, unsigned int length) {
  Serial.print("Message arrived on topic: ");
  Serial.print(topic);
  Serial.print(". Message: ");
  String messageTemp;
  
  for (int i = 0; i < length; i++) {
    Serial.print((char)message[i]);
    messageTemp += (char)message[i];
  }
  Serial.println();



// MQTT- LED Receiver with Action
//---------------------------------------------------------------------
// If a message is received on topic "room/LED" it checks if it says
// "on" or "off", this then turns the LED GPIO according to the message.
  if(topic=="room/LED"){
      Serial.print("Changing Room LED to ");
      if(messageTemp == "on"){
        digitalWrite(LED, HIGH);
        Serial.print("On");
      }
      else if(messageTemp == "off"){
        digitalWrite(LED, LOW);
        Serial.print("Off");
      }
  }
  Serial.println();
}

//ESP to MQTT Reconnection and Subscription
//---------------------------------------------------------------------
// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    /*
     YOU MIGHT NEED TO CHANGE THIS LINE, IF YOU'RE HAVING PROBLEMS WITH MQTT MULTIPLE CONNECTIONS
     To change the ESP device ID, you will have to give a new name to the ESP8266.
     Here's how it looks:
       if (client.connect("ESP8266Client")) {
     You can do it like this:
       if (client.connect("ESP1_Office")) {
     Then, for the other ESP:
       if (client.connect("ESP2_Garage")) {
      That should solve your MQTT multiple connections problem
    */
    if (client.connect("ESP8266Client")) {
      Serial.println("connected to MQTT");  

  ////--| ESP Subscribe to topic room/LED |---------  
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs)
      client.subscribe("room/LED");
      Serial.println("Subscribed to topic: room/LED");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

//-----/********|--VOID SETUP--|**********/-----//
void setup() {

//OLED Setup
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
}

  delay(5000);
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  
  pinMode(LED, OUTPUT);
  pinMode(statusLED, OUTPUT);
  
  digitalWrite(LED, LOW);
  digitalWrite(statusLED, LOW);
  
  dht.begin();
  
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);

////--| DHT Begin |---------
    sensors.begin();

}

//-----/********|--VOID LOOP--|**********/-----//
void loop() {

  if (!client.connected()) {
    digitalWrite(statusLED, HIGH);
    reconnect();
  }
  if(!client.loop())
    client.connect("ESP8266Client");

  now = millis();

////--| Read Sensors every 30 seconds (they are 2 sec delayed) |---------
  if (now - lastMeasure > 5000) { //30 seconds
    lastMeasure = now;
    
    /**********--------| DHT Values |---------**********/
    float h = dht.readHumidity();
    // Read temperature as Celsius (the default)
    float t = dht.readTemperature();
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);

/**********--------| OneWire Values |---------**********/
  sensors.requestTemperatures();
  float temperatureC = sensors.getTempCByIndex(0);
  float temperatureF = sensors.getTempFByIndex(0);
  delay(5000);

    // Check if any reads failed and exit early (to try again).
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      return;
    }
                //Computes temperature values in Celsius
               //float hic = dht.computeHeatIndex(t, h, false);
              //static char temperatureTemp[7];
             //dtostrf(hic, 6, 2, temperatureTemp);
    
    ////--| DHT Temperature Value |---------
    float hif = dht.computeHeatIndex(f, h);
    static char temperatureTemp[7];
    dtostrf(hif, 6, 2, temperatureTemp);
    
    ////--| DHT Value |---------
    static char humidityTemp[7];
    dtostrf(h, 6, 2, humidityTemp);


/**********--------|  Print |---------**********/  
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %| DHT-T ");
    //Serial.print(t);
    //Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" ºF| Heat Index: ");
    //Serial.print(hic);
    //Serial.println(" *C ");
    Serial.print(hif);
    Serial.print(" ºF| OWT: ");
      Serial.print(temperatureF);
      Serial.print("  ºF");
    Serial.println("-----");

/**********--------| MQTT Publishing |---------**********/
/**********--------|-----------------|---------**********/
    client.publish("room/DHTemperature", temperatureTemp);
    client.publish("room/humidity", humidityTemp);
    client.publish("room/OWTemp", temperatureF);



/**********--------| Display Print |---------**********/
  display.clearDisplay();
  
////--| Display Temperature on OLED |---------
  display.setTextSize(1);
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(f);
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("F");
  
////--| Display Humidity on OLED |---------
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h);
  display.print(" %");
  
  display.display();

  }
}

pert

temperatureF has the wrong type. It can't be a float. It has to be a string (null terminated char array). You can convert a float to a string using sprintf:
http://www.cplusplus.com/reference/cstdio/sprintf/

Go Up