Moin,
ich brauche mal einen Tipp, da das Programmieren absolut nicht meins ist.
Für das Logging meiner Heizung möchte ich gerne 10 Temperatursensoren mit einem ESP32 auslesen, dass funktioniert momentan soweit auch sehr gut, der Code ist untenstehend und das Ergebnis im seriellen Monitor ist auch alles korrekt.
Die Werte würde ich gerne alle 30 Sekunden an den ioBroker via MQTT senden und brauche da mal Hilfe.
Wenn ich die Variable "TempC" ohne "" schreibe, dann wirft er mir den folgenden Fehler - ich möchte natürlich aber den Wert von TempC haben und nicht den Text TempC übermitteln. Zudem würde ich gerne je Temperatursensor (1-10) ein Topic haben, sodass die Werte alle untereinander stehen.
Ich bekomme mit dem untenstehenden Code die folgende Fehlermeldung:
no matching function for call to 'PubSubClient::publish(const char*&, float&)'
Hier mein Quellcode:
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged TO GPIO 4
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// Number of temperature devices found
int numberOfDevices;
// We'll use this variable to store a found device address
DeviceAddress tempDeviceAddress;
// WiFi
const char *ssid = "Moes Taverne"; // Enter your WiFi name
const char *password = "Sageicheuchnicht"; // Enter WiFi password
// MQTT Broker
const char *mqtt_broker = "192.168.178.5";
const char *topic = "Heizung_OG";
const char *mqtt_username = "";
const char *mqtt_password = "";
const int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Set software serial baud to 115200;
Serial.begin(115200);
// connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
//connecting to a mqtt broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public mqtt broker\n", client_id.c_str());
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public emqx mqtt broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
// publish and subscribe
client.publish(topic, "Hi EMQ X I'm ESP32 ^^");
client.subscribe(topic);
// Start up the library
sensors.begin();
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void loop() {
client.loop();
sensors.requestTemperatures(); // Send the command to get temperatures
//client.publish(topic, "Hi EMQ X I'm ESP32 ^^");
//client.subscribe(topic);
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)){
// Output the device ID
Serial.print("Temperature for device: ");
Serial.print(i,DEC);
// Print the data
float tempC = sensors.getTempC(tempDeviceAddress);
Serial.print(" Temp C: ");
Serial.println(tempC);
//Serial.print(" Temp F: ");
//Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
client.publish(topic, "tempC"); //<<<<<-------- Wenn ich hier die "" herausnehme, dann kommt der untenstehende Fehler.
client.subscribe(topic);
}
}
delay(5000);
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++){
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
Hat da jemand einen guten Tipp?