Hallo,
ich versuche mich gerade an einigen Sensorprojekten. Einige laufen und mit dem aktuellen habe ich seltsame Probleme. Wenn ich den Topic ändere, wird ein zufälliger Wert der zu übergebenden Werte geschrieben. Aber immer nur einmal. Der Wemos stellt zwar eine Verbindung her, aber die Sensordaten werden nicht geschrieben. Was mache ich verkehrt?
Die Konsole bei Arduino zeigt:
WiFi connected
IP address:
192.168.xxx.xxx
connected
035.50
25.44
der Code ist:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Streaming.h>
#include <WiFiUdp.h>
// #define SLEEP_DELAY_IN_SECONDS 300
#define ONE_WIRE_BUS D4 // DS18B20 pin
const int sleepSeconds = 200;
ADC_MODE(ADC_VCC);
String Ubatt = "";
const int echoPin2 = D0;
const char* ssid = "xxx"; //ausge-x-t
const char* password = "xxx"; //ausge-x-t
const char* mqtt_server = "192.168.xxx.xxx"; //ausge-x-t
const char* SensorName = "ESP-Pool-Temp";
WiFiClient espClient;
PubSubClient client(espClient);
// WiFiUDP ntpUDP;
// NTPClient timeClient(ntpUDP);
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float tempSensor1, tempSensor2;
uint8_t sensor1[8] = { 0x28, xxxx, ... }; //ausge-x-t
uint8_t sensor2[8] = { 0x28, xxxx, ... }; //ausge-x-t
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
// Attempt to connect
if (client.connect(SensorName)) {
Serial.println("connected");
// client.subscribe("OsoyooCommand");
Serial.print(client.state());
} else {
Serial.print("failed, rc=");
// Serial.println(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
// setup serial port
pinMode(echoPin2, INPUT);
Serial.begin(115200);
pinMode(D0, WAKEUP_PULLUP);
// setup WiFi
setup_wifi();
client.setServer(mqtt_server, 1883);
// setup OneWire bus
DS18B20.begin();
DS18B20.requestTemperatures();
delay(500);
tempSensor1 = DS18B20.getTempC(sensor1); // Gets the values of the temperature
tempSensor2 = DS18B20.getTempC(sensor2); // Gets the values of the temperature
uint16_t my_getVcc_Voltage = ESP.getVcc();
float_t my_Voltage_calculated = ((float)my_getVcc_Voltage / 1024.0f);
Ubatt = String(my_Voltage_calculated, 3);
delay(500);
WiFi.begin(ssid, password);
delay(1000);
if (!client.connected()) {
reconnect();
}
String ip;
ip = WiFi.localIP().toString();
// client.subscribe("OsoyooCommand");
delay(200);
Serial.println(tempSensor1);
client.publish("ESP_PoolSensor/Temperatur01", String(tempSensor1).c_str(),true);
delay(200);
Serial.println(tempSensor2);
client.publish("ESP_PoolSensor/Temperatur02", String(tempSensor2).c_str(),true);
delay(200);
client.publish("ESP_PoolSensor/Power", String(Ubatt).c_str(),true);
delay(200);
client.publish("ESP_PoolSensor/IP", String(ip).c_str(),true);
delay(200);
ESP.deepSleep(sleepSeconds * 300000);
delay(100);
}
void loop() {
}
Kann mir bitte jemand helfen? Sitze seit ner Weile an dem Problem.
Wieso fängst du so kompliziert an? Belasse es doch erstmal bei “topic” und “payload”. https://pubsubclient.knolleary.net/api.html#publish1
Ich wandele die Daten mit dtostrf um, funktioniert wunderbar. Wie das mit Strings geht, keine Ahnung.
Hier meine letzte Anwendung:
if (!MQTTClient.connected()) {
MQTTClient.setServer(MQTTBroker, 1883);
int retries = 0;
while (!MQTTClient.connected() && retries < 3) {
MQTTClient.connect("BSB-LAN", MQTTUser, MQTTPass);
retries++;
if (!MQTTClient.connected()) {
delay(1000);
DebugOutput.println(F("Failed to connect to MQTT broker, retrying..."));
}
MQTTClient.publish("AkkuSpannung",dtostrf(akkuSpg, 6, 1, tempBuffer));
MQTTClient.disconnect();
}
}
}
Warum baust du die WLAN Verbindung 2 Mal auf? Vorallem einmal bevor du den MQTT Client startest und danach noch einmal. Ich weiß nicht ob das einen Einfluss auf den Client hat.
Als Nächstes würde ich erstmal nur einen einzelnen festen Wert übertragen und wenn das funktioniert erst weitergehen.
Zum Beispiel:
Lass die die ganzen delay mal weg, die bremsen dich nicht nur aus sondern könnten vielleicht auch Antworten deines Brokers verschlucken.
Außerdem nach der Übertragung die Verbindung wieder trennen mit
client.disconnect();
Es gibt auch in der Library auch eine Diagnose Funktion, hab das aber noch nie genutzt.
int state ()
Returns the current state of the client. If a connection attempt fails, this can be used to get more information about the failure.
Returns
int - the client state, which can take the following values (constants defined in PubSubClient.h):
-4 : MQTT_CONNECTION_TIMEOUT - the server didn't respond within the keepalive time
-3 : MQTT_CONNECTION_LOST - the network connection was broken
-2 : MQTT_CONNECT_FAILED - the network connection failed
-1 : MQTT_DISCONNECTED - the client is disconnected cleanly
0 : MQTT_CONNECTED - the client is connected
1 : MQTT_CONNECT_BAD_PROTOCOL - the server doesn't support the requested version of MQTT
2 : MQTT_CONNECT_BAD_CLIENT_ID - the server rejected the client identifier
3 : MQTT_CONNECT_UNAVAILABLE - the server was unable to accept the connection
4 : MQTT_CONNECT_BAD_CREDENTIALS - the username/password were rejected
5 : MQTT_CONNECT_UNAUTHORIZED - the client was not authorized to connect
Gonmyr:
hab alles umgeschrieben und es laut Funktion so deklariert:
char tempBuffer[20];
Das ist ein bisschen wenig, wenn ich auf unter 30 gehe fängt er schon an "Zufallswerte" zu senden, mit 50 funktioniert es sauber. Ich habe ihn auf 100 stehen da eh genug Speicher übrig war.