-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_rcv_cmd_pzem.ino
115 lines (74 loc) · 2.23 KB
/
send_rcv_cmd_pzem.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
uint8_t sendBuffer[8];
String respBuffer[25];
float V,C,P,E,F,pf,Alarm;
int idx = 0;
void setup() {
Serial.begin(9600);
Serial3.begin(9600);
Serial.println("Starting.......");
}
void loop() {
sendBuffer[0]=0x01;
sendBuffer[1]=0x04;
sendBuffer[2]=0x00;
sendBuffer[3]=0x00;
sendBuffer[4]=0x00;
sendBuffer[5]=0x0A;
sendBuffer[6]=0x70;
sendBuffer[7]=0x0D;
Serial.println("");
Serial.println("sendCMD done!");
Serial3.write(sendBuffer,8);
while(Serial3.available()){ // wait for a character
int incomingByte = Serial3.read();
if(idx==25){idx=0;}
respBuffer[idx] = String(incomingByte, HEX);
Serial.print(incomingByte,HEX);
Serial.print(' ');
idx=idx+1;
}
Serial.println("");
V=findOut(3, 4)/10;
Serial.print("Voltage: ");
Serial.print(V);
Serial.println(" V");
C=findOut(5, 6)/1000;
Serial.print("Current: ");
Serial.print(C);
Serial.println(" A" );
P=findOut(9, 10)/10;
Serial.print("Power: ");
Serial.print(P);
Serial.println(" W");
E=findOut(5, 6)/1000;
Serial.print("Energy: ");
Serial.print(E);
Serial.println(" kWh");
F=findOut(17, 18)/10;
Serial.print("Frequency: ");
Serial.print(F);
Serial.println(" Hz");
pf=findOut(19, 20)/100;
Serial.print("pf: ");
Serial.println(pf);
delay (9000);
}
float findOut(int start_byte_position, int end_byte_position){
int H_byte,L_byte;
String addH_L;
if (end_byte_position == start_byte_position+1){
H_byte=start_byte_position;
L_byte=end_byte_position;
addH_L= respBuffer[H_byte]+respBuffer[L_byte];
}
else if(end_byte_position > start_byte_position+1){
H_byte=start_byte_position;
L_byte=end_byte_position;
addH_L= respBuffer[H_byte]+respBuffer[H_byte+1]+respBuffer[L_byte-1]+respBuffer[L_byte];
}
//Serial.print("String formated hex: ");Serial.println(addH_L);
char char_array[addH_L.length() + 1];
addH_L.toCharArray(char_array, (addH_L.length() + 1));
float num = (int)strtol(char_array, NULL, 16);
return num;
}