I am using a power meter connected to a Nodemcu through a Max485. I find difficulties on programming the Nodemcu to get the data from the sensor. I have try to use some testing software to directly send a request to the sensor from the computer and we could retrieved the correct output. But we cant get it from using the Nodemcu.
The code now we have is
#include <ModbusRTU.h>
ModbusRTU mb;
#include <SoftwareSerial.h>
SoftwareSerial mod;
#define DE 2 // For MAX485 chip
#define RE 6
#define TX 15 // RX, TX
#define RX 13
#define RS485receive LOW
#define RS485transmit HIGH
SoftwareSerial S(RX, TX); // (RX , TX)
void setup() {
Serial.begin(9600);
S.begin(9600, SWSERIAL_8N1);
delay(1000);
}
float voltage;
void loop() {
digitalWrite(RE, HIGH); // init Transmit
digitalWrite(DE, HIGH);
byte request[] = {0xD1, 0x03, 0x00, 0x0B, 0x00, 0x01, 0xE6, 0x58}; // inquiry frame
S.write(request, sizeof(request));
delay(100);
S.flush();
digitalWrite(DE, LOW); // Init Receive
digitalWrite(RE, LOW);
byte buf[16];
S.readBytes(buf, 16);
Serial.print("voltage : ");
for( byte i=0; i<50; i++ ) {
Serial.print(buf[i], HEX);
Serial.print(" ");
}
Serial.print(" ==> ");
Serial.print(buf[3]);
Serial.print("V");
Serial.println();
delay(10000);
}
This is the result we get from the serial monitor