WJ4IoT
Published © GPL3+

Ground Water level Monitored with WeMos D1 R2 and HR-SC04

Combining stuff together to achieve the objective; monitor the Ground Water level.

BeginnerFull instructions provided1 hour5,542
Ground Water level Monitored with WeMos D1 R2 and HR-SC04

Things used in this project

Hardware components

WeMos D1 R2
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
or use the DHT11
×1
PVC 70mm Raintube
×1

Software apps and online services

Cayenne
myDevices Cayenne

Story

Read more

Schematics

Photo schematics of Wemos D1 R2

Schematic version of connections (not colorblind proof)

Code

sketch_DISTANCE_Cayenne.ino

Arduino
Main sketch for WeMos D1 R2
/* 
 *  ********************************************
 *  WARNING DISCONNECT SENSORS USING WEMOS D1 * 
 *  *******************************************
   
   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 (subject 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;

// for DHT22
#include <DHT.h>
// DHT22 temp & vochtmeter
#define DHTPIN D6        // what digital pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);

// for HC-SR04
#define echoPin D4 // Echo Pin
#define trigPin D5 // Trigger Pin
float h, t, f; //declare variable used in HC-SR04

// establish variables for duration of the ping, 
// and the distance result in inches and centimeters:
long duration, distance, distance_new, logecho; // 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_Cayenne");
  Serial.print("compiled: ");
  Serial.print(__DATE__);
  Serial.print(" ");
  Serial.println(__TIME__);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  timer.setInterval(60000, RepeatTask);
  timer.setInterval(6000, RepeatEcho);

  // initialise DHT
  Serial.println("DHTxx begin!\n");
  dht.begin();
}


CAYENNE_OUT(V3) {
  Cayenne.virtualWrite(V3,logecho); // virtualpin 3 plus distance
}

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

CAYENNE_OUT(V5) {
  Cayenne.virtualWrite(V5,t); // virtualpin 5 (temperature)
}

CAYENNE_OUT(V6) {
  Cayenne.virtualWrite(V6,h); // virtualpin 6 (huminity)
}

void loop() {
  Cayenne.run(); // Initiates Blynk
  timer.run();   // Initiates SimpleTimer
}

repeat.ino

Arduino
Sub-sketch should be stored in the same sketch folder in Arduino and linked into the main sketch
void RepeatEcho() {

// Start of Section: HC-SR04 - Distance
  /* 
  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_new = duration/58.2 + (196 - 55); //including correctie of measure height: 196 cm height cellar, 55 cm height tube) 
  Serial.println(distance_new);
  if (145 <= distance_new <= 300 && distance_new < distance) { 
    distance = distance_new;                                                  
    }

}

void RepeatTask() {

 // Start of Section: HC-SR04 - Distance
 logecho = distance;
 distance = 300;
 // End of Section: HC-SR04 - Distance

  // start of Section: DHT22 - Temperature / Huminity 
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  // End of Section: DHT22 - Temperature / Huminity 
  }

}

Credits

WJ4IoT

WJ4IoT

0 projects • 2 followers

Comments