HR-SC04 in combination with Wemos D1 R2 (or UNO+W5100)

There are some examples on Cayenne already of this sensor but my starting point was a variant I found on Blynk.
This variant will reduce the uploads to the Cayenne server without losing connection.
The sketch works for both an Arduino UNO plus W5100 as a Wemos D1 R2; (un)comment the relevant lines.
The sketch is further enriched with some additional info and URL’s.

Two remarks:
• Disconnected the sensor HC-SC04 when upload the sketch to the Wemos D1 R2.
• Also the Wemos hangs when it is connected to a PC; make sure you use a separate power source.
With this remarks in mind you avoid having an issue :slight_smile:

The repeattask function has normally its own tab:

/* HC-SR04 Sensor
   Output distance
   This sketch reads a HC-SR04 ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
   The circuit:
  * VCC connection of the sensor attached to +5V
  * GND connection of the sensor attached to ground
  * TRIG connection of the sensor attached to digital pin 5 (ubject to change)
  * ECHO connection of the sensor attached to digital pin 4
   Original code for Ping))) example was created by David A. Mellis
   Adapted for HC-SR04 by Tautvidas Sipavicius
   The speed of sound is 340 m/s or 29 microseconds per centimeter.
   The ping travels out and back, so to find the distance of the
   object we take half of the distance travelled.
   This example code is in the public domain.

//#define CAYENNE_PRINT Serial   // Comment this out to disable prints and save space
#include <SPI.h>
#include <SimpleTimer.h>
//#include <CayenneEthernet.h> //when using W5100
// with the ESP8266 module
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
// Your network name and password.
char ssid[] = "xxxx";
char password[] = "xxxx";

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "xxxx";

SimpleTimer timer;

#define echoPin D4 // Echo Pin
#define trigPin D5 // Trigger Pin

// establish variables for duration of the ping, 
// and the distance result in inches and centimeters:
long duration, distance; // Duration used to calculate distance

void setup() {
  //Cayenne.begin(token); //with W5100
  Cayenne.begin(token, ssid, password); //with ESP8266
  Serial.println("info: sketch_Distance");
  Serial.print("compiled: ");
  Serial.print(" ");
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  timer.setInterval(1000, RepeatTask);

  Cayenne.virtualWrite(V4,distance); // virtualpin 4 plus distance

void loop() {; // Initiates Blynk;   // Initiates SimpleTimer

void RepeatTask() {
 The following trigPin/echoPin cycle is used to determine the
 distance of the nearest object by bouncing soundwaves off of it.
 The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 Give a short LOW pulse beforehand to ensure a clean HIGH pulse.
 digitalWrite(trigPin, LOW); 
 digitalWrite(trigPin, HIGH);
 digitalWrite(trigPin, LOW);
 // Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
 duration = pulseIn(echoPin, HIGH);
 // Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

Thanks for sharing your sketch file with us, have any plans for what you’re going to do with the sensor?

I’m moving this out of the bugs/issues category as I don’t see a Cayenne related issue here, if I’m missing it please let me know!

The “bug/issue” is that is the sketch will not be uploaded to the Wemos but will result into an error:

Sketch uses 263,701 bytes (25%) of program storage space. Maximum is 1,044,464 bytes.
Global variables use 34,828 bytes (42%) of dynamic memory, leaving 47,092 bytes for local variables. Maximum is 81,920 bytes.
warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed

and will not (always?) run when it is connected to a PC. Both “bug/issue” can be resolved to follow this hints and tips. Both errors were for me not directly clear and I can imagine that other newbies like me will experiences similar problems. Perhaps FAQ’s was a better place to share but is it a frequent problem?

About the project itself: The neighbourhood where I live has often problems with flooded cellars due to high groundwater levels. For my house this is already solved by two pumps. The pumps are installed in two holes in my cellar and will start to drain when the water level is 10 cm below my cellar floor. For the project I will add a rain pipe in the hole and on top a standard cover with the HR-SC04 sensor. I realise that humidity will not be my best friend and also will need to add some additional lines to capture the maximum water level. The results i want to share on internet (and perhaps install this at other locations (depending on if other people are interested in this project). The beauty of the Wemos is the bigger memory (though no needed) and half the price of the UNO+W5100. However the cellar location might have WiFi problems; you aint always get what you want. Know for sure that people would price themselves fortunate if they find their groundwater at 2,3 meter from ground level.