Skip to content

Commit 6c4f05a

Browse files
committed
Library and Codes updated
1 parent bf01bea commit 6c4f05a

File tree

7 files changed

+80
-370
lines changed

7 files changed

+80
-370
lines changed

MCM_BL0940.cpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@
2525
#define ERR(...)
2626
#endif /* BL0940_DBG */
2727

28-
BL0940::BL0940()
28+
bool BL0940::begin(HardwareSerial& serial, int8_t rxPin, int8_t txPin)
2929
{
3030

31-
/* For M5STACK_PAPER */
32-
// Serial2.begin(4800, SERIAL_8N1, 18, 19);
31+
serialPtr = &serial; // default
3332

34-
Serial2.begin(4800);
35-
36-
delay(500);
37-
}
38-
39-
BL0940::~BL0940()
40-
{
41-
Serial2.end();
33+
#if defined (ARDUINO_RASPBERRY_PI_PICO)
34+
serialPtr->begin(4800);
35+
#else
36+
serialPtr->begin(4800, SERIAL_8N1, rxPin, txPin);
37+
#endif
38+
return true;
4239
}
4340

4441
uint8_t BL0940::_culcCheckSum(uint8_t *txData, int txLenght, uint8_t *rxData, int rxLenght)
@@ -60,38 +57,38 @@ uint8_t BL0940::_culcCheckSum(uint8_t *txData, int txLenght, uint8_t *rxData, in
6057
bool BL0940::_writeRegister(uint8_t address, uint32_t data)
6158
{
6259
// read buffer clear
63-
while (Serial2.available() != 0)
60+
while (serialPtr->available() != 0)
6461
{
65-
Serial2.read();
62+
serialPtr->read();
6663
}
6764

6865
// Register Unlock
6966
uint8_t unlockTxData[6] = {0xA8, 0x1A, 0x55, 0, 0, 0};
7067
unlockTxData[5] = _culcCheckSum(unlockTxData, sizeof(unlockTxData) - 1, 0, 0);
71-
Serial2.write(unlockTxData, sizeof(unlockTxData));
68+
serialPtr->write(unlockTxData, sizeof(unlockTxData));
7269

7370
// Write Register
7471
uint8_t txData[6] = {0xA8, address, (uint8_t)(data), (uint8_t)(data >> 8), (uint8_t)(data >> 16)};
7572
txData[5] = _culcCheckSum(txData, sizeof(txData) - 1, 0, 0);
76-
Serial2.write(txData, sizeof(txData));
73+
serialPtr->write(txData, sizeof(txData));
7774

7875
return true;
7976
}
8077

8178
bool BL0940::_readRegister(uint8_t address, uint32_t *data)
8279
{
8380
uint8_t txData[] = {0x58, address};
84-
Serial2.write(txData, sizeof(txData));
81+
serialPtr->write(txData, sizeof(txData));
8582

8683
uint8_t rxData[4] = {0, 0, 0, 0};
8784
uint32_t startTime = millis();
88-
while (Serial2.available() != sizeof(rxData))
85+
while (serialPtr->available() != sizeof(rxData))
8986
{
9087
delay(10);
9188
if ((millis() - startTime) > timeout)
9289
break;
9390
}
94-
int rxDataLength = Serial2.readBytes(rxData, sizeof(rxData));
91+
int rxDataLength = serialPtr->readBytes(rxData, sizeof(rxData));
9592

9693
if (rxDataLength == 0)
9794
{
@@ -359,9 +356,9 @@ bool BL0940::Reset()
359356
ERR("Can not write SOFT_RESET register.");
360357
return false;
361358
}
362-
while (Serial2.available() != 0)
359+
while (serialPtr->available() != 0)
363360
{
364-
Serial2.read();
361+
serialPtr->read();
365362
}
366363

367364
delay(500);

MCM_BL0940.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@
66
* Based on Kohacraft previews work.
77
*/
88

9+
#pragma once
10+
11+
#include <Arduino.h>
12+
913
#ifndef MCM_BL0940_h
1014
#define MCM_BL0940_h
1115

1216
class BL0940
1317
{
1418
public:
15-
BL0940();
16-
~BL0940();
19+
20+
bool begin(HardwareSerial& serial, int8_t rxPin = -1, int8_t txPin = -1);
1721
bool getCurrent( float *current ); //[A]
1822
bool getVoltage( float *voltage ); //[V]
1923
bool getActivePower( float *activePower ); //[W]
@@ -27,6 +31,7 @@ class BL0940
2731
bool Reset();
2832

2933
private:
34+
HardwareSerial* serialPtr = nullptr; // pointer to HardwareSerial
3035
const uint16_t timeout = 1000; //Serial timeout[ms]
3136
const float Vref = 1.218; //[V]
3237
const float R5 = 3.3; //[Ohm]
@@ -43,5 +48,6 @@ class BL0940
4348
uint8_t _culcCheckSum( uint8_t *txData , int txLenght , uint8_t *rxData , int rxLenght );
4449
bool _writeRegister( uint8_t address , uint32_t data );
4550
bool _readRegister( uint8_t address , uint32_t *data );
51+
4652
};
4753
#endif /* BL0940 */

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ An Arduino library to interface the BL0940 Energy Meter IC with Arduino, ESP32 a
88
![FotoModulo](https://user-images.githubusercontent.com/49886387/219874809-0241e54c-ceb6-4885-a2be-acae7203bad2.JPG)
99

1010
## Supported MCU's
11-
- ESP32
11+
- ESP32:
12+
- Adafruit Feather ESP32
13+
- Arduino Nano ESP32
14+
- M5Stack Paper
15+
- Xiao ESP32C3
1216
- Raspberry Pi Pico

examples/BL0940-ESP32/BL0940-ESP32.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
21
#include "MCM_BL0940.h"
32

43
BL0940 bl0940;
54

65
void setup() {
76
Serial.begin(115200);
87

8+
bl0940.begin(Serial1, RX, TX); // RX pin - TX pin
99
bl0940.Reset();
1010
bl0940.setFrequency(60); //50[Hz]
1111
bl0940.setUpdateRate(800); //400[ms]
@@ -19,15 +19,15 @@ void loop() {
1919

2020
float current;
2121
bl0940.getCurrent( &current );
22-
Serial.printf("%.2f [A]\n", current );
22+
Serial.printf("%.4f [A]\n", current );
2323

2424
float activePower;
2525
bl0940.getActivePower( &activePower );
2626
Serial.printf("%.2f [W]\n", activePower );
2727

2828
float activeEnergy;
2929
bl0940.getActiveEnergy( &activeEnergy );
30-
Serial.printf("%.3f [kWh]\n", activeEnergy );
30+
Serial.printf("%.6f [kWh]\n", activeEnergy );
3131

3232
float powerFactor;
3333
bl0940.getPowerFactor( &powerFactor );

0 commit comments

Comments
 (0)