Cayenne / HCSR04 / NODEMCU

this is because most of your distance value is greater than 95 cm.
so when we do this

  y = map(distance, 95, 10, 0, 100);
  y = constrain(y, 0, 100); 

all values in the range of 95 to 10 are taken and rest are ignored.

I agree but why my sensors that are the same distance from the wall do not return me the same distance while the code is the same.

Distance: 36
[345044] Publish: topic 1, channel 10, value 69, subkey , key
[345045] Publish: topic 1, channel 11, value 69, subkey null, key t1
[345051] Publish: topic 1, channel 12, value 69, subkey null, key analog_sensor
Distance2: 0
[351135] Publish: topic 1, channel 20, value 105, subkey , key
[351137] Publish: topic 1, channel 21, value 100, subkey null, key t1
[351145] Publish: topic 1, channel 22, value 100, subkey null, key analog_sensor
[352233] Connection ok
Distance: 35
[357236] Publish: topic 1, channel 10, value 70, subkey , key
[357238] Publish: topic 1, channel 11, value 70, subkey null, key t1
[357244] Publish: topic 1, channel 12, value 70, subkey null, key analog_sensor
Distance2: 0
[363328] Publish: topic 1, channel 20, value 105, subkey , key
[363329] Publish: topic 1, channel 21, value 100, subkey null, key t1
[363337] Publish: topic 1, channel 22, value 100, subkey null, key analog_sensor
[364427] Connection ok
Distance: 35
[369431] Publish: topic 1, channel 10, value 70, subkey , key
[369432] Publish: topic 1, channel 11, value 70, subkey null, key t1
[369438] Publish: topic 1, channel 12, value 70, subkey null, key analog_sensor
Distance2: 0

Other exemple :

Distance2: 2
[521273] Publish: topic 1, channel 20, value 103, subkey , key
[521274] Publish: topic 1, channel 21, value 100, subkey null, key t1
[521282] Publish: topic 1, channel 22, value 100, subkey null, key analog_sensor
[522373] Connection ok
Distance: 40
[527377] Publish: topic 1, channel 10, value 65, subkey , key
[527379] Publish: topic 1, channel 11, value 64, subkey null, key t1
[527384] Publish: topic 1, channel 12, value 64, subkey null, key analog_sensor
Distance2: 2
[532483] Publish: topic 1, channel 20, value 103, subkey , key
[532484] Publish: topic 1, channel 21, value 100, subkey null, key t1
[532492] Publish: topic 1, channel 22, value 100, subkey null, key analog_sensor
[533583] Connection ok
Distance: 40
[538586] Publish: topic 1, channel 10, value 65, subkey , key
[538588] Publish: topic 1, channel 11, value 64, subkey null, key t1
[538594] Publish: topic 1, channel 12, value 64, subkey null, key analog_sensor
Distance2: 2

Have you inverted sensor ? Perhaps a defect unit ?

1 Like

might be. give it a try. or remove all cayenne code and test only two sensor code.

So, without the cayenne code everything is OK

Distance2: 45
Distance: 45
Distance2: 44
Distance: 44
Distance2: 44
Distance: 44
Distance2: 167
Distance: 167
Distance2: 167
Distance: 167
Distance2: 167

n precision, if I comment on the Cayenne.virtualWrite lines for both sensors the measurements are good in the monitor.

void loop() {
Cayenne.loop();

//HCSR04 NĀ°1

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
y = map(distance, 95, 10, 0, 100);
y = constrain(y, 0, 100);
delay(5000);
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Cayenne.virtualWrite(10, 105-distance);
//Cayenne.virtualWrite(11, y, ā€œt1ā€, ā€œnullā€);
//Cayenne.virtualWrite(12, y, ā€œanalog_sensorā€, ā€œnullā€);

//HCSR04 NĀ°2

// Clears the trigPin NĀ°2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

// Sets the trigPin NĀ°2 on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

// Reads the echoPin NĀ°2, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);

// Calculating the distance NĀ°2
distance2 = duration2 * 0.034 / 2;
// Prints the distance NĀ°2 on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);
y2 = map(distance2, 95, 10, 0, 100);
y2 = constrain(y2, 0, 100);
delay(5000);
if (millis() - lastMillis2 > 10000) {
lastMillis2 = millis();
//Cayenne.virtualWrite(20, 105-distance2);
//Cayenne.virtualWrite(21, y2, ā€œt1ā€, ā€œnullā€);
//Cayenne.virtualWrite(22, y2, ā€œanalog_sensorā€, ā€œnullā€);

}
}
}

last test done:
if i comment only the 3 lines cayenne.virtualwrite of the first sensor it works and conversely if i comment only the 3 lines cayenne.virtualwrite the second sensor it works too.

Itā€™s OK :
void loop() {
Cayenne.loop();

//HCSR04 NĀ°1

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
y = map(distance, 95, 10, 0, 100);
y = constrain(y, 0, 100);
delay(5000);
if (millis() - lastMillis > 10000) {
lastMillis = millis();
//Cayenne.virtualWrite(10, 105-distance);
//Cayenne.virtualWrite(11, y, ā€œt1ā€, ā€œnullā€);
//Cayenne.virtualWrite(12, y, ā€œanalog_sensorā€, ā€œnullā€);

//HCSR04 NĀ°2

// Clears the trigPin NĀ°2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

// Sets the trigPin NĀ°2 on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

// Reads the echoPin NĀ°2, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);

// Calculating the distance NĀ°2
distance2 = duration2 * 0.034 / 2;
// Prints the distance NĀ°2 on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);
y2 = map(distance2, 95, 10, 0, 100);
y2 = constrain(y2, 0, 100);
delay(5000);
if (millis() - lastMillis2 > 10000) {
lastMillis2 = millis();
Cayenne.virtualWrite(20, 105-distance2);
Cayenne.virtualWrite(21, y2, ā€œt1ā€, ā€œnullā€);
Cayenne.virtualWrite(22, y2, ā€œanalog_sensorā€, ā€œnullā€);

}
}
}

itā€™sOk

void loop() {
Cayenne.loop();

//HCSR04 NĀ°1

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance = duration * 0.034 / 2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
y = map(distance, 95, 10, 0, 100);
y = constrain(y, 0, 100);
delay(5000);
if (millis() - lastMillis > 10000) {
lastMillis = millis();
Cayenne.virtualWrite(10, 105-distance);
Cayenne.virtualWrite(11, y, ā€œt1ā€, ā€œnullā€);
Cayenne.virtualWrite(12, y, ā€œanalog_sensorā€, ā€œnullā€);

//HCSR04 NĀ°2

// Clears the trigPin NĀ°2
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);

// Sets the trigPin NĀ°2 on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

// Reads the echoPin NĀ°2, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);

// Calculating the distance NĀ°2
distance2 = duration2 * 0.034 / 2;
// Prints the distance NĀ°2 on the Serial Monitor
Serial.print("Distance2: ");
Serial.println(distance2);
y2 = map(distance2, 95, 10, 0, 100);
y2 = constrain(y2, 0, 100);
delay(5000);
if (millis() - lastMillis2 > 10000) {
lastMillis2 = millis();
//Cayenne.virtualWrite(20, 105-distance2);
//Cayenne.virtualWrite(21, y2, ā€œt1ā€, ā€œnullā€);
//Cayenne.virtualWrite(22, y2, ā€œanalog_sensorā€, ā€œnullā€);

}
}
}

I just redo all my cabling and the code is OK now.

Thank you so much for the time spent helping me

you can also use NewPing library for ultrasonic sensors. much better and easier to add multiple sensors.