// 2020-07-11 ENC28J60 V1 - Extended Setup
// (c) Markus Pohle @Creative Commons BY-NC-SA
// https://en.wikipedia.org/wiki/File:Cc-by-nc-sa_icon.svg
//
// UIPEthernet und BME280 und OLED Display
//
// Ethernet
#include <UIPEthernet.h>
// Ethernet Konfigparameter
EthernetClient client;
uint8_t mac[6] = {0xaa,0xbb,0xcc,0xdd,0xee,0xff};
char server[] = "maker.ifttt.com";
// OLED Display
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
// 0X3C+SA0 - 0x3C or 0x3D
#define I2C_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1
SSD1306AsciiWire oled;
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme; // I2C
// Schwellwertzähler
static uint32_t highCount = 1;
static uint32_t sentTimer = 0;
void setup()
{
Serial.begin(9600); // Öffne serielle Schnittsatelle
Wire.begin();
Wire.setClock(400000L);
#if RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS, RST_PIN);
#else // RST_PIN >= 0
oled.begin(&Adafruit128x32, I2C_ADDRESS);
#endif // RST_PIN >= 0
oled.setFont(Adafruit5x7);
oled.set1X();
Serial.println(F("BME280 Init..."));
unsigned status;
status = bme.begin();
if (!status) {
Serial.print(F("BME280 ERROR!"));
oled.clear();
oled.setCursor(0, 0);
oled.print(F("Sensor Error!"));
while(true);
}
Serial.println("ENC28J60 Init...");
if(Ethernet.begin(mac) == 0){
Serial.println(F("Failed to configure Ethernet using DHCP"));
oled.clear();
oled.setCursor(0, 0);
oled.print(F("DHCP Network Error!"));
while(true); // kein Netzwerk? Dann Ende!
}
Serial.print(F("localIP: "));
Serial.println(Ethernet.localIP());
oled.clear();
}
void loop()
{
float h = bme.readHumidity();
float t = bme.readTemperature();
oled.setCursor(0, 0);
oled.print(F("Temperatur: "));
oled.print(t);
oled.println(F(" *C"));
oled.setCursor(0, 2);
oled.print(F("Luftfeuchte: "));
oled.print(h);
oled.println(F(" %"));
if ( t > 27 || t < 18 || h > 55 || h < 35 )
highCount++;
else
highCount = 1;
Serial.print(F("High Count: ")); Serial.println(highCount);
// Nur, wenn der Schwellwert mehr als eine Minuten ansteht wird alarmiert - verhindert flappen
// Modulo 13, weil Variable highCount initial 1 ist - wäre highCount zu Beginn 0, wäre hC % 12 == 0 sofort TRUE
if (highCount % 13 == 0) {
if (millis() > sentTimer) { // prüft ob seit dem letzten HTTP GET Request mehr als 10 Min vergangen sind
highCount < 780 ? sentTimer = millis() + 600000 : sentTimer = millis() + 3600000;
//sentTimer = millis() + 600000;
Serial.println("Sende an IFTTT");
if (client.connect(server,80)) {
client.print(F("GET /trigger/RZTemp/with/key/wD37A*****************************jlN?value1="));
client.print(t);
client.print(F("&value2="));
client.print(h);
client.println(F(" HTTP/1.1"));
client.println(F("Host: maker.ifttt.com"));
client.println();
}
while(client.connected()) {
if(client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
else
highCount < 780 ? Serial.println(F("max. alle 10 Minuten ein Alarm...")) : Serial.println(F("max. alle 60 Minuten ein Alarm..."));
}
delay(5000);
}