Tenho um problema, ou algo mal configurado,
Tenho valores em tempo real contudo em baixo no grafico não actualiza… estranho
@Nuno_Figueiredo, no código que publicas-te, experimenta no fim do ficheiro retira o delay(30000);
e adiciona o comando yield();
s = String(sdm.readVal(SDM220T_EXPORT_REACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/export_react_en", charBuf);
Serial.print("Export reactive energy: ");
Serial.print(s);
Serial.println(" VARh");
}
Serial.println("----------------------------------------");
}
yield();
}
inverte o TX e RX e confirma que tens o RE/DE unidos ligados ao mesmo DEREPIN que escolheste.
tenho tudo como metes te no esquema! vou trocar e ver!!
Edit: troquei e nada!
tens de ter ai alguma falha :\ confirma se tens o baud rate igual no codigo e no sdm.
Obrigado a todos pela ajuda em especial ao @Nuno_Figueiredo ja funciona vou precisar de ajuda amanha para o meter no quadro. @j_assuncao podes passar o teu codigo do Ha gosto muito do look da da coisa
Fico extramemente contente de a coisa estar a bombar por esses lados, peço a ti frederico que faças o mesmo que o Nuno fez, um esquema a mão com as ligações do Wemos, pode ser em papel mesmo. Pois quero actualizar este tópico com todos os cenários possíveis.
Por ir ao meu GitHub que está lá tudo.
para quem andar a pensar em SDM ou PZEM, fica aqui uma rapida comparação.
os totais estão desalinhados, pelo que por ai nao deve ser visto, ainda ando a procura de afinar os totais do SDM.
Muito bom @Nuno_Figueiredo obrigado pela tua partilha neste tópico estou a reunir mais informação e só à espera do esquema do Frederico para actualizar o tópico.
já procurei n encontrei algumas partes tens tudo commited?
Sim, está lá tudo…
Já encontraste o que querias no meu GitHub?
ainda não encontrei só a parte do grafico mas nao a parte do mqtt
config/config_files/packages/package_electrical_consumption.yam
ja encontreo obrigado . Já agora eu tenho bi diario a nive de luz sabes como poderia fazer isso?
adicionei OTA para flasharem e a sugestao do Jorge, em teste
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoOTA.h>
#include <SDM.h> //import SDM template library
//SDM<2400, 12, 13> sdm; //SDM120T baud, rx pin, tx pin
//SDM<4800, 12, 13> sdm; //SDM120C baud, rx pin, tx pin
//SDM<9600, 12, 13> sdm; //SDM630 baud, rx pin, tx pin
//or without parameters (default from SDM.h will be used):
SDM<2400, 12, 13, 4> sdm;
float m = 0;
char charBuf[50];
String s;
const char* ssid = "SSID"; // Wifi SSID
const char* password = "PASSWORD"; // Wifi password
const char* mqttClientName = "sdm120"; //will also be used hostname and OTA name
IPAddress ip(192,168,1,25); // IP address
IPAddress dns(8,8,8,8); // DNS server
IPAddress gateway(192,168,1,254); // Gateway
IPAddress subnet(255,255,255,0); // Subnet mask
const char* mqtt_server = "192.168.1.132";
#define mqtt_user "user"
#define mqtt_password "password"
int MQTT_WILL_QOS = 1; // MQTT last will QoS (0,1 or 2)
int MQTT_WILL_RETAIN = 1; // MQTT last will retain (0 or 1)
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.begin(115200); //initialize serial
sdm.begin(); //initalize SDM220 communication baudrate
//----------- OTA
ArduinoOTA.setHostname(mqttClientName);
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else { // U_SPIFFS
type = "filesystem";
}
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
delay(1000);
ESP.restart();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
void setup_wifi() {
delay(10);
// config static IP
Serial.print(F("Setting static ip to : "));
Serial.println(ip);
WiFi.config(ip, gateway, subnet, dns);
// 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.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("SDM120",mqtt_user,mqtt_password,"sdm120/status", 2, 0, "0")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("sdm120/status", "1");
// ... and resubscribe
client.subscribe("inTopic");
} 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() {
if (!client.connected()) {
reconnect();
}
client.loop();
//handle OTA
ArduinoOTA.handle();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
delay(5000);
s = String(sdm.readVal(SDM220T_VOLTAGE));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/volt", charBuf);
Serial.print("Voltage: ");
Serial.print(s);
Serial.println(" V");
}
delay(50);
s = String(sdm.readVal(SDM220T_CURRENT));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/curr", charBuf);
Serial.print("Current: ");
Serial.print(s);
Serial.println(" A");
}
delay(50);
s = String(sdm.readVal(SDM220T_POWER));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/pow", charBuf);
Serial.print("Power: ");
Serial.print(s);
Serial.println(" W");
}
delay(50);
s = String(sdm.readVal(SDM220T_ACTIVE_APPARENT_POWER));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/act_app_pow", charBuf);
Serial.print("Active apparent power: ");
Serial.print(s);
Serial.println(" VA");
}
delay(50);
s = String(sdm.readVal(SDM220T_REACTIVE_APPARENT_POWER));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/react_app_pow", charBuf);
Serial.print("Active apparent power: ");
Serial.print(s);
Serial.println(" VAR");
}
delay(50);
s = String(sdm.readVal(SDM220T_POWER_FACTOR));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/pow_factor", charBuf);
Serial.print("Power factor: ");
Serial.print(s);
Serial.println(" ");
}
delay(50);
s = String(sdm.readVal(SDM220T_PHASE_ANGLE));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/phase_angle", charBuf);
Serial.print("Phase angle: ");
Serial.print(s);
Serial.println(" Degree");
}
delay(50);
s = String(sdm.readVal(SDM220T_FREQUENCY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/freq", charBuf);
Serial.print("Frequency: ");
Serial.print(s);
Serial.println(" Hz");
}
delay(50);
s = String(sdm.readVal(SDM220T_TOTAL_ACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/tot_act_en", charBuf);
Serial.print("Total active energy: ");
Serial.print(s);
Serial.println(" Wh");
}
delay(50);
s = String(sdm.readVal(SDM220T_TOTAL_REACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/tot_react_en", charBuf);
Serial.print("Total reactive energy: ");
Serial.print(s);
Serial.println(" Wh");
}
delay(50);
s = String(sdm.readVal(SDM220T_IMPORT_ACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/import_act_en", charBuf);
Serial.print("Import active energy: ");
Serial.print(s);
Serial.println(" Wh");
}
delay(50);
s = String(sdm.readVal(SDM220T_EXPORT_ACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/export_act_en", charBuf);
Serial.print("Export active energy: ");
Serial.print(s);
Serial.println(" Wh");
}
delay(50);
s = String(sdm.readVal(SDM220T_IMPORT_REACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/import_react_en", charBuf);
Serial.print("Import reactive energy: ");
Serial.print(s);
Serial.println(" VARh");
}
delay(50);
s = String(sdm.readVal(SDM220T_EXPORT_REACTIVE_ENERGY));
if (s != "nan") {
s.toCharArray(charBuf, 50);
client.publish("sdm120/export_react_en", charBuf);
Serial.print("Export reactive energy: ");
Serial.print(s);
Serial.println(" VARh");
}
yield();
Serial.println("----------------------------------------");
}
//wait a while before next loop
}