WC_Maenner
ID: 297
Kategorie: Smarthome
Menge: 0
Position:

2. WC_Maenner

 

float BUILD_VERSION = 0.01;

int id = 2;
String nodename = "wc_maenner";
const char* host = "si.tatulo.de";

String URL_FIRMWARE_VERSION = "http://si.tatulo.de/esp_update/"+nodename+".version";
String URL_FIRMWARE_BIN = "http://si.tatulo.de/esp_update/"+nodename+".bin";

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WiFiClient.h>
#include <WiFiUdp.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266httpUpdate.h>
#include <DHT.h>

#define ventPIN 0
#define heizPIN 3
#define DHTPIN 2
#define LDRPIN 1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

ESP8266WiFiMulti wifiMulti;
ESP8266WebServer server(80);
char serverw[] = "si.tatulo.de"; //Webserver
const int httpPort = 80;
WiFiClient client;

long lastConnectionTime = 0;
long lastConnectionTime2 = 0;
long startConnectionTime = 0;
long postingInterval  = 10L * 3000L;
long postingInterval2 = 10L * 6000L;
long zeitInterval = 2 * 10L * 6000L;
long rssi;

float hum, temp, hum_old, temp_old, light;
int photocellReading = 0;
String website, webstring, line, ipnumber, ssid, url;
int vent_status, heater_status;

char* vent_on;
char* vent_off;
char* vent_fix;
char* heater_on;
char* heater_off;
char* heater_fix;
float vent_min, vent_max, heater_min, heater_max, vent_dauer, heater_dauer;

bool heater;

void setup() {
  Serial.begin(115200);
  
  pinMode(DHTPIN, INPUT);
  pinMode(LDRPIN, INPUT);
  pinMode(ventPIN, OUTPUT);
  pinMode(heizPIN, OUTPUT);
  digitalWrite(ventPIN, 1);
  digitalWrite(heizPIN, 1);
  
  delay(10);
  Serial.println();
  Serial.println();
  Serial.println(nodename);

  WiFi.mode(WIFI_STA);
  wifiMulti.addAP("bob", "westi@1503");
  wifiMulti.addAP("bobek", "westi@1503");
  wifiMulti.addAP("bobage", "westi@1503");
  wifiMulti.addAP("router", "westi@1503");

  Serial.print("Connecting..");
  while (wifiMulti.run() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  
  ssid = WiFi.SSID().c_str();
  ipnumber = WiFi.localIP().toString();

  
  Serial.println("");
  Serial.print("WiFi connected to ");  
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(ipnumber);
  Serial.println("");
  
  
  dht.begin();

  if (MDNS.begin("esp8266")) {
    Serial.println("mDNS started");
  }
  
  server.on("/", handleRoot);
  server.onNotFound(handleNotFound);
  server.begin();
  
  Serial.println("HTTP server started");

  options();
  
  delay(2000);
}

void loop() {
  options();

  /*
  Serial.print("vent_on: ");
  Serial.print(vent_max);
  Serial.print(" vent_off: ");
  Serial.print(vent_min);
  Serial.print(" vent_dauer: ");
  Serial.println(vent_dauer);
  */
  
  // -+-
  
  server.handleClient();
  
  hum = dht.readHumidity();
  temp = dht.readTemperature();
  //photocellReading = digitalRead(LDRPIN);

  
  delay(1000);

  lastConnectionTime = millis();
  
  if (digitalRead(LDRPIN) >= 1) {
    light = 1;         // an
    photocellReading ++;  // wlacz licznik
  } else {
    light = 0;         // aus
    photocellReading = 0; // wyzeruj licznik
  }
  
  // jak licznik osiagnie 10
  if (photocellReading >= 10) {
    //vent_dauer = 1;
    startConnectionTime = millis();
  }

  // start ventilator na 2 min
  if (lastConnectionTime - startConnectionTime > zeitInterval) {
    //Serial.print("zeitsteuerung aus.");
    if (vent_dauer == 0) { vent_dauer = 0; }
  } else {
    //Serial.print("zeitsteuerung an.");
    vent_dauer = 1;
  }

  
  rssi = getBarsSignal(WiFi.RSSI());
   
  if (isnan(hum) || isnan(temp)) {
    hum = hum_old;
    temp = temp_old;
  } else {
    hum_old = hum;
    temp_old = temp;
  }

  // heizung
  if (heater_dauer >= 1){
    digitalWrite(heizPIN, 0);       // eingeschaltet lassen
    heater_status = 1;
  } else {
    if (temp >= heater_max) {
      digitalWrite(heizPIN, 1);       // ausschalten
      heater_status = 0;
    }
    if (temp <= heater_min) {
      digitalWrite(heizPIN, 0);       // einschalten
      heater_status = 1;
    }
  }
  
  // wentylator
  if (vent_dauer >= 1) {
    digitalWrite(ventPIN, 0);       // eingeschaltet lassen
    vent_status = 1;
  } else {
    if (hum >= vent_max) {
      digitalWrite(ventPIN, 0);       // einschalten
      vent_status = 1;
    }
    if (hum <= vent_min) {
      digitalWrite(ventPIN, 1);       // ausschalten
      vent_status = 0;
    }
  }
  
  /*
  if (hum >= vent_max) {            // wenn feuchtigkeit über 70
    digitalWrite(ventPIN, 0);       // einschalten
    vent_status = 1;                // merker: ON
  } else if (hum <= vent_min) {     // wenn feuchtigkeit unter 60
    digitalWrite(ventPIN, 1);       // ausschalten
    vent_status = 0;                // merker: OFF
  } else if (vent_status = 1) {     // wenn zwischen 70 - 60 UND merker ON
    digitalWrite(ventPIN, 0);       // einschalten
    vent_status = 1;                // merker: ON
  } else {                          // wenn zwischen 70 - 60 UND kein merker
    digitalWrite(ventPIN, 1);       // ausschalten
    vent_status = 0;                // merker: OFF
  }
  */

WiFiClient client;
const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      //Serial.println("connection failed");
      return;
    }
    
    
    // We now create a URI for the request
    url = "/esp.php?send=true";
    url += "&id=";
    url += id;
    url += "&version=";
    url += BUILD_VERSION;
    url += "&ipnumber=";
    url += ipnumber;
    url += "&ssid=";
    url += ssid;
    url += "&bars=";
    url += rssi;
    url += "&humidity=";
    url += hum;
    url += "&temperature=";
    url += temp;
    url += "&vent=";
    url += vent_status;
    url += "&heater=";
    url += heater_status;
    url += "&light=";
    url += light;
   
    // This will send the request to the server
    client.print(String("GET ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        //Serial.println(">>> Client Timeout !");
        client.stop();
        return;
      }
    }
    
  if (millis() - lastConnectionTime > postingInterval) {
    //Serial.println(url);
    lastConnectionTime = millis();
  }
  if (millis() - lastConnectionTime2 > postingInterval2) {
    checkForNewFirmware();
    lastConnectionTime2 = millis();
  }
  
}

void options() {
  HTTPClient http;
  http.begin("http://si.tatulo.de/options.php?id=2");
  int httpCode = http.GET();
  if(httpCode == HTTP_CODE_OK) { line = http.getString(); }
  line.trim();
  
  int zahl;
  zahl = line.length() + 1;
  char buf[zahl];
  line.toCharArray(buf, zahl);
  //Serial.println(buf);
  vent_on = strtok(buf, "|");
  vent_off = strtok(NULL, "|");
  vent_fix = strtok(NULL, "|");
  heater_on = strtok(NULL, "|");
  heater_off = strtok(NULL, "|");
  heater_fix = strtok(NULL, "|");

  vent_max = atof(vent_on);  // über  70%
  vent_min = atof(vent_off); // unter 60%
  vent_dauer = atof(vent_fix);
  heater_min = atof(heater_on);  // unter  20°C
  heater_max = atof(heater_off); // über 30°C
  heater_dauer = atoi(heater_fix);
}

int getBarsSignal(long rssi){
  // 5. High quality: 90% ~= -55db
  // 4. Good quality: 75% ~= -65db
  // 3. Medium quality: 50% ~= -75db
  // 2. Low quality: 30% ~= -85db
  // 1. Unusable quality: 8% ~= -96db
  // 0. No signal
  int bars;
  
  if (rssi > -55) { 
    bars = 5;
  } else if (rssi < -55 & rssi > -65) {
    bars = 4;
  } else if (rssi < -65 & rssi > -75) {
    bars = 3;
  } else if (rssi < -75 & rssi > -85) {
    bars = 2;
  } else if (rssi < -85 & rssi > -96) {
    bars = 1;
  } else {
    bars = 0;
  }
  return bars;
}

void checkForNewFirmware(void){
    HTTPClient http;
    http.begin(URL_FIRMWARE_VERSION);
    int httpCode = http.GET();

    if(httpCode == HTTP_CODE_OK) {
        String payload = http.getString();
        //Serial.println(payload);
        
        float newVersion = payload.toFloat();

        if (BUILD_VERSION < newVersion){
            /*
            Serial.print("Aktualisierung... ");
            Serial.print(BUILD_VERSION);
            Serial.print(" -> ");
            Serial.println(newVersion);
            */
            t_httpUpdate_return ret = ESPhttpUpdate.update(URL_FIRMWARE_BIN);

            if (ret == HTTP_UPDATE_FAILED){
              //Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
              delay(1000);
            }
        } else { 
          //Serial.println("aktuell");
        }
    } else {
      //Serial.println("Updatefiles not found");
    }
}

void handleRoot() {  
  webstring =  "<head>";
  webstring += "  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
  webstring += "  <meta http-equiv=\"refresh\" content=\"3\" />";
  webstring += "  <title>WC Maenner</title>";
  webstring += "  <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">";
  webstring += "  <script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js\"></script>";
  webstring += "  <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js\"></script>";
  webstring += "</head>";
  webstring += "<div class=\"container-fluid\">";
  webstring += "  <div class=\"row\">";
  webstring += "    <div class=\"col-md-12\">";
  webstring += "      <h1>WC Maenner!</h1>";
  
  webstring += "<p>ID: ";
  webstring += id;
  webstring += "</p>";
  webstring += "<p>Version: ";
  webstring += BUILD_VERSION;
  webstring += "</p>";
  webstring += "<p>IP Number: ";
  webstring += ipnumber;
  webstring += "</p>";
  webstring += "<p>Network: ";
  webstring += ssid;
  webstring += "</p>";
  webstring += "<p>Signal: ";
  webstring += rssi;
  webstring += "</p>";
    
    webstring += "<p>Feuchtigkeit: <strong>";
    webstring += hum;
    webstring += "</strong></p>";
    webstring += "<p>Temperatur: <strong>";
    webstring += temp;
    webstring += "</strong></p>";
    webstring += "<p>Luftung: <strong>";
    webstring += vent_status;
    webstring += "</strong></p>";
    webstring += "<p>Heizung: <strong>";
    webstring += heater_status;
    webstring += "</strong></p>";
    webstring += "<p>Tageslicht: <strong>";
    webstring += nightday;
    webstring += "</strong></p>";
    
  webstring += "<p>Send Link: <a href=\"http://";
  webstring += host;
  webstring += url;
  webstring += "\">";
  webstring += "http://";
  webstring += host;
  webstring += url;
  webstring += "</a>";
    
  webstring += "    </div>";
  webstring += "  </div>";
  webstring += "</div>";
  
  server.send(200, "text/html", webstring);
}

void handleNotFound(){
  server.send(404, "text/plain", "404: Not found");
}