/* * ******************************************** * 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. Collected various values for the switches and adapted for Cayenne By Wouter-Jan Info Wemos D1 R2: https://wiki.wemos.cc/tutorials:get_started:get_started_in_arduino\ * http://arduino.esp8266.com/versions/1.6.5-947-g39819f0/doc/reference.html * Install additional Device Manager for Wemos: file - preferences - additional boardmanager: http://arduino.esp8266.com/stable/package_esp8266com_index.json * * Some additional useful links: * http://www.instructables.com/id/Programming-the-WeMos-Using-Arduino-SoftwareIDE/ * http://community.mydevices.com/t/data-types-for-cayenne-mqtt-api/3714 * http://community.mydevices.com/t/converting-cayenne-arduino-library-sketches-to-cayenne-mqtt-arduino/5759 * https://github.com/myDevicesIoT/Cayenne-MQTT-ESP8266 * https://roboticboyer.files.wordpress.com/2016/03/wemos_pins_00.pdf * https://opencircuit.nl/ProductInfo/1000178/WeMos-D1-Handleiding.pdf * Versions 0.01 Moved from the Arduino Library to Cayenne MQTT added Bitfrost library for HR-SR04 because pulsein give zero as result */ // WiFi network & Cayenne authentication info. #include char ssid[] = "ssid"; char wifiPassword[] = "wifi-pw"; char username[] = "user"; char password[] = "pass"; char clientID[] = "client"; // Device Waterlevel Cellar // some Cayenne stuff for Wemos D1 R2 with MQTT #include #define CAYANNE_DEBUG #define CAYANNE_PRINT Serial // library, variables for DHT22 (Temperature and Huminity) #include #define DHTPIN D8 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 float h, t, f; DHT dht(DHTPIN, DHTTYPE); // library, variables for HC-SR04 // Bitfrost library for HR-SR04 #include #define TRIG_PIN D6 // Trigger Pin #define ECHO_PIN D7 // Echo Pin HCSR04 hcsr04(TRIG_PIN, ECHO_PIN, 20, 4000); // establish variables for duration of the ping, // and the distance result in inches and centimeters: float duration; // Duration used to calculate distance float distance = 0 ; float distance_prev = 0; // various variables unsigned long lastMillis = 0; float uptime; // time system is up and running //int systemUpTimeMn, systemUpTimeHr, systemUpTimeDy; // used in calculation system uptime void setup() { Serial.begin(115200); Serial.println("sketch: MQTT_Cellar_Waterlevel"); Serial.print("compiled: "); Serial.print(__DATE__); Serial.print(" "); Serial.println(__TIME__); Serial.println("Following sensors are/should be connected"); Serial.println("-> Digital D6 = HC-SR04 Trigger"); Serial.println("-> Digital D7 = HC-SR04 Echo"); Serial.println("-> Digital D8 = DHT22"); Serial.println("-----------------------------------------"); // initialise DHT Serial.println("DHTxx begin!\n"); dht.begin(); Serial.println("Booted"); Serial.println("Connecting to Wi-Fi"); Cayenne.begin(username, password, clientID, ssid, wifiPassword); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println("WiFi connected"); } void loop() { Cayenne.loop(); //Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval. if (millis() - lastMillis > 10000) { lastMillis = millis(); //Misc Cayenne.virtualWrite(32, WiFi.RSSI()); //display signal WiFi connection //source: https://diyprojects.io/portable-wifi-scanner-oled-display-esp8266-signal-strength-connection-test-server/#.Whacbjco-Uk publishSystemUpTime(); //calculate and display system up time //Cayenne.virtualWrite(35, systemUpTimeDy); //Cayenne.virtualWrite(36, systemUpTimeHr); Cayenne.virtualWrite(37, uptime); DHTroutine(); Distance(); } } //Default function for processing actuator commands from the Cayenne Dashboard. //You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands. CAYENNE_IN_DEFAULT() { CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString()); //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message"); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void publishSystemUpTime(){ //source: http://community.mydevices.com/t/system-uptime-millis-to-dd-hh-mm-code-optimization-question-esp8266/4434 long millisecs = millis(); //systemUpTimeMn = int((millisecs / (1000*60)) % 60); //systemUpTimeHr = int((millisecs / (1000*60*60)) % 24); //systemUpTimeDy = int((millisecs / (1000*60*60*24)) % 365); uptime = int((millisecs / (1000*60*60)) % 24) + int((millisecs / (1000*60)) % 60) / 100.0; //Serial.println(uptime); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void DHTroutine() { // start of subroutine: 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(); // t = dht.readTemperature(); // Read temperature as Celcius (isFahrenheit = false) Serial.print("Humidity: "); Serial.print(h); Serial.println(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.println(" Celcius "); // 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!"); } else { Cayenne.virtualWrite(10, t, "temp", "c"); Cayenne.virtualWrite(11, h, "rel_hum", "p"); } } // End of subroutine: DHT22 - Temperature / Huminity //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ void Distance() { // Start of subroutine Sensor: HC-SR04 - Distance /* The following TRIG_PIN/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(TRIG_PIN, LOW); delayMicroseconds(2); digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, 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(ECHO_PIN, HIGH); // Calculate the distance (in cm) based on the speed of sound. distance = duration/58.2 + (196 - 55); //including correctie of measure height: 196 cm height cellar, 55 cm height tube) */ Serial.print("Distance Previous : "); Serial.println(distance_prev); // it looks like that the MQTT environment dislikes pulsein distance = (hcsr04.distanceInMillimeters() / 10.0 ) + 196 - 55 ; Serial.print("Distance : "); Serial.println(distance); if ( distance != 141.0 ) { // if SR04 did not give data if ( distance < distance_prev - 10 ) { distance = distance_prev ; Cayenne.virtualWrite(12, distance); Serial.print("Distance (prev) : "); Serial.println(distance_prev); } else { Cayenne.virtualWrite(12, distance); Serial.print("Distance (reported) : "); Serial.println(distance); distance_prev = distance; //logecho = distance; } } } // End of Section: HC-SR04 - Distance //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++