Hallo,
Warnung vorweg: Dies ist eines der gefürchteten "Crosspostings". Sogar ein internationales. Ich habe die selbe Frage bereits im englischsprachigen Forum gestellt, jedoch noch keine Idee zur Lösung erhalten. Daher wende ich mich jetzt voller Hoffnung an die deutschsprachige Forums-Community.
Folgendes. In meinem unten stehenden Sketch ist einiges vielleicht nicht schön aber er funktioniert für mich ganz prima, bis zu dem Punkt, an dem er - nachdem er im loop-Bereich 10 Wiederholungen gemacht hat - einen Wert via client.publish an den mqtt server schicken soll.
Es werden ganz hervorragend mqtt messages von dem sketch empfangen aber offenbar nicht versendet. Irgendetwas muss das falsch sein.
#include <WiFi.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include <PubSubClient.h>
#include <ArduinoFritzApi.h>
#include <MovingAverageFilter.h>
const char *ssid = "ssid";
const char *password = "dfghdfhg";
const char* fritz_user = "user";
const char* fritz_password = "nghfdgh";
const char* fritz_ip = "192.168.178.1"; // ip or fritz.local
const char* fritz_ain = "116570483787";
/*
The actor identification number (ain) can be fount in the fritzbox
web interface or is found on a label on the device
*/
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 3000; // interval at which to blink (milliseconds)
MovingAverageFilter movingAverageFilter(11);
FritzApi fritz(fritz_user, fritz_password, fritz_ip);
const char* mqtt_server = "192.168.178.44";
const int mqttPort = 1883;
const char* mqttUser = "mosq";
const char* mqttPassword = "44904490";
String temp = ""; //temporär
String zaehler = "0";
String packet = "";
float input = 0;
float output = 0;
float einsparung = 0;
float solar = 0;
float verbrauchkorr = 0;
float verbrauch = 0;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//int loops = 1;
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
reconnect();
// Initialize Fritzbox stuff;
try {
fritz.init();
} catch (int e) {
Serial.println("Could not connect to fritzbox: " + String(e));
}
Serial.println("Fritz connected");
}
void setup_wifi() {
delay(10);
//wifi
WiFi.begin(ssid, password);
Serial.print("Connecting.");
while ( WiFi.status() != WL_CONNECTED ) {
delay(500);
Serial.print(".");
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
}
void callback(char* topic, byte* payload, unsigned int length) {
String sTopic = String(topic);
Serial.print("topic empfangen: ");
Serial.println(String(topic));
if (sTopic == "strom/smartmeter/sensor/1/obis/1-0:16.7.0/255/value") {
temp = "";
for (int i = 0; i < length; i++) {
temp += ((char)payload[i]);
}
zaehler = temp;
Serial.println(zaehler);
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
// uint32_t chipid=ESP.getChipId();
char clientid[25];
snprintf(clientid, 25, "WIFI-Display-%08X", "12345"); //this adds the mac address to the client for a unique id
Serial.print("Client ID: ");
// Serial.println(clientid);
if (client.connect(clientid)) {
Serial.println("connected");
// Once connected, publish an announcement...
//client.publish("Say", "-t 'hello world'");
// ... and resubscribe
client.subscribe("strom/smartmeter/sensor/1/obis/1-0:16.7.0/255/value");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop()
{
// unsigned long currentMillis = millis();
// if (currentMillis - previousMillis >= interval){
for (int loops = 1; loops < 11; loops++) {
client.loop();
delay(3000);
solar = fritz.getSwitchPower(fritz_ain);
verbrauch = zaehler.toFloat();
Serial.print("Zähleranzeige: ");
Serial.println(verbrauch);
Serial.print ("Solarertrag: ");
Serial.println (solar);
verbrauchkorr = ((verbrauch) + (solar));
Serial.print("Zähleranzeige + Solar: ");
Serial.println(verbrauchkorr);
if ((verbrauch) <= 0) {
einsparung = verbrauchkorr;
Serial.print("Einsparung: ");
Serial.println(einsparung);
}
else {
einsparung = solar;
Serial.print("Einsparung: ");
Serial.println(einsparung);
}
// declare input and output variables
input = einsparung; // without a real input, looking at the step respons (input at unity, 1)
// float output = 0;
Serial.println("Now calling fir...");
output = movingAverageFilter.process(input); // here we call the fir routine with the input. The value 'fir' spits out is stored in the output variable.
Serial.print("fir presented the following value= ");
Serial.println(output); // just for debugging or to understand what it does, print the output value
Serial.println(loops);
// if (loops > 51) loops = 51;
}
Serial.print("senden zu node-red: ");
Serial.println(output);
convert2Json();
client.publish("sensors/power", "hello world"); //mqtt TEST
int loops = 1;
Serial.print("loops zurückgesetzt auf: ");
Serial.println(loops);
// previousMillis = currentMillis;
// }
}
void convert2Json()
{
client.publish("sensors/power", "hello world"); //mqtt TEST
packet = "";
packet.concat(("{\"einsparung\": "));
packet.concat(output);
packet.concat((", \"solarertrag\": "));
packet.concat(solar);
packet.concat((", \"verbrauch\": "));
packet.concat(verbrauch);
packet.concat("}");
Serial.println(packet);
client.publish("sensors/power", packet.c_str());
Serial.println("sensors/power/ published");
}
sowohl meine beiden
client.publish("sensors/power", "hello world"); //mqtt TEST
als auch das eigentlich gewollte
client.publish("sensors/power", packet.c_str());
gehen einfach nicht raus. im mqtt explorer kommt nix an.
Die Serial.prints kommen alle an der konsole an. Das Programm läuft also schon über die genannten Stellen drüber.
Was habe ich falsch zusammengeschustert? Sieht es vielleicht jemand auf den ersten Blick und kann mir weiterhelfen?
Vielen Dank schon einmal!
mfg