Hi, ich bin gerade am verzweifeln am versuchen meine ausgelesenen Daten via MQTT zu senden. Hier mein Code:
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <stdio.h>
#include <stdbool.h> // Header-file for boolean data-type.
#include "DHT.h"
WiFiClient espClient;
PubSubClient client(espClient);
char* clientId = "esp8266.bedroom.door";
bool closed;
const char* wifi_ssid = "Link2.4";
const char* wifi_password = "89686914456002748185";
const char* mqtt_server = "192.168.178.65";
const int mqtt_port = 1883;
const char* mqtt_user = "hass";
const char* mqtt_password = "2mhbff";
// variables for measured values
float temperature;
float humidity;
int raw=0;
float volt=0.0;
int x = 0;
#define DHTPIN 12 // pin of the arduino where the sensor is connected to
#define DHTTYPE DHT11 // define the type of sensor (DHT11 or DHT22)
// create instance of DHT
DHT dht(DHTPIN, DHTTYPE, 6);
#define D2 4 //use GPIO 4 for door sensor
void connectToMQTT() {
client.setServer(mqtt_server, mqtt_port);//MQTT Server, - Port
if (client.connect(clientId , mqtt_user, mqtt_password)) {
Serial.println("connected");
}
}
void setup() {
// Pin A0 Input for battery monitoring
pinMode(A0, INPUT);
// initialize measuring
pinMode(DHTPIN, INPUT);
dht.begin();
// Pin D2 INPUT for door sensor
pinMode(D2, INPUT);
// WIFI-Verbindung aufbauen
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void loop() {
connectToMQTT();
temperature = dht.readTemperature();
humidity = dht.readHumidity();
raw = analogRead(A0);
volt=raw/1023.0;
volt=volt*4.2;
if(x == 2000) {
if (!isnan(humidity) || !isnan(temperature)) {
client.publish("/home/bedroom/temperature", String(temperature));
client.publish("/home/bedroom/humidity", String(humidity));
}
if(!isnan(raw) || !isnan(volt)) {
client.publish("/home/bedroom/battery/raw", String(raw));
client.publish("/home/bedroom/battery/volt", String(volt));
}
//client.publish("/home/bedroom/battery/raw", String(raw));
x = 0;
}
if (digitalRead(D2) == HIGH && closed) {
closed = false;
client.publish("/home/bedroom/door", "open");
}
if(digitalRead(D2) == LOW && !closed) {
closed = true;
client.publish("/home/bedroom/door", "closed");
}
x = x + 1;
}
aber ich bekomme:
error: no matching function for call to 'PubSubClient::publish(const char [23], String)'
79 | client.publish("/home/bedroom/humidity", String(humidity));
und zwar auch für "temperature", "raw", und "volt". Ich erstehe es nicht ganz da ich doch unten wo ich den Status der Türe übergebe auch einfach das gleiche mache und das funktioniert.
Entschuldigt wenn ich mich einfach dumm anstelle.