Skip to content

Commit c020a2d

Browse files
committed
Initial Release
1 parent 4e31da0 commit c020a2d

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed

platformio.ini

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:esp12e]
12+
platform = espressif8266
13+
board = esp12e
14+
framework = arduino
15+
lib_deps =
16+
bblanchon/ArduinoJson@5.13.4
17+
knolleary/PubSubClient@^2.8
18+
olehs/PZEM004T@^1.1.5

src/config.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* 1.0.0 VERSION */
2+
3+
#ifndef config_h
4+
#define config_h
5+
6+
// USER SETTINGS
7+
#include "user-config.h"
8+
9+
// GENERAL SETTINGS
10+
#define READ_INTERVAL 2000 //ms
11+
#define MAX_SAMPLES 30
12+
#define DIFF_POWER_TRIGGER 2 //W
13+
#define LEDSTATUSPIN 2 //BUILTIN
14+
#define SERIAL_BAUDRATE 9600
15+
16+
// PZEM SETTINGS
17+
#define PZEMPIN1 4 // PZEM PIN SERIAL IN
18+
#define PZEMPIN2 5 // PZEM PIN SERIAL OUT
19+
20+
// WIFI SETTINGS (see user-config.h)
21+
const char *ssid = USER_SETTINGS_WIFI_SSID;
22+
const char *password = USER_SETTINGS_WIFI_PASSWORD;
23+
const char *wifihostname = USER_SETTINGS_WIFI_HOSTNAME;
24+
25+
// MQTT SETTINGS (see user-config.h)
26+
const char MQTT_HOST[] = USER_SETTINGS_MQTT_HOST;
27+
int MQTT_PORT = USER_SETTINGS_MQTT_PORT;
28+
const char TOPIC_P[] = USER_SETTINGS_MQTT_TOPIC_P;
29+
const char TOPIC_S[] = USER_SETTINGS_MQTT_TOPIC_S;
30+
const char DEVICE[] = USER_SETTINGS_MQTT_DEVICE;
31+
const char MQTT_USER[] = USER_SETTINGS_MQTT_USER;
32+
const char MQTT_PWD[] = USER_SETTINGS_MQTT_PWD;
33+
int MQTT_QOS = USER_SETTINGS_MQTT_QOS;
34+
35+
#endif

src/main.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* 1.0.0 VERSION */
2+
3+
#include <Arduino.h>
4+
#include <ArduinoJson.h>
5+
6+
#include "config.h"
7+
8+
#include <ddcommon.h>
9+
#include <ddwifi.h>
10+
#include <ddmqtt.h>
11+
#include <ddpzem004t.h>
12+
13+
//Wifi
14+
DDWifi wifi(ssid, password, wifihostname, LEDSTATUSPIN);
15+
16+
//MQTT
17+
DDMqtt clientMqtt(DEVICE, MQTT_HOST, MQTT_PORT, MQTT_USER, MQTT_PWD, TOPIC_S, MQTT_QOS, LEDSTATUSPIN);
18+
19+
//PZEM
20+
DDPzem004t pzemWrapper(LEDSTATUSPIN);
21+
22+
PZEM004T pzemPower = PZEM004T(PZEMPIN1, PZEMPIN2);
23+
IPAddress ipPower = IPAddress(192, 168, 1, 1);
24+
float lastPowerTriggerValue;
25+
26+
//JSON
27+
DynamicJsonBuffer jsonBuffer;
28+
JsonObject& configRoot = jsonBuffer.createObject();
29+
JsonObject& root = jsonBuffer.createObject();
30+
31+
unsigned long startMillis;
32+
33+
void createJsonConfig(){
34+
configRoot["readInterval"] = READ_INTERVAL;
35+
configRoot["maxSamples"] = MAX_SAMPLES;
36+
}
37+
38+
void printDebugPowerData(DDPZEM004TVal values){
39+
writeToSerial("POWER READ Success = ", false);
40+
writeToSerial(values.success ? "True" : "False", true);
41+
if(values.success){
42+
writeToSerial("Voltage: ", false);
43+
writeToSerial(values.voltage, false);
44+
writeToSerial(" V ", true);
45+
writeToSerial("Current: ", false);
46+
writeToSerial(values.current, false);
47+
writeToSerial(" A ", true);
48+
writeToSerial("Power: ", false);
49+
writeToSerial(values.power, false);
50+
writeToSerial(" W ", true);
51+
writeToSerial("Energy: ", false);
52+
writeToSerial(values.energy, false);
53+
writeToSerial(" Wh ", true);
54+
}
55+
}
56+
57+
String generateJsonMessage(DDPZEM004TVal values){
58+
root["voltage"] = values.voltage;
59+
root["current"] = values.current;
60+
root["power"] = values.power;
61+
root["energy"] = values.energy;
62+
String json;
63+
root.printTo(json);
64+
return json;
65+
}
66+
67+
void setup() {
68+
createJsonConfig();
69+
pinMode(LEDSTATUSPIN, OUTPUT);
70+
digitalWrite(LEDSTATUSPIN, LOW);
71+
if(SERIAL_ENABLED)
72+
Serial.begin(SERIAL_BAUDRATE);
73+
writeToSerial("ESP8266MCU12 Booting...", true);
74+
75+
// WIFI
76+
wifi.connect();
77+
78+
//MQTT
79+
clientMqtt.reconnectMQTT(&startMillis);
80+
81+
//PZEM
82+
pzemWrapper.init(&pzemPower, ipPower);
83+
84+
lastPowerTriggerValue = -999.0;
85+
startMillis = millis();
86+
}
87+
88+
void loop() {
89+
90+
// Wait a few seconds between measurements.
91+
if(myDelay(configRoot["readInterval"], &startMillis)){
92+
clientMqtt.loop();
93+
DDPZEM004TVal values = pzemWrapper.getValues(&pzemPower, ipPower);
94+
printDebugPowerData(values);
95+
writeToSerial("lastPowerTriggerValue ", false);
96+
writeToSerial(lastPowerTriggerValue, true);
97+
if(abs(lastPowerTriggerValue - values.power) >= DIFF_POWER_TRIGGER || (lastPowerTriggerValue != values.power && values.power == 0)){
98+
clientMqtt.sendMessage(TOPIC_P, generateJsonMessage(values), &startMillis);
99+
lastPowerTriggerValue = values.power;
100+
}
101+
}
102+
}

src/user-config-template.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* 1.0.0 VERSION */
2+
3+
#ifndef user_config_h
4+
#define user_config_h
5+
6+
// WIFI SETTINGS
7+
#define USER_SETTINGS_WIFI_SSID "X"
8+
#define USER_SETTINGS_WIFI_PASSWORD "X"
9+
#define USER_SETTINGS_WIFI_HOSTNAME "X"
10+
11+
// MQTT SETTINGS
12+
#define USER_SETTINGS_MQTT_HOST "X"
13+
#define USER_SETTINGS_MQTT_PORT 1833
14+
#define USER_SETTINGS_MQTT_TOPIC_P "X"
15+
#define USER_SETTINGS_MQTT_TOPIC_S "X" // not used
16+
#define USER_SETTINGS_MQTT_DEVICE "X"
17+
#define USER_SETTINGS_MQTT_USER "X"
18+
#define USER_SETTINGS_MQTT_PWD "X"
19+
#define USER_SETTINGS_MQTT_QOS 1
20+
21+
#endif

0 commit comments

Comments
 (0)