Dear Cayenne-Team,
I am transmitting 6 values over MQTT using a SIM800 module and a sim Card. More than 95% of the time, the transmissions work just fine but occasionally, the distance value of that sensor is not transmitted and it just shows 0:
In the data, this looks as follows:
Only the distance sensor, which is just read out over pulsewidth, isn’t transmitting. Is this a problem that I should address hardware-side?
Thank you!
Yes, i guess it is something to do with the code on the hardware device side. At what rate are you sending the data?
Hi Shramik, I am sending the code after each 10min.
Here is my code:
calculate_distance_5(temperature);
double calculate_distance_5(double temperature) {
delay(300);
digitalWrite(enable_5, HIGH);
Serial.println("Starting with the US sensor measurement ");
for (int i = 0; i < ARRAY_SIZE - 1; i++) {
distance_array_5[i] = calculateDistanceUS_5(temperature);
}
delay(50);
digitalWrite(enable_5, LOW);
}
double calculateDistanceUS_5(double temperature) {
float pwinput = pulseIn(pwpin, HIGH);
pwinput = pwinput / 10;
Serial.print(pwinput); Serial.println(" cm from pulsewidth");
/* Temperature calibration not required since Maxbotix sensors have an on-board temperature sensor
float coeff = (sqrt(temperature + 273.15) / 17.12);
float distance_compansated = distance_20 * coeff;
*/
return (double)(pwinput);
}
Thank you!
can you share the entire code.
Oh shit shramik, sorry for wasting your time, I’ve found the error I think. I am using another function to average the value and just add ‘good ones’ within a certain portion of one standard deviation:
double getgoodavg(double * val, int arrayCount, double avrg, double std_dev)
{
long total = 0;
double avg = avrg;
double dev = std_dev;
int j = 0;
for (int i = 0; i < arrayCount; i++) {
if (abs(val[i]-avg) < 0.2 * dev)
{
j = j + 1;
}
}
if (j == 0)
{
Serial.println(“No valid values have been detected”);
return avg;
}
double distance_array_good_5[j] = {0};
for (int i = 0; i < arrayCount; i++) {
int j = 0;
if (abs(val[i]-avg) < 0.2 * dev)
{
j = j + 1;
distance_array_good_5[j] = val[i];
}
}
for (int i = 0; i < j; i++) {
total += val[i];
}
Serial.print("Size of goodvaluearray. "); Serial.println(j);
if (j < 10)
{
Serial.println(“Detected under 10 good counts, setting goodaverage to average”);
float goodaverage = avg;
}
else
{
float goodaverage = total / float(j);
return goodaverage;
}
}
My mistake was that I’ve put the } too early at the very end so in some cases, I’ll call the function but no value will be returned.