For reading the status of contacts I use the following code (thanks to fabalci see viewtopic.php?f=56&t=19870&p=192478#p192428 ) This code works very well.
Code: Select all
// Schuur DOMOTICZ Connection
// fabalci zie Domoticz forum
// SPI and Ethernet is needed for Ethernet shield
#include <SPI.h>
#include <Ethernet.h>
//DEBUG 1 Sends output to SerialPort
#define DEBUG 1
#define OPEN 1 // HIGH
#define CLOSED 0 // LOW
#define UNKNOWN 2 // --- reset / force update
//INPUT PIN Numbers on Arduino
#define PIN_deurcontact_schuur 2
#define PIN_PIR_schuur 3
#define PIN_PIR_tuin 4
// Last Status of the Sensors
int deurcontact_schuurStatus = UNKNOWN;
int PIR_schuurStatus = UNKNOWN;
int PIR_tuinStatus = UNKNOWN;
// IDX numbers of PIR and Magnetc Contacts on Domoticz-Devices Page
int IDX_deurcontact_schuur = 7;
int IDX_PIR_schuur = 8;
int IDX_PIR_tuin = 9;
// Arduino MAC and IP addresses
byte mac[] = {
0xDE, 0xAD, 0xBE, 0x34, 0x39, 0x01
};//Random address
IPAddress ip(192, 168, 0, 118); //IP Address of Arduino
IPAddress domoticz(192, 168, 0, 12); // Domoticz IP Address
int port = 8080; // Domoticz Port No
EthernetClient client;
//httprequest functions, sends status info to Domoticz
// /json.htm?type=command¶m=switchlight&idx=XX&switchcmd=On
void httpRequestswitch(int IDX, String status) {
// When the connection is successful:
if (client.connect(domoticz, port)) {
#if DEBUG
Serial.print("GET /json.htm?type=command¶m=switchlight&idx=");
Serial.print(IDX);
Serial.print("&switchcmd=");
Serial.println(status);
#endif
client.print( "GET /json.htm?type=command¶m=switchlight&idx=");
client.print(IDX);
client.print("&switchcmd=");
client.print(status);
client.println( " HTTP/1.1");
client.print( "Host: ");
client.println(ip);
client.println( "Connection: close");
client.println();
client.println();
client.stop();
delay(150);
}
else {
client.stop();
}
}
void httpRequestvalue(int IDX, float value) {
if (client.connect(domoticz, port)) {
// When the connection is successful:
#if DEBUG
Serial.print("GET /json.htm?type=command¶m=udevice&idx=");
Serial.print(IDX);
Serial.print("&nvalue=0&svalue=");
Serial.println(value, 1);
#endif
client.print( "GET /json.htm?type=command¶m=udevice&idx=");
client.print(IDX);
client.print("&nvalue=0&svalue=");
client.print(value, 1);
client.println( " HTTP/1.1");
client.print( "Host: ");
client.println(ip);
client.println( "Connection: close");
client.println();
client.println();
client.stop();
delay(150);
}
else {
client.stop();
}
}
int contactState(int pin)
{
// LOW -> CLOSED
// HIGH -> OPEN
return (digitalRead(pin) == HIGH) ? OPEN : CLOSED;
}
void updateContactStatus()
{
int currentStatus;
const char* msg = NULL;
currentStatus = contactState(PIN_deurcontact_schuur);
if (deurcontact_schuurStatus != currentStatus)
{
deurcontact_schuurStatus = currentStatus;
if (deurcontact_schuurStatus == CLOSED) msg = "Off";
else msg = "On";
httpRequestswitch(IDX_deurcontact_schuur, msg);
return; // only one message per loop
}
currentStatus = contactState(PIN_PIR_schuur);
if (PIR_schuurStatus != currentStatus)
{
PIR_schuurStatus = currentStatus;
if (PIR_schuurStatus == CLOSED) msg = "Off";
else msg = "On";
httpRequestswitch(IDX_PIR_schuur, msg);
return; // only one message per loop
}
currentStatus = contactState(PIN_PIR_tuin);
if (PIR_tuinStatus != currentStatus)
{
PIR_tuinStatus = currentStatus;
if (PIR_tuinStatus == CLOSED) msg = "Off";
else msg = "On";
httpRequestswitch(IDX_PIR_tuin, msg);
return; // only one message per loop
}
}
void setup() {
pinMode(PIN_deurcontact_schuur , INPUT);
pinMode(PIN_PIR_schuur , INPUT);
pinMode(PIN_PIR_tuin , INPUT);
#if DEBUG
Serial.begin(9600);
Serial.println("Arduino Schuur - Domoticz Connection");
#endif
Ethernet.begin(mac, ip);
#if DEBUG
Serial.print("Arduino IP addresss: ");
delay(1000);
Serial.println(ip);
#endif
}
void loop()
{
updateContactStatus();
}
Thanks in advance