E
@Quiche-Lorraine sagte in Luftfeuchtigkeits- und Temperatursensor funktioniert anscheinend nur sporadisch:
Interpretiert deine readWhile() Funktion ein PWM Signal? Sie sieht nämlich empfindlich gegenüber Synchronisations- bzw. Timingfehler aus.
Danke für den Tipp!
Hab es hinbekommen, jetzt funktioniert nahezu jede Messung:
class Temp
{
public:
const int LOW_VAL = 0;
const int HIG_VAL = 1;
const int pin1;
int raw[6 * 8];
int h, c1, c2, sum;
float humi, temp;
Temp(const int pin1) : pin1(pin1)
{
if (!initFinish)
{
initFinish = TRUE;
wiringPiSetup();
}
}
bool isValidTemp() const
{
return sum != 0 && sum == (h + c1 + c2);
}
bool readTemp()
{
for (size_t i = 0; i < 10; i++)
{
readRaw();
readNumber(&h, 0, 8);
readNumber(&c1, 16, 24);
readNumber(&c2, 24, 32);
readNumber(&sum, 32, 40);
if (isValidTemp())
{
humi = h;
temp = (c1 * 10 + c2) / 10.0;
return TRUE;
}
delay(3000);
}
return FALSE;
}
void readNumber(int *x, int a, int b)
{
*x = 0;
for (size_t i = a; i < b; i++)
{
*x |= (raw[i] << (7 - (i - a)));
}
}
void readRaw()
{
pinMode(pin1, OUTPUT);
delay(1);
digitalWrite(pin1, LOW_VAL);
delay(20);
digitalWrite(pin1, HIG_VAL);
// delayMicroseconds(30);
delayMicroseconds(20);
pinMode(pin1, INPUT);
int len0 = 200;
int a[len0] = {};
readPulses(a, len0);
cout << "Raw: ";
for (int i : a)
{
cout << i << ", ";
}
cout << endl;
int start = 0;
int len1 = 0;
for (size_t i = 0; i < len0;)
{
int j = i;
while (a[j] >= 50 && a[j] < 60)
{
j += 2;
}
int len2 = (j - i);
if (len2 > len1)
{
start = i;
len1 = len2;
}
i = j + 2;
}
for (size_t i = 1; i < len0;)
{
int j = i;
while (a[j] >= 50 && a[j] < 60)
{
j += 2;
}
int len2 = (j - i);
if (len2 > len1)
{
start = i;
len1 = len2;
}
i = j + 2;
}
cout << start << " , " << len1 << endl;
for (size_t i = start + 1, j = 0; i < start + len1 && j < 6 * 8; i += 2, j++)
{
if (a[i] > 47)
{
raw[j] = 1;
}
else
{
raw[j] = 0;
}
}
}
int readWhile(int val, int micros)
{
int c = 0;
while (digitalRead(pin1) == val)
{
delayMicroseconds(1);
c++;
if (c == micros)
{
break;
}
}
return c;
}
void readPulses(int *a, int len)
{
for (int i = 0; i < len; i++)
{
if (i % 2 == 0)
a[i] = readWhile(LOW_VAL, 100);
else
a[i] = readWhile(HIG_VAL, 100);
}
}
};
ostream &operator<<(ostream &str, Temp const &t)
{
string raw = "";
for (int i : t.raw)
{
raw += to_string(i);
}
str << "Temperatur: " << t.temp
<< " Luftfeuchtigkeit: " << t.humi
<< " valid: " << t.isValidTemp()
<< " raw: " << raw;
return str;
}
Temp temp(4);
Raw: 69, 86, 54, 24, 54, 69, 54, 23, 54, 24, 54, 69, 54, 71, 53, 70, 53, 71, 54, 24, 54, 24, 54, 24, 54, 24, 54, 23, 53, 24, 54, 24, 53, 25, 53, 24, 54, 23, 53, 23, 54, 69, 54, 23, 53, 70, 53, 71, 53, 71, 53, 24, 54, 24, 54, 23, 54, 24, 53, 24, 53, 70, 53, 24, 54, 24, 53, 24, 54, 70, 53, 70, 53, 24, 54, 70, 53, 25, 52, 70, 52, 24, 54, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100, 0, 100,
2 , 82
1
Temperatur: 23.4 Luftfeuchtigkeit: 79 valid: 1 raw: 010011110000000000010111000001000110101010000000
(Bitte nicht wundern, ich sitze nicht in einer Aufgusssauna, die Luftfeuchtigkeit des Sensors ist etwas ungenau )