That good old trusty UNO / ENC28J60

Tags: #<Tag:0x00007f616d516708> #<Tag:0x00007f616d516528> #<Tag:0x00007f616d516438>

One of the first devices I connected to OpenHAB was an old Arduino UNO… with a paleontologic ENC28J60 Ethernet shield.
I realise that after I put it up there it has been faithfully sending MQTT codes without any reset.
Sure, it does not have to do much -memory constraints when using an ENC28J60 dont allow that- it just sends some temperature data, monitors some switches and a mousetrap, but it has been doing that undisturbed for the longest time. Something that cannot always be set from my ESP’s.

I know, the photo does not look like it is really ‘Solid state’ Just some quick connections with the idea to ‘one day put it all on a protoshield’, but in spite of its rinky dink outlook, it performs.

I am sure that its complete task could be handled by an ESP and have room for much more. But it works and otherwise it would just be gathering dust.
So, it is surely not the best solution, but if you have a UNO and an old shield, this is how to do it:

#include <UIPEthernet.h>
#include "PubSubClient.h"
#include "DHT.h"

#define CLIENT_ID       "UnoMQTT"
#define INTERVAL        3000 // 3 sec delay between publishing
#define DHTPIN          3
#define DHTTYPE         DHT11
bool statusKD=HIGH;//living room door
bool statusBD=HIGH;//front door
bool statusGD=HIGH;//garage door
int lichtstatus;
uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};

EthernetClient ethClient;
PubSubClient mqttClient;
DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

void setup() {
pinMode(4,INPUT_PULLUP);
pinMode(5,INPUT_PULLUP);
pinMode(6,INPUT_PULLUP);
  // setup serial communication
  //Serial.begin(9600);
  // setup ethernet communication using DHCP
  if(Ethernet.begin(mac) == 0) {
    //Serial.println(F("Ethernet configuration using DHCP failed"));
    for(;;);
  }
  // setup mqtt client
  mqttClient.setClient(ethClient);
  //mqttClient.setServer("test.mosquitto.org",1883);
  mqttClient.setServer("192.168.1.xxx",1883); //for using local broker
  //mqttClient.setServer("broker.hivemq.com",1883);
 

  // setup DHT sensor
  dht.begin();
  previousMillis = millis();
}

void loop() {
  statusBD=digitalRead(4);
  statusGD=digitalRead(5);
  statusKD=digitalRead(6);
  lichtstatus = analogRead(A0);
  // check interval
  if(millis() - previousMillis > INTERVAL) {
    sendData();
    previousMillis = millis();
  }
  mqttClient.loop();
}

void sendData() {
  char msgBuffer[20];
  float h=dht.readHumidity();
  float t = dht.readTemperature();
  if(mqttClient.connect(CLIENT_ID)) {
   mqttClient.publish("hal/temp", dtostrf(t, 6, 2, msgBuffer));
   mqttClient.publish("hal/humid", dtostrf(h, 6, 2, msgBuffer));
   mqttClient.publish("hal/door", (statusBD == HIGH) ? "OPEN" : "DICHT");
   mqttClient.publish("hal/garage",(statusGD == HIGH) ? "OPEN" : "DICHT");
   mqttClient.publish("hal/kamer",(statusKD == HIGH) ? "OPEN" : "DICHT");
   mqttClient.publish("hal/licht", dtostrf(lichtstatus, 4, 0, msgBuffer));
 //hal=hallway, DICHT=Closed, kamer=room, licht=light
 }
}

I kept the MQTT topics short, just to save valuable memory space

A sketch with some more functions, like reporting back IP number is this ine:

#include <UIPEthernet.h>
#include "PubSubClient.h"
#include "DHT.h"
#define CLIENT_ID       "Hal"
#define PUBLISH_DELAY   10000
#define DHTPIN          3
#define DHTTYPE         DHT11
bool statusKD = HIGH;
bool statusBD = HIGH;
bool statusGD = HIGH;
bool statusBEL = HIGH;
bool statusVAL = HIGH;
int lichtstatus;
int verwarming;
float batterijCV;
int  upTime = 0;
byte i;
String ip = "192.168";
String MAC = "";
uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05};

EthernetClient ethClient;
PubSubClient mqttClient;
DHT dht(DHTPIN, DHTTYPE);

long previousMillis;

void setup() {

  pinMode(4, INPUT_PULLUP);//buitendeur  PORTD
  pinMode(5, INPUT_PULLUP);//garagedeur PORT D
  pinMode(6, INPUT_PULLUP);//kamerdeur PORTD
  pinMode(7, INPUT_PULLUP);//bel  PORT D
  pinMode(8, OUTPUT);
  digitalWrite(8, LOW);
  pinMode(9, INPUT_PULLUP);//muizenval


  // setup ethernet communication using DHCP
  if (Ethernet.begin(mac) == 0) {
    //Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
  }

  
  for (i = 2; i < 4; i++) {
    ip = ip + ".";
    ip = ip + String (Ethernet.localIP()[i]);
  }
  //---mac

  for (i = 0; i < 6; i++)
  {
    MAC = MAC + String ((mac[i]), HEX);
    MAC = MAC + ":";
  }

  /*
    Serial.println(F("Ethernet configured via DHCP"));
    Serial.print("IP address: ");
    Serial.println(Ethernet.localIP());
    Serial.println();
  */
  // setup mqtt client
  mqttClient.setClient(ethClient);
  //  mqttClient.setServer( "raspberrypi.local",1883);

  mqttClient.setServer("192.168.1.103", 1883);
  //Serial.println(F("MQTT client configured"));

  // setup DHT sensor
  dht.begin();
  //Serial.println(F("DHT sensor initialized"));

  //Serial.println();
  // Serial.println(F("Ready to send data"));
  previousMillis = millis();
}

void loop() {
  statusBD = digitalRead(4);// buitendeur
  statusGD = digitalRead(5);// Garagedeur
  statusKD = digitalRead(6);// Kamerdeur /
  lichtstatus = analogRead(A0);//eigenlijk onzin
  statusBEL = digitalRead(7);
  digitalWrite(8, !statusBEL);
  statusVAL = digitalRead(9);
  upTime = millis() / 60000;
  verwarming = analogRead(A1); //NTC  of via een comperator op digitale I/O
  batterijCV = analogRead(A2);
  batterijCV = (batterijCV * 5) / 1023.0;
  // it's time to send new data?
  if (millis() - previousMillis > PUBLISH_DELAY) {
    sendData();
    previousMillis = millis();
  }

  mqttClient.loop();
}

void sendData() {

  char msgBuffer[7];//was 20
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (mqttClient.connect(CLIENT_ID)) {
    mqttClient.publish("hal/temp", deblank(dtostrf(t, 4, 1, msgBuffer)));//was 6, 2
    mqttClient.publish("hal/humid", deblank(dtostrf(h, 3, 0, msgBuffer)));
    mqttClient.publish("hal/deur", (statusBD == HIGH) ? "OPEN" : "CLOSED");//voordeur
    mqttClient.publish("hal/garage", (statusGD == HIGH) ? "OPEN" : "CLOSED");//garagedeur
    mqttClient.publish("hal/kamer", (statusKD == HIGH) ? "OPEN" : "CLOSED");//kamerdeur
    mqttClient.publish("hal/l", deblank(dtostrf(lichtstatus, 4, 0, msgBuffer)));
    mqttClient.publish("hal/b", (statusBEL == HIGH) ? "OPEN" : "CLOSED");
    mqttClient.publish("hal/bat", deblank(dtostrf(batterijCV, 4, 2, msgBuffer)));
    mqttClient.publish("home/t/t4", (statusVAL == HIGH) ? "OPEN" : "CLOSED"); //mousetrap
    mqttClient.publish("hal/versie", "enc28j60MQTT_v2");
    mqttClient.publish("hal/v", (verwarming > 500) ? "OPEN" : "CLOSED"); //500 arbitrair   evt met comperator op digitale I/O
    mqttClient.publish("hal/ip", ip.c_str());
    ///Mac beter in setup, maar kost meer geheugen
    /*
      MAC="";
      for (i=0;i<6;i++)
      {
      if(mac[i] < 0x10)
      {MAC=MAC+"0";}
      MAC=MAC+ String ((mac[i]),HEX);
      MAC=MAC+":";
      }
      MAC[(MAC.length())-1]='\0';//verwijder laatst toegevoegde ":"
    */
    mqttClient.publish("hal/mac", MAC.c_str());


    //--------

  }
}

char * deblank(char *str) {
  char *out = str;
  char *put = str;

  for (; *str != '\0'; ++str) {

    if (*str != ' ') {
      *put++ = *str;
    }
  }
  *put = '\0';
  return out;
}


Yes, one day this will all be orderly and solid state

1 Like