Domoticz ESP8266 + senzor HC-SR04
please check the code for Domoticz

#include
const char* ssid = ”;
const char* password = ”;
WiFiClient client;
const char * domoticz_server = ‘192.168.0.41’;
int port = 8080; //Domoticz port
int idx = 17; //IDX for virtual sensor, found in Setup -> Devices

#define echoPin D7 // Echo Pin
#define trigPin D6 // Trigger Pin

float distance= 0; // Duration used to calculate distance
long duration ;
unsigned int values_avg = 0;
void setup()

{
Serial.begin (115200);
delay(10);
Serial.println();
Serial.println();
Serial.print(‘Connecting to ‘);
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(‘.’);

}
Serial.println(”);
Serial.println(‘WiFi connected’);
Serial.println(‘IP address: ‘);
Serial.println(WiFi.localIP());
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
delay(2000);
}

void loop()
{
printInfo();
delay(600); // Wait 60 seconds
Serial.print(‘Starting variables.’);
delay(500);
Serial.print(‘..’);
delay(250);
Serial.println(‘…’);
delay(250);

distance = 0;
client.print(idx);
Serial.print(‘Average values: ‘);
Serial.println(values_avg);
Serial.println(distance);
distance = map(values_avg,0,400,0,100);
/* 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 cm) based on the speed of sound.
distance = duration/58.2;
Serial.println(distance);
//Delay 50ms before next reading.
delay(5000);
}

void printInfo()
{
// Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=distance

if (client.connect(domoticz_server,port)) {
Serial.println(‘Sending Values to server…:’);
Serial.println(distance);
client.print(‘GET /json.htm?type=command&param=udevice&idx=’);
client.print(idx);
client.print(‘&nvalue==0&svalue=’);
client.print(distance);
client.print(‘;’);
client.print(‘&svalue=’);

client.print(‘;0’);
client.println(‘ HTTP/1.1’);
client.print(‘Host: ‘);
client.print(domoticz_server);
client.print(‘:’);
client.println(port);
client.println(‘User-Agent: Arduino-ethernet’);
client.println(‘Connection: close’);
client.println(‘Cm’);
client.stop();
Serial.println(distance);

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...