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
The repeattask function has normally its own tab:
/* HC-SR04 Sensor
https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
http://www.tautvidas.com/blog/2012/08/distance-sensing-with-ultrasonic-sensor-and-arduino/
moreinfo: http://m5.img.dxcdn.com/CDDriver/CD/sku.133696.pdf
Output distance http://keyes-arduino.taobao.com
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.begin(115200);
Serial.println("info: sketch_Distance");
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.println(__TIME__);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
timer.setInterval(1000, RepeatTask);
}
CAYENNE_OUT(V4) {
Cayenne.virtualWrite(V4,distance); // virtualpin 4 plus distance
}
void loop() {
Cayenne.run(); // Initiates Blynk
timer.run(); // 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);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
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;
Serial.println(distance);
}