Skip to content

Commit

Permalink
Merge pull request #38 from reaper7/serial_pin_defined
Browse files Browse the repository at this point in the history
Serial pin defined
  • Loading branch information
reaper7 authored Oct 18, 2019
2 parents 0454d52 + 458b040 commit ab0af65
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 75 deletions.
117 changes: 60 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ _Tested on Wemos D1 Mini with Arduino IDE 1.8.3-1.8.10 & ESP8266 core 2.3.0-2.5.

### Configuring: ###
Default configuration is specified in the [SDM.h](https://github.com/reaper7/SDM_Energy_Meter/blob/master/SDM.h#L18) file, and parameters are set to:</br>
<i>Software Serial, baud 4800, uart config SERIAL_8N1, without DE/RE pin</i>.</br>
<i>Software Serial mode, baud 4800, uart config SERIAL_8N1, without DE/RE pin,</br>
uart pins for esp32 hwserial and esp32/esp8266/avr swserial as NOT_A_PIN (-1).</br></br>
For esp32 hwserial this means using the default pins for the selected uart port,</br>
specified in the core library (HardwareSerial.cpp).</br>
For swserial option (esp32/esp8266/avr) is necessary</br>
to specify the pin numbers, as described below.</i>

User can set the parameters in two ways:
- by editing the [SDM_Config_User.h](https://github.com/reaper7/SDM_Energy_Meter/blob/master/SDM_Config_User.h) file
Expand All @@ -58,24 +63,22 @@ initialization is limited to passing serial port reference (software or hardware
and looks as follows:
```cpp
//lib init when Software Serial is used:
#include <SoftwareSerial.h>
#include <SDM.h>
#include <SoftwareSerial.h>

// for ESP8266 and ESP32
SoftwareSerial swSerSDM;
// ______________________________________________software serial reference
// | ________________________________________baudrate
// | | _____________________________dere pin for max485
// | | | _______________software uart config
// | | | | ___________rx pin number(obligatory)
// | | | | | _______tx pin number(obligatory)
// | | | | | |
SDM sdm(swSerSDM, 9600, NOT_A_PIN, SWSERIAL_8N1, 13, 15);
// _______________________________software serial reference
// |
SDM sdm(swSerSDM);


// for AVR
SoftwareSerial swSerSDM(10, 11);
// _software serial reference
SoftwareSerial swSerSDM(SDM_RX_PIN, SDM_TX_PIN);
// | |_tx pin definition(from SDM_Config_User.h)
// |_____________rx pin definition(from SDM_Config_User.h)
//
// _______________________________software serial reference
// |
SDM sdm(swSerSDM);
```
Expand All @@ -84,34 +87,34 @@ SDM sdm(swSerSDM);
//lib init when Hardware Serial is used:
#include <SDM.h>
// _hardware serial reference
// _________________________________hardware serial reference
// |
SDM sdm(Serial);
```
If the user wants to temporarily change the configuration during the initialization process</br>
then can pass additional parameters as below:
```cpp
//lib init when Software Serial is used:
#include <SoftwareSerial.h>
#include <SDM.h>
#include <SoftwareSerial.h>

// for ESP8266 and ESP32
SoftwareSerial swSerSDM;
// ______________________________________________software serial reference
// | ________________________________________baudrate
// | | _____________________________dere pin for max485
// | | | _______________software uart config
// | | | | ___________rx pin number(obligatory)
// | | | | | _______tx pin number(obligatory)
// ________________________________________software serial reference
// | __________________________________baudrate(optional, default from SDM_Config_User.h)
// | | _______________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | _________software uart config(optional, default from SDM_Config_User.h)
// | | | | _____rx pin number(optional, default from SDM_Config_User.h)
// | | | | | _tx pin number(optional, default from SDM_Config_User.h)
// | | | | | |
SDM sdm(swSerSDM, 9600, NOT_A_PIN, SWSERIAL_8N1, 13, 15);


// for AVR
SoftwareSerial swSerSDM(10, 11);
// __________________software serial reference
// | ____________baudrate(optional, default from SDM_Config_User.h)
// | | _dere pin for max485(optional, default from SDM_Config_User.h)
// ________________________________________software serial reference
// | __________________________________baudrate(optional, default from SDM_Config_User.h)
// | | _______________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | |
SDM sdm(swSerSDM, 9600, NOT_A_PIN);
```
Expand All @@ -121,31 +124,31 @@ SDM sdm(swSerSDM, 9600, NOT_A_PIN);
#include <SDM.h>
// for ESP8266
// _____________________________________hardware serial reference
// | _______________________________baudrate(optional, default from SDM_Config_User.h)
// | | ____________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | ________hardware uart config(optional, default from SDM_Config_User.h)
// | | | | _swap hw serial pins from 3/1 to 13/15(optional, default from SDM_Config_User.h)
// ______________________________________hardware serial reference
// | ________________________________baudrate(optional, default from SDM_Config_User.h)
// | | _____________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | _________hardware uart config(optional, default from SDM_Config_User.h)
// | | | | __swap hw serial pins from 3/1 to 13/15(optional, default from SDM_Config_User.h)
// | | | | |
SDM sdm(Serial, 9600, NOT_A_PIN, SERIAL_8N1, false);
// for ESP32
// ____________________________________________hardware serial reference
// | ______________________________________baudrate(optional, default from SDM_Config_User.h)
// | | ___________________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | _______________hardware uart config(optional, default from SDM_Config_User.h)
// | | | | ________rx pin number(optional)
// | | | | | _tx pin number(optional)
// | | | | | |
SDM sdm(Serial, 9600, NOT_A_PIN, SERIAL_8N1, rxpin, txpin);
// ______________________________________hardware serial reference
// | ________________________________baudrate(optional, default from SDM_Config_User.h)
// | | _____________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | _________hardware uart config(optional, default from SDM_Config_User.h)
// | | | | _____rx pin number(optional, default from SDM_Config_User.h)
// | | | | | _tx pin number(optional, default from SDM_Config_User.h)
// | | | | | |
SDM sdm(Serial, 9600, NOT_A_PIN, SERIAL_8N1, 13, 15);
// for AVR
// _____________________________________hardware serial reference
// | _______________________________baudrate(optional, default from SDM_Config_User.h)
// | | ____________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | ________hardware uart config(optional, default from SDM_Config_User.h)
// ______________________________________hardware serial reference
// | ________________________________baudrate(optional, default from SDM_Config_User.h)
// | | _____________________dere pin for max485(optional, default from SDM_Config_User.h)
// | | | _________hardware uart config(optional, default from SDM_Config_User.h)
// | | | |
// | | | |
SDM sdm(Serial, 9600, NOT_A_PIN, SERIAL_8N1);
Expand All @@ -163,15 +166,15 @@ List of available registers for SDM72/120/220/230/630:</br>
https://github.com/reaper7/SDM_Energy_Meter/blob/master/SDM.h#L58
```cpp
//reading voltage from SDM with slave address 0x01 (default)
// __________register name
// |
// ____register name
// |
float voltage = sdm.readVal(SDM220T_VOLTAGE);

//reading power from 1st SDM with slave address ID = 0x01
//reading power from 2nd SDM with slave address ID = 0x02
//useful with several meters on RS485 line
// __________register name
// | ____SDM device ID
// _______register name
// | _SDM device ID
// | |
float power1 = sdm.readVal(SDM220T_POWER, 0x01);
float power2 = sdm.readVal(SDM220T_POWER, 0x02);
Expand Down Expand Up @@ -211,11 +214,11 @@ The most common problems are:
You can get last error code using function:
```cpp
//get last error code
// __________optional parameter,
// | true -> read and reset error code
// | false or no parameter -> read error code
// | but not reset stored code (for future checking)
// | will be overwriten when next error occurs
// ______optional parameter,
// | true -> read and reset error code
// | false or no parameter -> read error code
// | but not reset stored code (for future checking)
// | will be overwriten when next error occurs
uint16_t lasterror = sdm.getErrCode(true);

//clear error code also available with:
Expand All @@ -227,10 +230,10 @@ https://github.com/reaper7/SDM_Energy_Meter/blob/master/SDM.h#L177</br>
You can also check total number of errors using function:
```cpp
//get total errors counter
// _________optional parameter,
// | true -> read and reset errors counter
// | false or no parameter -> read errors counter
// | but not reset stored counter (for future checking)
// _____optional parameter,
// | true -> read and reset errors counter
// | false or no parameter -> read errors counter
// | but not reset stored counter (for future checking)
uint16_t cnterrors = sdm.getErrCount(true);

//clear errors counter also available with:
Expand All @@ -240,10 +243,10 @@ sdm.clearErrCount();
And finally you can read the counter of correctly made readings:
```cpp
//get total success counter
// _________optional parameter,
// | true -> read and reset success counter
// | false or no parameter -> read success counter
// | but not reset stored counter (for future checking)
// ___optional parameter,
// | true -> read and reset success counter
// | false or no parameter -> read success counter
// | but not reset stored counter (for future checking)
uint16_t cntsuccess = sdm.getSuccCount(true);

//clear success counter also available with:
Expand Down
32 changes: 26 additions & 6 deletions SDM.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
#define SWAPHWSERIAL 0 //(only esp8266) when hwserial used, then swap uart pins from 3/1 to 13/15 (default not swap)
#endif

#if defined ( ESP32 )
#if !defined ( SDM_RX_PIN )
#define SDM_RX_PIN -1 //use default rx pin for selected port
#endif
#if !defined ( SDM_TX_PIN )
#define SDM_TX_PIN -1 //use default tx pin for selected port
#endif
#endif

#else

#if defined ( ESP8266 ) || defined ( ESP32 )
Expand All @@ -43,6 +52,17 @@
#endif
#endif

// #if !defined ( SDM_RX_PIN ) || !defined ( SDM_TX_PIN )
// #error "SDM_RX_PIN and SDM_TX_PIN must be defined in SDM_Config_User.h for Software Serial option)"
// #endif

#if !defined ( SDM_RX_PIN )
#define SDM_RX_PIN -1
#endif
#if !defined ( SDM_TX_PIN )
#define SDM_TX_PIN -1
#endif

#endif

#if !defined ( MAX_MILLIS_TO_WAIT )
Expand Down Expand Up @@ -186,13 +206,13 @@ class SDM {
#if defined ( ESP8266 ) // on esp8266
SDM(HardwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG, bool swapuart = SWAPHWSERIAL);
#elif defined ( ESP32 ) // on esp32
SDM(HardwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG, int8_t rx_pin=-1, int8_t tx_pin=-1);
SDM(HardwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG, int8_t rx_pin = SDM_RX_PIN, int8_t tx_pin = SDM_TX_PIN);
#else // on avr
SDM(HardwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG);
#endif
#else //software serial
#if defined ( ESP8266 ) || defined ( ESP32 ) // on esp8266/esp32
SDM(SoftwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG, int8_t rx_pin=-1, int8_t tx_pin=-1);
SDM(SoftwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN, int config = SDM_UART_CONFIG, int8_t rx_pin = SDM_RX_PIN, int8_t tx_pin = SDM_TX_PIN);
#else // on avr
SDM(SoftwareSerial& serial, long baud = SDM_UART_BAUD, int dere_pin = DERE_PIN);
#endif
Expand Down Expand Up @@ -220,15 +240,15 @@ class SDM {
#if defined ( ESP8266 )
bool _swapuart = SWAPHWSERIAL;
#elif defined ( ESP32 )
int8_t _rx_pin=-1;
int8_t _tx_pin=-1;
int8_t _rx_pin = -1;
int8_t _tx_pin = -1;
#endif
#else
#if defined ( ESP8266 ) || defined ( ESP32 )
int _config = SDM_UART_CONFIG;
int8_t _rx_pin=-1;
int8_t _tx_pin=-1;
#endif
int8_t _rx_pin = -1;
int8_t _tx_pin = -1;
#endif
long _baud = SDM_UART_BAUD;
int _dere_pin = DERE_PIN;
Expand Down
21 changes: 21 additions & 0 deletions SDM_Config_User.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,27 @@

//------------------------------------------------------------------------------

/*
* define user SDM_RX_PIN and SDM_TX_PIN for esp/avr Software Serial option
* or ESP32 with Hardware Serial if default core pins are not suitable
*/
#if defined ( USE_HARDWARESERIAL )
#if defined ( ESP32 )
#define SDM_RX_PIN 13
#define SDM_TX_PIN 15
#endif
#else
#if defined ( ESP8266 ) || defined ( ESP32 )
#define SDM_RX_PIN 13
#define SDM_TX_PIN 15
#else
#define SDM_RX_PIN 10
#define SDM_TX_PIN 11
#endif
#endif

//------------------------------------------------------------------------------

/*
* define user DERE_PIN for control MAX485 DE/RE lines (connect DE & /RE together to this pin)
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//sdm live page example by reaper7

//REMEMBER! uncomment #define USE_HARDWARESERIAL in SDM_Config_User.h file too.

#define READSDMEVERY 2000 //read sdm every 2000ms
#define NBREG 5 //number of sdm registers to read
//#define USE_STATIC_IP
Expand Down Expand Up @@ -33,6 +31,10 @@ TX SSer/HSer swap D8|15 |GND
#include <SDM.h> //https://github.com/reaper7/SDM_Energy_Meter

#include "index_page.h"

#if !defined ( USE_HARDWARESERIAL )
#error "This example works with Hardware Serial on esp8266, please uncomment #define USE_HARDWARESERIAL in SDM_Config_User.h"
#endif
//------------------------------------------------------------------------------
AsyncWebServer server(80);

Expand Down
Loading

0 comments on commit ab0af65

Please sign in to comment.