Comments (5)
I've read the Adafruit Discord channel thread about this issue... I wasn't conscious at all of the memory problem (which isn't strange as I'm a complete beginner to C++...). Although I'm not planning to use as many sensors, I'd also do it like this on my sketch. Thanks to @mzero!
Now, you could try to combine this approach with the array's one (at the end of this post), I read @mzero said it didn't solve the memory issue by itself, but combined with your mod it'll probably work.
The code could be like this (using the modified library, of course):
####################################
SoftwareSerial serial56(D5, D6);
SoftwareSerial serial34(D3, D4);
SoftwareSerial serial12(D1, D2);
PZEM004Tv30 pzem[18] = {
PZEM004Tv30(serial56, 0x01),
PZEM004Tv30(serial56, 0x02),
PZEM004Tv30(serial34, 0x03),
PZEM004Tv30(serial34, 0x04),
PZEM004Tv30(serial12, 0x05),
PZEM004Tv30(serial12, 0x06),
PZEM004Tv30(serial56, 0x07),
PZEM004Tv30(serial56, 0x08),
PZEM004Tv30(serial34, 0x09),
PZEM004Tv30(serial34, 0x0a),
PZEM004Tv30(serial12, 0x0b),
PZEM004Tv30(serial12, 0x0c),
PZEM004Tv30(serial56, 0x0d),
PZEM004Tv30(serial56, 0x0e),
PZEM004Tv30(serial34, 0x0f),
PZEM004Tv30(serial34, 0x10),
PZEM004Tv30(serial12, 0x11),
PZEM004Tv30(serial12, 0x12)
};
####################################
The memory problem is avoided as only 3 instances of the soft serial port are created, instead of 18.
And when you read the sensors, instead of using (example for sensor nΒΊ5):
float voltage = pzem5.voltage();
You should use:
float voltage = pzem[4].voltage();
With the great advantage that you can use a variable instead of the fixed number, so you can include that into a loop, for example.
(Note I'm using 4 instead of 5 in the second case because the array starts at 0, so the pzem's are numbered 0...17 instead of 1...18).
It would be very good if mandulaj included this simple mod in the main branch. Apparently, is fully backward compatible with the present version and best suited for multiple sensors (a use that, probably, was unforeseen when designing the library, as @mzero said).
from pzem-004t-v30.
Another question: You don't really need to use different serial ports for each 6 PZEM's if you use a buffer between microcontroller's TX and PZEM's RX. This can be as simple as a single PNP transistor. This way, you can free 2 GPIOs for other uses (and save two soft serial port instances). Also, it's better to connect the PZEMs to +5V instead of 3.3V to avoid saturating the WemosD1 weak regulator.
In the previous posts to that one, you can also see considerations about the current used by each PZEM and also a mod I made to the library that allows passing the address of the PZEM as a parameter (I'll try to combine it with the one proposed here).
from pzem-004t-v30.
- @morganflint thats a good point with the array & loops, i already tried but i wasnt able to manage to work the correct syntax for client publish. There the topic must be inside quotes and dont know how to make it variable
example:
for (int i=0; i<18; i++){
Serial.print("pzem "); Serial.println(i+1);
float voltage = pzem[i].voltage(); client.publish("/topic/pzem[i]",String(voltage).c_str());
}
this is the issue:"/topic/pzem[i]" in mqtt explorer i can see it created new topic /topic/pzem[i] instead of pzem1..pzem18... any idea ? thanx
- i am powering via 5V, 3.3v didnt work good.
- i have no use for additional gpio so adding pnp transistor might be good idea but not for my setup and will it leave this way
from pzem-004t-v30.
I haven't worked with mqtt so I'm not sure if this will help but I'm afraid client.publish("/topic/pzem[i]",String(voltage).c_str()) is not working because the variable i is inside the quotation marks, so it's considering it part of the string and not as a variable.
I'd try to define an array also for mqtt topics, something like:
String topic[18] = {
"/topic/pzem1",
"/topic/pzem2",
...
"/topic/pzem18"
};
And then:
float voltage = pzem[i].voltage(); client.publish(topic[i],String(voltage).c_str());
Maybe you can get better ideas from here.
from pzem-004t-v30.
It looks like, this issue was resolved. I am closing it now.
from pzem-004t-v30.
Related Issues (20)
- ESP32-WROOM-32 (DOIT ESP32 DEVKIT V1) + PZEM-004T-100A(V3.0) - all returned values are NaN HOT 7
- Negative power, for consumption or production measurement HOT 3
- reset energy, set max power , read alarm flag. HOT 5
- Read Only Current HOT 4
- I can not read 3 pzem HOT 3
- Pzem-004t v3 + Esp32 DEVKITV1 HOT 2
- Mycropyton HOT 1
- HOW TO RESET PZEM004TV3 ESP32? HOT 1
- when I try to upload, I get an error like this: no matching function for call to 'PZEM004Tv30::PZEM004Tv30(int, int) HOT 7
- Seeed Wio LTE
- instantaneous value HOT 1
- Module PZEM-004T-100A-D-P HOT 1
- Issue with Teensy 4.1, using any of the hardware serials (Serial1...Serial8)
- Voltage measurement up to 500 volts HOT 2
- Pzem measuring wrong voltage values HOT 2
- Can you make this code non-blocking? HOT 10
- How to modify this code to only output certain values HOT 6
- is there possible to change the CT (Current Transformer)? HOT 1
- Check if module is connected HOT 2
- Only read voltage HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pzem-004t-v30.