SDM library is superb. But how to use with RS485? May any brilliant brother willing to share here? tq
SDM library is superb. But how to use with RS485? May any brilliant brother willing to share here? tq
Explore... Chat... Share...
#include <SDM.h> //import SDM template library
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
//NODEMCU MAPPING PORT WITH ARDUINO
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
#define ASCII_ESC 27
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
char bufout[10];
float v,a,p,fre,pf,tu;
SDM<2400, D9, D10> sdm; //SDM120T baud, rx pin, tx pin
//SDM<4800, 12, 13> sdm; //SDM220T 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<> sdm;
//////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
USE_SERIAL.begin(115200);//initialize serial
USE_SERIAL.flush();
WiFiMulti.addAP("myssid", "mypass");
while ((!(WiFi.status() == WL_CONNECTED))){
delay(300);
Serial.print("..");
}
Serial.println("Connected");
Serial.println("Your IP is");
Serial.println((WiFi.localIP()));
sdm.begin(); //initalize SDM120 communication baudrate
//SET PIN FOR RECEIVING DATA FROM RS485
pinMode(D2, OUTPUT);
digitalWrite(D2, LOW); //receiving = LOW; Sending = HIGH
}
//////////////////////////////////////////////////////////////////////////////
void loop() {
//sprintf(bufout,"%c[1;0H",ASCII_ESC);
// Serial.print(bufout);
v=(sdm.readVal(SDM120C_VOLTAGE)); //display voltage
delay(10);
a=(sdm.readVal(SDM120C_CURRENT)); //display current
delay(10);
p=(sdm.readVal(SDM120C_POWER)); //display power
delay(10);
fre=(sdm.readVal(SDM120C_FREQUENCY)); //display frequency
delay(10);
pf=(sdm.readVal(SDM120C_POWER_FACTOR)); //display frequency
delay(10);
tu=(sdm.readVal(SDM120C_TOTAL_ACTIVE_ENERGY)); //display frequency
Serial.print("Volts: ");Serial.println(v);
Serial.print("current: ");Serial.println(a);
Serial.print("Power: ");Serial.println(p);
Serial.print("Frequency: ");Serial.println(fre);
Serial.print("Power factor: ");Serial.println(pf);
Serial.print("Total Energy: ");Serial.println(tu);
delay(5000); //wait a while before next loop
}
reaper7 wrote:@manmoncang
You are using hardware Serial for communication with pc (debug) and for communication with sdm ??
Default sdm lib use SoftwareSerial and can not be set on the same pins as hardware serial.
First of all, You have to select another pins (not D9 gpio3 & D10 gpio1) for communicating with sdm,
second, check everything (communication) on example attached to sdm lib
if You are using rs485 interface with pin for flow control, then You must initialise sdm library with pointing to this pin:
```
#define YOUR_TX_PIN D7
#define YOUR_RX_PIN D6
#define YOUR_DERE_CONTROL_PIN D2
SDM<2400, YOUR_TX_PIN, YOUR_RX_PIN, YOUR_DERE_CONTROL_PIN> sdm;
```
btw, for what You are redefine all nodemcu pins?
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]