Hello there
I’ve made a project to measure the water niveau in my rain well. I’m using an arduino but I can’t get the niveau of the water in my homey. Here is my code:
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Homey.h>
#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin
const char* ssid = *******;
const char* password = ************;
const char* name = "Waterput";
long duration, distance; // Duration used to calculate distance
int water = 0;
unsigned long previousMillis = 0;
void setup()
{
Serial.println("Starting ethernet..."); //If this is the last message you see appear in the serial monitor...
WiFi.begin(ssid, password);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Homey.begin(name);
Homey.setClass("sensor");
Homey.addCapability("niveau");
}
void loop()
{
Homey.loop();
unsigned long currentMillis = millis();
if (currentMillis-previousMillis >= 1000) { //Code in this if statement is run once every second
previousMillis = currentMillis;
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in mm) based on the speed of sound.
distance = duration * 0.34/2;
water = (1650 - distance) * 3.03;
updateSensor();
}
}
void updateSensor() {
Serial.println(water);
Homey.setCapabilityValue("niveau", water);
}
Thanks in advance
Greetings
Maarten