A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By manmoncang
#65404 May anybody brilliant here can fix my problem?

I cannot get value from SDM120. Hopefully anybody here can help. tq

Info:
    I use nodemcu.
    I connect RS485 to nodemcu as below:
  • RE tie with DE to pin D2 in nodemcu.
  • DI to Tx
  • RO to Rx

Dont why below code did not get any value from SDM120.

Code: Select all#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

}
User avatar
By reaper7
#65470 @manmoncang

this lib never been tested with MAX485 in real world, but with proper configuration, it should work properly

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?
Last edited by reaper7 on Tue May 02, 2017 3:28 am, edited 1 time in total.
User avatar
By manmoncang
#65471 Siperb and very very useful answer.


Many thanks.

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?