Read digital inputs / write digital outputs of a nano via ethernet

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.
Post Reply
Jan Jansen
Posts: 172
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Read digital inputs / write digital outputs of a nano via ethernet

Post by Jan Jansen » Wednesday 14 November 2018 22:10

A cable is present. I want to use PoE (nano + W5100 ethernet shield), which means that I only need one UPS for the whole installation.

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&param=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&param=switchlight&idx=");
    Serial.print(IDX);
    Serial.print("&switchcmd=");
    Serial.println(status);
#endif
    client.print( "GET /json.htm?type=command&param=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&param=udevice&idx=");
    Serial.print(IDX);
    Serial.print("&nvalue=0&svalue=");
    Serial.println(value, 1);
#endif
    client.print( "GET /json.htm?type=command&param=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();
}
I would like to operate 3 relays with the same Nano. I hope someone is willing to help me with an appropriate code.
Thanks in advance

freijn
Posts: 401
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: Read digital inputs / write digital outputs of a nano via ethernet

Post by freijn » Wednesday 14 November 2018 22:13


Jan Jansen
Posts: 172
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Read digital inputs / write digital outputs of a nano via ethernet

Post by Jan Jansen » Sunday 18 November 2018 21:36

@freijn, thanks for your answer.

Espeasy is easy indeed. However, I prefer wired solutions where possible. In addition, I want to gain more knowledge about communication between an arduino, its sensors and actuators and Domoticz. For me that is a lot of searching, reading, cutting, pasting and trial and error.

As said in my first post, I can read the digital inputs (high speed, that’s nice when a door sensor is involved).
Domoticz offers the possibility to switch relays via a http request. In Domoticz each switch has a field for on or off action.
On the web I find examples by which you can control relays (via ethernet) that are connected to an arduino. After connecting to the arduino via a browser, buttons are displayed. With this buttons you can then switch relays on or off. I get stuck when merging the sketch for reading the inputs and the sketch for writing the outputs. I need some help.

Thanks in advance.

Post Reply

Who is online

Users browsing this forum: Camiel and 2 guests