-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsensorCorrente.cpp
More file actions
200 lines (126 loc) · 3.3 KB
/
Copy pathsensorCorrente.cpp
File metadata and controls
200 lines (126 loc) · 3.3 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//#include "Arduino.h"
#include <sensorCorrente.h>
sensorCorrente::sensorCorrente(int _pino,sensorCorrente_type modelo,char _tipomedicao,char _fase){ //construction method for the different types of sensors
pino =_pino;
tipomedicao =_tipomedicao;
fase =_fase;
switch(modelo){
case T5B:
fatorConversao = 0.185;
break;
case T20A:
fatorConversao = 0.100;
break;
case T30A:
fatorConversao = 0.066;
break;
case SCT013A5:
fatorConversao = 0.2;
break;
case SCT013A10:
fatorConversao = 0.1;
break;
case SCT013A15:
fatorConversao = 0.066;
break;
case SCT013A20:
fatorConversao = 0.05;
break;
case SCT13A25:
fatorConversao = 0.04;
break;
case SCT013A30:
fatorConversao = 0.033;
break;
case SCT013A50:
fatorConversao = 0.02;
break;
case SCT013A60:
fatorConversao = 0.016;
break;
}
}
void sensorCorrente::calibrar(){ //calibrates the sensor (no current must flow throught it)
float n = 0;
for(int i =0;i<10;i++){ //calibrates de offset 2,5 V
n = n + analogRead(pino);
}
zero = n/10;
//calibrates VCC
long result;
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2);
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1125300L / result;
trueVCC = (float) result/1000;
}
int sensorCorrente::getzero(){
return(zero);
}
float sensorCorrente::getTrueVcc(){
return (trueVCC);
}
char sensorCorrente::getFase(){
return(fase);
}
float sensorCorrente::calculaRMS(){ //Calculates RMS value
float periodo= (float) 1000/60;
float soma=0;
float M;
uint32_t inicio = millis();
int N=0;
while (millis()-inicio < periodo){
M=analogRead(pino)-zero;
N++;
soma += M*M;
}
return (sqrt(soma/N)/1024*trueVCC)/fatorConversao;
}
float sensorCorrente::calculaCorrenteDC(){ //calculates DC value
float A=0;
for(int n=0; n<5; n++){
A += analogRead(pino)-zero;
}
A = A/5;
return (((float) (trueVCC*A))/1024)/fatorConversao;
}
float sensorCorrente::medir(){//simples function for measuring a sensor value
float I;
if(tipomedicao == 'A')
I = calculaRMS();
if(tipomedicao == 'D')
I = calculaCorrenteDC();
return I;
}
void sensorCorrente::printCalibrar(){
Serial.println( (String("Sensor de CORRENTE da fase ") + getFase() +String(" Calibrado!"));
Serial.println(String("Valor de calibracao:") + getzero());
Serial.println(String("Valor de Vcc:") + getTrueVcc());
Serial.println();
}
void sensorCorrente::printCorrente(float valor){
Serial.println(String("Fase ") + getFase() + String(": ") + valor + String(" A"));
}
void enviar(int valor) {
int count = 0;
int flag = 0;
char abc[60];
dtostrf(valor, 51, 3, abc);
char * str = (char *) malloc(1 + strlen(s6) + strlen(abc) );
strcpy(str, s6);
strcat(str, abc);
Serial.println(str);
while (flag == 0) {
if (flag == 0) {
flag = RotinaGSM(str);
count++;
}
if (count > 5)
return 0;
if (flag == 1)
return 1;
}
}