Thanks!
Of course I will share code. I tried to attach the PDF I used for reference but the forum considers me too new and will not allow me. I’ll see what I can come up with to show how it’s connected but for now here’s an excerpt from my RD200 helper class. Some of this is stolen directly from the PDF I wanted to attach and some of it is my own creation. This probably references a couple member variables that I forgot to include but if there’s anything more I can elaborate on let me know and I’ll do my best!
void RD200::Handle() {
this->ProcessReceivedData();
if ((millis() - this->lastRequestMillis) > this->pollInterval) {
this->Request();
}
}
void RD200::ProcessReceivedData() {
int recSize = mySerial.available();
if (recSize < 8) { return; }
byte recData[8];
int offset = recSize - 8;
for (int i = 0; i < recSize; i++) {
if (i < offset) {
//skip everything before the final 8 bytes, it's garbage that accumulated in the buffer
//NOTE: this has never actually happened because we purge the buffer in the request call, it's just paranoia
} else {
recData[i - offset] = (byte)this->mySerial.read();
}
}
if (this->CheckSum(recData, 8) == false) {
return;
}
this->lastResponseMillis = millis();
this->outstandingRequest = false;
this->currentState = recData[3];
switch (this->currentState) {
case 0x00:
this->lastReading = -1.0f;
break; // Power On ~ 200sec
case 0x01:
this->startupMinutesElapsed = recData[4];
this->lastReading = -1.0f;
break; // 200sec to 1hour
case 0x10:
// according to the guide this means:
// Measuring time is within 30min and radon count is over 10
// not really sure if that means less than 30 minutes or between 30 and 60
this->startupMinutesElapsed = recData[4];
this->lastReading = -1.0f;
break;
case 0x02:
this->lastReading = ((recData[5] * 100) + recData[6]);
break; // After 1hour
case 0xE0:
this->lastReading = -1.0f;
break; // Detect vibrations
}
}
bool RD200::CheckSum(byte data[], int data_size) {
int sum = 0;
for (int i = 1; i < data_size - 1; i++) {
sum += data[i];
sum %= 256;
}
if (255 - sum == data[data_size - 1]) return true;
else return false;
}
void RD200::Init() {
this->mySerial.begin(19200);
byte period_time = (byte) min(max(this->measurementInterval, 0), 60);
byte send_data[5] = {0x02, cmdRD200M_SEND_PERIOD_SET, 0x01, period_time, 0xFF - (cmdRD200M_SEND_PERIOD_SET + 0x01 + period_time)};
this->mySerial.write(send_data, 5);
}
void RD200::ResetSensor() { // don't do this unless you really want to, it clears all measurement data and restarts the 1 hour timeout
byte send_data[5] = {0x02, cmdRD200M_RESET, 0x00, 0xFF - (cmdRD200M_RESET)};
this->mySerial.write(send_data, 5);
}
void RD200::SetMeasurementInterval(int intervalMinutes) { // to be called BEFORE Init() only
this->measurementInterval = min(max(intervalMinutes, 0), 60);
}
void RD200::Request() {
while (mySerial.available()) { // throw away anything that could be in the buffer
mySerial.read();
}
byte send_data[4] = {0x02, cmdRD200M_RESULT_QUERY, 0x00, 0xFF - cmdRD200M_RESULT_QUERY};
mySerial.write(send_data, 4);
this->lastRequestMillis = millis();
this->outstandingRequest = true;
}